Last active
February 17, 2016 17:13
-
-
Save frozeman/8f0863a47568f6b1c5b6 to your computer and use it in GitHub Desktop.
New contract object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// un"addresses" contract object | |
var myContrac2 = new web3.eth.contract(abi) | |
myContrac2.address = '0x12345678...'; // add address later | |
// initiate with address | |
var myContrac = new web3.eth.contract(abi, address) | |
// deploy contract | |
eventemitter = new web3.eth.contract(abi).deploy(param1, {data: '0x23456'}); | |
eventEmitter.on('mined', function(err, address){ | |
new web3.eth.contract(abi, address); | |
}); | |
eventEmitter.on('transactionHash', function(err, hash){ | |
}); | |
new web3.eth.contract(abi).deploy.getData(param1, {data: '0x23456'}) | |
> 0x23456780000005345345 | |
// contract object events and methods | |
myContrac.call({from: '0x1234...'}).myMethod(param1 , cb) // calls cb with result | |
myContrac.transact({from: '0x1234...'}).myMethod(param1, cb) // calls cb with tx hash, in the future we could return a event transmitter | |
myContrac.getData.myMethod(param1) // get only the data of the call '0x23456787654323456765432340000000000000000000000034' |
Was thinking - if we're going to make the contract object an EventEmitter, what's stopping us from triggering actual solidity events? i.e.,
Take this Solidity code:
contract MyCoin {
..
event CoinsReceived(address sender, uint amount);
function sendCoin(address receiver, uint amount) returns(bool sufficient) {
...
CoinsReceived(msg.sender,amount);
return true;
}
}
We could do this in JS:
var mycoin = new web3.eth.contract(abi, address);
// Add event handler
mycoin.on("CoinsReceived", function(sender, amount) {
// Do something with the event
});
// Trigger event via function call
mycoin.sendCoin("0x1234...", 1000, function(tx) {
// ...
});
intersting idea, but the main problem is namespace problems, which i try to avoid. What if you name your contract event "mined"? then it would clash..
The new keyword is necessary when creating a new instance, to make it clear that it is a new instance, imo:
var contract = new web3.eth.contract(abi, address);
Ah, not sure what I was thinking. new
looks good. 👍
Moved it to here: ethereum/EIPs#68
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Love the events vs. the (current) double callback.