MyCrypto/common/libs/contracts/ABIFunction.ts

198 lines
5.8 KiB
TypeScript
Raw Normal View History

import abi from 'ethereumjs-abi';
import { toChecksumAddress } from 'ethereumjs-util';
Replace bignumber.js with bn.js (#319) * Add definition file for bn.js * Remove types-bn * make isBN a static property * progress commit -- swap out bignumber.js for bn.js * Swap out bignumber for bn in vendor * Change modn to number return * Start to strip out units lib for a string manipulation based lib * Convert codebase to only base units * Get rid of useless component * Handle only wei in values * Use unit conversion in sidebar * Automatically strip hex prefix, and handle decimal edge case * Handle base 16 wei in transactions * Make a render callback component for dealing with unit conversion * Switch contracts to use bn.js, and get transaction values from signedTx instead of state * Get send transaction working with bn.js * Remove redundant hex stripping, return base value of tokens * Cleanup unit file * Re-implement toFixed for strings * Use formatNumber in codebase * Cleanup code * Undo package test changes * Update snapshot and remove console logs * Use TokenValue / Wei more consistently where applicable * Add typing to deterministicWallets, fix confirmation modal, make UnitDisplay more flexible * Clean up prop handling in UnitDisplay * Change instanceof to typeof check, change boolean of displayBalance * Fix tsc errors * Fix token row displaying wrong decimals * Fix deterministic modal token display * Handle hex and non hex strings automatically in BN conversion * Fix handling of strings and numbers for BN * add web3 fixes & comments * Display short balances on deterministic modals * add more tests, fix rounding * Add spacer to balance sidebar network name * Fix tsc error
2017-11-12 19:45:52 +00:00
import BN from 'bn.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;
Replace bignumber.js with bn.js (#319) * Add definition file for bn.js * Remove types-bn * make isBN a static property * progress commit -- swap out bignumber.js for bn.js * Swap out bignumber for bn in vendor * Change modn to number return * Start to strip out units lib for a string manipulation based lib * Convert codebase to only base units * Get rid of useless component * Handle only wei in values * Use unit conversion in sidebar * Automatically strip hex prefix, and handle decimal edge case * Handle base 16 wei in transactions * Make a render callback component for dealing with unit conversion * Switch contracts to use bn.js, and get transaction values from signedTx instead of state * Get send transaction working with bn.js * Remove redundant hex stripping, return base value of tokens * Cleanup unit file * Re-implement toFixed for strings * Use formatNumber in codebase * Cleanup code * Undo package test changes * Update snapshot and remove console logs * Use TokenValue / Wei more consistently where applicable * Add typing to deterministicWallets, fix confirmation modal, make UnitDisplay more flexible * Clean up prop handling in UnitDisplay * Change instanceof to typeof check, change boolean of displayBalance * Fix tsc errors * Fix token row displaying wrong decimals * Fix deterministic modal token display * Handle hex and non hex strings automatically in BN conversion * Fix handling of strings and numbers for BN * add web3 fixes & comments * Display short balances on deterministic modals * add more tests, fix rounding * Add spacer to balance sidebar network name * Fix tsc error
2017-11-12 19:45:52 +00:00
gasLimit: BN;
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,
Contracts UI (#277) * 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 * Misc. Optimizations to tsconfig + webpack * Convert Contracts to TS * Remove nested prop passing from contracts, get rid of contract reducers / sagas / redux state * Add disclaimer modal to footer * Remove duplicate code & unnecessary styles * Add contracts to nav * Wrap Contracts in App * Add ether/hex validation override for contract creation calls * First iteration of working deploy contract * Delete routing file that shouldnt exist * Revert "Misc. Optimizations to tsconfig + webpack" This reverts commit 70cba3a07f4255153a9e277b3c41032a4b661c94. * Cleanup HOC code * Fix formatting noise * remove un-used css style * Remove deterministic contract address computation * 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 * Fix tslint error & add media query for modals * Nest Media Query * Fix contracts to include new router fixes * Add transaction capability to contracts * Get ABI parsing + contract calls almost fully integrated using dynamic contract parser lib * Refactor contract deploy to have a reusable HOC for contract interact * Move modal and tx comparasion up file tree * Include ABI outputs in display * Cleanup privaite/public members * Remove broadcasting step from a contract transaction * Update TX contract components to inter-op with interact and deploy * Finish contracts-interact functionality * Add transaction capability to contracts * Cleanup privaite/public members * Remove broadcasting step from a contract transaction * Apply James's CSS fix * Cleanup uneeded types * Remove unecessary class * Add UI side validation and helper utils, addresess PR comments * Fix spacing + remove unused imports / types * Fix spacing + remove unused imports / types * Address PR comments * Actually address PR comments * Actually address PR comments
2017-10-17 04:01:28 +00:00
transactionInput,
false
);
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)
Replace bignumber.js with bn.js (#319) * Add definition file for bn.js * Remove types-bn * make isBN a static property * progress commit -- swap out bignumber.js for bn.js * Swap out bignumber for bn in vendor * Change modn to number return * Start to strip out units lib for a string manipulation based lib * Convert codebase to only base units * Get rid of useless component * Handle only wei in values * Use unit conversion in sidebar * Automatically strip hex prefix, and handle decimal edge case * Handle base 16 wei in transactions * Make a render callback component for dealing with unit conversion * Switch contracts to use bn.js, and get transaction values from signedTx instead of state * Get send transaction working with bn.js * Remove redundant hex stripping, return base value of tokens * Cleanup unit file * Re-implement toFixed for strings * Use formatNumber in codebase * Cleanup code * Undo package test changes * Update snapshot and remove console logs * Use TokenValue / Wei more consistently where applicable * Add typing to deterministicWallets, fix confirmation modal, make UnitDisplay more flexible * Clean up prop handling in UnitDisplay * Change instanceof to typeof check, change boolean of displayBalance * Fix tsc errors * Fix token row displaying wrong decimals * Fix deterministic modal token display * Handle hex and non hex strings automatically in BN conversion * Fix handling of strings and numbers for BN * add web3 fixes & comments * Display short balances on deterministic modals * add more tests, fix rounding * Add spacer to balance sidebar network name * Fix tsc error
2017-11-12 19:45:52 +00:00
: BN.isBN(value) ? value.toString() : value;
};
private parsePreEncodedValue = (_: string, value: any) =>
Replace bignumber.js with bn.js (#319) * Add definition file for bn.js * Remove types-bn * make isBN a static property * progress commit -- swap out bignumber.js for bn.js * Swap out bignumber for bn in vendor * Change modn to number return * Start to strip out units lib for a string manipulation based lib * Convert codebase to only base units * Get rid of useless component * Handle only wei in values * Use unit conversion in sidebar * Automatically strip hex prefix, and handle decimal edge case * Handle base 16 wei in transactions * Make a render callback component for dealing with unit conversion * Switch contracts to use bn.js, and get transaction values from signedTx instead of state * Get send transaction working with bn.js * Remove redundant hex stripping, return base value of tokens * Cleanup unit file * Re-implement toFixed for strings * Use formatNumber in codebase * Cleanup code * Undo package test changes * Update snapshot and remove console logs * Use TokenValue / Wei more consistently where applicable * Add typing to deterministicWallets, fix confirmation modal, make UnitDisplay more flexible * Clean up prop handling in UnitDisplay * Change instanceof to typeof check, change boolean of displayBalance * Fix tsc errors * Fix token row displaying wrong decimals * Fix deterministic modal token display * Handle hex and non hex strings automatically in BN conversion * Fix handling of strings and numbers for BN * add web3 fixes & comments * Display short balances on deterministic modals * add more tests, fix rounding * Add spacer to balance sidebar network name * Fix tsc error
2017-11-12 19:45:52 +00:00
BN.isBN(value) ? value.toString() : value;
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;
});
}