contract-proxy-kit
Version:
Enable batched transactions and contract account interactions using a unique deterministic Gnosis Safe.
158 lines • 7.96 kB
JavaScript
"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, privateMap, value) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to set private field on non-instance");
}
privateMap.set(receiver, value);
return value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, privateMap) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to get private field on non-instance");
}
return privateMap.get(receiver);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _versionUtils, _contract, _proxyFactory, _masterCopyAddress, _multiSend, _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) {
_versionUtils.set(this, void 0);
_contract.set(this, void 0);
_proxyFactory.set(this, void 0);
_masterCopyAddress.set(this, void 0);
_multiSend.set(this, void 0);
_fallbackHandlerAddress.set(this, void 0);
__classPrivateFieldSet(this, _masterCopyAddress, network.masterCopyAddressVersions[0].address);
__classPrivateFieldSet(this, _fallbackHandlerAddress, network.fallbackHandlerAddress);
__classPrivateFieldSet(this, _multiSend, ethLibAdapter.getContract(MultiSendAbi_json_1.default, network.multiSendAddress));
__classPrivateFieldSet(this, _proxyFactory, ethLibAdapter.getContract(CpkFactoryAbi_json_1.default, network.proxyFactoryAddress));
}
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, _contract, ethLibAdapter.getContract(SafeAbiV1_2_0_json_1.default, proxyAddress));
__classPrivateFieldSet(this, _versionUtils, new ContractV120Utils_1.default(__classPrivateFieldGet(this, _contract)));
break;
case '1.1.1':
__classPrivateFieldSet(this, _contract, ethLibAdapter.getContract(SafeAbiV1_1_1_json_1.default, proxyAddress));
__classPrivateFieldSet(this, _versionUtils, new ContractV111Utils_1.default(__classPrivateFieldGet(this, _contract)));
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, _proxyFactory).call('proxyCreationCode', []) }, {
type: 'bytes',
value: ethLibAdapter.abiEncode(['address'], [masterCopyAddress])
});
const proxyAddress = ethLibAdapter.calcCreate2Address(__classPrivateFieldGet(this, _proxyFactory).address, salt, initCode);
return proxyAddress;
});
}
get versionUtils() {
return __classPrivateFieldGet(this, _versionUtils);
}
/**
* Returns the instance of the Safe contract in use.
*
* @returns The instance of the Safe contract in use
*/
get contract() {
return __classPrivateFieldGet(this, _contract);
}
/**
* 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, _proxyFactory);
}
/**
* Returns the Master Copy contract address in use.
*
* @returns The Master Copy contract address in use
*/
get masterCopyAddress() {
return __classPrivateFieldGet(this, _masterCopyAddress);
}
/**
* Returns the instance of the MultiSend contract in use.
*
* @returns The instance of the MultiSend contract in use
*/
get multiSend() {
return __classPrivateFieldGet(this, _multiSend);
}
/**
* Returns the FallbackHandler contract address in use.
*
* @returns The FallbackHandler contract address in use
*/
get fallbackHandlerAddress() {
return __classPrivateFieldGet(this, _fallbackHandlerAddress);
}
}
_versionUtils = new WeakMap(), _contract = new WeakMap(), _proxyFactory = new WeakMap(), _masterCopyAddress = new WeakMap(), _multiSend = new WeakMap(), _fallbackHandlerAddress = new WeakMap();
exports.default = ContractManager;
//# sourceMappingURL=index.js.map