contract-proxy-kit
Version:
Enable batched transactions and contract account interactions using a unique deterministic Gnosis Safe.
153 lines • 7.46 kB
JavaScript
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 _versionUtils, _contract, _proxyFactory, _masterCopyAddress, _multiSend, _fallbackHandlerAddress;
import cpkFactoryAbi from '../abis/CpkFactoryAbi.json';
import multiSendAbi from '../abis/MultiSendAbi.json';
import safeAbiV111 from '../abis/SafeAbiV1-1-1.json';
import safeAbiV120 from '../abis/SafeAbiV1-2-0.json';
import ContractV111Utils from './ContractV111Utils';
import ContractV120Utils from './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, network.multiSendAddress));
__classPrivateFieldSet(this, _proxyFactory, ethLibAdapter.getContract(cpkFactoryAbi, 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(safeAbiV111, 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(safeAbiV111, 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(safeAbiV120, proxyAddress));
__classPrivateFieldSet(this, _versionUtils, new ContractV120Utils(__classPrivateFieldGet(this, _contract)));
break;
case '1.1.1':
__classPrivateFieldSet(this, _contract, ethLibAdapter.getContract(safeAbiV111, proxyAddress));
__classPrivateFieldSet(this, _versionUtils, new ContractV111Utils(__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();
export default ContractManager;
//# sourceMappingURL=index.js.map