19 changed files with 810 additions and 158 deletions
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -0,0 +1,67 @@ |
|||
<!doctype> |
|||
<html> |
|||
<head> |
|||
<script type="text/javascript" src="js/bignumber.js/bignumber.min.js"></script> |
|||
<script type="text/javascript" src="../dist/ethereum.js"></script> |
|||
<script type="text/javascript"> |
|||
var web3 = require('web3'); |
|||
web3.setProvider(new web3.providers.HttpSyncProvider('http://localhost:8080')); |
|||
|
|||
var desc = [{ |
|||
"type":"event", |
|||
"inputs": [{"name":"a","type":"uint256","indexed":true},{"name":"b","type":"hash256","indexed":false}], |
|||
"name":"Event" |
|||
}, { |
|||
"type":"event", |
|||
"inputs": [{"name":"a","type":"uint256","indexed":true},{"name":"b","type":"hash256","indexed":false}], |
|||
"name":"Event2" |
|||
}, { |
|||
"type":"function", |
|||
"inputs": [{"name":"a","type":"uint256"}], |
|||
"name":"foo", |
|||
"outputs": [] |
|||
}]; |
|||
|
|||
var address = '0x01'; |
|||
|
|||
var contract = web3.eth.contract(address, desc); |
|||
|
|||
function test1() { |
|||
web3.eth.watch(contract).changed(function (res) { |
|||
|
|||
}); |
|||
}; |
|||
|
|||
function test2() { |
|||
web3.eth.watch(contract.Event).changed(function (res) { |
|||
|
|||
}); |
|||
}; |
|||
|
|||
function test3() { |
|||
contract.Event().changed(function (res) { |
|||
|
|||
}); |
|||
}; |
|||
|
|||
// not valid |
|||
// function test4() { |
|||
// web3.eth.watch([contract.Event, contract.Event2]).changed(function (res) { |
|||
// }); |
|||
// }; |
|||
|
|||
</script> |
|||
</head> |
|||
|
|||
<body> |
|||
<div> |
|||
<button type="button" onClick="test1();">test1</button> |
|||
</div> |
|||
<div> |
|||
<button type="button" onClick="test2();">test2</button> |
|||
</div> |
|||
<div> |
|||
<button type="button" onClick="test3();">test3</button> |
|||
</div> |
|||
</body> |
|||
</html> |
@ -0,0 +1,35 @@ |
|||
/* |
|||
This file is part of ethereum.js. |
|||
|
|||
ethereum.js is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU Lesser General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
ethereum.js is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU Lesser General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU Lesser General Public License |
|||
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file event.js |
|||
* @authors: |
|||
* Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2014 |
|||
*/ |
|||
|
|||
var implementationOfEvent = function (address, signature) { |
|||
|
|||
return function (options) { |
|||
var o = options || {}; |
|||
o.address = o.address || address; |
|||
o.topics = o.topics || []; |
|||
o.topics.push(signature); |
|||
return o; |
|||
}; |
|||
}; |
|||
|
|||
module.exports = implementationOfEvent; |
|||
|
@ -0,0 +1,49 @@ |
|||
var assert = require('assert'); |
|||
var abi = require('../lib/abi.js'); |
|||
|
|||
describe('abi', function() { |
|||
it('should filter functions and events from input array properly', function () { |
|||
|
|||
// given
|
|||
var description = [{ |
|||
"name": "test", |
|||
"type": "function", |
|||
"inputs": [{ |
|||
"name": "a", |
|||
"type": "uint256" |
|||
} |
|||
], |
|||
"outputs": [ |
|||
{ |
|||
"name": "d", |
|||
"type": "uint256" |
|||
} |
|||
], |
|||
}, { |
|||
"name": "test2", |
|||
"type": "event", |
|||
"inputs": [{ |
|||
"name": "a", |
|||
"type": "uint256" |
|||
} |
|||
], |
|||
"outputs": [ |
|||
{ |
|||
"name": "d", |
|||
"type": "uint256" |
|||
} |
|||
] |
|||
}]; |
|||
|
|||
// when
|
|||
var events = abi.filterEvents(description); |
|||
var functions = abi.filterFunctions(description); |
|||
|
|||
// then
|
|||
assert.equal(events.length, 1); |
|||
assert.equal(events[0].name, 'test2'); |
|||
assert.equal(functions.length, 1); |
|||
assert.equal(functions[0].name, 'test'); |
|||
|
|||
}); |
|||
}); |
@ -0,0 +1,201 @@ |
|||
var assert = require('assert'); |
|||
var contract = require('../lib/contract.js'); |
|||
|
|||
describe('contract', function() { |
|||
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" |
|||
} |
|||
] |
|||
}]; |
|||
|
|||
// when
|
|||
var con = contract(null, description); |
|||
|
|||
// then
|
|||
assert.equal('function', typeof con.test); |
|||
assert.equal('function', typeof con.test['uint256']); |
|||
}); |
|||
|
|||
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" |
|||
} |
|||
] |
|||
}]; |
|||
|
|||
// when
|
|||
var con = contract(null, description); |
|||
|
|||
// then
|
|||
assert.equal('function', typeof con.test); |
|||
assert.equal('function', typeof con.test['uint256']); |
|||
}); |
|||
|
|||
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" |
|||
} |
|||
] |
|||
}]; |
|||
|
|||
// when
|
|||
var con = contract(null, description); |
|||
|
|||
// then
|
|||
assert.equal('function', typeof con.test); |
|||
assert.equal('function', typeof con.test['uint256']); |
|||
assert.equal('function', typeof con.test2); |
|||
assert.equal('function', typeof con.test2['uint256']); |
|||
}); |
|||
|
|||
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" |
|||
} |
|||
] |
|||
}]; |
|||
|
|||
// when
|
|||
var con = contract(null, description); |
|||
|
|||
// then
|
|||
assert.equal('function', typeof con.test); |
|||
assert.equal('function', typeof con.test['uint256']); |
|||
assert.equal('function', typeof con.test['string']); |
|||
}); |
|||
|
|||
it('should create contract with no methods', function () { |
|||
|
|||
// given
|
|||
var description = [{ |
|||
"name": "test(uint256)", |
|||
"inputs": [{ |
|||
"name": "a", |
|||
"type": "uint256" |
|||
} |
|||
], |
|||
"outputs": [ |
|||
{ |
|||
"name": "d", |
|||
"type": "uint256" |
|||
} |
|||
] |
|||
}]; |
|||
|
|||
|
|||
// when
|
|||
var con = contract(null, description); |
|||
|
|||
// then
|
|||
assert.equal('undefined', typeof con.test); |
|||
|
|||
}); |
|||
|
|||
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" |
|||
} |
|||
] |
|||
}]; |
|||
|
|||
|
|||
// when
|
|||
var con = contract(null, description); |
|||
|
|||
// then
|
|||
assert.equal('function', typeof con.test); |
|||
assert.equal('function', typeof con.test['uint256']); |
|||
|
|||
}); |
|||
|
|||
}); |
|||
|
@ -0,0 +1,22 @@ |
|||
var assert = require('assert'); |
|||
var event = require('../lib/event.js'); |
|||
|
|||
describe('event', function () { |
|||
it('should create filter input object from given', function () { |
|||
|
|||
// given
|
|||
var address = '0x012345'; |
|||
var signature = '0x987654'; |
|||
|
|||
// when
|
|||
var impl = event(address, signature); |
|||
var result = impl(); |
|||
|
|||
// then
|
|||
assert.equal(result.address, address); |
|||
assert.equal(result.topics.length, 1); |
|||
assert.equal(result.topics[0], signature); |
|||
|
|||
}); |
|||
}); |
|||
|
Loading…
Reference in new issue