Contract Refactor (#175)

* Refactor BaseNode to be an interface INode

* Initial contract commit

* Remove redundant fallback ABI function

* First working iteration of Contract generator to be used in ENS branch

* Hide abi to clean up logging output

* Strip 0x prefix from output decode

* Handle unnamed output params

* Implement ability to supply output mappings to ABI functions

* Fix null case in outputMapping

* Add flow typing

* Add .call method to functions

* Partial commit for type refactor

* Temp contract type fix -- waiting for NPM modularization

* Remove empty files

* Cleanup contract

* Add call request to node interface

* Fix output mapping types

* Revert destructuring overboard

* Add sendCallRequest to rpcNode class and add typing

* Use enum for selecting ABI methods

* Add transaction capability to contracts

* Cleanup privaite/public members

* Remove broadcasting step from a contract transaction

* Cleanup uneeded types

* Fix spacing + remove unused imports /  types

* Actually address PR comments
This commit is contained in:
HenryNguyen5 2017-10-16 19:48:03 -04:00 committed by Daniel Ternyak
parent efed9b4803
commit 2f8e0fe272
6 changed files with 413 additions and 4 deletions

View File

@ -0,0 +1,205 @@
import abi from 'ethereumjs-abi';
import { toChecksumAddress } from 'ethereumjs-util';
import Big, { BigNumber } from 'bignumber.js';
import { INode } from 'libs/nodes/INode';
import { FuncParams, FunctionOutputMappings, Output, Input } from './types';
import {
generateCompleteTransaction as makeAndSignTx,
TransactionInput
} from 'libs/transaction';
import { ISetConfigForTx } from './index';
export interface IUserSendParams {
input;
to: string;
gasLimit: BigNumber;
value: string;
}
export type ISendParams = IUserSendParams & ISetConfigForTx;
export default class AbiFunction {
public constant: boolean;
public outputs: Output[];
public inputs: Input[];
private funcParams: FuncParams;
private inputNames: string[];
private inputTypes: string[];
private outputNames: string[];
private outputTypes: string[];
private methodSelector: string;
private name: string;
constructor(abiFunc: any, outputMappings: FunctionOutputMappings) {
Object.assign(this, abiFunc);
this.init(outputMappings);
}
public call = async (input, node: INode, to) => {
if (!node || !node.sendCallRequest) {
throw Error(`No node given to ${this.name}`);
}
const data = this.encodeInput(input);
const returnedData = await node
.sendCallRequest({
to,
data
})
.catch(e => {
throw Error(`Node call request error at: ${this.name}
Params:${JSON.stringify(input, null, 2)}
Message:${e.message}
EncodedCall:${data}`);
});
const decodedOutput = this.decodeOutput(returnedData);
return decodedOutput;
};
public send = async (params: ISendParams) => {
const { nodeLib, chainId, wallet, gasLimit, ...userInputs } = params;
if (!nodeLib || !nodeLib.sendRawTx) {
throw Error(`No node given to ${this.name}`);
}
const data = this.encodeInput(userInputs.input);
const transactionInput: TransactionInput = {
data,
to: userInputs.to,
unit: 'ether',
value: userInputs.value
};
const { signedTx, rawTx } = await makeAndSignTx(
wallet,
nodeLib,
userInputs.gasPrice,
gasLimit,
chainId,
transactionInput
);
return { signedTx, rawTx: JSON.parse(rawTx) };
};
public encodeInput = (suppliedInputs: object = {}) => {
const args = this.processSuppliedArgs(suppliedInputs);
const encodedCall = this.makeEncodedFuncCall(args);
return encodedCall;
};
public decodeInput = (argString: string) => {
// Remove method selector from data, if present
argString = argString.replace(`0x${this.methodSelector}`, '');
// Convert argdata to a hex buffer for ethereumjs-abi
const argBuffer = new Buffer(argString, 'hex');
// Decode!
const argArr = abi.rawDecode(this.inputTypes, argBuffer);
//TODO: parse checksummed addresses
return argArr.reduce((argObj, currArg, index) => {
const currName = this.inputNames[index];
const currType = this.inputTypes[index];
return {
...argObj,
[currName]: this.parsePostDecodedValue(currType, currArg)
};
}, {});
};
public decodeOutput = (argString: string) => {
// Remove method selector from data, if present
argString = argString.replace(`0x${this.methodSelector}`, '');
// Remove 0x prefix
argString = argString.replace('0x', '');
// Convert argdata to a hex buffer for ethereumjs-abi
const argBuffer = new Buffer(argString, 'hex');
// Decode!
const argArr = abi.rawDecode(this.outputTypes, argBuffer);
//TODO: parse checksummed addresses
return argArr.reduce((argObj, currArg, index) => {
const currName = this.outputNames[index];
const currType = this.outputTypes[index];
return {
...argObj,
[currName]: this.parsePostDecodedValue(currType, currArg)
};
}, {});
};
private init(outputMappings: FunctionOutputMappings = []) {
this.funcParams = this.makeFuncParams();
//TODO: do this in O(n)
this.inputTypes = this.inputs.map(({ type }) => type);
this.outputTypes = this.outputs.map(({ type }) => type);
this.inputNames = this.inputs.map(({ name }) => name);
this.outputNames = this.outputs.map(
({ name }, i) => outputMappings[i] || name || `${i}`
);
this.methodSelector = abi
.methodID(this.name, this.inputTypes)
.toString('hex');
}
private parsePostDecodedValue = (type: string, value: any) => {
const valueMapping = {
address: val => toChecksumAddress(val.toString(16))
};
return valueMapping[type]
? valueMapping[type](value)
: this.isBigNumber(value) ? value.toString() : value;
};
private parsePreEncodedValue = (_: string, value: any) =>
this.isBigNumber(value) ? value.toString() : value;
private isBigNumber = (object: object) =>
object instanceof Big ||
(object &&
object.constructor &&
(object.constructor.name === 'BigNumber' ||
object.constructor.name === 'BN'));
private makeFuncParams = () =>
this.inputs.reduce((accumulator, currInput) => {
const { name, type } = currInput;
const inputHandler = inputToParse =>
//TODO: introduce typechecking and typecasting mapping for inputs
({ name, type, value: this.parsePreEncodedValue(type, inputToParse) });
return {
...accumulator,
[name]: { processInput: inputHandler, type, name }
};
}, {});
private makeEncodedFuncCall = (args: string[]) => {
const encodedArgs = abi.rawEncode(this.inputTypes, args).toString('hex');
return `0x${this.methodSelector}${encodedArgs}`;
};
private processSuppliedArgs = (suppliedArgs: object) =>
this.inputNames.map(name => {
const type = this.funcParams[name].type;
//TODO: parse args based on type
if (!suppliedArgs[name]) {
throw Error(
`Expected argument "${name}" of type "${type}" missing, suppliedArgs: ${JSON.stringify(
suppliedArgs,
null,
2
)}`
);
}
const value = suppliedArgs[name];
const processedArg = this.funcParams[name].processInput(value);
return processedArg.value;
});
}

View File

@ -0,0 +1,153 @@
import AbiFunction, { IUserSendParams, ISendParams } from './ABIFunction';
import { IWallet } from 'libs/wallet/IWallet';
import { RPCNode } from 'libs/nodes';
import { ContractOutputMappings } from './types';
import { Wei } from 'libs/units';
const ABIFUNC_METHOD_NAMES = [
'encodeInput',
'decodeInput',
'decodeOutput',
'call'
];
export interface ISetConfigForTx {
wallet: IWallet;
nodeLib: RPCNode;
chainId: number;
gasPrice: Wei;
}
enum ABIMethodTypes {
FUNC = 'function'
}
export type TContract = typeof Contract;
export default class Contract {
public static setConfigForTx = (
contract: Contract,
{ wallet, nodeLib, chainId, gasPrice }: ISetConfigForTx
): Contract =>
contract
.setWallet(wallet)
.setNode(nodeLib)
.setChainId(chainId)
.setGasPrice(gasPrice);
public static getFunctions = (contract: Contract) =>
Object.getOwnPropertyNames(
contract
).reduce((accu, currContractMethodName) => {
const currContractMethod = contract[currContractMethodName];
const methodNames = Object.getOwnPropertyNames(currContractMethod);
const isFunc = ABIFUNC_METHOD_NAMES.reduce(
(isAbiFunc, currAbiFuncMethodName) =>
isAbiFunc && methodNames.includes(currAbiFuncMethodName),
true
);
return isFunc
? { ...accu, [currContractMethodName]: currContractMethod }
: accu;
}, {});
public address: string;
public abi;
private wallet: IWallet;
private gasPrice: Wei;
private chainId: number;
private node: RPCNode;
constructor(abi, outputMappings: ContractOutputMappings = {}) {
this.assignABIFuncs(abi, outputMappings);
}
public at = (addr: string) => {
this.address = addr;
return this;
};
public setWallet = (w: IWallet) => {
this.wallet = w;
return this;
};
public setGasPrice = (gasPrice: Wei) => {
this.gasPrice = gasPrice;
return this;
};
public setChainId = (chainId: number) => {
this.chainId = chainId;
return this;
};
public setNode = (node: RPCNode) => {
//TODO: caching
this.node = node;
return this;
};
private assignABIFuncs = (abi, outputMappings: ContractOutputMappings) => {
abi.forEach(currentABIMethod => {
const { name, type } = currentABIMethod;
if (type === ABIMethodTypes.FUNC) {
//only grab the functions we need
const {
encodeInput,
decodeInput,
decodeOutput,
call,
send,
constant,
outputs,
inputs
} = new AbiFunction(currentABIMethod, outputMappings[name]);
const proxiedCall = new Proxy(call, {
apply: this.applyTrapForCall
});
const proxiedSend = new Proxy(send, { apply: this.applyTrapForSend });
const funcToAssign = {
[name]: {
encodeInput,
decodeInput,
decodeOutput,
call: proxiedCall,
send: proxiedSend,
constant,
outputs,
inputs
}
};
Object.assign(this, funcToAssign);
}
});
};
private applyTrapForCall = (target, _, argumentsList) => {
return target(
//TODO: pass object instead
...(argumentsList.length > 0 ? argumentsList : [null]),
this.node,
this.address
);
};
private applyTrapForSend = (
target: (sendParams: ISendParams) => void,
_,
[userSendParams]: [IUserSendParams]
) => {
return target({
chainId: this.chainId,
gasPrice: this.gasPrice,
to: this.address,
nodeLib: this.node,
wallet: this.wallet,
...userSendParams
});
};
}

View File

@ -0,0 +1,38 @@
/**
* @export
* @interface Input
*/
export interface Input {
/**
* @type {string}
* @memberof Input
* @desc The name of the parameter.
*/
name: string;
/**
* @type {string}
* @memberof Input
* @desc The canonical type of the parameter.
*/
type: string;
}
export type Output = Input;
/**
*
* @export
* @interface ABIFunction
* @template T
*/
export interface ContractOutputMappings {
[key: string]: string[];
}
export type FunctionOutputMappings = string[];
export interface FuncParams {
[name: string]: {
type: string;
processInput(value: any): any;
};
}

View File

@ -3,6 +3,10 @@ import { Token } from 'config/data';
import { TransactionWithoutGas } from 'libs/messages';
import { Wei } from 'libs/units';
export interface TxObj {
to: string;
data: string;
}
export interface INode {
getBalance(address: string): Promise<Wei>;
getTokenBalance(address: string, token: Token): Promise<BigNumber>;
@ -10,4 +14,5 @@ export interface INode {
estimateGas(tx: TransactionWithoutGas): Promise<BigNumber>;
getTransactionCount(address: string): Promise<string>;
sendRawTx(tx: string): Promise<string>;
sendCallRequest(txObj: TxObj): Promise<string>;
}

View File

@ -2,7 +2,7 @@ import Big, { BigNumber } from 'bignumber.js';
import { Token } from 'config/data';
import { TransactionWithoutGas } from 'libs/messages';
import { Wei } from 'libs/units';
import { INode } from '../INode';
import { INode, TxObj } from '../INode';
import RPCClient from './client';
import RPCRequests from './requests';
@ -15,6 +15,14 @@ export default class RpcNode implements INode {
this.requests = new RPCRequests();
}
public sendCallRequest(txObj: TxObj): Promise<string> {
return this.client.call(this.requests.ethCall(txObj)).then(r => {
if (r.error) {
throw Error(r.error.message);
}
return r.result;
});
}
public getBalance(address: string): Promise<Wei> {
return this.client
.call(this.requests.getBalance(address))

View File

@ -9,7 +9,7 @@ import {
SendRawTxRequest
} from './types';
import { hexEncodeData } from './utils';
import { TxObj } from '../INode';
export default class RPCRequests {
public sendRawTx(signedTx: string): SendRawTxRequest | any {
return {
@ -32,10 +32,10 @@ export default class RPCRequests {
};
}
public ethCall(transaction): CallRequest | any {
public ethCall(txObj: TxObj): CallRequest | any {
return {
method: 'eth_call',
params: [transaction, 'pending']
params: [txObj, 'pending']
};
}