[Ethereum] web3.jsによりgethを操作

– web3.jsとは、Ethereumネットワーク上でデータを扱えるようにするライブラリ
– HTTPまたはIPC接続を利用して、ローカルまたはリモートのEthereumブロックチェーンのnodeとやり取りできる

$ npm install web3@0.20.0

### geth起動
$ geth –networkid “15” –nodiscover –port 30304 –datadir ./ console 2>> ./geth_err.log
> admin.startRPC(‘0.0.0.0’, 8545, ”, ‘eth,net,web3,personal’)
true

### web3によるアカウント作成

var Web3 = require('web3');
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://192.168.33.10:8545'));
var _account = web3.personal.newAccount("test");
console.log(_account)

$ node create_account.js
0x41a4a74bfd4659b742d45d4cafe2bbbd0c6b5d65

### 残高確認

var Web3 = require('web3');
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://192.168.33.10:8545'));
var _balance = web3.eth.getBalance("0x6752a913de4f4530ed3f071371db8d70acc42fce");
console.log(_balance)

$ node getBalance.js
BigNumber { s: 1, e: 22, c: [ 322200000, 21000000000000 ] }

### 送金

var Web3 = require('web3');
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://192.168.33.10:8545'));
var _sendTransaction = web3.eth.sendTransaction({from: "0x6752a913de4f4530ed3f071371db8d70acc42fce", to:"0x87b2d715dafdfa88da7d76b25069162153967e23", value:200});
console.log(_sendTransaction)

実行する前に、アカウントをアンロックする
> personal.unlockAccount(eth.accounts[0],”pass1″)
true

$ node sendTransaction.js
0x61a0c52aa933c7d5448928d322b7c545726037c65a982d83f8c1669c1fd837ac

### balanceの確認

var Web3 = require('web3');
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://192.168.33.10:8545'));
web3.eth.defaultAccount=web3.eth.accounts[0]

var abi = [{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"type":"function"},{"inputs":[{"name":"initialSupply","type":"uint256"}],"payable":false,"type":"constructor"}];

var contract_address = "0x5e722e71c6bd4127eec0b4fa8c2e415755985826"
var balance_address = "0x87b2d715dafdfa88da7d76b25069162153967e23"

var _balanceOf = web3.eth.contract(abi).at(contract_address).balanceOf(balance_address);
console.log(_balanceOf);

$ node balanceOf.js
BigNumber { s: 1, e: 5, c: [ 200200 ] }

contractのaddressがあるのね。

### transfer

var Web3 = require('web3');
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://192.168.33.10:8545'));
web3.eth.defaultAccount=web3.eth.accounts[0]

var abi = [{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"type":"function"},{"inputs":[{"name":"initialSupply","type":"uint256"}],"payable":false,"type":"constructor"}];

var contract_address = "0x5e722e71c6bd4127eec0b4fa8c2e415755985826"
var transfer_address = "0x87b2d715dafdfa88da7d76b25069162153967e23"
var value = 1000

var _transfer = web3.eth.contract(abi).at(contract_address).transfer.sendTransaction(transfer_address, value);
console.log(_transfer);

$ node transfer.js
0xda2cec8a827e4890920a0d28728e7cc162b807c2bf34b7017ad9e43fab19a6c9

$ node balanceOf.js
BigNumber { s: 1, e: 5, c: [ 201200 ] }

このweb3.jsはかなり便利