Javascript API – How it works – Contracts



Javascript API – How it works – Contracts

0 0


ethereum.js-presentation


On Github debris / ethereum.js-presentation

Javascript API

  • How it works
  • Contracts
  • Tools
Oh hey, these are some notes. They'll be hidden in your presentation, but you can see them if you open the speaker notes window (hit 's' on your keyboard).

How it works

  • ethereum.js
  • providers:
    • qt
    • http
    • websockets
  • cpp / go implementation
  • jsonrpc server
  • connectors:
    • qt
    • http
    • websockets
  • ethereum.js
  • providers:
    • qt
    • http
    • websockets
  • cpp / go implementation
  • jsonrpc server
  • connectors:
    • qt
    • http
    • websockets

Get coinbase

  • javascript code

    web3.eth.coinbase.then(function (result) {
    	var coinbase = result;
    });
  • json object

    {
    	"jsonrpc" : "2.0",
    	"method" : "eth_coinbase",
    	"params" : null,
    	"id" : 1
    }	
    								
  • curl request

    curl -X POST --data '{"jsonrpc":"2.0","method":"eth_number","params":null,"id":1}' http://localhost:8080

Why promises?

Callback hell

Callback hell

asyncCall(function (result) {
    // do something 

    anotherAsyncCall(function (result2) {
        // do something 2

        oneMoreAsyncCall(function (err, result3) {
            // the third and final async response

            if (err) {
                //handle any error resulting from any of the above calls	

                return; // finally!
            }
        })
    })
})

Promises

asyncCall()
.then(function(data1){
    // do something...
    return anotherAsyncCall();
})
.then(function(data2){
    // do something...  
    return oneMoreAsyncCall();    
})
.then(function(data3){
   // the third and final async response
})
.fail(function(err) {
   // handle any error resulting from any of the above calls    
})
.done();

Example

web3.eth.coinbase.then(function (result) {
	var coinbase = result;
	console.log(coinbase); // "0x407d73d8a49eeb85d32cf465507dd71d507100c1"
	return web3.eth.balanceAt(coinbase);
}).then(function (result) {
	var balance = result;
	console.log(balance); // "0x32884442997a37a000"
});

Improved Example

web3.eth.balanceAt(web3.eth.coinbase).then(function (result) {
	var balance = result;
	console.log(balance); // "0x32884442997a37a000"
});

Autoresolving promises

web3.eth.somemethod(x).then(function (result) {
	// some code
});
var x = {
	"a" : "text",
	"b" : Promise,
	"c" : 7,
	"d" : Promise
}
var x = {
	"a" : "text",
	"b" : "Hello World!",
	"c" : 7,
	"d" : ["Hello", "World", Promise]
}
var x = {
	"a" : "text",
	"b" : "Hello World!",
	"c" : 7,
	"d" : ["Hello", "World", 14]
}

Contracts

Contract creation

var source = "" + 
"contract test {" +
"   function multiply(uint a) returns(uint d) {" +
"       return a * 7;" +
"   }" +
"}";

// create contract
web3.eth.transact({code: web3.eth.solidity(source)}).then(function (address) {
	// ...
});

Contract object

var desc =  [{
    "name": "multiply",
    "inputs": [{
        "name": "a",
        "type": "uint256"
    }],
    "outputs": [{
        "name": "d",
        "type": "uint256"
    }]
}];

// create description
var account = "0x407d73d8a49eeb85d32cf465507dd71d507100c1"; // 
var contract = web3.contract(account, desc); // create contract object here!

contract.multiply(1).call().then(function(res) {
	var result = res[0];
	console.log(result); // 7
});

Tools

  • npm
  • bower
  • gulp
  • browserify

Contribute!

ethereum/ethereum.js

  • Jeffrey Wilcke
  • Marian Oancea
  • Marek Kotewicz
  • Corey Farwell