2019-05-14 18:25:46 -04:00
|
|
|
"use strict";
|
|
|
|
|
2019-08-01 18:04:06 -04:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-02-04 08:01:26 -05:00
|
|
|
export function defineReadOnly<T, K extends keyof T>(object: T, name: K, value: T[K]): void {
|
2019-05-14 18:25:46 -04:00
|
|
|
Object.defineProperty(object, name, {
|
|
|
|
enumerable: true,
|
|
|
|
value: value,
|
|
|
|
writable: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-11 23:42:35 -04:00
|
|
|
// Crawl up the constructor chain to find a static method
|
|
|
|
export function getStatic<T>(ctor: any, key: string): T {
|
|
|
|
for (let i = 0; i < 32; i++) {
|
|
|
|
if (ctor[key]) { return ctor[key]; }
|
|
|
|
if (!ctor.prototype || typeof(ctor.prototype) !== "object") { break; }
|
|
|
|
ctor = Object.getPrototypeOf(ctor.prototype).constructor;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-06-01 04:46:37 -04:00
|
|
|
export type Deferrable<T> = {
|
|
|
|
[ K in keyof T ]: T[K] | Promise<T[K]>;
|
2020-02-06 18:21:34 -05:00
|
|
|
}
|
|
|
|
|
2020-06-01 04:46:37 -04:00
|
|
|
|
2019-07-27 18:45:51 -03:00
|
|
|
type Result = { key: string, value: any};
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-06-01 04:46:37 -04:00
|
|
|
export async function resolveProperties<T>(object: Readonly<Deferrable<T>>): Promise<T> {
|
2019-11-01 23:33:51 +09:00
|
|
|
const promises: Array<Promise<Result>> = Object.keys(object).map((key) => {
|
2020-06-01 04:46:37 -04:00
|
|
|
const value = object[<keyof Deferrable<T>>key];
|
2020-05-01 16:32:32 -04:00
|
|
|
return Promise.resolve(value).then((v) => ({ key: key, value: v }));
|
2019-05-14 18:25:46 -04:00
|
|
|
});
|
|
|
|
|
2020-05-01 16:32:32 -04:00
|
|
|
const results = await Promise.all(promises);
|
|
|
|
|
|
|
|
return results.reduce((accum, result) => {
|
2020-05-29 21:15:17 -04:00
|
|
|
accum[<keyof T>(result.key)] = result.value;
|
2020-05-01 16:32:32 -04:00
|
|
|
return accum;
|
2020-05-29 21:15:17 -04:00
|
|
|
}, <T>{ });
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function checkProperties(object: any, properties: { [ name: string ]: boolean }): void {
|
|
|
|
if (!object || typeof(object) !== "object") {
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.throwArgumentError("invalid object", "object", object);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Object.keys(object).forEach((key) => {
|
|
|
|
if (!properties[key]) {
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.throwArgumentError("invalid object key - " + key, "transaction:" + key, object);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-29 21:15:17 -04:00
|
|
|
export function shallowCopy<T>(object: T): T {
|
2019-11-01 23:33:51 +09:00
|
|
|
const result: any = {};
|
|
|
|
for (const key in object) { result[key] = object[key]; }
|
2019-05-14 18:25:46 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-03-30 15:11:23 -04:00
|
|
|
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);
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-06-11 17:52:42 -04:00
|
|
|
// Returns a new copy of object, such that no properties may be replaced.
|
|
|
|
// New properties may be added only to objects.
|
2020-02-06 18:21:34 -05:00
|
|
|
function _deepCopy(object: any): any {
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-03-30 15:11:23 -04:00
|
|
|
if (_isFrozen(object)) { return object; }
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
// Arrays are mutable, so we need to create a copy
|
|
|
|
if (Array.isArray(object)) {
|
2019-06-11 17:52:42 -04:00
|
|
|
return Object.freeze(object.map((item) => deepCopy(item)));
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(object) === "object") {
|
2019-11-01 23:33:51 +09:00
|
|
|
const result: { [ key: string ]: any } = {};
|
|
|
|
for (const key in object) {
|
|
|
|
const value = object[key];
|
2019-05-14 18:25:46 -04:00
|
|
|
if (value === undefined) { continue; }
|
2019-06-11 17:52:42 -04:00
|
|
|
defineReadOnly(result, key, deepCopy(value));
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-03-30 15:11:23 -04:00
|
|
|
return logger.throwArgumentError(`Cannot deepCopy ${ typeof(object) }`, "object", object);
|
2020-02-06 18:21:34 -05:00
|
|
|
}
|
|
|
|
|
2020-05-29 21:15:17 -04:00
|
|
|
export function deepCopy<T>(object: T): T {
|
2020-02-06 18:21:34 -05:00
|
|
|
return _deepCopy(object);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-01-07 19:44:45 -05:00
|
|
|
export class Description<T = any> {
|
2020-04-25 01:06:28 -04:00
|
|
|
constructor(info: { [ K in keyof T ]: T[K] }) {
|
2019-11-01 23:33:51 +09:00
|
|
|
for (const key in info) {
|
2019-06-11 17:52:42 -04:00
|
|
|
(<any>this)[key] = deepCopy(info[key]);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|