web3.js/test/web3.eth.contract.js

251 lines
6.8 KiB
JavaScript
Raw Normal View History

2015-01-28 13:20:36 +00:00
var assert = require('assert');
var FakeHttpProvider = require('./helpers/FakeHttpProvider');
2015-10-08 06:41:55 +00:00
var Web3 = require('../index');
2015-10-15 06:46:47 +00:00
2015-01-28 13:20:36 +00:00
2015-03-25 22:50:39 +00:00
describe('web3.eth.contract', function() {
2015-01-28 13:20:36 +00:00
it('should create simple contract with one method from abi with explicit type name', function () {
// given
var description = [{
"name": "test(uint256)",
"type": "function",
"inputs": [{
"name": "a",
"type": "uint256"
}
],
"outputs": [
{
"name": "d",
"type": "uint256"
}
]
}];
2015-10-15 06:46:47 +00:00
var address = '0x1234567890123456789012345678901234567892';
2015-01-28 13:20:36 +00:00
// when
2015-10-15 06:46:47 +00:00
var web3 = new Web3();
2015-10-08 06:41:55 +00:00
var myCon = web3.eth.contract(description).at(address);
2015-01-28 13:20:36 +00:00
// then
2015-03-06 15:09:21 +00:00
assert.equal('function', typeof myCon.test);
assert.equal('function', typeof myCon.test['uint256']);
2015-01-28 13:20:36 +00:00
});
it('should create simple contract with one method from abi with implicit type name', function () {
// given
var description = [{
"name": "test",
"type": "function",
"inputs": [{
"name": "a",
"type": "uint256"
}
],
"outputs": [
{
"name": "d",
"type": "uint256"
}
]
}];
2015-10-15 06:46:47 +00:00
var address = '0x1234567890123456789012345678901234567892';
2015-01-28 13:20:36 +00:00
// when
2015-10-15 06:46:47 +00:00
var web3 = new Web3();
2015-10-08 06:41:55 +00:00
var myCon = web3.eth.contract(description).at(address);
2015-01-28 13:20:36 +00:00
// then
2015-03-06 15:09:21 +00:00
assert.equal('function', typeof myCon.test);
assert.equal('function', typeof myCon.test['uint256']);
2015-01-28 13:20:36 +00:00
});
it('should create contract with multiple methods', function () {
// given
var description = [{
"name": "test",
"type": "function",
"inputs": [{
"name": "a",
"type": "uint256"
}
],
"outputs": [
{
"name": "d",
"type": "uint256"
}
],
}, {
"name": "test2",
"type": "function",
"inputs": [{
"name": "a",
"type": "uint256"
}
],
"outputs": [
{
"name": "d",
"type": "uint256"
}
]
}];
2015-10-15 06:46:47 +00:00
var address = '0x1234567890123456789012345678901234567892';
2015-01-28 13:20:36 +00:00
// when
2015-10-15 06:46:47 +00:00
var web3 = new Web3();
2015-10-08 06:41:55 +00:00
var myCon = web3.eth.contract(description).at(address);
2015-01-28 13:20:36 +00:00
// then
2015-03-06 15:09:21 +00:00
assert.equal('function', typeof myCon.test);
assert.equal('function', typeof myCon.test['uint256']);
assert.equal('function', typeof myCon.test2);
assert.equal('function', typeof myCon.test2['uint256']);
2015-01-28 13:20:36 +00:00
});
it('should create contract with overloaded methods', function () {
// given
var description = [{
"name": "test",
"type": "function",
"inputs": [{
"name": "a",
"type": "uint256"
}
],
"outputs": [
{
"name": "d",
"type": "uint256"
}
],
}, {
"name": "test",
"type": "function",
"inputs": [{
"name": "a",
"type": "string"
}
],
"outputs": [
{
"name": "d",
"type": "uint256"
}
]
}];
2015-10-15 06:46:47 +00:00
var address = '0x1234567890123456789012345678901234567892';
2015-01-28 13:20:36 +00:00
// when
2015-10-15 06:46:47 +00:00
var web3 = new Web3();
2015-10-08 06:41:55 +00:00
var myCon = web3.eth.contract(description).at(address);
2015-01-28 13:20:36 +00:00
// then
2015-03-06 15:09:21 +00:00
assert.equal('function', typeof myCon.test);
assert.equal('function', typeof myCon.test['uint256']);
assert.equal('function', typeof myCon.test['string']);
2015-01-28 13:20:36 +00:00
});
it('should create contract with no methods', function () {
// given
var description = [{
"name": "test(uint256)",
"inputs": [{
"name": "a",
"type": "uint256"
}
],
"outputs": [
{
"name": "d",
"type": "uint256"
}
]
}];
2015-10-15 06:46:47 +00:00
var address = '0x1234567890123456789012345678901234567892';
2015-01-28 13:20:36 +00:00
// when
2015-10-15 06:46:47 +00:00
var web3 = new Web3();
2015-10-08 06:41:55 +00:00
var myCon = web3.eth.contract(description).at(address);
2015-01-28 13:20:36 +00:00
// then
2015-03-06 15:09:21 +00:00
assert.equal('undefined', typeof myCon.test);
2015-01-28 13:20:36 +00:00
});
2015-01-29 11:35:21 +00:00
it('should create contract with one event', function () {
// given
var description = [{
"name": "test",
"type": "event",
"inputs": [{
"name": "a",
"type": "uint256"
}
],
"outputs": [
{
"name": "d",
"type": "uint256"
}
]
}];
2015-10-15 06:46:47 +00:00
var address = '0x1234567890123456789012345678901234567892';
2015-01-29 11:35:21 +00:00
// when
2015-10-15 06:46:47 +00:00
var web3 = new Web3();
2015-10-08 06:41:55 +00:00
var myCon = web3.eth.contract(description).at(address);
2015-01-29 11:35:21 +00:00
// then
2015-03-06 15:09:21 +00:00
assert.equal('function', typeof myCon.test);
assert.equal('function', typeof myCon.test['uint256']);
2015-01-29 11:35:21 +00:00
});
it('should create contract with nondefault constructor', function (done) {
var provider = new FakeHttpProvider();
2015-10-15 06:46:47 +00:00
var web3 = new Web3(provider);
var address = '0x1234567890123456789012345678901234567894';
var code = '0x31241231231123123123123121cf121212i123123123123123512312412512111111';
var description = [{
"name": "test",
"type": "constructor",
"inputs": [{
"name": "a",
"type": "uint256"
}
]
}];
2015-07-06 14:47:49 +00:00
var steps = 1;
provider.injectResult(address);
provider.injectValidation(function (payload) {
if (steps === 1) {
2015-07-06 14:47:49 +00:00
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, 'eth_sendTransaction');
assert.equal(payload.params[0].data, code + '0000000000000000000000000000000000000000000000000000000000000002');
steps++;
} else if (steps === 2) {
2015-07-06 14:47:49 +00:00
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, 'eth_newBlockFilter');
steps++;
2015-07-06 14:47:49 +00:00
}
});
2015-10-08 06:41:55 +00:00
web3.eth.contract(description).new(2, {from: address, data: code}, function(e, myCon){
2015-10-15 06:46:47 +00:00
done();
web3.stopWatching();
2015-07-06 14:47:49 +00:00
});
});
2015-01-28 13:20:36 +00:00
});