Skip to content

Commit

Permalink
Fixed Event args keyword access.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Mar 30, 2020
1 parent bd32ee0 commit 2692e78
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
4 changes: 2 additions & 2 deletions packages/abi/src.ts/coders/abstract-coder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { Logger } from "@ethersproject/logger";
import { version } from "../_version";
const logger = new Logger(version);

export interface Result extends Array<any> {
[key: string]: any;
export interface Result extends ReadonlyArray<any> {
readonly [key: string]: any;
}

export type CoerceFunc = (type: string, value: any) => any;
Expand Down
2 changes: 1 addition & 1 deletion packages/abi/src.ts/coders/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function unpack(reader: Reader, coders: Array<Coder>): Array<any> {
values[name] = values[index];
});

return values;
return Object.freeze(values);
}


Expand Down
5 changes: 3 additions & 2 deletions packages/abi/src.ts/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ export class Interface {
let resultIndexed = (topics != null) ? this._abiCoder.decode(indexed, concat(topics)): null;
let resultNonIndexed = this._abiCoder.decode(nonIndexed, data);

let result: Result = [ ];
let result: (Array<any> & { [ key: string ]: any }) = [ ];
let nonIndexedIndex = 0, indexedIndex = 0;
eventFragment.inputs.forEach((param, index) => {
if (param.indexed) {
Expand All @@ -416,10 +416,11 @@ export class Interface {
} else {
result[index] = resultNonIndexed[nonIndexedIndex++];
}

if (param.name && result[param.name] == null) { result[param.name] = result[index]; }
});

return result;
return Object.freeze(result);
}

// Given a transaction, find the matching function fragment (if any) and
Expand Down
3 changes: 2 additions & 1 deletion packages/contracts/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ function runMethod(contract: Contract, functionName: string, options: RunOptions
let parsed: LogDescription = null;
try {
parsed = contract.interface.parseLog(log);
} catch (e){}
} catch (e){ }

if (parsed) {
event.args = parsed.args;
event.decode = (data: BytesLike, topics?: Array<any>) => {
Expand Down
35 changes: 22 additions & 13 deletions packages/properties/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,39 @@ export function shallowCopy<T>(object: T): Similar<T> {
return result;
}

const opaque: { [key: string]: boolean } = { bigint: true, boolean: true, number: true, string: true };
const opaque: { [key: string]: boolean } = { bigint: true, boolean: true, "function": true, number: true, string: true };

function _isFrozen(object: any): boolean {

// Opaque objects are not mutable, so safe to copy by assignment
if (object === undefined || object === null || opaque[typeof(object)]) { return true; }

if (Array.isArray(object) || typeof(object) === "object") {
if (!Object.isFrozen(object)) { return false; }

const keys = Object.keys(object);
for (let i = 0; i < keys.length; i++) {
if (!_isFrozen(object[keys[i]])) { return false; }
}

return true;
}

return logger.throwArgumentError(`Cannot deepCopy ${ typeof(object) }`, "object", object);
}

// Returns a new copy of object, such that no properties may be replaced.
// New properties may be added only to objects.
function _deepCopy(object: any): any {

// Opaque objects are not mutable, so safe to copy by assignment
if (object === undefined || object === null || opaque[typeof(object)]) { return object; }
if (_isFrozen(object)) { return object; }

// Arrays are mutable, so we need to create a copy
if (Array.isArray(object)) {
return Object.freeze(object.map((item) => deepCopy(item)));
}

if (typeof(object) === "object") {

// Immutable objects are safe to just use
if (Object.isFrozen(object)) { return object; }

const result: { [ key: string ]: any } = {};
for (const key in object) {
const value = object[key];
Expand All @@ -101,12 +115,7 @@ function _deepCopy(object: any): any {
return result;
}

// The function type is also immutable, so safe to copy by assignment
if (typeof(object) === "function") {
return object;
}

logger.throwArgumentError(`Cannot deepCopy ${ typeof(object) }`, "object", object);
return logger.throwArgumentError(`Cannot deepCopy ${ typeof(object) }`, "object", object);
}

export function deepCopy<T>(object: T): Similar<T> {
Expand Down

0 comments on commit 2692e78

Please sign in to comment.