2017-08-08 03:45:08 +00:00
|
|
|
// 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
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
interface ABITypedSlot {
|
|
|
|
name: string;
|
|
|
|
type: ABIType;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ABIMethod {
|
|
|
|
name: string;
|
|
|
|
type: 'function';
|
|
|
|
constant: boolean;
|
|
|
|
inputs: ABITypedSlot[];
|
|
|
|
outputs: ABITypedSlot[];
|
2017-08-08 03:45:08 +00:00
|
|
|
// default - false
|
2017-09-25 02:06:28 +00:00
|
|
|
payable?: boolean;
|
|
|
|
}
|
2017-08-08 03:45:08 +00:00
|
|
|
|
|
|
|
export type ABI = ABIMethod[];
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export interface DecodedCall {
|
|
|
|
method: ABIMethod;
|
2017-08-23 06:57:18 +00:00
|
|
|
// TODO: Type this to be an array of BNs when we switch
|
2017-09-25 02:06:28 +00:00
|
|
|
args: any[];
|
|
|
|
}
|
2017-08-08 03:45:08 +00:00
|
|
|
|
|
|
|
// Contract helper, returns data for given call
|
|
|
|
export default class Contract {
|
2017-09-25 02:06:28 +00:00
|
|
|
public abi: ABI;
|
|
|
|
constructor(ethJsAbi: ABI) {
|
2017-08-23 06:57:18 +00:00
|
|
|
// TODO: Check ABI, throw if it's malformed
|
2017-09-25 02:06:28 +00:00
|
|
|
this.abi = ethJsAbi;
|
2017-08-08 03:45:08 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public getMethodAbi(name: string): ABIMethod {
|
2017-08-08 03:45:08 +00:00
|
|
|
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-09-25 02:06:28 +00:00
|
|
|
public getMethodTypes(method: ABIMethod): string[] {
|
2017-08-23 06:57:18 +00:00
|
|
|
return method.inputs.map(i => i.type);
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public getMethodSelector(method: ABIMethod): string {
|
2017-08-23 06:57:18 +00:00
|
|
|
return abi
|
|
|
|
.methodID(method.name, this.getMethodTypes(method))
|
|
|
|
.toString('hex');
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public call(name: string, args: any[]): string {
|
2017-08-08 03:45:08 +00:00
|
|
|
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-09-25 02:06:28 +00:00
|
|
|
public $call(data: string): DecodedCall {
|
2017-08-23 06:57:18 +00:00
|
|
|
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-09-25 02:06:28 +00:00
|
|
|
public encodeArgs(method: ABIMethod, args: any[]): string {
|
2017-08-08 03:45:08 +00:00
|
|
|
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
|
2017-09-25 02:06:28 +00:00
|
|
|
public decodeArgs(method: ABIMethod, argData: string): any[] {
|
2017-08-23 06:57:18 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|