Skip to content

Instantly share code, notes, and snippets.

@frozeman
Last active February 17, 2016 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frozeman/8f0863a47568f6b1c5b6 to your computer and use it in GitHub Desktop.
Save frozeman/8f0863a47568f6b1c5b6 to your computer and use it in GitHub Desktop.
New contract object
// 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'
@tcoulter
Copy link

tcoulter commented Feb 6, 2016

I like the first option better: deploy without the new keyword.

@tcoulter
Copy link

tcoulter commented Feb 6, 2016

Love the events vs. the (current) double callback.

@tcoulter
Copy link

tcoulter commented Feb 7, 2016

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) {
  // ...
});

@frozeman
Copy link
Author

frozeman commented Feb 7, 2016

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..

@frozeman
Copy link
Author

frozeman commented Feb 7, 2016

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);

@tcoulter
Copy link

Ah, not sure what I was thinking. new looks good. 👍

@frozeman
Copy link
Author

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