2017-08-08 03:45:08 +00:00
|
|
|
// @flow
|
|
|
|
// TODO support events, constructors, fallbacks, array slots, types
|
2017-08-23 06:57:18 +00:00
|
|
|
import abi from 'ethereumjs-abi';
|
2017-08-08 03:45:08 +00:00
|
|
|
|
2017-08-23 06:57:18 +00:00
|
|
|
// 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;
|
2017-08-08 03:45:08 +00:00
|
|
|
|
|
|
|
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[];
|
|
|
|
|
2017-08-23 06:57:18 +00:00
|
|
|
export type DecodedCall = {
|
|
|
|
method: ABIMethod,
|
|
|
|
// TODO: Type this to be an array of BNs when we switch
|
|
|
|
args: Array<any>
|
|
|
|
};
|
2017-08-08 03:45:08 +00:00
|
|
|
|
|
|
|
// Contract helper, returns data for given call
|
|
|
|
export default class Contract {
|
|
|
|
abi: ABI;
|
|
|
|
constructor(abi: ABI) {
|
2017-08-23 06:57:18 +00:00
|
|
|
// TODO: Check ABI, throw if it's malformed
|
2017-08-08 03:45:08 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-08-23 06:57:18 +00:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2017-08-08 03:45:08 +00:00
|
|
|
call(name: string, args: any[]): string {
|
|
|
|
const method = this.getMethodAbi(name);
|
|
|
|
|
|
|
|
return (
|
2017-08-23 06:57:18 +00:00
|
|
|
'0x' + this.getMethodSelector(method) + this.encodeArgs(method, args)
|
2017-08-08 03:45:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-08-23 06:57:18 +00:00
|
|
|
$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)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-08-08 03:45:08 +00:00
|
|
|
encodeArgs(method: ABIMethod, args: any[]): string {
|
|
|
|
if (method.inputs.length !== args.length) {
|
|
|
|
throw new Error('Invalid number of arguments');
|
|
|
|
}
|
|
|
|
|
2017-08-23 06:57:18 +00:00
|
|
|
const inputTypes = method.inputs.map(input => input.type);
|
|
|
|
return abi.rawEncode(inputTypes, args).toString('hex');
|
2017-08-08 03:45:08 +00:00
|
|
|
}
|
|
|
|
|
2017-08-23 06:57:18 +00:00
|
|
|
// 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);
|
2017-08-08 03:45:08 +00:00
|
|
|
}
|
|
|
|
}
|