UNPKG

dp-contract-proxy-kit

Version:

Enable batched transactions and contract account interactions using a unique deterministic Gnosis Safe.

156 lines 9.15 kB
"use strict"; 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()); }); }; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _ContractManager_versionUtils, _ContractManager_contract, _ContractManager_proxyFactory, _ContractManager_masterCopyAddress, _ContractManager_multiSend, _ContractManager_fallbackHandlerAddress; Object.defineProperty(exports, "__esModule", { value: true }); const CpkFactoryAbi_json_1 = __importDefault(require("../abis/CpkFactoryAbi.json")); const MultiSendAbi_json_1 = __importDefault(require("../abis/MultiSendAbi.json")); const SafeAbiV1_1_1_json_1 = __importDefault(require("../abis/SafeAbiV1-1-1.json")); const SafeAbiV1_2_0_json_1 = __importDefault(require("../abis/SafeAbiV1-2-0.json")); const ContractV111Utils_1 = __importDefault(require("./ContractV111Utils")); const ContractV120Utils_1 = __importDefault(require("./ContractV120Utils")); class ContractManager { constructor(ethLibAdapter, network) { _ContractManager_versionUtils.set(this, void 0); _ContractManager_contract.set(this, void 0); _ContractManager_proxyFactory.set(this, void 0); _ContractManager_masterCopyAddress.set(this, void 0); _ContractManager_multiSend.set(this, void 0); _ContractManager_fallbackHandlerAddress.set(this, void 0); __classPrivateFieldSet(this, _ContractManager_masterCopyAddress, network.masterCopyAddressVersions[0].address, "f"); __classPrivateFieldSet(this, _ContractManager_fallbackHandlerAddress, network.fallbackHandlerAddress, "f"); __classPrivateFieldSet(this, _ContractManager_multiSend, ethLibAdapter.getContract(MultiSendAbi_json_1.default, network.multiSendAddress), "f"); __classPrivateFieldSet(this, _ContractManager_proxyFactory, ethLibAdapter.getContract(CpkFactoryAbi_json_1.default, network.proxyFactoryAddress), "f"); } static create(opts) { return __awaiter(this, void 0, void 0, function* () { const contractManager = new ContractManager(opts.ethLibAdapter, opts.network); yield contractManager.init(opts); return contractManager; }); } init(opts) { return __awaiter(this, void 0, void 0, function* () { yield this.calculateVersionUtils(opts); }); } calculateVersionUtils(opts) { return __awaiter(this, void 0, void 0, function* () { const { ethLibAdapter, ownerAccount, saltNonce, network, isSafeApp, isConnectedToSafe } = opts; let proxyAddress; let properVersion; if (isSafeApp || isConnectedToSafe) { const temporaryContract = ethLibAdapter.getContract(SafeAbiV1_1_1_json_1.default, ownerAccount); properVersion = yield temporaryContract.call('VERSION', []); proxyAddress = ownerAccount; } else { const salt = ethLibAdapter.keccak256(ethLibAdapter.abiEncode(['address', 'uint256'], [ownerAccount, saltNonce])); for (const masterCopyVersion of network.masterCopyAddressVersions) { proxyAddress = yield this.calculateProxyAddress(masterCopyVersion.address, salt, ethLibAdapter); const codeAtAddress = yield ethLibAdapter.getCode(proxyAddress); const isDeployed = codeAtAddress !== '0x'; if (isDeployed) { const temporaryContract = ethLibAdapter.getContract(SafeAbiV1_1_1_json_1.default, proxyAddress); properVersion = yield temporaryContract.call('VERSION', []); break; } } if (!properVersion) { // Last version released properVersion = network.masterCopyAddressVersions[0].version; proxyAddress = yield this.calculateProxyAddress(network.masterCopyAddressVersions[0].address, salt, ethLibAdapter); } } switch (properVersion) { case '1.2.0': __classPrivateFieldSet(this, _ContractManager_contract, ethLibAdapter.getContract(SafeAbiV1_2_0_json_1.default, proxyAddress), "f"); __classPrivateFieldSet(this, _ContractManager_versionUtils, new ContractV120Utils_1.default(__classPrivateFieldGet(this, _ContractManager_contract, "f")), "f"); break; case '1.1.1': __classPrivateFieldSet(this, _ContractManager_contract, ethLibAdapter.getContract(SafeAbiV1_1_1_json_1.default, proxyAddress), "f"); __classPrivateFieldSet(this, _ContractManager_versionUtils, new ContractV111Utils_1.default(__classPrivateFieldGet(this, _ContractManager_contract, "f")), "f"); break; default: throw new Error('CPK Proxy version is not valid'); } }); } calculateProxyAddress(masterCopyAddress, salt, ethLibAdapter) { return __awaiter(this, void 0, void 0, function* () { const initCode = ethLibAdapter.abiEncodePacked({ type: 'bytes', value: yield __classPrivateFieldGet(this, _ContractManager_proxyFactory, "f").call('proxyCreationCode', []) }, { type: 'bytes', value: ethLibAdapter.abiEncode(['address'], [masterCopyAddress]) }); const proxyAddress = ethLibAdapter.calcCreate2Address(__classPrivateFieldGet(this, _ContractManager_proxyFactory, "f").address, salt, initCode); return proxyAddress; }); } get versionUtils() { return __classPrivateFieldGet(this, _ContractManager_versionUtils, "f"); } /** * Returns the instance of the Safe contract in use. * * @returns The instance of the Safe contract in use */ get contract() { return __classPrivateFieldGet(this, _ContractManager_contract, "f"); } /** * Returns the instance of the Proxy Factory contract in use. * * @returns The instance of the Proxy Factory contract in use */ get proxyFactory() { return __classPrivateFieldGet(this, _ContractManager_proxyFactory, "f"); } /** * Returns the Master Copy contract address in use. * * @returns The Master Copy contract address in use */ get masterCopyAddress() { return __classPrivateFieldGet(this, _ContractManager_masterCopyAddress, "f"); } /** * Returns the instance of the MultiSend contract in use. * * @returns The instance of the MultiSend contract in use */ get multiSend() { return __classPrivateFieldGet(this, _ContractManager_multiSend, "f"); } /** * Returns the FallbackHandler contract address in use. * * @returns The FallbackHandler contract address in use */ get fallbackHandlerAddress() { return __classPrivateFieldGet(this, _ContractManager_fallbackHandlerAddress, "f"); } } _ContractManager_versionUtils = new WeakMap(), _ContractManager_contract = new WeakMap(), _ContractManager_proxyFactory = new WeakMap(), _ContractManager_masterCopyAddress = new WeakMap(), _ContractManager_multiSend = new WeakMap(), _ContractManager_fallbackHandlerAddress = new WeakMap(); exports.default = ContractManager; //# sourceMappingURL=index.js.map