ethers.js/tests/test-contracts.js

91 lines
3.2 KiB
JavaScript
Raw Normal View History

2016-07-27 00:58:17 +03:00
'use strict';
var Wallet = require('../index.js');
var provider = new Wallet.providers.EtherscanProvider({testnet: true});
2016-07-27 00:58:17 +03:00
module.exports = function(test) {
var contractAddress = '0xdfaf84077cF4bCECA4F79d167F47041Ed3006D5b';
var contractABI = {
"SimpleStorage": [
{
"constant":true,
"inputs":[],
"name":"getValue",
"outputs":[{"name":"value","type":"string"}],
2016-07-27 00:58:17 +03:00
"type":"function"
}, {
"constant":false,
"inputs":[{"name":"value","type":"string"}],
"name":"setValue",
"outputs":[],
"type":"function"
}, {
"anonymous":false,
"inputs":[
{"indexed":false,"name":"oldValue","type":"string"},
{"indexed":false,"name":"newValue","type":"string"}
],
"name":"valueChanged",
"type":"event"
}
]
}
var contractInterface = new Wallet._Contract.Interface(contractABI.SimpleStorage);
var getValue = contractInterface.getValue()
var setValue = contractInterface.setValue("foobar");
var valueChanged = contractInterface.valueChanged()
test.equal(getValue.data, '0x20965255', "wrong call data");
test.equal(setValue.data, '0x93a0935200000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000006666f6f6261720000000000000000000000000000000000000000000000000000', "wrong transaction data");
test.ok(
(valueChanged.topics.length === 1 && valueChanged.topics[0] === '0x68ad6719a0070b3bb2f866fa0d46c8123b18cefe9b387ddb4feb6647ca418435'),
"wrong call data"
);
// @TODO - test decode
var privateKey = new Buffer(32);
privateKey.fill(0x42);
var wallet = new Wallet(privateKey, provider);
var contract = wallet.getContract(contractAddress, contractABI.SimpleStorage);
2016-07-27 00:58:17 +03:00
function testCall() {
return new Promise(function(resolve, reject) {
contract.getValue().then(function(result) {
test.equal(result[0], 'test888888', 'failed to call getVaue (positional)');
test.equal(result.value, 'test888888', 'failed to call getVaue (keyword)');
2016-07-27 00:58:17 +03:00
resolve(result);
}, function(error) {
test.ok(false, 'failed to call getValue (is parity running on this host?)');
2016-07-27 00:58:17 +03:00
reject(error);
});
});
}
function testEstimate() {
return new Promise(function(resolve, reject) {
contract.estimate.setValue('foo').then(function(result) {
test.equal(result.toString(16), '8f5a', 'failed to estimate setVaue');
resolve(result);
}, function(error) {
test.ok(false, 'failed to call getValue (is parity running on this host?)');
reject(error);
});
});
}
2016-07-27 00:58:17 +03:00
Promise.all([
testCall(),
testEstimate(),
2016-07-27 00:58:17 +03:00
]).then(function(results) {
test.done();
}, function(error) {
console.log('ERROR', error);
test.done();
});
};
2016-08-02 01:34:12 +03:00
module.exports.testSelf = module.exports;