Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hasEvent and hasFunction methods to contract and interface #3901

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src.ts/_tests/test-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,32 @@ describe("Test Contract", function() {
await specificEvent;
await allEvents;
});

it("tests hasFunction", async function() {
const contract = new Contract(addr, abi);

assert.equal(contract.hasFunction('testCallAdd'), true);
assert.equal(contract.hasFunction('notExistFn'), false);
assert.equal(contract.hasFunction('function testCallAdd(uint256 a, uint256 b) pure returns (uint256 result)'), true);
assert.equal(contract.hasFunction('function testCallAdd(uint256 a) pure returns (uint256 result)'), false);
assert('testCallAdd' in contract);
assert(!('notExistFn' in contract));
assert('function testCallAdd(uint256 a, uint256 b) pure returns (uint256 result)' in contract);
assert(!('function testCallAdd(uint256 a) pure returns (uint256 result)' in contract));
});

it("tests hasEvent", async function() {
const contract = new Contract(addr, abi);

assert.equal(contract.hasEvent('EventUint256'), true);
assert.equal(contract.hasEvent('NotExistEvent'), false);
assert.equal(contract.hasEvent('event EventUint256(uint256 indexed value)'), true);
assert.equal(contract.hasEvent('event EventUint256()'), false);
assert('EventUint256' in contract.filters);
assert(!('NotExistEvent' in contract.filters));
assert('event EventUint256(uint256 indexed value)' in contract.filters);
assert(!('event EventUint256()' in contract.filters));
});
});

describe("Test Typed Contract Interaction", function() {
Expand Down
16 changes: 16 additions & 0 deletions src.ts/abi/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,14 @@ export class Interface {
return null;
}

/**
* Check if interface has function %%key%%, which may be a function
* selector, function name or function signature that belongs to the ABI.
*/
hasFunction(key: string): boolean {
return !!this.#getFunction(key, null, false);
}

/**
* Get the function name for %%key%%, which may be a function selector,
* function name or function signature that belongs to the ABI.
Expand Down Expand Up @@ -504,6 +512,14 @@ export class Interface {
return null;
}

/**
* Check if interface has event %%key%%, which may be a topic hash,
* event name or event signature that belongs to the ABI.
*/
hasEvent(key: string): boolean {
return !!this.#getEvent(key, null, false);
}

/**
* Get the event name for %%key%%, which may be a topic hash,
* event name or event signature that belongs to the ABI.
Expand Down
22 changes: 20 additions & 2 deletions src.ts/contract/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,11 @@ export class BaseContract implements Addressable, EventEmitterable<ContractEvent
if (result) { return result; }

throw new Error(`unknown contract event: ${ prop }`);
}
},
has: (target, _prop) => (
Reflect.has(target, _prop) ||
this.hasEvent(String(_prop))
),
});
defineProperties<BaseContract>(this, { filters });

Expand All @@ -706,7 +710,11 @@ export class BaseContract implements Addressable, EventEmitterable<ContractEvent
if (result) { return result; }

throw new Error(`unknown contract method: ${ prop }`);
}
},
has: (target, _prop) => (
Reflect.has(target, _prop) ||
this.hasFunction(String(_prop))
),
});

}
Expand Down Expand Up @@ -762,11 +770,21 @@ export class BaseContract implements Addressable, EventEmitterable<ContractEvent
return getInternal(this).deployTx;
}

hasFunction(key: string | FunctionFragment): boolean {
if (typeof(key) !== "string") { key = key.format(); }
return this.interface.hasFunction(key);
}

getFunction<T extends ContractMethod = ContractMethod>(key: string | FunctionFragment): T {
if (typeof(key) !== "string") { key = key.format(); }
return <T><unknown>(new WrappedMethod(this, key));
}

hasEvent(key: string | EventFragment): boolean {
if (typeof(key) !== "string") { key = key.format(); }
return this.interface.hasEvent(key);
}

getEvent(key: string | EventFragment): ContractEvent {
if (typeof(key) !== "string") { key = key.format(); }
return <ContractEvent><unknown>(new WrappedEvent(this, key));
Expand Down