web3.js
https://gyazo.com/eb80fc5be891a9c4152791d1a4fdc918
Ethereum JavaScript API
ethereum/web3.js: Ethereum JavaScript API
JavaScriptライブラリ
This is the Ethereum JavaScript API which connects to the Generic JSON-RPC spec.
Ethereumのノード Nodeと通信するJavaScriptAPIライブラリ
デプロイされたスマートコントラクト Smart Contractへのアクセスが可能に
Web3オブジェクト
コンストラクタ関数Web3
web3オブジェクトの生成
web3コンストラクタに引数にWeb3プロバイダを持たせて初期化
→Ethereumノード Nodeとの通信が可能に
code:web3.js
//コンストラクタでweb3オブジェクトの生成
web3js = new Web3(web3.currentProvider);
コントラクトオブジェクト
スマートコントラクト Smart Contractにアクセスするために、web3オブジェクトとContract ABI、Contract Addressで、コントラクトオブジェクトを生成する必要あり
web3オブジェクト
npmなどでインストールし
require(web3)などでモジュールを用いる
Contract ABI
対応するcontract コントラクトのContract ABIを置くファイルを作り、rewuireなどでimport
Contract Address
contract コントラクトをdeployした際に得られる、Contract Addressを変数に渡す
code:cont-ob.js
var cryptoZombies;
function startApp(){
var cryptoZombieAddress = "ADDRESS";
// ContractABIとContractAddressを引数に渡してコントラクトオブジェクト生成
cryptoZombies = new web3js.eth.Contract(cryptoZombiesABI,cryptoZombiesAddress);
}
コントラクト関数の呼び出し
call() 戻り値:Promise プロミス
viewとpure修飾子が付与されている関数を呼び出す(Blockchain ブロックチェーンにトランザクション Transactionを発行しない関数)
send() 引数:{アカウント}
viewとpure修飾子が付与されていない関数を呼び出す(トランザクション Transactionを発行する関数)
code:con-func.js
myContract.methods.myMethod(123).call();
myCOntract.methods.myMethod(123).send({from: user});