Skip to content

Commit

Permalink
Only error on duplicate signatures in Contract ABI (#499).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed May 3, 2020
1 parent a6c1174 commit 098d7ef
Showing 1 changed file with 54 additions and 28 deletions.
82 changes: 54 additions & 28 deletions packages/contracts/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,52 +560,78 @@ export class Contract {
}
}

const uniqueFunctions: { [ name: string ]: Array<string> } = { };
Object.keys(this.interface.functions).forEach((name) => {
const fragment = this.interface.functions[name];
const uniqueNames: { [ name: string ]: Array<string> } = { };
const uniqueSignatures: { [ signature: string ]: boolean } = { };
Object.keys(this.interface.functions).forEach((signature) => {
const fragment = this.interface.functions[signature];

// Check that the signature is unique; if not the ABI generation has
// not been cleaned or may be incorrectly generated
if (uniqueSignatures[signature]) {
logger.warn(`Duplicate ABI entry for ${ JSON.stringify(name) }`);
return;
}
uniqueSignatures[signature] = true;

// Track unique names; we only expose bare named functions if they
// are ambiguous
{
const name = fragment.name;
if (!uniqueNames[name]) { uniqueNames[name] = [ ]; }
uniqueNames[name].push(signature);
}

// @TODO: This should take in fragment
const run = runMethod(this, signature, { });

const run = runMethod(this, name, { });
if (this[signature] == null) {
defineReadOnly<any, any>(this, signature, run);
}

if (this[name] == null) {
defineReadOnly<any, any>(this, name, run);
if (this.functions[signature] == null) {
defineReadOnly(this.functions, signature, run);
}

if (this.functions[name] == null) {
defineReadOnly(this.functions, name, run);
if (this.callStatic[signature] == null) {
defineReadOnly(this.callStatic, signature, runMethod(this, signature, { callStatic: true }));
}

if (this.callStatic[name] == null) {
defineReadOnly(this.callStatic, name, runMethod(this, name, { callStatic: true }));
if (this.populateTransaction[signature] == null) {
defineReadOnly(this.populateTransaction, signature, runMethod(this, signature, { transaction: true }));
}

if (this.populateTransaction[name] == null) {
defineReadOnly(this.populateTransaction, name, runMethod(this, name, { transaction: true }));
if (this.estimateGas[signature] == null) {
defineReadOnly(this.estimateGas, signature, runMethod(this, signature, { estimate: true }));
}
});

if (this.estimateGas[name] == null) {
defineReadOnly(this.estimateGas, name, runMethod(this, name, { estimate: true }));
Object.keys(uniqueNames).forEach((name) => {

// Ambiguous names to not get attached as bare names
const signatures = uniqueNames[name];
if (signatures.length > 1) { return; }

const signature = signatures[0];

if (this[name] == null) {
defineReadOnly(this, name, this[signature]);
}

if (!uniqueFunctions[fragment.name]) { uniqueFunctions[fragment.name] = [ ]; }
uniqueFunctions[fragment.name].push(name);
});
if (this.functions[name] == null) {
defineReadOnly(this.functions, name, this.functions[signature]);
}

Object.keys(uniqueFunctions).forEach((name) => {
const signatures = uniqueFunctions[name];
if (signatures.length > 1) {
logger.warn(`Duplicate definition of ${ name } (${ signatures.join(", ")})`);
return;
if (this.callStatic[name] == null) {
defineReadOnly(this.callStatic, name, this.callStatic[signature]);
}

if (this[name] == null) {
defineReadOnly(this, name, this[signatures[0]]);
if (this.populateTransaction[name] == null) {
defineReadOnly(this.populateTransaction, name, this.populateTransaction[signature]);
}

defineReadOnly(this.functions, name, this.functions[signatures[0]]);
defineReadOnly(this.callStatic, name, this.callStatic[signatures[0]]);
defineReadOnly(this.populateTransaction, name, this.populateTransaction[signatures[0]]);
defineReadOnly(this.estimateGas, name, this.estimateGas[signatures[0]]);
if (this.estimateGas[name] == null) {
defineReadOnly(this.estimateGas, name, this.estimateGas[signature]);
}
});
}

Expand Down

0 comments on commit 098d7ef

Please sign in to comment.