dp-contract-proxy-kit
Version:
Enable batched transactions and contract account interactions using a unique deterministic Gnosis Safe.
254 lines • 9.24 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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.toTxResult = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const transactions_1 = require("../../utils/transactions");
const EthLibAdapter_1 = __importDefault(require("../EthLibAdapter"));
const Web3ContractAdapter_1 = __importDefault(require("./Web3ContractAdapter"));
function toTxResult(promiEvent, sendOptions) {
return new Promise((resolve, reject) => promiEvent
.once('transactionHash', (hash) => resolve({ sendOptions, promiEvent, hash }))
.catch(reject));
}
exports.toTxResult = toTxResult;
class Web3Adapter extends EthLibAdapter_1.default {
/**
* Creates an instance of Web3Adapter
*
* @param options - Web3Adapter configuration
* @returns The Web3Adapter instance
*/
constructor({ web3 }) {
super();
if (!web3) {
throw new Error('web3 property missing from options');
}
this.web3 = web3;
}
/**
* Returns the current provider
*
* @returns The current provider
*/
getProvider() {
return this.web3.currentProvider;
}
/**
* Sends a network request via JSON-RPC.
*
* @param method - JSON-RPC method
* @param params - Params
* @returns The request response
*/
providerSend(method, params) {
// TO-DO: Use semver comparison
return this.web3.version.split('.')[0] > 1
? this.web3.currentProvider.send(method, params)
: new Promise((resolve, reject) => this.web3.currentProvider.send({
jsonrpc: '2.0',
id: new Date().getTime(),
method,
params
}, (err, result) => {
if (err)
return reject(err);
if (result.error)
return reject(result.error);
return resolve(result.result);
}));
}
/**
* Signs data using a specific account.
*
* @param message - Data to sign
* @param ownerAccount - Address to sign the data with
* @returns The signature
*/
signMessage(message, ownerAccount) {
return this.web3.eth.sign(message, ownerAccount);
}
/**
* Returns the current network ID.
*
* @returns The network ID
*/
getNetworkId() {
return __awaiter(this, void 0, void 0, function* () {
return this.web3.eth.net.getId();
});
}
/**
* Returns the default account used as the default "from" property.
*
* @returns The default account address
*/
getAccount() {
return __awaiter(this, void 0, void 0, function* () {
return this.web3.eth.defaultAccount || (yield this.web3.eth.getAccounts())[0];
});
}
/**
* Returns the balance of an address.
*
* @param address - The desired address
* @returns The balance of the address
*/
getBalance(address) {
return __awaiter(this, void 0, void 0, function* () {
const balance = yield this.web3.eth.getBalance(address);
return new bignumber_js_1.default(balance.toString());
});
}
/**
* Returns the keccak256 hash of the data.
*
* @param data - Desired data
* @returns The keccak256 of the data
*/
keccak256(data) {
return this.web3.utils.keccak256(data);
}
/**
* Encodes a function parameters based on its JSON interface object.
*
* @param types - An array with the types or a JSON interface of a function
* @param values - The parameters to encode
* @returns The ABI encoded parameters
*/
abiEncode(types, values) {
return this.web3.eth.abi.encodeParameters(types, values);
}
/**
* Decodes ABI encoded parameters to is JavaScript types.
*
* @param types - An array with the types or a JSON interface outputs array
* @param data - The ABI byte code to decode
* @returns The ABI encoded parameters
*/
abiDecode(types, data) {
return this.web3.eth.abi.decodeParameters(types, data);
}
/**
* Deterministically returns the address where a contract will be deployed.
*
* @param deployer - Account that deploys the contract
* @param salt - Salt
* @param initCode - Code to be deployed
* @returns The address where the contract will be deployed
*/
calcCreate2Address(deployer, salt, initCode) {
return this.web3.utils.toChecksumAddress(this.web3.utils
.soliditySha3('0xff', { t: 'address', v: deployer }, { t: 'bytes32', v: salt }, this.keccak256(initCode))
.slice(-40));
}
/**
* Returns the code at a specific address.
*
* @param address - The desired address
* @returns The code of the contract
*/
getCode(address) {
return this.web3.eth.getCode(address);
}
/**
* Returns a block matching the block number or block hash.
*
* @param blockHashOrBlockNumber - The block number or block hash
* @returns The block object
*/
getBlock(blockHashOrBlockNumber) {
return this.web3.eth.getBlock(blockHashOrBlockNumber);
}
/**
* Returns an instance of a contract.
*
* @param abi - ABI of the desired contract
* @param address - Contract address
* @returns The contract instance
*/
getContract(abi, address) {
const contract = new this.web3.eth.Contract(abi, address);
return new Web3ContractAdapter_1.default(contract);
}
/**
* Returns the revert reason when a call fails.
*
* @param tx - Transaction to execute
* @param block - Block number
* @returns The revert data when the call fails
*/
getCallRevertData(tx, block) {
return __awaiter(this, void 0, void 0, function* () {
try {
// this block handles old Geth/Ganache --noVMErrorsOnRPCResponse
// use a low level eth_call instead of web3.eth.call so
// full error data from eth node is available if provider is Web3 1.x
return yield this.providerSend('eth_call', [transactions_1.formatCallTx(tx), block]);
}
catch (e) {
let errData = e.data;
if (!errData && e.message.startsWith('Node error: ')) {
// parse out error data from eth node if provider is Web3 2.x
errData = JSON.parse(e.message.slice(12)).data;
}
if (typeof errData === 'string') {
if (errData.startsWith('Reverted 0x'))
// handle OpenEthereum revert data format
return errData.slice(9);
if (errData.startsWith('0x'))
// handle new Geth format
return errData;
}
// handle Ganache revert data format
const txHash = Object.getOwnPropertyNames(errData).filter((k) => k.startsWith('0x'))[0];
return errData[txHash].return;
}
});
}
/**
* Sends a transaction to the network.
*
* @param tx - Transaction to send
* @returns The transaction response
*/
ethSendTransaction(tx) {
return toTxResult(this.web3.eth.sendTransaction(transactions_1.normalizeGasLimit(tx)), tx);
}
/**
* Formats transaction result depending on the current provider.
*
* @param txHash - Transaction hash
* @param tx - Transaction response
* @returns The formatted transaction response
*/
toSafeRelayTxResult(txHash, tx) {
tx['transactionHash'] = tx['txHash'];
delete tx['txHash'];
return new Promise((resolve, reject) => resolve({
promiEvent: new Promise((resolve, reject) => resolve(tx)),
hash: txHash
}));
}
toRocksideRelayTxResult(tx) {
tx['transactionHash'] = tx['transaction_hash'];
delete tx['transaction_hash'];
return new Promise((resolve, reject) => resolve({
promiEvent: new Promise((resolve, reject) => resolve(tx)),
hash: tx['transactionHash']
}));
}
}
exports.default = Web3Adapter;
//# sourceMappingURL=index.js.map