Ethereum(イーサー坊)インテリジェント契約最も簡単なHelloWorld
2156 ワード
引用:ネット上で多くの以太坊の知能契約の翻訳文章を見て、理論の解釈はまあまあで、実操の方面は少しくどくて、繰り返しの練習に適していません;最近自分で創業した会社が止まって、少し時間があって、少し興味があって、コードはメモを取って忘れます.
以太坊の基礎理論を理解し、js開発 を少し理解する.本人の実験環境は以下の である.
os
node.js
solc
testrpc
web3
linux mint 18
v7.2
v0.4.6
v3.0.2
0.17.0-alpha
端末term 1を開き、testrpcを起動します.別の端末term 2を開き、nodeスクリプトを実行します.
ネット上の数千字の文章コードの核心部分は以上ですが、他のノードがどのように契約にアクセスするかは紹介されていません.以下のようにします.
新しい端末term 3を開く
問題処理:
1.cb not defined---例外に基づいて、指定ディレクトリにnpm moudleのlinkを作成すればよい.out of gas---コードなどのgas数を指定します.
終了およびその他:
以太坊契約の開発は現在敷居が低く、フロントエンドのプログラマーはすべて適任だが、水も深く、複雑な欠点に遭遇し、古い運転手であることを期待している.
一、前提
os
node.js
solc
testrpc
web3
linux mint 18
v7.2
v0.4.6
v3.0.2
0.17.0-alpha
二、契約開発の配置--HelloWorld.sol
pragma solidity ^0.4.2;
contract HelloWorld {
function greet(string a) returns(string b){
return a;
}
}
端末term 1を開き、testrpcを起動します.別の端末term 2を開き、nodeスクリプトを実行します.
Web3 = require('web3')
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
fs = require('fs')
fileName = "HelloWorld.sol"
compiled_contract1 = web3.eth.compile.solidity(fs.readFileSync(fileName,'utf-8'))
console.log('1, ')
// js
constract_object = web3.eth.contract(compiled_contract1.info.abiDefinition);
console.log('2, ')
//
deployed_contract = constract_object.new({from: web3.eth.accounts[0], data:"0x"+compiled_contract1.code,gas:120000})
console.log('3, ')
//
setTimeout(function() {//repl setTimeout
deployed_contract.greet.call('liunix')
console.log(deployed_contract.address) // ,
}, 1000);
三、契約訪問
ネット上の数千字の文章コードの核心部分は以上ですが、他のノードがどのように契約にアクセスするかは紹介されていません.以下のようにします.
新しい端末term 3を開く
// abi
solc --abi HelloWorld.sol | sed -n '4p' >> HelloWorld.abi
node // nodejs
Web3 = require('web3')
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
fs = require('fs')
helloworld_abi = eval(fs.readFileSync('HelloWorld.abi'))
contract_object = web3.eth.contract(helloworld_abi);
// ,
var contract_from_address = constract_object.at(deployed_contract.address);//
contract_from_address.greet.call('hello world')
問題処理:
1.cb not defined---例外に基づいて、指定ディレクトリにnpm moudleのlinkを作成すればよい.out of gas---コードなどのgas数を指定します.
終了およびその他:
以太坊契約の開発は現在敷居が低く、フロントエンドのプログラマーはすべて適任だが、水も深く、複雑な欠点に遭遇し、古い運転手であることを期待している.