2019-05-14 18:48:48 -04:00
|
|
|
"use strict";
|
2020-05-01 17:00:44 -04:00
|
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
|
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
|
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
|
|
});
|
|
|
|
};
|
2019-08-25 02:39:20 -04:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
export function defineReadOnly(object, name, value) {
|
2019-05-14 18:48:48 -04:00
|
|
|
Object.defineProperty(object, name, {
|
|
|
|
enumerable: true,
|
|
|
|
value: value,
|
|
|
|
writable: false,
|
|
|
|
});
|
|
|
|
}
|
2019-06-12 01:01:04 -04:00
|
|
|
// Crawl up the constructor chain to find a static method
|
2019-08-25 02:39:20 -04:00
|
|
|
export function getStatic(ctor, key) {
|
|
|
|
for (let i = 0; i < 32; i++) {
|
2019-06-12 01:01:04 -04:00
|
|
|
if (ctor[key]) {
|
|
|
|
return ctor[key];
|
|
|
|
}
|
|
|
|
if (!ctor.prototype || typeof (ctor.prototype) !== "object") {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ctor = Object.getPrototypeOf(ctor.prototype).constructor;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function resolveProperties(object) {
|
2020-05-01 17:00:44 -04:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
const promises = Object.keys(object).map((key) => {
|
|
|
|
const value = object[key];
|
|
|
|
return Promise.resolve(value).then((v) => ({ key: key, value: v }));
|
2019-07-27 19:02:24 -03:00
|
|
|
});
|
2020-05-01 17:00:44 -04:00
|
|
|
const results = yield Promise.all(promises);
|
|
|
|
return results.reduce((accum, result) => {
|
|
|
|
accum[(result.key)] = result.value;
|
2019-07-27 19:02:24 -03:00
|
|
|
return accum;
|
2020-05-01 17:00:44 -04:00
|
|
|
}, {});
|
2019-05-14 18:48:48 -04:00
|
|
|
});
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function checkProperties(object, properties) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (!object || typeof (object) !== "object") {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("invalid object", "object", object);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
Object.keys(object).forEach((key) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (!properties[key]) {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("invalid object key - " + key, "transaction:" + key, object);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function shallowCopy(object) {
|
2019-11-20 18:57:38 +09:00
|
|
|
const result = {};
|
|
|
|
for (const key in object) {
|
2019-05-14 18:48:48 -04:00
|
|
|
result[key] = object[key];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2020-03-30 17:24:33 -04:00
|
|
|
const opaque = { bigint: true, boolean: true, "function": true, number: true, string: true };
|
|
|
|
function _isFrozen(object) {
|
|
|
|
// 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-06-11 17:57:04 -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) {
|
2020-03-30 17:24:33 -04:00
|
|
|
if (_isFrozen(object)) {
|
2019-05-14 18:48:48 -04:00
|
|
|
return object;
|
|
|
|
}
|
|
|
|
// Arrays are mutable, so we need to create a copy
|
|
|
|
if (Array.isArray(object)) {
|
2019-08-25 02:39:20 -04:00
|
|
|
return Object.freeze(object.map((item) => deepCopy(item)));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
if (typeof (object) === "object") {
|
2019-11-20 18:57:38 +09:00
|
|
|
const result = {};
|
|
|
|
for (const key in object) {
|
|
|
|
const value = object[key];
|
2019-05-14 18:48:48 -04:00
|
|
|
if (value === undefined) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-06-11 17:57:04 -04:00
|
|
|
defineReadOnly(result, key, deepCopy(value));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2020-03-30 17:24:33 -04:00
|
|
|
return logger.throwArgumentError(`Cannot deepCopy ${typeof (object)}`, "object", object);
|
2020-02-06 18:21:34 -05:00
|
|
|
}
|
|
|
|
export function deepCopy(object) {
|
|
|
|
return _deepCopy(object);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export class Description {
|
|
|
|
constructor(info) {
|
2019-11-20 18:57:38 +09:00
|
|
|
for (const key in info) {
|
2019-06-11 17:57:04 -04:00
|
|
|
this[key] = deepCopy(info[key]);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|