mirror of
https://github.com/status-im/MyCrypto.git
synced 2025-01-11 11:34:26 +00:00
bf4171dfbd
* Add a little arrow icon. * Replaced toEther function with toUnit to reduce the number of conversion functions wed need. Add tests for conversion functions. * First pass at a styled confirm transaction modal. * More data about data * Hook up generated transaction with modal * Fix modal position * Add from address. Restyle a bit. * Only show textareas and button if transaction has been generated. * Remove need for param. * Copy. * Use non-relative path. * Initial crack at transaction token support. * Fix flow type * Unit tests for contracts and erc20 * Convert contract class to ethereumjs-abi, caught a bug * Add decodeArgs for contracts, decodeTransfer for erc20 * Show token value in modal * Show value from transaction data in confirmation. * Show address of receiver, not token contract * Flow type * Only accept bigs * Unlog * Use ethereumjs-abis method ID function * Get transaction stuff out of state. Leave todo notes. * Intuit token from transaction to address. * Move generate transaction out of node and into libs/transaction. * timeout -> interval * Promise.reject -> throw * Get default currency from network. * Add more unit tests for decoding. Adopt the $ prefix for decoding calls. * Use signed transaction in confirmation modal.
103 lines
2.5 KiB
JavaScript
103 lines
2.5 KiB
JavaScript
// @flow
|
|
// TODO support events, constructors, fallbacks, array slots, types
|
|
import abi from 'ethereumjs-abi';
|
|
|
|
// There are too many to enumerate since they're somewhat dynamic, list here
|
|
// https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI#types
|
|
type ABIType = string;
|
|
|
|
type ABITypedSlot = {
|
|
name: string,
|
|
type: ABIType
|
|
};
|
|
|
|
type ABIMethod = {
|
|
name: string,
|
|
type: 'function',
|
|
constant: boolean,
|
|
inputs: ABITypedSlot[],
|
|
outputs: ABITypedSlot[],
|
|
// default - false
|
|
payable?: boolean
|
|
};
|
|
|
|
export type ABI = ABIMethod[];
|
|
|
|
export type DecodedCall = {
|
|
method: ABIMethod,
|
|
// TODO: Type this to be an array of BNs when we switch
|
|
args: Array<any>
|
|
};
|
|
|
|
// Contract helper, returns data for given call
|
|
export default class Contract {
|
|
abi: ABI;
|
|
constructor(abi: ABI) {
|
|
// TODO: Check ABI, throw if it's malformed
|
|
this.abi = abi;
|
|
}
|
|
|
|
getMethodAbi(name: string): ABIMethod {
|
|
const method = this.abi.find(x => x.name === name);
|
|
if (!method) {
|
|
throw new Error('Unknown method');
|
|
}
|
|
if (method.type !== 'function') {
|
|
throw new Error('Not a function');
|
|
}
|
|
return method;
|
|
}
|
|
|
|
getMethodTypes(method: ABIMethod): string[] {
|
|
return method.inputs.map(i => i.type);
|
|
}
|
|
|
|
getMethodSelector(method: ABIMethod): string {
|
|
return abi
|
|
.methodID(method.name, this.getMethodTypes(method))
|
|
.toString('hex');
|
|
}
|
|
|
|
call(name: string, args: any[]): string {
|
|
const method = this.getMethodAbi(name);
|
|
|
|
return (
|
|
'0x' + this.getMethodSelector(method) + this.encodeArgs(method, args)
|
|
);
|
|
}
|
|
|
|
$call(data: string): DecodedCall {
|
|
const method = this.abi.find(
|
|
mth => data.indexOf(this.getMethodSelector(mth)) !== -1
|
|
);
|
|
|
|
if (!method) {
|
|
throw new Error('Unknown method');
|
|
}
|
|
|
|
return {
|
|
method,
|
|
args: this.decodeArgs(method, data)
|
|
};
|
|
}
|
|
|
|
encodeArgs(method: ABIMethod, args: any[]): string {
|
|
if (method.inputs.length !== args.length) {
|
|
throw new Error('Invalid number of arguments');
|
|
}
|
|
|
|
const inputTypes = method.inputs.map(input => input.type);
|
|
return abi.rawEncode(inputTypes, args).toString('hex');
|
|
}
|
|
|
|
// TODO: Type this return to be an array of BNs when we switch
|
|
decodeArgs(method: ABIMethod, argData: string): Array<any> {
|
|
// Remove method selector from data, if present
|
|
argData = argData.replace(`0x${this.getMethodSelector(method)}`, '');
|
|
// Convert argdata to a hex buffer for ethereumjs-abi
|
|
const argBuffer = new Buffer(argData, 'hex');
|
|
// Decode!
|
|
return abi.rawDecode(this.getMethodTypes(method), argBuffer);
|
|
}
|
|
}
|