2015-03-27 08:42:23 +01:00
|
|
|
var chai = require('chai');
|
|
|
|
var assert = chai.assert;
|
2015-10-08 04:09:51 +02:00
|
|
|
var Web3 = require('../index');
|
2015-03-27 08:42:23 +01:00
|
|
|
var FakeHttpProvider = require('./helpers/FakeHttpProvider');
|
|
|
|
var utils = require('../lib/utils/utils');
|
2015-04-26 10:27:21 +02:00
|
|
|
var BigNumber = require('bignumber.js');
|
2015-05-16 17:03:27 +02:00
|
|
|
var sha3 = require('../lib/utils/sha3');
|
2015-03-27 08:42:23 +01:00
|
|
|
|
2015-10-08 08:41:55 +02:00
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
var abi = [{
|
|
|
|
"type": "constructor",
|
|
|
|
"inputs": [{
|
|
|
|
"name": "who",
|
|
|
|
"type": "address"
|
|
|
|
},{
|
|
|
|
"name": "myValue",
|
|
|
|
"type": "uint256"
|
|
|
|
}]
|
|
|
|
},{
|
|
|
|
"name": "balance",
|
2015-03-27 08:42:23 +01:00
|
|
|
"type": "function",
|
|
|
|
"inputs": [{
|
|
|
|
"name": "who",
|
|
|
|
"type": "address"
|
|
|
|
}],
|
|
|
|
"constant": true,
|
|
|
|
"outputs": [{
|
|
|
|
"name": "value",
|
|
|
|
"type": "uint256"
|
|
|
|
}]
|
|
|
|
}, {
|
2016-11-09 15:47:00 +01:00
|
|
|
"name": "mySend",
|
2015-03-27 08:42:23 +01:00
|
|
|
"type": "function",
|
|
|
|
"inputs": [{
|
|
|
|
"name": "to",
|
|
|
|
"type": "address"
|
|
|
|
}, {
|
|
|
|
"name": "value",
|
|
|
|
"type": "uint256"
|
|
|
|
}],
|
|
|
|
"outputs": []
|
2015-04-26 10:08:58 +02:00
|
|
|
}, {
|
2016-10-07 13:07:42 +02:00
|
|
|
"name": "testArr",
|
2015-04-26 10:08:58 +02:00
|
|
|
"type": "function",
|
|
|
|
"inputs": [{
|
|
|
|
"name": "value",
|
|
|
|
"type": "int[]"
|
|
|
|
}],
|
|
|
|
"constant": true,
|
|
|
|
"outputs": [{
|
|
|
|
"name": "d",
|
|
|
|
"type": "int"
|
|
|
|
}]
|
2015-03-27 08:42:23 +01:00
|
|
|
}, {
|
|
|
|
"name":"Changed",
|
|
|
|
"type":"event",
|
|
|
|
"inputs": [
|
|
|
|
{"name":"from","type":"address","indexed":true},
|
|
|
|
{"name":"amount","type":"uint256","indexed":true},
|
2015-03-27 11:27:50 +01:00
|
|
|
{"name":"t1","type":"uint256","indexed":false},
|
|
|
|
{"name":"t2","type":"uint256","indexed":false}
|
2016-10-07 13:07:42 +02:00
|
|
|
]
|
|
|
|
}, {
|
|
|
|
"name":"Unchanged",
|
|
|
|
"type":"event",
|
|
|
|
"inputs": [
|
|
|
|
{"name":"value","type":"uint256","indexed":true},
|
|
|
|
{"name":"addressFrom","type":"address","indexed":true},
|
|
|
|
{"name":"t1","type":"uint256","indexed":false}
|
|
|
|
]
|
|
|
|
}];
|
2015-03-27 08:42:23 +01:00
|
|
|
|
2015-10-15 14:46:47 +08:00
|
|
|
var address = '0x1234567890123456789012345678901234567891';
|
2016-10-07 13:07:42 +02:00
|
|
|
var address2 = '0x5555567890123456789012345678901234567891';
|
2015-03-27 08:42:23 +01:00
|
|
|
|
2015-08-08 21:22:01 +02:00
|
|
|
describe('contract', function () {
|
2016-10-07 13:07:42 +02:00
|
|
|
describe('internal method', function () {
|
|
|
|
it('_encodeEventABI should return the encoded event object without topics', function (done) {
|
|
|
|
var provider = new FakeHttpProvider();
|
|
|
|
var web3 = new Web3(provider);
|
|
|
|
|
|
|
|
var contract = new web3.eth.contract(abi, address);
|
|
|
|
|
|
|
|
var result = contract._encodeEventABI({
|
|
|
|
signature: '0x1234',
|
|
|
|
"name":"Changed",
|
|
|
|
"type":"event",
|
|
|
|
"inputs": [
|
|
|
|
{"name":"from","type":"address","indexed":true},
|
|
|
|
{"name":"amount","type":"uint256","indexed":true},
|
|
|
|
{"name":"t1","type":"uint256","indexed":false},
|
|
|
|
{"name":"t2","type":"uint256","indexed":false}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepEqual(result, {
|
|
|
|
address: address,
|
|
|
|
topics: [
|
|
|
|
'0x1234',
|
|
|
|
null,
|
|
|
|
null
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
it('_encodeEventABI should return the encoded event object with topics', function (done) {
|
|
|
|
var provider = new FakeHttpProvider();
|
|
|
|
var web3 = new Web3(provider);
|
|
|
|
|
|
|
|
var contract = new web3.eth.contract(abi, address);
|
|
|
|
|
|
|
|
var result = contract._encodeEventABI({
|
|
|
|
signature: '0x1234',
|
|
|
|
"name":"Changed",
|
|
|
|
"type":"event",
|
|
|
|
"inputs": [
|
|
|
|
{"name":"from","type":"address","indexed":true},
|
|
|
|
{"name":"amount","type":"uint256","indexed":true},
|
|
|
|
{"name":"t1","type":"uint256","indexed":false},
|
|
|
|
{"name":"t2","type":"uint256","indexed":false}
|
|
|
|
]
|
|
|
|
}, {filter: {amount: 12}, fromBlock: 2});
|
|
|
|
|
|
|
|
assert.deepEqual(result, {
|
|
|
|
address: address,
|
|
|
|
fromBlock: '0x2',
|
|
|
|
topics: [
|
|
|
|
'0x1234',
|
|
|
|
null,
|
|
|
|
'0x000000000000000000000000000000000000000000000000000000000000000c'
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
it('_decodeEventABI should return the decoded event object with topics', function (done) {
|
2015-03-27 08:42:23 +01:00
|
|
|
var provider = new FakeHttpProvider();
|
2016-10-07 13:07:42 +02:00
|
|
|
var web3 = new Web3(provider);
|
2015-05-16 17:03:27 +02:00
|
|
|
var signature = 'Changed(address,uint256,uint256,uint256)';
|
2016-10-07 13:07:42 +02:00
|
|
|
|
|
|
|
var contract = new web3.eth.contract(abi, address);
|
|
|
|
|
|
|
|
var result = contract._decodeEventABI.call({
|
|
|
|
signature: '0x'+ sha3(signature),
|
|
|
|
"name":"Changed",
|
|
|
|
"type":"event",
|
|
|
|
"inputs": [
|
|
|
|
{"name":"from","type":"address","indexed":true},
|
|
|
|
{"name":"amount","type":"uint256","indexed":true},
|
|
|
|
{"name":"t1","type":"uint256","indexed":false},
|
|
|
|
{"name":"t2","type":"uint256","indexed":false}
|
|
|
|
]
|
|
|
|
}, {
|
|
|
|
address: address,
|
|
|
|
topics: [
|
|
|
|
'0x' + sha3(signature),
|
|
|
|
'0x000000000000000000000000'+ address.replace('0x',''),
|
|
|
|
'0x0000000000000000000000000000000000000000000000000000000000000001'
|
|
|
|
],
|
|
|
|
blockNumber: '0x3',
|
|
|
|
transactionHash: '0x1234',
|
|
|
|
blockHash: '0x1345',
|
|
|
|
logIndex: '0x4',
|
|
|
|
data: '0x0000000000000000000000000000000000000000000000000000000000000001' +
|
|
|
|
'0000000000000000000000000000000000000000000000000000000000000008'
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(result.blockNumber, 3);
|
|
|
|
assert.equal(result.blockHash, '0x1345');
|
|
|
|
assert.equal(result.logIndex, 4);
|
|
|
|
assert.equal(result.id, 'log_9ff24cb4');
|
|
|
|
assert.equal(result.transactionIndex, 0);
|
|
|
|
assert.equal(result.returnValues.from, address);
|
|
|
|
assert.equal(result.returnValues.amount, 1);
|
|
|
|
assert.equal(result.returnValues.t1, 1);
|
|
|
|
assert.equal(result.returnValues.t2, 8);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
it('_decodeMethodReturn should return the decoded values', function (done) {
|
|
|
|
var provider = new FakeHttpProvider();
|
|
|
|
var web3 = new Web3(provider);
|
|
|
|
var signature = 'Changed(address,uint256,uint256,uint256)';
|
|
|
|
|
|
|
|
var contract = new web3.eth.contract(abi, address);
|
|
|
|
|
|
|
|
var result = contract._decodeMethodReturn([{
|
|
|
|
"name": "myAddress",
|
|
|
|
"type": "address"
|
|
|
|
},{
|
|
|
|
"name": "value",
|
|
|
|
"type": "uint256"
|
|
|
|
}], '0x000000000000000000000000'+ address.replace('0x','')+
|
|
|
|
'000000000000000000000000000000000000000000000000000000000000000a');
|
|
|
|
|
|
|
|
assert.equal(result[0], address);
|
|
|
|
assert.equal(result[1], 10);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
it('_checkForContractAddress should subscribe and check for receipts and code', function (done) {
|
|
|
|
var provider = new FakeHttpProvider();
|
|
|
|
var web3 = new Web3(provider);
|
|
|
|
|
2015-03-27 08:42:23 +01:00
|
|
|
provider.injectValidation(function (payload) {
|
2016-10-07 13:07:42 +02:00
|
|
|
assert.equal(payload.method, 'eth_subscribe');
|
|
|
|
assert.deepEqual(payload.params, ['newBlocks', {}]);
|
|
|
|
});
|
|
|
|
provider.injectResult('0x1234567');
|
|
|
|
|
|
|
|
// fake newBlock
|
|
|
|
provider.injectNotification({
|
|
|
|
method: 'eth_subscription',
|
|
|
|
params: {
|
|
|
|
subscription: '0x1234567',
|
|
|
|
result: {
|
|
|
|
blockNumber: '0x10'
|
|
|
|
}
|
2015-03-27 08:42:23 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.method, 'eth_getTransactionReceipt');
|
|
|
|
assert.deepEqual(payload.params, ['0x1234000000000000000000000000000000000000000000000000000000056789']);
|
|
|
|
});
|
|
|
|
provider.injectResult({
|
|
|
|
contractAddress: address,
|
|
|
|
cumulativeGasUsed: '0xa',
|
|
|
|
transactionIndex: '0x3',
|
|
|
|
blockNumber: '0xa'
|
|
|
|
});
|
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.method, 'eth_getCode');
|
|
|
|
assert.deepEqual(payload.params, [address, 'latest']);
|
|
|
|
});
|
|
|
|
provider.injectResult('0x321');
|
|
|
|
|
2015-03-27 08:42:23 +01:00
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
var contract = new web3.eth.contract(abi, address);
|
|
|
|
|
|
|
|
var deploy = contract._checkForContractAddress('0x1234000000000000000000000000000000000000000000000000000000056789', function (err, result) {
|
|
|
|
|
|
|
|
assert.equal(result.contractAddress, address);
|
|
|
|
assert.equal(result.blockNumber, 10);
|
|
|
|
assert.equal(result.transactionIndex, 3);
|
|
|
|
assert.equal(result.cumulativeGasUsed, 10);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
it('_executeMethod should sendTransaction and check for receipts', function (done) {
|
|
|
|
var provider = new FakeHttpProvider();
|
|
|
|
var web3 = new Web3(provider);
|
2016-11-09 15:47:00 +01:00
|
|
|
var signature = sha3('mySend(address,uint256)').slice(0, 8);
|
2016-10-07 13:07:42 +02:00
|
|
|
|
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.method, 'eth_sendTransaction');
|
|
|
|
assert.deepEqual(payload.params, [{
|
|
|
|
data: "0x"+ signature +"0000000000000000000000001234567890123456789012345678901234567891000000000000000000000000000000000000000000000000000000000000000a",
|
|
|
|
from: address2,
|
|
|
|
to: address
|
|
|
|
}]);
|
|
|
|
});
|
|
|
|
provider.injectResult('0x1234000000000000000000000000000000000000000000000000000000056789');
|
|
|
|
|
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.method, 'eth_subscribe');
|
|
|
|
assert.deepEqual(payload.params, ['newBlocks', {}]);
|
|
|
|
});
|
|
|
|
provider.injectResult('0x1234567');
|
|
|
|
|
|
|
|
// fake newBlock
|
|
|
|
provider.injectNotification({
|
|
|
|
method: 'eth_subscription',
|
|
|
|
params: {
|
|
|
|
subscription: '0x1234567',
|
|
|
|
result: {
|
|
|
|
blockNumber: '0x10'
|
|
|
|
}
|
2015-04-09 09:15:04 +02:00
|
|
|
}
|
2015-03-27 08:42:23 +01:00
|
|
|
});
|
2016-01-21 15:25:44 +01:00
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.method, 'eth_getTransactionReceipt');
|
|
|
|
assert.deepEqual(payload.params, ['0x1234000000000000000000000000000000000000000000000000000000056789']);
|
|
|
|
});
|
|
|
|
provider.injectResult({
|
|
|
|
contractAddress: address,
|
|
|
|
cumulativeGasUsed: '0xa',
|
|
|
|
transactionIndex: '0x3',
|
|
|
|
blockNumber: '0xa'
|
|
|
|
});
|
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.method, 'eth_unsubscribe');
|
|
|
|
assert.deepEqual(payload.params, ['0x1234567']);
|
|
|
|
});
|
|
|
|
provider.injectResult('0x321');
|
|
|
|
|
|
|
|
|
|
|
|
var contract = new web3.eth.contract(abi, address);
|
|
|
|
|
|
|
|
var txObject = {};
|
|
|
|
txObject._method = {
|
|
|
|
signature: signature,
|
|
|
|
"name": "send",
|
|
|
|
"type": "function",
|
|
|
|
"inputs": [{
|
|
|
|
"name": "to",
|
|
|
|
"type": "address"
|
|
|
|
}, {
|
|
|
|
"name": "value",
|
|
|
|
"type": "uint256"
|
|
|
|
}],
|
|
|
|
"outputs": []
|
|
|
|
};
|
|
|
|
txObject._parent = contract;
|
|
|
|
txObject.encodeABI = contract._encodeMethodABI.bind(txObject);
|
|
|
|
txObject.arguments = [address, 10];
|
|
|
|
|
|
|
|
var deploy = contract._executeMethod.call(txObject, 'send', {from: address2}, function (err, result) {
|
|
|
|
// tx hash
|
|
|
|
assert.equal(result, '0x1234000000000000000000000000000000000000000000000000000000056789');
|
|
|
|
})
|
2016-11-01 16:03:03 +01:00
|
|
|
.on('receipt', function(result){
|
2016-10-07 13:07:42 +02:00
|
|
|
assert.deepEqual(result, {
|
|
|
|
contractAddress: address,
|
|
|
|
cumulativeGasUsed: 10,
|
|
|
|
transactionIndex: 3,
|
|
|
|
blockNumber: 10,
|
|
|
|
gasUsed: 0
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
it('_executeMethod should call and return values', function (done) {
|
|
|
|
var provider = new FakeHttpProvider();
|
|
|
|
var web3 = new Web3(provider);
|
|
|
|
var signature = sha3('balance(address)').slice(0, 8);
|
|
|
|
|
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.method, 'eth_call');
|
|
|
|
assert.deepEqual(payload.params, [{
|
|
|
|
data: "0x"+ signature +"0000000000000000000000001234567890123456789012345678901234567891",
|
|
|
|
from: address2,
|
|
|
|
to: address
|
|
|
|
}, 'latest']);
|
|
|
|
});
|
|
|
|
provider.injectResult('0x000000000000000000000000000000000000000000000000000000000000000a');
|
|
|
|
|
|
|
|
|
|
|
|
var contract = new web3.eth.contract(abi, address);
|
|
|
|
|
|
|
|
var txObject = {};
|
|
|
|
txObject._method = {
|
|
|
|
signature: signature,
|
|
|
|
"name": "balance",
|
|
|
|
"type": "function",
|
|
|
|
"inputs": [{
|
|
|
|
"name": "who",
|
|
|
|
"type": "address"
|
|
|
|
}],
|
|
|
|
"constant": true,
|
|
|
|
"outputs": [{
|
|
|
|
"name": "value",
|
|
|
|
"type": "uint256"
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
txObject._parent = contract;
|
|
|
|
txObject.encodeABI = contract._encodeMethodABI.bind(txObject);
|
|
|
|
txObject.arguments = [address];
|
|
|
|
|
|
|
|
var deploy = contract._executeMethod.call(txObject, 'call', {from: address2}, function (err, result) {
|
|
|
|
assert.deepEqual(result, new BigNumber(0xa));
|
|
|
|
})
|
|
|
|
.on('data', function(result){
|
|
|
|
assert.deepEqual(result, new BigNumber(0xa));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('event', function () {
|
|
|
|
it('should create event subscription', function (done) {
|
|
|
|
var provider = new FakeHttpProvider();
|
|
|
|
var web3 = new Web3(provider);
|
|
|
|
var signature = 'Changed(address,uint256,uint256,uint256)';
|
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.jsonrpc, '2.0');
|
|
|
|
assert.equal(payload.method, 'eth_subscribe');
|
|
|
|
assert.deepEqual(payload.params[1], {
|
|
|
|
topics: [
|
|
|
|
'0x' + sha3(signature),
|
|
|
|
('0x000000000000000000000000' + address.replace('0x', '')),
|
|
|
|
null
|
|
|
|
],
|
|
|
|
address: address
|
|
|
|
});
|
|
|
|
});
|
|
|
|
provider.injectResult('0x123');
|
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.jsonrpc, '2.0');
|
|
|
|
assert.equal(payload.method, 'eth_unsubscribe');
|
|
|
|
done();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2016-01-21 15:25:44 +01:00
|
|
|
provider.injectNotification({
|
|
|
|
method: 'eth_subscription',
|
|
|
|
params: {
|
|
|
|
subscription: '0x123',
|
|
|
|
result: {
|
|
|
|
address: address,
|
|
|
|
topics: [
|
|
|
|
'0x' + sha3(signature),
|
2016-10-07 13:07:42 +02:00
|
|
|
('0x000000000000000000000000' + address.replace('0x', '')),
|
2016-01-21 15:25:44 +01:00
|
|
|
'0x0000000000000000000000000000000000000000000000000000000000000001'
|
|
|
|
],
|
2016-10-07 13:07:42 +02:00
|
|
|
blockNumber: '0x3',
|
|
|
|
transactionHash: '0x1234',
|
|
|
|
blockHash: '0x1345',
|
|
|
|
logIndex: '0x4',
|
2016-01-21 15:25:44 +01:00
|
|
|
data: '0x0000000000000000000000000000000000000000000000000000000000000001' +
|
2016-10-07 13:07:42 +02:00
|
|
|
'0000000000000000000000000000000000000000000000000000000000000008'
|
2016-01-21 15:25:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-10-07 13:07:42 +02:00
|
|
|
|
|
|
|
var contract = new web3.eth.contract(abi, address);
|
|
|
|
|
2016-11-09 15:47:00 +01:00
|
|
|
var event = contract.events.Changed({filter: {from: address}}, function (err, result, sub) {
|
2016-10-07 13:07:42 +02:00
|
|
|
assert.equal(result.returnValues.from, address);
|
|
|
|
assert.equal(result.returnValues.amount, 1);
|
|
|
|
assert.equal(result.returnValues.t1, 1);
|
|
|
|
assert.equal(result.returnValues.t2, 8);
|
|
|
|
|
|
|
|
sub.unsubscribe();
|
|
|
|
});
|
|
|
|
|
2015-03-27 08:42:23 +01:00
|
|
|
});
|
2015-03-31 09:34:06 +02:00
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
it('should create event from the events object and use the fromBlock option', function (done) {
|
2015-06-24 10:28:12 +02:00
|
|
|
var provider = new FakeHttpProvider();
|
2016-10-07 13:07:42 +02:00
|
|
|
var web3 = new Web3(provider);
|
2015-06-24 10:28:12 +02:00
|
|
|
var signature = 'Changed(address,uint256,uint256,uint256)';
|
2016-10-07 13:07:42 +02:00
|
|
|
|
2015-06-24 10:28:12 +02:00
|
|
|
provider.injectValidation(function (payload) {
|
2016-10-07 13:07:42 +02:00
|
|
|
assert.equal(payload.jsonrpc, '2.0');
|
|
|
|
assert.equal(payload.method, 'eth_getLogs');
|
2016-01-21 15:25:44 +01:00
|
|
|
});
|
2016-10-07 13:07:42 +02:00
|
|
|
provider.injectResult([{
|
|
|
|
address: address,
|
|
|
|
topics: [
|
|
|
|
'0x' + sha3(signature),
|
|
|
|
'0x000000000000000000000000'+ address.replace('0x',''),
|
|
|
|
'0x0000000000000000000000000000000000000000000000000000000000000002'
|
|
|
|
],
|
|
|
|
blockNumber: '0x3',
|
|
|
|
transactionHash: '0x1234',
|
|
|
|
blockHash: '0x1345',
|
|
|
|
logIndex: '0x4',
|
|
|
|
data: '0x0000000000000000000000000000000000000000000000000000000000000002' +
|
|
|
|
'0000000000000000000000000000000000000000000000000000000000000009'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
address: address,
|
|
|
|
topics: [
|
|
|
|
'0x' + sha3(signature),
|
|
|
|
'0x000000000000000000000000'+ address.replace('0x',''),
|
|
|
|
'0x0000000000000000000000000000000000000000000000000000000000000003'
|
|
|
|
],
|
|
|
|
blockNumber: '0x4',
|
|
|
|
transactionHash: '0x1235',
|
|
|
|
blockHash: '0x1346',
|
|
|
|
logIndex: '0x1',
|
|
|
|
data: '0x0000000000000000000000000000000000000000000000000000000000000004' +
|
|
|
|
'0000000000000000000000000000000000000000000000000000000000000005'
|
|
|
|
}]);
|
2016-01-21 15:25:44 +01:00
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.jsonrpc, '2.0');
|
|
|
|
assert.equal(payload.method, 'eth_subscribe');
|
|
|
|
assert.deepEqual(payload.params[1], {
|
|
|
|
topics: [
|
|
|
|
'0x' + sha3(signature),
|
|
|
|
'0x000000000000000000000000'+ address.replace('0x',''),
|
|
|
|
null
|
|
|
|
],
|
|
|
|
address: address
|
|
|
|
});
|
|
|
|
});
|
|
|
|
provider.injectResult('0x321');
|
2016-01-21 15:25:44 +01:00
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.jsonrpc, '2.0');
|
|
|
|
assert.equal(payload.method, 'eth_unsubscribe');
|
|
|
|
done();
|
2016-01-21 15:25:44 +01:00
|
|
|
});
|
2016-10-07 13:07:42 +02:00
|
|
|
provider.injectResult(true);
|
2016-01-21 15:25:44 +01:00
|
|
|
|
|
|
|
provider.injectNotification({
|
|
|
|
method: 'eth_subscription',
|
|
|
|
params: {
|
|
|
|
subscription: '0x321',
|
|
|
|
result: {
|
2015-06-24 10:28:12 +02:00
|
|
|
address: address,
|
|
|
|
topics: [
|
|
|
|
'0x' + sha3(signature),
|
2016-10-07 13:07:42 +02:00
|
|
|
'0x000000000000000000000000'+ address.replace('0x',''),
|
2015-06-24 10:28:12 +02:00
|
|
|
'0x0000000000000000000000000000000000000000000000000000000000000001'
|
|
|
|
],
|
2016-10-07 13:07:42 +02:00
|
|
|
blockNumber: '0x3',
|
|
|
|
transactionHash: '0x1234',
|
|
|
|
blockHash: '0x1345',
|
|
|
|
logIndex: '0x4',
|
2015-06-24 10:28:12 +02:00
|
|
|
data: '0x0000000000000000000000000000000000000000000000000000000000000001' +
|
2016-10-07 13:07:42 +02:00
|
|
|
'0000000000000000000000000000000000000000000000000000000000000008'
|
2016-01-21 15:25:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
var contract = new web3.eth.contract(abi, address);
|
|
|
|
var count = 0;
|
|
|
|
var event = contract.events.Changed({fromBlock: 0,filter: {from: address}})
|
|
|
|
.on('data', function (result) {
|
|
|
|
count++;
|
|
|
|
|
|
|
|
if(count === 1) {
|
|
|
|
assert.equal(result.returnValues.from, address);
|
|
|
|
assert.equal(result.returnValues.amount, 2);
|
|
|
|
assert.equal(result.returnValues.t1, 2);
|
|
|
|
assert.equal(result.returnValues.t2, 9);
|
|
|
|
|
|
|
|
}
|
|
|
|
if(count === 2) {
|
|
|
|
assert.equal(result.returnValues.from, address);
|
|
|
|
assert.equal(result.returnValues.amount, 3);
|
|
|
|
assert.equal(result.returnValues.t1, 4);
|
|
|
|
assert.equal(result.returnValues.t2, 5);
|
|
|
|
|
|
|
|
}
|
|
|
|
if(count === 3) {
|
|
|
|
assert.equal(result.returnValues.from, address);
|
|
|
|
assert.equal(result.returnValues.amount, 1);
|
|
|
|
assert.equal(result.returnValues.t1, 1);
|
|
|
|
assert.equal(result.returnValues.t2, 8);
|
|
|
|
|
|
|
|
event.unsubscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should create event from the events object using a signature and callback', function (done) {
|
|
|
|
var provider = new FakeHttpProvider();
|
|
|
|
var web3 = new Web3(provider);
|
|
|
|
var signature = 'Changed(address,uint256,uint256,uint256)';
|
|
|
|
|
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.jsonrpc, '2.0');
|
|
|
|
assert.equal(payload.method, 'eth_subscribe');
|
|
|
|
assert.deepEqual(payload.params[1], {
|
|
|
|
topics: [
|
|
|
|
'0x' + sha3(signature),
|
|
|
|
'0x000000000000000000000000'+ address.replace('0x',''),
|
|
|
|
null
|
|
|
|
],
|
|
|
|
address: address
|
|
|
|
});
|
|
|
|
});
|
|
|
|
provider.injectResult('0x321');
|
|
|
|
|
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.jsonrpc, '2.0');
|
|
|
|
assert.equal(payload.method, 'eth_unsubscribe');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
provider.injectResult(true);
|
|
|
|
|
2016-01-21 15:25:44 +01:00
|
|
|
provider.injectNotification({
|
|
|
|
method: 'eth_subscription',
|
|
|
|
params: {
|
|
|
|
subscription: '0x321',
|
|
|
|
result: {
|
2015-06-24 10:28:12 +02:00
|
|
|
address: address,
|
|
|
|
topics: [
|
|
|
|
'0x' + sha3(signature),
|
2016-10-07 13:07:42 +02:00
|
|
|
'0x000000000000000000000000'+ address.replace('0x',''),
|
2015-06-24 10:28:12 +02:00
|
|
|
'0x0000000000000000000000000000000000000000000000000000000000000001'
|
|
|
|
],
|
2016-10-07 13:07:42 +02:00
|
|
|
blockNumber: '0x3',
|
|
|
|
transactionHash: '0x1234',
|
|
|
|
blockHash: '0x1345',
|
|
|
|
logIndex: '0x4',
|
2015-06-24 10:28:12 +02:00
|
|
|
data: '0x0000000000000000000000000000000000000000000000000000000000000001' +
|
2016-10-07 13:07:42 +02:00
|
|
|
'0000000000000000000000000000000000000000000000000000000000000008'
|
2016-01-21 15:25:44 +01:00
|
|
|
}
|
2015-06-24 10:28:12 +02:00
|
|
|
}
|
|
|
|
});
|
2016-10-07 13:07:42 +02:00
|
|
|
|
|
|
|
var contract = new web3.eth.contract(abi, address);
|
|
|
|
var event = contract.events['0x792991ed5ba9322deaef76cff5051ce4bedaaa4d097585970f9ad8f09f54e651']({filter: {from: address}}, function (err, result) {
|
|
|
|
assert.equal(result.returnValues.from, address);
|
|
|
|
assert.equal(result.returnValues.amount, 1);
|
|
|
|
assert.equal(result.returnValues.t1, 1);
|
|
|
|
assert.equal(result.returnValues.t2, 8);
|
|
|
|
|
|
|
|
event.unsubscribe();
|
|
|
|
});
|
2015-06-24 10:28:12 +02:00
|
|
|
});
|
2015-06-24 08:50:19 +02:00
|
|
|
|
2016-11-09 15:47:00 +01:00
|
|
|
it('should create event from the events object using event name and parameters', function (done) {
|
|
|
|
var provider = new FakeHttpProvider();
|
|
|
|
var web3 = new Web3(provider);
|
|
|
|
var signature = 'Changed(address,uint256,uint256,uint256)';
|
|
|
|
|
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.jsonrpc, '2.0');
|
|
|
|
assert.equal(payload.method, 'eth_subscribe');
|
|
|
|
assert.deepEqual(payload.params[1], {
|
|
|
|
topics: [
|
|
|
|
'0x' + sha3(signature),
|
|
|
|
'0x000000000000000000000000'+ address.replace('0x',''),
|
|
|
|
null
|
|
|
|
],
|
|
|
|
address: address
|
|
|
|
});
|
|
|
|
});
|
|
|
|
provider.injectResult('0x321');
|
|
|
|
|
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.jsonrpc, '2.0');
|
|
|
|
assert.equal(payload.method, 'eth_unsubscribe');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
provider.injectResult(true);
|
|
|
|
|
|
|
|
provider.injectNotification({
|
|
|
|
method: 'eth_subscription',
|
|
|
|
params: {
|
|
|
|
subscription: '0x321',
|
|
|
|
result: {
|
|
|
|
address: address,
|
|
|
|
topics: [
|
|
|
|
'0x' + sha3(signature),
|
|
|
|
'0x000000000000000000000000'+ address.replace('0x',''),
|
|
|
|
'0x0000000000000000000000000000000000000000000000000000000000000001'
|
|
|
|
],
|
|
|
|
blockNumber: '0x3',
|
|
|
|
transactionHash: '0x1234',
|
|
|
|
blockHash: '0x1345',
|
|
|
|
logIndex: '0x4',
|
|
|
|
data: '0x0000000000000000000000000000000000000000000000000000000000000001' +
|
|
|
|
'0000000000000000000000000000000000000000000000000000000000000008'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var contract = new web3.eth.contract(abi, address);
|
|
|
|
var event = contract.events[signature]({filter: {from: address}}, function (err, result) {
|
|
|
|
assert.equal(result.returnValues.from, address);
|
|
|
|
assert.equal(result.returnValues.amount, 1);
|
|
|
|
assert.equal(result.returnValues.t1, 1);
|
|
|
|
assert.equal(result.returnValues.t2, 8);
|
|
|
|
|
|
|
|
event.unsubscribe();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-11-09 16:06:46 +01:00
|
|
|
it('should create event using the once function and unsubscribe after one log received', function (done) {
|
|
|
|
var provider = new FakeHttpProvider();
|
|
|
|
var web3 = new Web3(provider);
|
|
|
|
var signature = 'Changed(address,uint256,uint256,uint256)';
|
|
|
|
|
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.jsonrpc, '2.0');
|
|
|
|
assert.equal(payload.method, 'eth_subscribe');
|
|
|
|
assert.deepEqual(payload.params[1], {
|
|
|
|
topics: [
|
|
|
|
'0x' + sha3(signature),
|
|
|
|
'0x000000000000000000000000'+ address.replace('0x',''),
|
|
|
|
null
|
|
|
|
],
|
|
|
|
address: address
|
|
|
|
});
|
|
|
|
});
|
|
|
|
provider.injectResult('0x321');
|
|
|
|
|
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.jsonrpc, '2.0');
|
|
|
|
assert.equal(payload.method, 'eth_unsubscribe');
|
|
|
|
});
|
|
|
|
provider.injectResult(true);
|
|
|
|
|
|
|
|
provider.injectNotification({
|
|
|
|
method: 'eth_subscription',
|
|
|
|
params: {
|
|
|
|
subscription: '0x321',
|
|
|
|
result: {
|
|
|
|
address: address,
|
|
|
|
topics: [
|
|
|
|
'0x' + sha3(signature),
|
|
|
|
'0x000000000000000000000000'+ address.replace('0x',''),
|
|
|
|
'0x0000000000000000000000000000000000000000000000000000000000000001'
|
|
|
|
],
|
|
|
|
blockNumber: '0x3',
|
|
|
|
transactionHash: '0x1234',
|
|
|
|
blockHash: '0x1345',
|
|
|
|
logIndex: '0x4',
|
|
|
|
data: '0x0000000000000000000000000000000000000000000000000000000000000001' +
|
|
|
|
'0000000000000000000000000000000000000000000000000000000000000008'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
provider.injectNotification({
|
|
|
|
method: 'eth_subscription',
|
|
|
|
params: {
|
|
|
|
subscription: '0x321',
|
|
|
|
result: {
|
|
|
|
address: address,
|
|
|
|
topics: [
|
|
|
|
'0x' + sha3(signature),
|
|
|
|
'0x000000000000000000000000'+ address.replace('0x',''),
|
|
|
|
'0x0000000000000000000000000000000000000000000000000000000000000001'
|
|
|
|
],
|
|
|
|
blockNumber: '0x3',
|
|
|
|
transactionHash: '0x1234',
|
|
|
|
blockHash: '0x1345',
|
|
|
|
logIndex: '0x4',
|
|
|
|
data: '0x0000000000000000000000000000000000000000000000000000000000000001' +
|
|
|
|
'0000000000000000000000000000000000000000000000000000000000000008'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var count = 1;
|
|
|
|
var contract = new web3.eth.contract(abi, address);
|
|
|
|
var event = contract.once('Changed', {filter: {from: address}}, function (err, result) {
|
|
|
|
assert.equal(result.returnValues.from, address);
|
|
|
|
assert.equal(result.returnValues.amount, 1);
|
|
|
|
assert.equal(result.returnValues.t1, 1);
|
|
|
|
assert.equal(result.returnValues.t2, 8);
|
|
|
|
assert.deepEqual(event.options.requestManager.subscriptions, {});
|
|
|
|
|
|
|
|
assert.equal(count, 1);
|
|
|
|
count++;
|
|
|
|
|
|
|
|
setTimeout(done, 600);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
it('should create all event filter and receive two logs', function (done) {
|
2015-06-24 08:50:19 +02:00
|
|
|
var provider = new FakeHttpProvider();
|
2016-10-07 13:07:42 +02:00
|
|
|
var web3 = new Web3(provider);
|
2015-06-24 08:50:19 +02:00
|
|
|
var signature = 'Changed(address,uint256,uint256,uint256)';
|
2016-10-07 13:07:42 +02:00
|
|
|
|
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.jsonrpc, '2.0');
|
|
|
|
assert.equal(payload.method, 'eth_subscribe');
|
|
|
|
assert.deepEqual(payload.params[1], {
|
|
|
|
topics: [],
|
|
|
|
address: address
|
|
|
|
});
|
2016-01-21 15:25:44 +01:00
|
|
|
});
|
2016-10-07 13:07:42 +02:00
|
|
|
provider.injectResult('0x333');
|
2016-01-21 15:25:44 +01:00
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.jsonrpc, '2.0');
|
|
|
|
assert.equal(payload.method, 'eth_unsubscribe');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
provider.injectResult(true);
|
|
|
|
|
|
|
|
|
|
|
|
var contract = new web3.eth.contract(abi, address);
|
|
|
|
|
|
|
|
var count = 0;
|
2016-11-09 15:47:00 +01:00
|
|
|
var event = contract.events.allEvents(function (err, result) {
|
2016-10-07 13:07:42 +02:00
|
|
|
count++;
|
|
|
|
|
|
|
|
if(count === 1) {
|
|
|
|
assert.equal(result.returnValues.from, address);
|
|
|
|
assert.equal(result.returnValues.amount, 1);
|
|
|
|
assert.equal(result.returnValues.t1, 1);
|
|
|
|
assert.equal(result.returnValues.t2, 8);
|
|
|
|
}
|
|
|
|
if(count === 2) {
|
|
|
|
assert.equal(result.returnValues.addressFrom, address);
|
|
|
|
assert.equal(result.returnValues.value, 2);
|
|
|
|
assert.equal(result.returnValues.t1, 5);
|
2016-01-21 15:25:44 +01:00
|
|
|
|
|
|
|
event.unsubscribe();
|
|
|
|
}
|
2016-10-07 13:07:42 +02:00
|
|
|
|
2016-01-21 15:25:44 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
provider.injectNotification({
|
|
|
|
method: 'eth_subscription',
|
|
|
|
params: {
|
|
|
|
subscription: '0x333',
|
|
|
|
result: {
|
2015-06-24 08:50:19 +02:00
|
|
|
address: address,
|
|
|
|
topics: [
|
2016-10-07 13:07:42 +02:00
|
|
|
'0x' + sha3('Changed(address,uint256,uint256,uint256)'),
|
|
|
|
'0x000000000000000000000000'+ address.replace('0x',''),
|
2015-06-24 08:50:19 +02:00
|
|
|
'0x0000000000000000000000000000000000000000000000000000000000000001'
|
|
|
|
],
|
2016-10-07 13:07:42 +02:00
|
|
|
blockNumber: '0x3',
|
|
|
|
transactionHash: '0x1234',
|
|
|
|
blockHash: '0x1345',
|
|
|
|
logIndex: '0x4',
|
2015-06-24 08:50:19 +02:00
|
|
|
data: '0x0000000000000000000000000000000000000000000000000000000000000001' +
|
2016-10-07 13:07:42 +02:00
|
|
|
'0000000000000000000000000000000000000000000000000000000000000008'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
provider.injectNotification({
|
|
|
|
method: 'eth_subscription',
|
|
|
|
params: {
|
|
|
|
subscription: '0x333',
|
|
|
|
result: {
|
|
|
|
address: address,
|
|
|
|
topics: [
|
|
|
|
'0x' + sha3('Unchanged(uint256,address,uint256)'),
|
|
|
|
'0x0000000000000000000000000000000000000000000000000000000000000002',
|
|
|
|
'0x000000000000000000000000'+ address.replace('0x','')
|
|
|
|
],
|
|
|
|
blockNumber: '0x3',
|
|
|
|
transactionHash: '0x1234',
|
|
|
|
blockHash: '0x1345',
|
|
|
|
logIndex: '0x4',
|
|
|
|
data: '0x0000000000000000000000000000000000000000000000000000000000000005'
|
2016-01-21 15:25:44 +01:00
|
|
|
}
|
2015-06-24 08:50:19 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2016-10-07 13:07:42 +02:00
|
|
|
});
|
|
|
|
describe('with methods', function () {
|
2016-11-10 10:51:45 +01:00
|
|
|
it('should change the address', function () {
|
|
|
|
var provider = new FakeHttpProvider();
|
|
|
|
var web3 = new Web3(provider);
|
|
|
|
var signature = 'balance(address)';
|
|
|
|
|
|
|
|
var contract = new web3.eth.contract(abi, address);
|
|
|
|
|
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.method, 'eth_call');
|
|
|
|
assert.deepEqual(payload.params, [{
|
|
|
|
data: '0x' + sha3(signature).slice(0, 8) + '0000000000000000000000001234567890123456789012345678901234567891',
|
|
|
|
to: address,
|
|
|
|
from: address2
|
|
|
|
}, 'latest']);
|
|
|
|
});
|
|
|
|
|
|
|
|
contract.methods.balance('0x1234567890123456789012345678901234567891').call({from: address2});
|
|
|
|
|
|
|
|
// change address
|
2016-11-10 11:14:01 +01:00
|
|
|
contract.options.address = address2;
|
2016-11-10 10:51:45 +01:00
|
|
|
|
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.method, 'eth_call');
|
|
|
|
assert.deepEqual(payload.params, [{
|
|
|
|
data: '0x' + sha3(signature).slice(0, 8) + '0000000000000000000000001234567890123456789012345678901234567891',
|
|
|
|
to: address2,
|
|
|
|
from: address
|
|
|
|
}, 'latest']);
|
|
|
|
});
|
|
|
|
|
|
|
|
contract.methods.balance('0x1234567890123456789012345678901234567891').call({from: address});
|
|
|
|
});
|
|
|
|
|
2016-11-09 16:33:49 +01:00
|
|
|
it('should reset functions when resetting json interface', function () {
|
|
|
|
var provider = new FakeHttpProvider();
|
|
|
|
var web3 = new Web3(provider);
|
|
|
|
|
|
|
|
var contract = new web3.eth.contract(abi);
|
|
|
|
|
|
|
|
assert.isFunction(contract.methods.mySend);
|
|
|
|
assert.isFunction(contract.events.Changed);
|
|
|
|
|
2016-11-10 11:14:01 +01:00
|
|
|
contract.options.jsonInterface = [{
|
2016-11-09 16:33:49 +01:00
|
|
|
"name": "otherSend",
|
|
|
|
"type": "function",
|
|
|
|
"inputs": [{
|
|
|
|
"name": "to",
|
|
|
|
"type": "address"
|
|
|
|
}, {
|
|
|
|
"name": "value",
|
|
|
|
"type": "uint256"
|
|
|
|
}],
|
|
|
|
"outputs": []
|
|
|
|
}, {
|
|
|
|
"name":"Unchanged",
|
|
|
|
"type":"event",
|
|
|
|
"inputs": [
|
|
|
|
{"name":"value","type":"uint256","indexed":true},
|
|
|
|
{"name":"addressFrom","type":"address","indexed":true},
|
|
|
|
{"name":"t1","type":"uint256","indexed":false}
|
|
|
|
]
|
|
|
|
}];
|
|
|
|
|
|
|
|
assert.isFunction(contract.methods.otherSend);
|
|
|
|
assert.isFunction(contract.events.Unchanged);
|
|
|
|
|
|
|
|
assert.isUndefined(contract.methods.mySend);
|
|
|
|
assert.isUndefined(contract.events.Changed);
|
|
|
|
});
|
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
it('should encode a function call', function () {
|
|
|
|
var provider = new FakeHttpProvider();
|
|
|
|
var web3 = new Web3(provider);
|
|
|
|
var signature = 'balance(address)';
|
|
|
|
|
|
|
|
var contract = new web3.eth.contract(abi);
|
|
|
|
|
2016-11-09 15:47:00 +01:00
|
|
|
var result = contract.methods.balance(address).encodeABI();
|
2016-10-07 13:07:42 +02:00
|
|
|
|
|
|
|
assert.equal(result, '0x' + sha3(signature).slice(0, 8) + '0000000000000000000000001234567890123456789012345678901234567891');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should encode a constructor call', function () {
|
|
|
|
var provider = new FakeHttpProvider();
|
|
|
|
var web3 = new Web3(provider);
|
|
|
|
var signature = 'balance(address)';
|
|
|
|
|
|
|
|
var contract = new web3.eth.contract(abi, {data: '0x1234'});
|
|
|
|
|
2016-11-09 15:47:00 +01:00
|
|
|
var result = contract.methods.constructor(address, 10).encodeABI();
|
2016-10-07 13:07:42 +02:00
|
|
|
|
|
|
|
assert.equal(result, '0x1234' + '0000000000000000000000001234567890123456789012345678901234567891'+ '000000000000000000000000000000000000000000000000000000000000000a');
|
|
|
|
});
|
2015-06-24 08:50:19 +02:00
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
it('should estimate a function', function (done) {
|
2015-03-31 09:34:06 +02:00
|
|
|
var provider = new FakeHttpProvider();
|
2016-10-07 13:07:42 +02:00
|
|
|
var web3 = new Web3(provider);
|
|
|
|
var signature = 'balance(address)';
|
|
|
|
|
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.method, 'eth_estimateGas');
|
|
|
|
assert.deepEqual(payload.params, [{
|
|
|
|
data: '0x' + sha3(signature).slice(0, 8) + '0000000000000000000000001234567890123456789012345678901234567891',
|
|
|
|
to: address
|
|
|
|
}]);
|
|
|
|
});
|
2015-05-16 17:03:27 +02:00
|
|
|
provider.injectResult('0x0000000000000000000000000000000000000000000000000000000000000032');
|
2016-10-07 13:07:42 +02:00
|
|
|
|
|
|
|
var contract = new web3.eth.contract(abi, address);
|
|
|
|
|
2016-11-09 15:47:00 +01:00
|
|
|
contract.methods.balance(address).estimateGas(function (err, res) {
|
2016-10-07 13:07:42 +02:00
|
|
|
assert.deepEqual(res, 50);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should estimate the constructor', function (done) {
|
|
|
|
var provider = new FakeHttpProvider();
|
|
|
|
var web3 = new Web3(provider);
|
|
|
|
var signature = 'balance(address)';
|
|
|
|
|
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.method, 'eth_estimateGas');
|
|
|
|
assert.deepEqual(payload.params, [{
|
|
|
|
data: '0x123400000000000000000000000012345678901234567890123456789012345678910000000000000000000000000000000000000000000000000000000000000032',
|
|
|
|
to: address
|
|
|
|
}]);
|
|
|
|
});
|
|
|
|
provider.injectResult('0x000000000000000000000000000000000000000000000000000000000000000a');
|
|
|
|
|
|
|
|
var contract = new web3.eth.contract(abi, address, {data: '0x1234'});
|
|
|
|
|
2016-11-09 15:47:00 +01:00
|
|
|
contract.methods.constructor(address, 50).estimateGas(function (err, res) {
|
2016-10-07 13:07:42 +02:00
|
|
|
assert.deepEqual(res, 10);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call constant function', function (done) {
|
|
|
|
var provider = new FakeHttpProvider();
|
|
|
|
var web3 = new Web3(provider);
|
|
|
|
var signature = 'balance(address)';
|
|
|
|
|
2015-03-31 09:34:06 +02:00
|
|
|
provider.injectValidation(function (payload) {
|
2015-05-16 17:03:27 +02:00
|
|
|
assert.equal(payload.method, 'eth_call');
|
|
|
|
assert.deepEqual(payload.params, [{
|
2015-10-15 14:46:47 +08:00
|
|
|
data: '0x' + sha3(signature).slice(0, 8) + '0000000000000000000000001234567890123456789012345678901234567891',
|
2015-05-16 17:03:27 +02:00
|
|
|
to: address
|
|
|
|
}, 'latest']);
|
2015-03-31 09:34:06 +02:00
|
|
|
});
|
2016-10-07 13:07:42 +02:00
|
|
|
provider.injectResult('0x0000000000000000000000000000000000000000000000000000000000000032');
|
2015-03-31 09:34:06 +02:00
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
var contract = new web3.eth.contract(abi, address);
|
2015-03-31 09:34:06 +02:00
|
|
|
|
2016-11-09 15:47:00 +01:00
|
|
|
contract.methods.balance(address).call(function (err, res) {
|
2016-10-07 13:07:42 +02:00
|
|
|
assert.deepEqual(new BigNumber(0x32), res);
|
|
|
|
done();
|
|
|
|
});
|
2015-03-31 09:34:06 +02:00
|
|
|
});
|
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
it('should call constant function with default block', function (done) {
|
2015-06-05 11:34:50 +02:00
|
|
|
var provider = new FakeHttpProvider();
|
2016-10-07 13:07:42 +02:00
|
|
|
var web3 = new Web3(provider);
|
|
|
|
var signature = 'balance(address)';
|
|
|
|
|
2015-06-05 11:34:50 +02:00
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.method, 'eth_call');
|
|
|
|
assert.deepEqual(payload.params, [{
|
2015-10-15 14:46:47 +08:00
|
|
|
data: '0x' + sha3(signature).slice(0, 8) + '0000000000000000000000001234567890123456789012345678901234567891',
|
2015-06-05 11:34:50 +02:00
|
|
|
to: address
|
|
|
|
}, '0xb']);
|
|
|
|
});
|
2016-10-07 13:07:42 +02:00
|
|
|
provider.injectResult('0x0000000000000000000000000000000000000000000000000000000000000032');
|
2015-06-05 11:34:50 +02:00
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
var contract = new web3.eth.contract(abi, address);
|
2015-06-05 11:34:50 +02:00
|
|
|
|
2016-11-09 15:47:00 +01:00
|
|
|
contract.methods.balance(address).call(11)
|
2016-10-07 13:07:42 +02:00
|
|
|
.then(function (r) {
|
|
|
|
assert.deepEqual(new BigNumber(0x32), r);
|
|
|
|
done();
|
|
|
|
});
|
2015-06-05 11:34:50 +02:00
|
|
|
});
|
|
|
|
|
2015-03-31 09:42:03 +02:00
|
|
|
it('should sendTransaction to contract function', function () {
|
2015-03-31 09:34:06 +02:00
|
|
|
var provider = new FakeHttpProvider();
|
2016-10-07 13:07:42 +02:00
|
|
|
var web3 = new Web3(provider);
|
2016-11-09 15:47:00 +01:00
|
|
|
var signature = 'mySend(address,uint256)';
|
2016-10-07 13:07:42 +02:00
|
|
|
|
2015-03-31 09:34:06 +02:00
|
|
|
provider.injectValidation(function (payload) {
|
2015-05-16 17:03:27 +02:00
|
|
|
assert.equal(payload.method, 'eth_sendTransaction');
|
|
|
|
assert.deepEqual(payload.params, [{
|
2016-10-07 13:07:42 +02:00
|
|
|
data: '0x' + sha3(signature).slice(0, 8) +
|
|
|
|
'0000000000000000000000001234567890123456789012345678901234567891' +
|
2015-05-16 17:03:27 +02:00
|
|
|
'0000000000000000000000000000000000000000000000000000000000000011' ,
|
2015-08-08 21:22:01 +02:00
|
|
|
from: address,
|
2015-05-16 17:03:27 +02:00
|
|
|
to: address
|
|
|
|
}]);
|
2015-03-31 09:34:06 +02:00
|
|
|
});
|
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
var contract = new web3.eth.contract(abi, address);
|
2015-03-31 09:34:06 +02:00
|
|
|
|
2016-11-09 15:47:00 +01:00
|
|
|
contract.methods.mySend(address, 17).send({from: address});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should sendTransaction to contract function using the function namen incl. parameters', function () {
|
|
|
|
var provider = new FakeHttpProvider();
|
|
|
|
var web3 = new Web3(provider);
|
|
|
|
var signature = '0x'+ sha3('mySend(address,uint256)').slice(0, 8);
|
|
|
|
|
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.method, 'eth_sendTransaction');
|
|
|
|
assert.deepEqual(payload.params, [{
|
|
|
|
data: signature +
|
|
|
|
'0000000000000000000000001234567890123456789012345678901234567891' +
|
|
|
|
'0000000000000000000000000000000000000000000000000000000000000011' ,
|
|
|
|
from: address,
|
|
|
|
to: address
|
|
|
|
}]);
|
|
|
|
});
|
|
|
|
|
|
|
|
var contract = new web3.eth.contract(abi, address);
|
|
|
|
|
|
|
|
contract.methods['mySend(address,uint256)'](address, 17).send({from: address});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should sendTransaction to contract function using the signature', function () {
|
|
|
|
var provider = new FakeHttpProvider();
|
|
|
|
var web3 = new Web3(provider);
|
|
|
|
var signature = '0x'+ sha3('mySend(address,uint256)').slice(0, 8);
|
|
|
|
|
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.method, 'eth_sendTransaction');
|
|
|
|
assert.deepEqual(payload.params, [{
|
|
|
|
data: signature +
|
|
|
|
'0000000000000000000000001234567890123456789012345678901234567891' +
|
|
|
|
'0000000000000000000000000000000000000000000000000000000000000011' ,
|
|
|
|
from: address,
|
|
|
|
to: address
|
|
|
|
}]);
|
|
|
|
});
|
|
|
|
|
|
|
|
var contract = new web3.eth.contract(abi, address);
|
|
|
|
|
|
|
|
contract.methods[signature](address, 17).send({from: address});
|
2015-03-31 09:34:06 +02:00
|
|
|
});
|
2015-03-31 09:42:03 +02:00
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
it('should make a call with optional params', function (done) {
|
2015-03-31 09:42:03 +02:00
|
|
|
var provider = new FakeHttpProvider();
|
2016-10-07 13:07:42 +02:00
|
|
|
var web3 = new Web3(provider);
|
2015-05-16 17:03:27 +02:00
|
|
|
var signature = 'balance(address)';
|
2016-10-07 13:07:42 +02:00
|
|
|
var count = 0;
|
|
|
|
|
|
|
|
|
2015-03-31 09:42:03 +02:00
|
|
|
provider.injectValidation(function (payload) {
|
2016-10-07 13:07:42 +02:00
|
|
|
count++;
|
|
|
|
if(count > 1) return;
|
|
|
|
|
2015-05-16 17:03:27 +02:00
|
|
|
assert.equal(payload.method, 'eth_call');
|
|
|
|
assert.deepEqual(payload.params, [{
|
2015-10-15 14:46:47 +08:00
|
|
|
data: '0x' + sha3(signature).slice(0, 8) + '0000000000000000000000001234567890123456789012345678901234567891',
|
2015-05-16 17:03:27 +02:00
|
|
|
to: address,
|
|
|
|
from: address,
|
|
|
|
gas: '0xc350'
|
|
|
|
}, 'latest']);
|
2015-03-31 09:42:03 +02:00
|
|
|
});
|
2016-10-07 13:07:42 +02:00
|
|
|
provider.injectResult('0x0000000000000000000000000000000000000000000000000000000000000032');
|
2015-03-31 09:42:03 +02:00
|
|
|
|
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
var contract = new web3.eth.contract(abi, address);
|
2015-03-31 09:42:03 +02:00
|
|
|
|
2016-11-09 15:47:00 +01:00
|
|
|
contract.methods.balance(address).call({from: address, gas: 50000})
|
2016-10-07 13:07:42 +02:00
|
|
|
.on('data', function (r) {
|
|
|
|
assert.deepEqual(new BigNumber(0x32), r);
|
|
|
|
done();
|
|
|
|
});
|
2015-03-31 09:42:03 +02:00
|
|
|
});
|
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
it('should explicitly make a call with optional params', function (done) {
|
2015-04-22 15:24:53 +02:00
|
|
|
var provider = new FakeHttpProvider();
|
2016-10-07 13:07:42 +02:00
|
|
|
var web3 = new Web3(provider);
|
2015-05-16 17:03:27 +02:00
|
|
|
var signature = 'balance(address)';
|
2016-10-07 13:07:42 +02:00
|
|
|
|
2015-04-22 15:24:53 +02:00
|
|
|
provider.injectValidation(function (payload) {
|
2015-05-16 17:03:27 +02:00
|
|
|
assert.equal(payload.method, 'eth_call');
|
|
|
|
assert.deepEqual(payload.params, [{
|
2015-10-15 14:46:47 +08:00
|
|
|
data: '0x' + sha3(signature).slice(0, 8) + '0000000000000000000000001234567890123456789012345678901234567891',
|
2015-05-16 17:03:27 +02:00
|
|
|
to: address,
|
|
|
|
from: address,
|
|
|
|
gas: '0xc350'
|
|
|
|
}, 'latest']);
|
2015-04-22 15:24:53 +02:00
|
|
|
});
|
2016-10-07 13:07:42 +02:00
|
|
|
provider.injectResult('0x0000000000000000000000000000000000000000000000000000000000000032');
|
2015-04-22 15:24:53 +02:00
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
var contract = new web3.eth.contract(abi, address);
|
2015-04-22 15:24:53 +02:00
|
|
|
|
2016-11-09 15:47:00 +01:00
|
|
|
contract.methods.balance(address).call({from: address, gas: 50000})
|
2016-10-07 13:07:42 +02:00
|
|
|
.then(function (r) {
|
|
|
|
assert.deepEqual(new BigNumber(0x32), r);
|
|
|
|
done();
|
|
|
|
});
|
2015-03-31 09:42:03 +02:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
it('should explicitly make a call with optional params and defaultBlock', function (done) {
|
2015-06-05 11:34:50 +02:00
|
|
|
var provider = new FakeHttpProvider();
|
2016-10-07 13:07:42 +02:00
|
|
|
var web3 = new Web3(provider);
|
2015-06-05 11:34:50 +02:00
|
|
|
var signature = 'balance(address)';
|
2016-10-07 13:07:42 +02:00
|
|
|
|
2015-06-05 11:34:50 +02:00
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.method, 'eth_call');
|
|
|
|
assert.deepEqual(payload.params, [{
|
2015-10-15 14:46:47 +08:00
|
|
|
data: '0x' + sha3(signature).slice(0, 8) + '0000000000000000000000001234567890123456789012345678901234567891',
|
2015-06-05 11:34:50 +02:00
|
|
|
to: address,
|
|
|
|
from: address,
|
|
|
|
gas: '0xc350'
|
|
|
|
}, '0xb']);
|
|
|
|
});
|
2016-10-07 13:07:42 +02:00
|
|
|
provider.injectResult('0x0000000000000000000000000000000000000000000000000000000000000032');
|
2015-06-05 11:34:50 +02:00
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
var contract = new web3.eth.contract(abi, address);
|
2015-06-05 11:34:50 +02:00
|
|
|
|
2016-11-09 15:47:00 +01:00
|
|
|
contract.methods.balance(address).call({from: address, gas: 50000}, 11)
|
2016-10-07 13:07:42 +02:00
|
|
|
.then(function (r) {
|
|
|
|
assert.deepEqual(new BigNumber(0x32), r);
|
|
|
|
done();
|
|
|
|
});
|
2015-06-05 11:34:50 +02:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2015-03-31 10:35:36 +02:00
|
|
|
it('should sendTransaction with optional params', function () {
|
|
|
|
var provider = new FakeHttpProvider();
|
2016-10-07 13:07:42 +02:00
|
|
|
var web3 = new Web3(provider);
|
2016-11-09 15:47:00 +01:00
|
|
|
var signature = 'mySend(address,uint256)';
|
2016-10-07 13:07:42 +02:00
|
|
|
|
2015-03-31 10:35:36 +02:00
|
|
|
provider.injectValidation(function (payload) {
|
2015-05-16 17:03:27 +02:00
|
|
|
assert.equal(payload.method, 'eth_sendTransaction');
|
|
|
|
assert.deepEqual(payload.params, [{
|
2016-10-07 13:07:42 +02:00
|
|
|
data: '0x' + sha3(signature).slice(0, 8) +
|
|
|
|
'0000000000000000000000001234567890123456789012345678901234567891' +
|
2015-05-16 17:03:27 +02:00
|
|
|
'0000000000000000000000000000000000000000000000000000000000000011' ,
|
|
|
|
to: address,
|
|
|
|
from: address,
|
|
|
|
gas: '0xc350',
|
|
|
|
gasPrice: '0xbb8',
|
|
|
|
value: '0x2710'
|
|
|
|
}]);
|
2015-03-31 10:35:36 +02:00
|
|
|
});
|
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
var contract = new web3.eth.contract(abi, address);
|
2015-03-31 10:35:36 +02:00
|
|
|
|
2016-11-09 15:47:00 +01:00
|
|
|
contract.methods.mySend(address, 17).send({from: address, gas: 50000, gasPrice: 3000, value: 10000});
|
2015-03-31 10:35:36 +02:00
|
|
|
});
|
2015-04-22 15:24:53 +02:00
|
|
|
|
|
|
|
it('should explicitly sendTransaction with optional params', function () {
|
|
|
|
var provider = new FakeHttpProvider();
|
2016-10-07 13:07:42 +02:00
|
|
|
var web3 = new Web3(provider);
|
2016-11-09 15:47:00 +01:00
|
|
|
var signature = 'mySend(address,uint256)';
|
2016-10-07 13:07:42 +02:00
|
|
|
|
2015-04-22 15:24:53 +02:00
|
|
|
provider.injectValidation(function (payload) {
|
2015-05-16 17:03:27 +02:00
|
|
|
assert.equal(payload.method, 'eth_sendTransaction');
|
|
|
|
assert.deepEqual(payload.params, [{
|
2016-10-07 13:07:42 +02:00
|
|
|
data: '0x' + sha3(signature).slice(0, 8) +
|
|
|
|
'0000000000000000000000001234567890123456789012345678901234567891' +
|
2015-05-16 17:03:27 +02:00
|
|
|
'0000000000000000000000000000000000000000000000000000000000000011' ,
|
|
|
|
to: address,
|
|
|
|
from: address,
|
|
|
|
gas: '0xc350',
|
|
|
|
gasPrice: '0xbb8',
|
|
|
|
value: '0x2710'
|
|
|
|
}]);
|
2015-04-22 15:24:53 +02:00
|
|
|
});
|
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
var contract = new web3.eth.contract(abi, address);
|
2015-04-22 15:24:53 +02:00
|
|
|
|
2016-11-09 15:47:00 +01:00
|
|
|
contract.methods.mySend(address, 17).send({from: address, gas: 50000, gasPrice: 3000, value: 10000});
|
2015-03-31 10:35:36 +02:00
|
|
|
});
|
2015-04-26 10:08:58 +02:00
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
it('should explicitly call sendTransaction with optional params and call callback without error', function (done) {
|
2015-05-11 18:59:46 +02:00
|
|
|
var provider = new FakeHttpProvider();
|
2016-10-07 13:07:42 +02:00
|
|
|
var web3 = new Web3(provider);
|
2016-11-09 15:47:00 +01:00
|
|
|
var signature = 'mySend(address,uint256)';
|
2016-10-07 13:07:42 +02:00
|
|
|
|
2015-05-11 18:59:46 +02:00
|
|
|
provider.injectValidation(function (payload) {
|
2016-10-07 13:07:42 +02:00
|
|
|
|
2015-05-16 17:03:27 +02:00
|
|
|
assert.equal(payload.method, 'eth_sendTransaction');
|
|
|
|
assert.deepEqual(payload.params, [{
|
2016-10-07 13:07:42 +02:00
|
|
|
data: '0x' + sha3(signature).slice(0, 8) +
|
|
|
|
'0000000000000000000000001234567890123456789012345678901234567891' +
|
2015-05-16 17:03:27 +02:00
|
|
|
'0000000000000000000000000000000000000000000000000000000000000011' ,
|
|
|
|
to: address,
|
|
|
|
from: address,
|
|
|
|
gas: '0xc350',
|
|
|
|
gasPrice: '0xbb8',
|
|
|
|
value: '0x2710'
|
|
|
|
}]);
|
2015-05-11 18:59:46 +02:00
|
|
|
});
|
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
var contract = new web3.eth.contract(abi, address);
|
2015-05-11 18:59:46 +02:00
|
|
|
|
2016-11-09 15:47:00 +01:00
|
|
|
contract.methods.mySend(address, 17).send({from: address, gas: 50000, gasPrice: 3000, value: 10000}, function (err) {
|
2015-05-11 18:59:46 +02:00
|
|
|
assert.equal(err, null);
|
|
|
|
done();
|
|
|
|
});
|
2016-10-07 13:07:42 +02:00
|
|
|
})
|
2015-05-11 18:59:46 +02:00
|
|
|
|
2015-05-20 11:58:09 +02:00
|
|
|
it('should explicitly estimateGas with optional params', function () {
|
|
|
|
var provider = new FakeHttpProvider();
|
2016-10-07 13:07:42 +02:00
|
|
|
var web3 = new Web3(provider);
|
2016-11-09 15:47:00 +01:00
|
|
|
var signature = 'mySend(address,uint256)';
|
2016-10-07 13:07:42 +02:00
|
|
|
|
2015-05-20 11:58:09 +02:00
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.method, 'eth_estimateGas');
|
|
|
|
assert.deepEqual(payload.params, [{
|
2016-10-07 13:07:42 +02:00
|
|
|
data: '0x' + sha3(signature).slice(0, 8) +
|
|
|
|
'0000000000000000000000001234567890123456789012345678901234567891' +
|
2015-05-20 11:58:09 +02:00
|
|
|
'0000000000000000000000000000000000000000000000000000000000000011' ,
|
|
|
|
to: address,
|
|
|
|
from: address,
|
|
|
|
gas: '0xc350',
|
|
|
|
gasPrice: '0xbb8',
|
|
|
|
value: '0x2710'
|
|
|
|
}]);
|
|
|
|
});
|
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
var contract = new web3.eth.contract(abi, address);
|
2015-05-20 11:58:09 +02:00
|
|
|
|
2016-11-09 15:47:00 +01:00
|
|
|
contract.methods.mySend(address, 17).estimateGas({from: address, gas: 50000, gasPrice: 3000, value: 10000});
|
2015-05-20 11:58:09 +02:00
|
|
|
});
|
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
it('getPastEvents should get past events and format them correctly', function (done) {
|
|
|
|
var provider = new FakeHttpProvider();
|
|
|
|
var web3 = new Web3(provider);
|
2015-05-16 17:03:27 +02:00
|
|
|
var signature = 'testArr(int[])';
|
2016-10-07 13:07:42 +02:00
|
|
|
|
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.method, 'eth_getLogs');
|
|
|
|
assert.deepEqual(payload.params, [{
|
|
|
|
address: address,
|
|
|
|
topics: [
|
|
|
|
"0x792991ed5ba9322deaef76cff5051ce4bedaaa4d097585970f9ad8f09f54e651",
|
|
|
|
"0x000000000000000000000000" + address2.replace('0x',''),
|
|
|
|
null
|
|
|
|
]
|
|
|
|
}]);
|
|
|
|
});
|
|
|
|
|
|
|
|
var topic1 = [
|
|
|
|
'0x' + sha3(signature),
|
|
|
|
'0x000000000000000000000000'+ address.replace('0x',''),
|
|
|
|
'0x000000000000000000000000000000000000000000000000000000000000000a'
|
|
|
|
];
|
|
|
|
var topic2 = [
|
|
|
|
'0x' + sha3(signature),
|
|
|
|
'0x000000000000000000000000'+ address.replace('0x',''),
|
|
|
|
'0x0000000000000000000000000000000000000000000000000000000000000003'
|
|
|
|
];
|
|
|
|
|
|
|
|
provider.injectResult([{
|
|
|
|
address: address,
|
|
|
|
topics: topic1,
|
|
|
|
blockNumber: '0x3',
|
|
|
|
transactionHash: '0x1234',
|
|
|
|
blockHash: '0x1345',
|
|
|
|
logIndex: '0x4',
|
|
|
|
data: '0x0000000000000000000000000000000000000000000000000000000000000002' +
|
|
|
|
'0000000000000000000000000000000000000000000000000000000000000009'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
address: address,
|
|
|
|
topics: topic2,
|
|
|
|
blockNumber: '0x4',
|
|
|
|
transactionHash: '0x1235',
|
|
|
|
blockHash: '0x1346',
|
|
|
|
logIndex: '0x1',
|
|
|
|
data: '0x0000000000000000000000000000000000000000000000000000000000000004' +
|
|
|
|
'0000000000000000000000000000000000000000000000000000000000000005'
|
2015-04-26 10:08:58 +02:00
|
|
|
}]);
|
2016-10-07 13:07:42 +02:00
|
|
|
|
|
|
|
var contract = new web3.eth.contract(abi, address);
|
|
|
|
contract.getPastEvents('Changed',{filter: {from: address2}})
|
|
|
|
.on('data', function (result) {
|
|
|
|
|
|
|
|
assert.deepEqual(result, [{
|
|
|
|
event: "Changed",
|
|
|
|
id: "log_9ff24cb4",
|
|
|
|
address: address,
|
|
|
|
topics: topic1,
|
|
|
|
blockNumber: 3,
|
|
|
|
transactionHash: '0x1234',
|
|
|
|
blockHash: '0x1345',
|
|
|
|
logIndex: 4,
|
|
|
|
transactionIndex: 0,
|
|
|
|
data: '0x0000000000000000000000000000000000000000000000000000000000000002' +
|
|
|
|
'0000000000000000000000000000000000000000000000000000000000000009',
|
|
|
|
returnValues: {
|
|
|
|
from: address,
|
|
|
|
amount: new BigNumber(0xa),
|
|
|
|
t1: new BigNumber(0x2),
|
|
|
|
t2: new BigNumber(0x9),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
event: "Changed",
|
|
|
|
id: "log_29c93e15",
|
|
|
|
address: address,
|
|
|
|
topics: topic2,
|
|
|
|
blockNumber: 4,
|
|
|
|
transactionHash: '0x1235',
|
|
|
|
blockHash: '0x1346',
|
|
|
|
logIndex: 1,
|
|
|
|
transactionIndex: 0,
|
|
|
|
data: '0x0000000000000000000000000000000000000000000000000000000000000004' +
|
|
|
|
'0000000000000000000000000000000000000000000000000000000000000005',
|
|
|
|
returnValues: {
|
|
|
|
from: address,
|
|
|
|
amount: new BigNumber(0x3),
|
|
|
|
t1: new BigNumber(0x4),
|
|
|
|
t2: new BigNumber(0x5),
|
|
|
|
}
|
|
|
|
}]);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call testArr method and properly parse result', function (done) {
|
|
|
|
var provider = new FakeHttpProvider();
|
|
|
|
var web3 = new Web3(provider);
|
|
|
|
var signature = 'testArr(int[])';
|
|
|
|
|
2015-04-26 10:08:58 +02:00
|
|
|
provider.injectValidation(function (payload) {
|
2015-05-16 17:03:27 +02:00
|
|
|
assert.equal(payload.method, 'eth_call');
|
|
|
|
assert.deepEqual(payload.params, [{
|
2016-10-07 13:07:42 +02:00
|
|
|
data: '0x' + sha3(signature).slice(0, 8) +
|
|
|
|
'0000000000000000000000000000000000000000000000000000000000000020' +
|
|
|
|
'0000000000000000000000000000000000000000000000000000000000000001' +
|
2015-05-16 17:03:27 +02:00
|
|
|
'0000000000000000000000000000000000000000000000000000000000000003',
|
|
|
|
to: address
|
|
|
|
},
|
2016-10-07 13:07:42 +02:00
|
|
|
'latest'
|
|
|
|
]);
|
2015-04-26 10:08:58 +02:00
|
|
|
});
|
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
provider.injectResult('0x0000000000000000000000000000000000000000000000000000000000000005');
|
|
|
|
|
|
|
|
var contract = new web3.eth.contract(abi, address);
|
2016-11-09 15:47:00 +01:00
|
|
|
contract.methods.testArr([3]).call()
|
2016-10-07 13:07:42 +02:00
|
|
|
.then(function (result) {
|
|
|
|
assert.deepEqual(new BigNumber(5), result);
|
|
|
|
done();
|
|
|
|
});
|
2015-04-26 10:27:21 +02:00
|
|
|
|
2015-04-26 10:08:58 +02:00
|
|
|
});
|
2016-10-07 13:07:42 +02:00
|
|
|
|
|
|
|
it('should call testArr method, properly parse result and return the result in a callback', function (done) {
|
|
|
|
var provider = new FakeHttpProvider();
|
|
|
|
var web3 = new Web3(provider);
|
2015-05-16 17:03:27 +02:00
|
|
|
var signature = 'testArr(int[])';
|
2016-10-07 13:07:42 +02:00
|
|
|
|
2015-05-11 18:59:46 +02:00
|
|
|
provider.injectValidation(function (payload) {
|
2015-05-16 17:03:27 +02:00
|
|
|
assert.equal(payload.method, 'eth_call');
|
|
|
|
assert.deepEqual(payload.params, [{
|
2016-10-07 13:07:42 +02:00
|
|
|
data: '0x' + sha3(signature).slice(0, 8) +
|
|
|
|
'0000000000000000000000000000000000000000000000000000000000000020' +
|
|
|
|
'0000000000000000000000000000000000000000000000000000000000000001' +
|
2015-05-16 17:03:27 +02:00
|
|
|
'0000000000000000000000000000000000000000000000000000000000000003',
|
|
|
|
to: address
|
|
|
|
},
|
2016-10-07 13:07:42 +02:00
|
|
|
'latest'
|
2015-05-16 17:03:27 +02:00
|
|
|
]);
|
2015-05-11 18:59:46 +02:00
|
|
|
});
|
2016-10-07 13:07:42 +02:00
|
|
|
provider.injectResult('0x0000000000000000000000000000000000000000000000000000000000000005');
|
2015-05-11 18:59:46 +02:00
|
|
|
|
2016-10-07 13:07:42 +02:00
|
|
|
var contract = new web3.eth.contract(abi, address);
|
2015-05-11 18:59:46 +02:00
|
|
|
|
2016-11-09 15:47:00 +01:00
|
|
|
contract.methods.testArr([3]).call(function (err, result) {
|
2015-05-11 18:59:46 +02:00
|
|
|
assert.deepEqual(new BigNumber(5), result);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2015-03-27 08:42:23 +01:00
|
|
|
});
|
2016-10-07 13:07:42 +02:00
|
|
|
describe('with data', function () {
|
|
|
|
it('should deploy a contract and use callback', function (done) {
|
|
|
|
var provider = new FakeHttpProvider();
|
|
|
|
var web3 = new Web3(provider);
|
|
|
|
|
|
|
|
provider.injectValidation(function () {});
|
|
|
|
|
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.method, 'eth_sendTransaction');
|
|
|
|
assert.deepEqual(payload.params, [{
|
|
|
|
data: '0x1234567000000000000000000000000555456789012345678901234567890123456789100000000000000000000000000000000000000000000000000000000000000c8' ,
|
|
|
|
from: address,
|
|
|
|
gas: '0xc350',
|
|
|
|
gasPrice: '0xbb8'
|
|
|
|
}]);
|
|
|
|
});
|
|
|
|
|
|
|
|
var contract = new web3.eth.contract(abi);
|
|
|
|
|
|
|
|
contract.deploy({
|
|
|
|
from: address,
|
|
|
|
data: '0x1234567',
|
|
|
|
arguments: ['0x5554567890123456789012345678901234567891', 200],
|
|
|
|
gas: 50000,
|
|
|
|
gasPrice: 3000
|
|
|
|
}, function (err) {
|
|
|
|
assert.equal(err, null);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should deploy a contract and use all promise steps', function (done) {
|
|
|
|
var provider = new FakeHttpProvider();
|
|
|
|
var web3 = new Web3(provider);
|
|
|
|
|
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
|
|
|
|
assert.equal(payload.method, 'eth_sendTransaction');
|
|
|
|
assert.deepEqual(payload.params, [{
|
|
|
|
data: '0x1234567000000000000000000000000123456789012345678901234567890123456789100000000000000000000000000000000000000000000000000000000000000c8' ,
|
|
|
|
from: address,
|
|
|
|
gas: '0xc350',
|
|
|
|
gasPrice: '0xbb8'
|
|
|
|
}]);
|
|
|
|
|
|
|
|
});
|
|
|
|
provider.injectResult('0x5550000000000000000000000000000000000000000000000000000000000032');
|
|
|
|
|
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.method, 'eth_subscribe');
|
|
|
|
assert.deepEqual(payload.params, ['newBlocks', {}]);
|
|
|
|
});
|
|
|
|
provider.injectResult('0x1234567');
|
|
|
|
|
|
|
|
// fake newBlock
|
|
|
|
provider.injectNotification({
|
|
|
|
method: 'eth_subscription',
|
|
|
|
params: {
|
|
|
|
subscription: '0x1234567',
|
|
|
|
result: {
|
|
|
|
blockNumber: '0x10'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.method, 'eth_getTransactionReceipt');
|
|
|
|
assert.deepEqual(payload.params, ['0x5550000000000000000000000000000000000000000000000000000000000032']);
|
|
|
|
});
|
|
|
|
provider.injectResult({
|
|
|
|
contractAddress: address
|
|
|
|
});
|
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.method, 'eth_getCode');
|
|
|
|
assert.deepEqual(payload.params, [address, 'latest']);
|
|
|
|
});
|
|
|
|
provider.injectResult('0x321');
|
|
|
|
|
|
|
|
|
|
|
|
var contract = new web3.eth.contract(abi);
|
|
|
|
|
2016-11-01 16:03:03 +01:00
|
|
|
contract.deploy({
|
2016-10-07 13:07:42 +02:00
|
|
|
from: address,
|
|
|
|
data: '0x1234567',
|
|
|
|
arguments: [address, 200],
|
|
|
|
gas: 50000,
|
|
|
|
gasPrice: 3000
|
2016-11-01 16:03:03 +01:00
|
|
|
})
|
|
|
|
.on('transactionHash', function (value) {
|
2016-10-07 13:07:42 +02:00
|
|
|
assert.equal('0x5550000000000000000000000000000000000000000000000000000000000032', value);
|
2016-11-01 16:03:03 +01:00
|
|
|
})
|
|
|
|
.on('receipt', function (receipt) {
|
|
|
|
assert.equal(address, receipt.contractAddress);
|
2016-10-07 13:07:42 +02:00
|
|
|
done();
|
|
|
|
});
|
2016-11-01 16:03:03 +01:00
|
|
|
// .on('error', function (value) {
|
2016-10-07 13:07:42 +02:00
|
|
|
// console.log('error', value);
|
|
|
|
// done();
|
|
|
|
// });
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO add error check
|
|
|
|
});
|
2015-03-27 08:42:23 +01:00
|
|
|
});
|
2015-05-12 07:01:33 +02:00
|
|
|
|