Skip to content

Commit

Permalink
Return undefined for Contract properties that do not exist instead of…
Browse files Browse the repository at this point in the history
… throwing an error (#4266).
  • Loading branch information
ricmoo committed Jul 24, 2023
1 parent bcc4d8c commit 5bf7b34
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions src.ts/contract/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { isAddressable, resolveAddress } from "../address/index.js";
import { copyRequest, Log, TransactionResponse } from "../providers/provider.js";
import {
defineProperties, getBigInt, isCallException, isHexString, resolveProperties,
makeError, assert, assertArgument
isError, makeError, assert, assertArgument
} from "../utils/index.js";

import {
Expand Down Expand Up @@ -724,18 +724,21 @@ export class BaseContract implements Addressable, EventEmitterable<ContractEvent

// Add the event filters
const filters = new Proxy({ }, {
get: (target, _prop, receiver) => {
get: (target, prop, receiver) => {
// Pass important checks (like `then` for Promise) through
if (passProperties.indexOf(<string>_prop) >= 0) {
return Reflect.get(target, _prop, receiver);
if (typeof(prop) === "symbol" || passProperties.indexOf(prop) >= 0) {
return Reflect.get(target, prop, receiver);
}

const prop = String(_prop);

const result = this.getEvent(prop);
if (result) { return result; }
try {
return this.getEvent(prop);
} catch (error) {
if (!isError(error, "INVALID_ARGUMENT") || error.argument !== "key") {
throw error;
}
}

throw new Error(`unknown contract event: ${ prop }`);
return undefined;
},
has: (target, prop) => {
// Pass important checks (like `then` for Promise) through
Expand All @@ -754,24 +757,28 @@ export class BaseContract implements Addressable, EventEmitterable<ContractEvent

// Return a Proxy that will respond to functions
return new Proxy(this, {
get: (target, _prop, receiver) => {
if (_prop in target || passProperties.indexOf(<string>_prop) >= 0 || typeof(_prop) === "symbol") {
return Reflect.get(target, _prop, receiver);
get: (target, prop, receiver) => {
if (typeof(prop) === "symbol" || prop in target || passProperties.indexOf(prop) >= 0) {
return Reflect.get(target, prop, receiver);
}

const prop = String(_prop);

const result = target.getFunction(prop);
if (result) { return result; }
// Undefined properties should return undefined
try {
return target.getFunction(prop);
} catch (error) {
if (!isError(error, "INVALID_ARGUMENT") || error.argument !== "key") {
throw error;
}
}

throw new Error(`unknown contract method: ${ prop }`);
return undefined;
},
has: (target, prop) => {
if (prop in target || passProperties.indexOf(<string>prop) >= 0 || typeof(prop) === "symbol") {
if (typeof(prop) === "symbol" || prop in target || passProperties.indexOf(prop) >= 0) {
return Reflect.has(target, prop);
}

return target.interface.hasFunction(String(prop));
return target.interface.hasFunction(prop);
}
});

Expand Down

0 comments on commit 5bf7b34

Please sign in to comment.