Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 6c6f8c0

Browse files
committed
Allow encoder to lookup functions by full signature
1 parent 6615720 commit 6c6f8c0

File tree

2 files changed

+39
-47
lines changed

2 files changed

+39
-47
lines changed

packages/encoder/lib/encoders.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -892,10 +892,10 @@ export class ContractEncoder {
892892
* select an overload by other means. For enums you may also specify the
893893
* enum type as documented in [[encodeTransaction]].
894894
*
895-
* @param abisOrName The ABI entries for the overloads, or the name of the
896-
* function. Note that if you are inputting ABI entries, they must be
897-
* for functions, not constructors. The entries must be ones associated
898-
* with this contract.
895+
* @param abisOrNameOrSig The ABI entries for the overloads, or the name or
896+
* full signature of the function. Note that if you are inputting ABI
897+
* entries, they must be for functions, not constructors. The entries must
898+
* be ones associated with this contract.
899899
* @param inputs An array of the inputs to the transaction. May include a
900900
* transaction options argument on the end if the `allowOptions` flag is
901901
* set.
@@ -904,11 +904,11 @@ export class ContractEncoder {
904904
* [[Resolution]] object.
905905
*/
906906
public async resolveAndWrap(
907-
abisOrName: Abi.FunctionEntry[] | string,
907+
abisOrNameOrSig: Abi.FunctionEntry[] | string,
908908
inputs: unknown[],
909909
options: Types.ResolveOptions = {}
910910
): Promise<Codec.Wrap.Resolution> {
911-
const abis = this.getAbis(abisOrName);
911+
const abis = this.getAbis(abisOrNameOrSig);
912912
const methods = abis.map(abi => this.getMethod(abi));
913913
//note we can't just write abis.map(this.getMethod)
914914
//because this would be undefined inside of it... I could
@@ -1251,22 +1251,22 @@ export class ContractEncoder {
12511251
* In addition, input may also be given as a
12521252
* [[Format.Values.OptionsValue|OptionsValue]].
12531253
*
1254-
* @param abisOrName The ABI entries for the overloads, or the name of the
1255-
* function. Note that if you are inputting ABI entries, they must be
1256-
* for functions, not constructors. The entries must be ones associated
1257-
* with this contract.
1254+
* @param abisOrNameOrSig The ABI entries for the overloads, or the name
1255+
* or full signature of the function. Note that if you are inputting ABI
1256+
* entries, they must be for functions, not constructors. The entries must
1257+
* be ones associated with this contract.
12581258
* @param input The value to be interpreted. This can take a number of
12591259
* forms depending on the data type, as documented above.
12601260
* @return An object with a `tx` field, holding the transaction options,
12611261
* including the encoded `data`, and an `abi` field, indicating which
12621262
* ABI entry was used for encoding.
12631263
*/
12641264
public async encodeTransaction(
1265-
abisOrName: Abi.FunctionEntry[] | string,
1265+
abisOrNameOrSig: Abi.FunctionEntry[] | string,
12661266
inputs: unknown[],
12671267
options: Types.ResolveOptions = {}
12681268
): Promise<Types.TxAndAbi> {
1269-
const abis = this.getAbis(abisOrName);
1269+
const abis = this.getAbis(abisOrNameOrSig);
12701270
const methods = abis.map(abi => this.getMethod(abi));
12711271
//note we can't just write abis.map(this.getMethod)
12721272
//because this would be undefined inside of it... I could
@@ -1307,21 +1307,23 @@ export class ContractEncoder {
13071307
}
13081308

13091309
private getAbis(
1310-
abisOrName: Abi.FunctionEntry[] | string
1310+
abisOrNameOrSig: Abi.FunctionEntry[] | string
13111311
): Abi.FunctionEntry[] {
13121312
const abis: Abi.FunctionEntry[] =
1313-
typeof abisOrName === "string"
1313+
typeof abisOrNameOrSig === "string"
13141314
? this.abi.filter(
13151315
(abi): abi is Abi.FunctionEntry =>
1316-
abi.type === "function" && abi.name === abisOrName
1316+
abi.type === "function" &&
1317+
(abi.name === abisOrNameOrSig ||
1318+
Codec.AbiData.Utils.abiSignature(abi) === abisOrNameOrSig)
13171319
)
1318-
: abisOrName;
1319-
if (typeof abisOrName === "string" && abis.length === 0) {
1320+
: abisOrNameOrSig;
1321+
if (typeof abisOrNameOrSig === "string" && abis.length === 0) {
13201322
//we don't throw this if the input was an empty list of ABIs
13211323
//rather than a name... the user knew what they were doing if they
13221324
//did that :P
13231325
throw new NoFunctionByThatNameError(
1324-
abisOrName,
1326+
abisOrNameOrSig,
13251327
this.contract.contractName
13261328
);
13271329
}
@@ -1513,12 +1515,12 @@ export class ContractInstanceEncoder {
15131515
* transaction option, it will be recognized but ignored.
15141516
*/
15151517
public async encodeTransaction(
1516-
abisOrName: Abi.FunctionEntry[] | string,
1518+
abisOrNameOrSig: Abi.FunctionEntry[] | string,
15171519
inputs: unknown[],
15181520
options: Types.ResolveOptions = {}
15191521
): Promise<Types.TxAndAbi> {
15201522
const encoded = await this.contractEncoder.encodeTransaction(
1521-
abisOrName,
1523+
abisOrNameOrSig,
15221524
inputs,
15231525
options
15241526
);

packages/encoder/lib/errors.ts

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
*/
55
export class NoInternalInfoError extends Error {
66
constructor() {
7-
super("No compilations provided, but userDefinedTypes or allocations is missing");
7+
super(
8+
"No compilations provided, but userDefinedTypes or allocations is missing"
9+
);
810
this.name = "NoInternalInfoError";
911
}
1012
}
@@ -25,14 +27,14 @@ export class NoCompilationsForSpawnerError extends Error {
2527
* @protected
2628
*/
2729
export class NoFunctionByThatNameError extends Error {
28-
public functionName: string;
30+
public functionNameOrSig: string;
2931
public contractName: string | undefined;
30-
constructor(functionName: string, contractName: string) {
32+
constructor(functionNameOrSig: string, contractName: string) {
3133
const message = contractName
32-
? `Contract ${contractName} has no function named ${functionName}`
33-
: `This contract has no function named ${functionName}`
34+
? `Contract ${contractName} has no function with name or signature ${functionNameOrSig}`
35+
: `This contract has no function with name or signature ${functionNameOrSig}`;
3436
super(message);
35-
this.functionName = functionName;
37+
this.functionNameOrSig = functionNameOrSig;
3638
this.contractName = contractName;
3739
this.name = "NoFunctionByThatNameError";
3840
}
@@ -64,13 +66,8 @@ export class InvalidAddressError extends Error {
6466
export class UnlinkedContractError extends Error {
6567
public contractName: string | undefined;
6668
public bytecode: string | undefined;
67-
constructor(
68-
contractName: string | undefined,
69-
bytecode: string | undefined,
70-
) {
71-
const nameString = contractName !== undefined
72-
? contractName + " "
73-
: "";
69+
constructor(contractName: string | undefined, bytecode: string | undefined) {
70+
const nameString = contractName !== undefined ? contractName + " " : "";
7471
super(`Contract ${nameString}has not had all its libraries linked`);
7572
this.contractName = contractName;
7673
this.bytecode = bytecode;
@@ -86,14 +83,11 @@ export class UnlinkedContractError extends Error {
8683
export class ContractNotDeployedError extends Error {
8784
public contractName: string | undefined;
8885
public networkId: number;
89-
constructor(
90-
contractName: string | undefined,
91-
networkId: number
92-
) {
93-
const nameString = contractName !== undefined
94-
? contractName + " "
95-
: "";
96-
super(`Contract ${nameString}has not been deployed to network ${networkId} with deployer; address must be given explicitly`);
86+
constructor(contractName: string | undefined, networkId: number) {
87+
const nameString = contractName !== undefined ? contractName + " " : "";
88+
super(
89+
`Contract ${nameString}has not been deployed to network ${networkId} with deployer; address must be given explicitly`
90+
);
9791
this.contractName = contractName;
9892
this.name = "ContractNotDeployedError";
9993
}
@@ -106,12 +100,8 @@ export class ContractNotDeployedError extends Error {
106100
*/
107101
export class NoBytecodeError extends Error {
108102
public contractName: string | undefined;
109-
constructor(
110-
contractName: string | undefined,
111-
) {
112-
const nameString = contractName !== undefined
113-
? contractName + " "
114-
: "";
103+
constructor(contractName: string | undefined) {
104+
const nameString = contractName !== undefined ? contractName + " " : "";
115105
super(`Contract ${nameString}has missing or empty constructor bytecode`);
116106
this.contractName = contractName;
117107
this.name = "NoBytecodeError";

0 commit comments

Comments
 (0)