2016-07-25 10:55:16 +03:00
|
|
|
'use strict';
|
|
|
|
|
2016-07-16 06:47:35 +03:00
|
|
|
var utils = require('./utils.js');
|
|
|
|
|
2016-07-21 11:21:44 +03:00
|
|
|
// Creates property that is immutable
|
2016-07-16 06:47:35 +03:00
|
|
|
function defineFrozen(object, name, value) {
|
|
|
|
var frozen = JSON.stringify(value);
|
|
|
|
Object.defineProperty(object, name, {
|
|
|
|
enumerable: true,
|
|
|
|
get: function() { return JSON.parse(frozen); }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-21 11:21:44 +03:00
|
|
|
// getKeys([{a: 1, b: 2}, {a: 3, b: 4}], 'a') => [1, 3]
|
2016-11-10 01:27:34 +03:00
|
|
|
function getKeys(params, key, allowEmpty) {
|
2016-07-16 06:47:35 +03:00
|
|
|
if (!Array.isArray(params)) { throw new Error('invalid params'); }
|
|
|
|
|
|
|
|
var result = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < params.length; i++) {
|
2016-11-10 01:27:34 +03:00
|
|
|
var value = params[i][key];
|
|
|
|
if (allowEmpty && !value) {
|
|
|
|
value = '';
|
|
|
|
} else if (typeof(value) !== 'string') {
|
|
|
|
throw new Error('invalid abi');
|
|
|
|
}
|
|
|
|
result.push(value);
|
2016-07-16 06:47:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-07-21 11:21:44 +03:00
|
|
|
// Convert the value from a Number to a BN (if necessary)
|
2016-07-21 01:06:03 +03:00
|
|
|
function numberOrBN(value) {
|
|
|
|
if (!value.eq) {
|
|
|
|
if (typeof(value) !== 'number') {
|
|
|
|
throw new Error('invalid number');
|
|
|
|
}
|
|
|
|
value = new utils.BN(value);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2016-07-29 11:45:46 +03:00
|
|
|
function zpad(buffer, length) {
|
|
|
|
var zero = new Buffer([0]);
|
|
|
|
while (buffer.length < length) {
|
|
|
|
buffer = Buffer.concat([zero, buffer]);
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2016-07-21 11:21:44 +03:00
|
|
|
function coderNumber(size, signed) {
|
2016-07-21 01:06:03 +03:00
|
|
|
return {
|
|
|
|
encode: function(value) {
|
|
|
|
value = numberOrBN(value)
|
2016-11-10 00:39:33 +03:00
|
|
|
value = value.toTwos(size * 8).maskn(size * 8);
|
2016-07-29 11:45:46 +03:00
|
|
|
if (signed) {
|
2016-08-02 00:57:00 +03:00
|
|
|
value = value.fromTwos(size * 8).toTwos(256);
|
2016-07-29 11:45:46 +03:00
|
|
|
}
|
|
|
|
return value.toArrayLike(Buffer, 'be', 32);
|
2016-07-21 01:06:03 +03:00
|
|
|
},
|
|
|
|
decode: function(data, offset) {
|
2016-08-02 01:26:12 +03:00
|
|
|
var junkLength = 32 - size;
|
|
|
|
var value = new utils.BN(data.slice(offset + junkLength, offset + 32));
|
2016-07-29 11:45:46 +03:00
|
|
|
if (signed) {
|
|
|
|
value = value.fromTwos(size * 8);
|
|
|
|
} else {
|
2016-11-10 00:39:33 +03:00
|
|
|
value = value.maskn(size * 8);
|
2016-07-29 11:45:46 +03:00
|
|
|
}
|
2016-07-21 01:06:03 +03:00
|
|
|
return {
|
|
|
|
consumed: 32,
|
|
|
|
value: value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2016-07-21 11:21:44 +03:00
|
|
|
var uint256Coder = coderNumber(32, false);
|
|
|
|
|
|
|
|
var coderBoolean = {
|
|
|
|
encode: function(value) {
|
|
|
|
return uint256Coder.encode(value ? 1: 0);
|
|
|
|
},
|
|
|
|
decode: function(data, offset) {
|
|
|
|
var result = uint256Coder.decode(data, offset);
|
|
|
|
return {
|
|
|
|
consumed: result.consumed,
|
|
|
|
value: !result.value.isZero()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-21 01:06:03 +03:00
|
|
|
|
|
|
|
function coderFixedBytes(length) {
|
|
|
|
return {
|
|
|
|
encode: function(value) {
|
|
|
|
value = utils.hexOrBuffer(value);
|
|
|
|
if (length === 32) { return value; }
|
|
|
|
|
|
|
|
var result = new Buffer(32);
|
|
|
|
result.fill(0);
|
|
|
|
value.copy(result);
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
decode: function(data, offset) {
|
|
|
|
if (data.length < offset + 32) { throw new Error('invalid bytes' + length); }
|
|
|
|
|
|
|
|
return {
|
|
|
|
consumed: 32,
|
|
|
|
value: '0x' + data.slice(offset, offset + length).toString('hex')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
var coderAddress = {
|
|
|
|
encode: function(value) {
|
|
|
|
if (!utils.isHexString(value, 20)) { throw new Error('invalid address'); }
|
|
|
|
value = utils.hexOrBuffer(value);
|
|
|
|
var result = new Buffer(32);
|
|
|
|
result.fill(0);
|
|
|
|
value.copy(result, 12);
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
decode: function(data, offset) {
|
|
|
|
if (data.length < offset + 32) { throw new Error('invalid address'); }
|
|
|
|
return {
|
|
|
|
consumed: 32,
|
|
|
|
value: '0x' + data.slice(offset + 12, offset + 32).toString('hex')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _encodeDynamicBytes(value) {
|
|
|
|
var dataLength = parseInt(32 * Math.ceil(value.length / 32));
|
|
|
|
var padding = new Buffer(dataLength - value.length);
|
|
|
|
padding.fill(0);
|
|
|
|
|
|
|
|
return Buffer.concat([
|
2016-07-21 11:21:44 +03:00
|
|
|
uint256Coder.encode(value.length),
|
2016-07-21 01:06:03 +03:00
|
|
|
value,
|
|
|
|
padding
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function _decodeDynamicBytes(data, offset) {
|
|
|
|
if (data.length < offset + 32) { throw new Error('invalid bytes'); }
|
|
|
|
|
2016-07-21 11:21:44 +03:00
|
|
|
var length = uint256Coder.decode(data, offset).value;
|
2016-07-21 01:06:03 +03:00
|
|
|
length = length.toNumber();
|
|
|
|
if (data.length < offset + 32 + length) { throw new Error('invalid bytes'); }
|
|
|
|
|
|
|
|
return {
|
|
|
|
consumed: parseInt(32 + 32 * Math.ceil(length / 32)),
|
|
|
|
value: data.slice(offset + 32, offset + 32 + length),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var coderDynamicBytes = {
|
|
|
|
encode: function(value) {
|
|
|
|
return _encodeDynamicBytes(utils.hexOrBuffer(value));
|
|
|
|
},
|
|
|
|
decode: function(data, offset) {
|
|
|
|
var result = _decodeDynamicBytes(data, offset);
|
|
|
|
result.value = '0x' + result.value.toString('hex');
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
dynamic: true
|
|
|
|
};
|
|
|
|
|
|
|
|
var coderString = {
|
|
|
|
encode: function(value) {
|
|
|
|
return _encodeDynamicBytes(new Buffer(value, 'utf8'));
|
|
|
|
},
|
|
|
|
decode: function(data, offset) {
|
|
|
|
var result = _decodeDynamicBytes(data, offset);
|
|
|
|
result.value = result.value.toString('utf8');
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
dynamic: true
|
|
|
|
};
|
|
|
|
|
|
|
|
function coderArray(coder, length) {
|
|
|
|
return {
|
|
|
|
encode: function(value) {
|
|
|
|
if (!Array.isArray(value)) { throw new Error('invalid array'); }
|
|
|
|
|
|
|
|
var result = new Buffer(0);
|
|
|
|
if (length === -1) {
|
|
|
|
length = value.length;
|
2016-07-21 11:21:44 +03:00
|
|
|
result = uint256Coder.encode(length);
|
2016-07-21 01:06:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (length !== value.length) { throw new Error('size mismatch'); }
|
|
|
|
|
|
|
|
value.forEach(function(value) {
|
|
|
|
result = Buffer.concat([
|
|
|
|
result,
|
|
|
|
coder.encode(value)
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
decode: function(data, offset) {
|
|
|
|
// @TODO:
|
|
|
|
//if (data.length < offset + length * 32) { throw new Error('invalid array'); }
|
|
|
|
|
|
|
|
var consumed = 0;
|
|
|
|
|
|
|
|
var result;
|
|
|
|
if (length === -1) {
|
2016-07-21 11:21:44 +03:00
|
|
|
result = uint256Coder.decode(data, offset);
|
2016-07-21 01:06:03 +03:00
|
|
|
length = result.value.toNumber();
|
|
|
|
consumed += result.consumed;
|
|
|
|
offset += result.consumed;
|
|
|
|
}
|
|
|
|
|
|
|
|
var value = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
var result = coder.decode(data, offset);
|
|
|
|
consumed += result.consumed;
|
|
|
|
offset += result.consumed;
|
|
|
|
value.push(result.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
consumed: consumed,
|
|
|
|
value: value,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
dynamic: (length === -1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 11:21:44 +03:00
|
|
|
// Break the type up into [staticType][staticArray]*[dynamicArray]? | [dynamicType] and
|
|
|
|
// build the coder up from its parts
|
|
|
|
var paramTypePart = new RegExp(/^((u?int|bytes)([0-9]*)|(address|bool|string)|(\[([0-9]*)\]))/);
|
|
|
|
function getParamCoder(type) {
|
|
|
|
var coder = null;
|
|
|
|
while (type) {
|
|
|
|
var part = type.match(paramTypePart);
|
|
|
|
if (!part) { throw new Error('invalid type: ' + type); }
|
|
|
|
type = type.substring(part[0].length);
|
|
|
|
|
|
|
|
var prefix = (part[2] || part[4] || part[5]);
|
|
|
|
switch (prefix) {
|
|
|
|
case 'int': case 'uint':
|
|
|
|
if (coder) { throw new Error('invalid type ' + type); }
|
|
|
|
var size = parseInt(part[3] || 256);
|
|
|
|
if (size === 0 || size > 256 || (size % 8) !== 0) {
|
|
|
|
throw new Error('invalid type ' + type);
|
|
|
|
}
|
|
|
|
coder = coderNumber(size / 8, (prefix === 'int'));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'bool':
|
|
|
|
if (coder) { throw new Error('invalid type ' + type); }
|
|
|
|
coder = coderBoolean;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'string':
|
|
|
|
if (coder) { throw new Error('invalid type ' + type); }
|
|
|
|
coder = coderString;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'bytes':
|
|
|
|
if (coder) { throw new Error('invalid type ' + type); }
|
|
|
|
if (part[3]) {
|
|
|
|
var size = parseInt(part[3]);
|
|
|
|
if (size === 0 || size > 32) {
|
|
|
|
throw new Error('invalid type ' + type);
|
|
|
|
}
|
|
|
|
coder = coderFixedBytes(size);
|
|
|
|
} else {
|
|
|
|
coder = coderDynamicBytes;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'address':
|
|
|
|
if (coder) { throw new Error('invalid type ' + type); }
|
|
|
|
coder = coderAddress;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '[]':
|
|
|
|
if (!coder || coder.dynamic) { throw new Error('invalid type ' + type); }
|
|
|
|
coder = coderArray(coder, -1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// "[0-9+]"
|
|
|
|
default:
|
|
|
|
if (!coder || coder.dynamic) { throw new Error('invalid type ' + type); }
|
|
|
|
var size = parseInt(part[6]);
|
|
|
|
coder = coderArray(coder, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!coder) { throw new Error('invalid type'); }
|
|
|
|
return coder;
|
|
|
|
}
|
|
|
|
|
2016-11-10 01:27:34 +03:00
|
|
|
function populateDescription(object, items) {
|
|
|
|
for (var key in items) {
|
|
|
|
utils.defineProperty(object, key, items[key]);
|
|
|
|
}
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
function CallDescription() { }
|
|
|
|
utils.defineProperty(CallDescription.prototype, 'type', 'call');
|
|
|
|
|
|
|
|
function TransactionDescription() { }
|
|
|
|
utils.defineProperty(TransactionDescription.prototype, 'type', 'transaction');
|
|
|
|
|
|
|
|
function EventDescription() { }
|
|
|
|
utils.defineProperty(EventDescription.prototype, 'type', 'event');
|
|
|
|
|
|
|
|
function UnsupportedDescription() { }
|
|
|
|
utils.defineProperty(UnsupportedDescription.prototype, 'type', 'unknown');
|
|
|
|
|
|
|
|
|
2016-07-21 11:21:44 +03:00
|
|
|
function Interface(abi) {
|
|
|
|
if (!(this instanceof Interface)) { throw new Error('missing new'); }
|
|
|
|
|
|
|
|
//defineProperty(this, 'address', address);
|
|
|
|
|
|
|
|
// Wrap this up as JSON so we can return a "copy" and avoid mutation
|
|
|
|
defineFrozen(this, 'abi', abi);
|
|
|
|
|
|
|
|
var methods = [], events = [];
|
|
|
|
abi.forEach(function(method) {
|
|
|
|
|
2016-07-25 10:55:16 +03:00
|
|
|
var func = null;
|
|
|
|
|
2016-07-21 11:21:44 +03:00
|
|
|
switch (method.type) {
|
|
|
|
case 'function':
|
|
|
|
methods.push(method.name);
|
|
|
|
func = (function() {
|
|
|
|
var inputTypes = getKeys(method.inputs, 'type');
|
|
|
|
var outputTypes = getKeys(method.outputs, 'type');
|
2016-11-10 01:27:34 +03:00
|
|
|
var outputNames = getKeys(method.outputs, 'name', true);
|
2016-07-21 11:21:44 +03:00
|
|
|
|
|
|
|
var func = function() {
|
|
|
|
var signature = method.name + '(' + getKeys(method.inputs, 'type').join(',') + ')';
|
|
|
|
var result = {
|
|
|
|
name: method.name,
|
|
|
|
signature: signature,
|
|
|
|
};
|
|
|
|
|
|
|
|
var params = Array.prototype.slice.call(arguments, 0);
|
|
|
|
|
|
|
|
if (params.length < inputTypes.length) {
|
|
|
|
throw new Error('missing parameter');
|
|
|
|
} else if (params.length > inputTypes.length) {
|
|
|
|
throw new Error('too many parameters');
|
|
|
|
}
|
|
|
|
|
|
|
|
signature = '0x' + utils.sha3(signature).slice(0, 4).toString('hex');
|
|
|
|
|
2016-07-22 21:49:26 +03:00
|
|
|
result.data = signature + Interface.encodeParams(inputTypes, params).substring(2);
|
2016-07-21 11:21:44 +03:00
|
|
|
if (method.constant) {
|
|
|
|
result.parse = function(data) {
|
|
|
|
return Interface.decodeParams(
|
2016-11-10 01:27:34 +03:00
|
|
|
outputNames,
|
2016-07-21 11:21:44 +03:00
|
|
|
outputTypes,
|
2016-07-27 00:57:11 +03:00
|
|
|
utils.hexOrBuffer(data)
|
2016-07-29 11:45:46 +03:00
|
|
|
);
|
2016-07-21 11:21:44 +03:00
|
|
|
};
|
2016-11-10 01:27:34 +03:00
|
|
|
return populateDescription(new CallDescription(), result);
|
2016-07-21 11:21:44 +03:00
|
|
|
}
|
|
|
|
|
2016-11-10 01:27:34 +03:00
|
|
|
return populateDescription(new TransactionDescription(), result);
|
2016-07-21 11:21:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
defineFrozen(func, 'inputs', getKeys(method.inputs, 'name'));
|
|
|
|
defineFrozen(func, 'outputs', getKeys(method.outputs, 'name'));
|
|
|
|
|
|
|
|
return func;
|
|
|
|
})();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'event':
|
|
|
|
events.push(method.name);
|
|
|
|
func = (function() {
|
|
|
|
var inputTypes = getKeys(method.inputs, 'type');
|
2016-11-10 01:27:34 +03:00
|
|
|
var inputNames = getKeys(method.inputs, 'name', true);
|
2016-07-21 11:21:44 +03:00
|
|
|
var func = function() {
|
|
|
|
var signature = method.name + '(' + getKeys(method.inputs, 'type').join(',') + ')';
|
|
|
|
var result = {
|
|
|
|
inputs: method.inputs,
|
|
|
|
name: method.name,
|
|
|
|
signature: signature,
|
|
|
|
topics: ['0x' + utils.sha3(signature).toString('hex')],
|
|
|
|
};
|
|
|
|
result.parse = function(data) {
|
|
|
|
return Interface.decodeParams(
|
2016-11-10 01:27:34 +03:00
|
|
|
inputNames,
|
2016-07-21 11:21:44 +03:00
|
|
|
inputTypes,
|
2016-07-27 00:57:11 +03:00
|
|
|
utils.hexOrBuffer(data)
|
2016-07-21 11:21:44 +03:00
|
|
|
);
|
|
|
|
};
|
2016-11-10 01:27:34 +03:00
|
|
|
return populateDescription(new EventDescription(), result);
|
2016-07-21 11:21:44 +03:00
|
|
|
}
|
|
|
|
defineFrozen(func, 'inputs', getKeys(method.inputs, 'name'));
|
|
|
|
return func;
|
|
|
|
})();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
func = (function() {
|
|
|
|
return function() {
|
2016-11-10 01:27:34 +03:00
|
|
|
return populateDescription(new UnsupportedDescription(), {});
|
2016-07-21 11:21:44 +03:00
|
|
|
}
|
|
|
|
})();
|
|
|
|
break;
|
|
|
|
}
|
2016-07-25 10:55:16 +03:00
|
|
|
|
2016-07-21 11:21:44 +03:00
|
|
|
utils.defineProperty(this, method.name, func);
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
defineFrozen(this, 'methods', methods);
|
|
|
|
defineFrozen(this, 'events', events);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
utils.defineProperty(Interface, 'encodeParams', function(types, values) {
|
2016-07-21 01:06:03 +03:00
|
|
|
if (types.length !== values.length) { throw new Error('types/values mismatch'); }
|
|
|
|
|
|
|
|
var parts = [];
|
|
|
|
|
|
|
|
types.forEach(function(type, index) {
|
|
|
|
var coder = getParamCoder(type);
|
|
|
|
parts.push({dynamic: coder.dynamic, value: coder.encode(values[index])});
|
|
|
|
})
|
|
|
|
|
|
|
|
function alignSize(size) {
|
|
|
|
return parseInt(32 * Math.ceil(size / 32));
|
|
|
|
}
|
|
|
|
|
|
|
|
var staticSize = 0, dynamicSize = 0;
|
|
|
|
parts.forEach(function(part) {
|
|
|
|
if (part.dynamic) {
|
|
|
|
staticSize += 32;
|
|
|
|
dynamicSize += alignSize(part.value.length);
|
|
|
|
} else {
|
|
|
|
staticSize += alignSize(part.value.length);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var offset = 0, dynamicOffset = staticSize;
|
|
|
|
var data = new Buffer(staticSize + dynamicSize);
|
|
|
|
|
|
|
|
parts.forEach(function(part, index) {
|
|
|
|
if (part.dynamic) {
|
2016-07-21 11:21:44 +03:00
|
|
|
uint256Coder.encode(dynamicOffset).copy(data, offset);
|
2016-07-21 01:06:03 +03:00
|
|
|
offset += 32;
|
|
|
|
|
|
|
|
part.value.copy(data, dynamicOffset);
|
|
|
|
dynamicOffset += alignSize(part.value.length);
|
|
|
|
} else {
|
|
|
|
part.value.copy(data, offset);
|
|
|
|
offset += alignSize(part.value.length);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return '0x' + data.toString('hex');
|
|
|
|
});
|
|
|
|
|
2016-11-10 01:27:34 +03:00
|
|
|
function Result() {}
|
|
|
|
|
|
|
|
utils.defineProperty(Interface, 'decodeParams', function(names, types, data) {
|
2016-11-22 01:46:36 +03:00
|
|
|
|
|
|
|
// Names is optional, so shift over all the parameters if not provided
|
|
|
|
if (arguments.length < 3) {
|
|
|
|
data = types;
|
|
|
|
types = names;
|
|
|
|
names = [];
|
|
|
|
}
|
|
|
|
|
2016-07-21 01:06:03 +03:00
|
|
|
data = utils.hexOrBuffer(data);
|
2016-11-10 01:27:34 +03:00
|
|
|
var values = new Result();
|
2016-07-21 01:06:03 +03:00
|
|
|
|
|
|
|
var offset = 0;
|
2016-11-10 01:27:34 +03:00
|
|
|
types.forEach(function(type, index) {
|
2016-07-21 01:06:03 +03:00
|
|
|
var coder = getParamCoder(type);
|
|
|
|
if (coder.dynamic) {
|
2016-07-21 11:21:44 +03:00
|
|
|
var dynamicOffset = uint256Coder.decode(data, offset);
|
2016-07-21 01:06:03 +03:00
|
|
|
var result = coder.decode(data, dynamicOffset.value.toNumber());
|
|
|
|
offset += dynamicOffset.consumed;
|
|
|
|
} else {
|
|
|
|
var result = coder.decode(data, offset);
|
|
|
|
offset += result.consumed;
|
|
|
|
}
|
2016-11-10 01:27:34 +03:00
|
|
|
values[index] = result.value;
|
|
|
|
if (names[index]) { values[names[index]] = result.value; }
|
2016-07-21 01:06:03 +03:00
|
|
|
});
|
|
|
|
return values;
|
|
|
|
});
|
|
|
|
|
2016-07-21 11:21:44 +03:00
|
|
|
|
|
|
|
var allowedTransactionKeys = {
|
|
|
|
data: true, from: true, gasLimit: true, gasPrice:true, to: true, value: true
|
|
|
|
}
|
|
|
|
|
2016-08-03 00:43:18 +03:00
|
|
|
function Contract(wallet, contractAddress, contractInterface) {
|
2016-07-21 11:21:44 +03:00
|
|
|
utils.defineProperty(this, 'wallet', wallet);
|
2016-07-27 00:57:11 +03:00
|
|
|
|
2016-07-21 11:21:44 +03:00
|
|
|
utils.defineProperty(this, 'contractAddress', contractAddress);
|
2016-07-25 10:55:16 +03:00
|
|
|
utils.defineProperty(this, 'interface', contractInterface);
|
2016-08-03 00:43:18 +03:00
|
|
|
|
2016-07-21 11:21:44 +03:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
var filters = {};
|
|
|
|
function setupFilter(call, callback) {
|
|
|
|
var info = filters[call.name];
|
|
|
|
|
|
|
|
// Stop and remove the filter
|
|
|
|
if (!callback) {
|
|
|
|
if (info) { info.filter.stopWatching(); }
|
|
|
|
delete filters[call.name];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(callback) !== 'function') {
|
|
|
|
throw new Error('invalid callback');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Already have a filter, just update the callback
|
|
|
|
if (info) {
|
|
|
|
info.callback = callback;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
info = {callback: callback};
|
|
|
|
filters[call.name] = info;
|
|
|
|
|
|
|
|
// Start a new filter
|
2016-07-27 00:57:11 +03:00
|
|
|
/*
|
2016-07-21 11:21:44 +03:00
|
|
|
info.filter = web3.eth.filter({
|
|
|
|
address: contractAddress,
|
|
|
|
topics: call.topics
|
|
|
|
}, function(error, result) {
|
|
|
|
// @TODO: Emit errors to .onerror? Maybe?
|
|
|
|
if (error) {
|
|
|
|
console.log(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
info.callback.apply(self, call.parse(result.data));
|
|
|
|
} catch(error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
});
|
2016-07-27 00:57:11 +03:00
|
|
|
*/
|
2016-07-21 11:21:44 +03:00
|
|
|
}
|
2016-08-03 00:43:18 +03:00
|
|
|
function runMethod(method, estimateOnly) {
|
2016-07-21 11:21:44 +03:00
|
|
|
return function() {
|
2016-08-03 00:43:18 +03:00
|
|
|
var provider = wallet._provider;
|
|
|
|
|
2016-07-21 11:21:44 +03:00
|
|
|
var transaction = {}
|
|
|
|
|
|
|
|
var params = Array.prototype.slice.call(arguments);
|
2016-07-25 10:55:16 +03:00
|
|
|
if (params.length == contractInterface[method].inputs.length + 1) {
|
2016-07-21 11:21:44 +03:00
|
|
|
transaction = params.pop();
|
|
|
|
if (typeof(transaction) !== 'object') {
|
|
|
|
throw new Error('invalid transaction overrides');
|
|
|
|
}
|
|
|
|
for (var key in transaction) {
|
|
|
|
if (!allowedTransactionKeys[key]) {
|
|
|
|
throw new Error('unknown transaction override ' + key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 00:57:11 +03:00
|
|
|
|
2016-07-25 10:55:16 +03:00
|
|
|
var call = contractInterface[method].apply(contractInterface, params);
|
2016-07-21 11:21:44 +03:00
|
|
|
switch (call.type) {
|
|
|
|
case 'call':
|
|
|
|
['data', 'gasLimit', 'gasPrice', 'to', 'value'].forEach(function(key) {
|
|
|
|
if (transaction[key] != null) {
|
|
|
|
throw new Error('call cannot override ' + key) ;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
transaction.data = call.data;
|
|
|
|
if (transaction.from == null) {
|
|
|
|
transaction.from = wallet.address;
|
|
|
|
}
|
|
|
|
transaction.to = contractAddress;
|
|
|
|
|
2016-08-03 00:43:18 +03:00
|
|
|
if (estimateOnly) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
resolve(new utils.BN(0));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-21 11:21:44 +03:00
|
|
|
return new Promise(function(resolve, reject) {
|
2016-08-03 00:43:18 +03:00
|
|
|
provider.call(transaction).then(function(value) {
|
2016-07-21 11:21:44 +03:00
|
|
|
resolve(call.parse(value));
|
|
|
|
}, function(error) {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
case 'transaction':
|
|
|
|
['data', 'from', 'to'].forEach(function(key) {
|
|
|
|
if (transaction[key] != null) {
|
|
|
|
throw new Error('transaction cannot override ' + key) ;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
transaction.data = call.data;
|
|
|
|
transaction.to = contractAddress;
|
|
|
|
if (transaction.gasLimit == null) {
|
|
|
|
transaction.gasLimit = 3000000;
|
|
|
|
}
|
|
|
|
|
2016-08-03 00:43:18 +03:00
|
|
|
if (estimateOnly) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
provider.estimateGas(transaction).then(function(gasEstimate) {
|
2016-08-03 09:26:36 +03:00
|
|
|
resolve(gasEstimate);
|
2016-08-03 00:43:18 +03:00
|
|
|
}, function(error) {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-21 11:21:44 +03:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
Promise.all([
|
2016-08-03 00:43:18 +03:00
|
|
|
provider.getTransactionCount(wallet.address, 'pending'),
|
|
|
|
provider.getGasPrice(),
|
2016-07-21 11:21:44 +03:00
|
|
|
]).then(function(results) {
|
|
|
|
if (transaction.nonce == null) {
|
|
|
|
transaction.nonce = results[0];
|
|
|
|
} else if (console.warn) {
|
|
|
|
console.warn('Overriding suggested nonce: ' + results[0]);
|
|
|
|
}
|
|
|
|
if (transaction.gasPrice == null) {
|
|
|
|
transaction.gasPrice = results[1];
|
|
|
|
} else if (console.warn) {
|
|
|
|
console.warn('Overriding suggested gasPrice: ' + utils.hexlify(results[1]));
|
|
|
|
}
|
2016-07-27 00:57:11 +03:00
|
|
|
|
2016-07-21 11:21:44 +03:00
|
|
|
var signedTransaction = wallet.sign(transaction);
|
2016-08-03 00:43:18 +03:00
|
|
|
provider.sendTransaction(signedTransaction).then(function(txid) {
|
2016-07-21 11:21:44 +03:00
|
|
|
resolve(txid);
|
|
|
|
}, function(error) {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
}, function(error) {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-08-03 00:43:18 +03:00
|
|
|
var estimate = {};
|
|
|
|
utils.defineProperty(this, 'estimate', estimate);
|
|
|
|
|
2016-07-25 10:55:16 +03:00
|
|
|
contractInterface.methods.forEach(function(method) {
|
2016-08-03 00:43:18 +03:00
|
|
|
utils.defineProperty(this, method, runMethod(method, false));
|
|
|
|
utils.defineProperty(estimate, method, runMethod(method, true));
|
2016-07-21 11:21:44 +03:00
|
|
|
}, this);
|
|
|
|
|
2016-07-25 10:55:16 +03:00
|
|
|
contractInterface.events.forEach(function(method) {
|
|
|
|
var call = contractInterface[method].apply(contractInterface, []);
|
2016-07-21 11:21:44 +03:00
|
|
|
Object.defineProperty(self, 'on' + call.name.toLowerCase(), {
|
|
|
|
enumerable: true,
|
|
|
|
get: function() {
|
2016-07-27 00:57:11 +03:00
|
|
|
//console.log('get');
|
2016-07-21 11:21:44 +03:00
|
|
|
var info = filters[call.name];
|
|
|
|
if (!info || !info[call.name]) { return null; }
|
|
|
|
return info.callback;
|
|
|
|
},
|
|
|
|
set: function(value) {
|
2016-07-27 00:57:11 +03:00
|
|
|
//console.log('set');
|
2016-07-21 11:21:44 +03:00
|
|
|
setupFilter(call, value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, this);
|
|
|
|
}
|
|
|
|
utils.defineProperty(Contract, 'Interface', Interface);
|
|
|
|
|
2016-07-16 06:47:35 +03:00
|
|
|
module.exports = Contract;
|