This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Switch to use promises - Support function overloads - Add support for events Signed-off-by: Silas Davis <[email protected]>
- Loading branch information
Silas Davis
committed
May 21, 2021
1 parent
e3d11fa
commit 07509f9
Showing
40 changed files
with
1,748 additions
and
760 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { EventFragment, Fragment, FunctionFragment, Interface } from 'ethers/lib/utils'; | ||
import { postDecodeResult, preEncodeResult, prefixedHexString, toBuffer } from './convert'; | ||
|
||
export type ContractCodec = { | ||
encodeDeploy(...args: unknown[]): Buffer; | ||
encodeFunctionData(signature: string, ...args: unknown[]): Buffer; | ||
decodeFunctionResult(signature: string, data: Uint8Array | undefined): any; | ||
decodeEventLog(signature: string, data: Uint8Array | undefined, topics?: Uint8Array[]): any; | ||
}; | ||
|
||
export function getContractCodec(iface: Interface): ContractCodec { | ||
return { | ||
encodeDeploy(...args: unknown[]): Buffer { | ||
const frag = iface.deploy; | ||
try { | ||
return toBuffer(iface.encodeDeploy(preEncodeResult(args, frag.inputs))); | ||
} catch (err) { | ||
throwErr(err, 'encode deploy', 'constructor', args, frag); | ||
} | ||
}, | ||
|
||
encodeFunctionData(signature: string, ...args: unknown[]): Buffer { | ||
let frag: FunctionFragment | undefined; | ||
try { | ||
frag = iface.getFunction(formatSignature(signature)); | ||
return toBuffer(iface.encodeFunctionData(frag, preEncodeResult(args, frag.inputs))); | ||
} catch (err) { | ||
throwErr(err, 'encode function data', signature, args, frag); | ||
} | ||
}, | ||
|
||
decodeFunctionResult(signature: string, data: Uint8Array = new Uint8Array()): any { | ||
let frag: FunctionFragment | undefined; | ||
try { | ||
frag = iface.getFunction(formatSignature(signature)); | ||
return postDecodeResult(iface.decodeFunctionResult(frag, data), frag.outputs); | ||
} catch (err) { | ||
throwErr(err, 'decode function result', signature, { data }, frag); | ||
} | ||
}, | ||
|
||
decodeEventLog(signature: string, data: Uint8Array = new Uint8Array(), topics?: Uint8Array[]): any { | ||
let frag: EventFragment | undefined; | ||
try { | ||
frag = iface.getEvent(formatSignature(signature)); | ||
return postDecodeResult( | ||
iface.decodeEventLog( | ||
frag, | ||
prefixedHexString(data), | ||
topics?.map((topic) => prefixedHexString(topic)), | ||
), | ||
frag.inputs, | ||
); | ||
} catch (err) { | ||
throwErr(err, 'decode event log', signature, { data, topics }, frag); | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
function formatSignature(signature: string): string { | ||
return prefixedHexString(signature); | ||
} | ||
|
||
function throwErr( | ||
err: unknown, | ||
action: string, | ||
signature: string, | ||
args: Record<string, unknown> | unknown[], | ||
frag?: Fragment, | ||
): never { | ||
const name = frag ? frag.name : `member with signature '${signature}'`; | ||
const inputs = frag?.inputs ? ` (inputs: ${JSON.stringify(frag.inputs)})` : ''; | ||
throw new Error(`ContractCodec could not ${action} for ${name} with args ${JSON.stringify(args)}${inputs}: ${err}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.