aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contracts/util/multi_sig_wrapper.ts
blob: 0a066df536fa997d4c88dc03a1936a24b63cc3a6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import ABI = require('ethereumjs-abi');
import ethUtil = require('ethereumjs-util');
import * as _ from 'lodash';
import * as Web3 from 'web3';

import { TransactionDataParams } from './types';

export class MultiSigWrapper {
    private _multiSig: Web3.ContractInstance;
    public static encodeFnArgs(name: string, abi: Web3.AbiDefinition[], args: any[]) {
        const abiEntity = _.find(abi, { name }) as Web3.MethodAbi;
        if (_.isUndefined(abiEntity)) {
            throw new Error(`Did not find abi entry for name: ${name}`);
        }
        const types = _.map(abiEntity.inputs, input => input.type);
        const funcSig = ethUtil.bufferToHex(ABI.methodID(name, types));
        const argsData = _.map(args, arg => {
            const target = _.isBoolean(arg) ? +arg : arg;
            const targetBuff = ethUtil.toBuffer(target);
            return ethUtil.setLengthLeft(targetBuff, 32).toString('hex');
        });
        return funcSig + argsData.join('');
    }
    constructor(multiSigContractInstance: Web3.ContractInstance) {
        this._multiSig = multiSigContractInstance;
    }
    public async submitTransactionAsync(
        destination: string,
        from: string,
        dataParams: TransactionDataParams,
        value: number = 0,
    ) {
        const { name, abi, args = [] } = dataParams;
        const encoded = MultiSigWrapper.encodeFnArgs(name, abi, args);
        return this._multiSig.submitTransaction(destination, value, encoded, {
            from,
        });
    }
}