如何在測試機制中創建“hello world”——我在 Parity Wallet 中的第一個簡單的 Win 合約
- 奇偶校驗程序
- “契約”
- 《制定契約》
- 在“New Solidity Contract”視窗中寫道:
/* from here: https://ethereum-homestead.readthedocs.io/en/latest/contracts-and-transactions/contracts.html#writing-a-contract */ contract HelloWorld { event Print(string out); function() { Print("Hello, World!"); } }
- 編譯
- 部署
- 然後呢?
我被困在這裡(我現在的賬戶裡沒有汽油/任何錢)
注意:我不清楚 Parity 關於契約的原始文件:
Parity Ethereum 支持使用乙太坊區塊鏈的 Solidity 程式語言對智能合約進行面向狀態的程式。錢包包含一個集成的 Solidity 開發環境,允許您編寫、編譯和部署合約。
合約開發視圖包含合約程式碼面板(左)和配置參數(右)。
要開始您的第一份契約,可以載入或導入現有契約。
我們的第一個契約將是一個問候語“Hello, World!”。契約:
pragma solidity ^0.4.11; contract greeter { address owner; string message; function greeter(string _message) public { owner = msg.sender; message = _message; } function say() constant returns (string) { return message; } function die() { if (msg.sender == owner) { selfdestruct(owner); } } }
該合約包含兩個成員變數、一個建構子和兩個函式。建構子將在合約部署時被呼叫一次,將合約的所有者設置為交易的發送者,並儲存提供的消息。
常量函式
say()
只是在每次呼叫時返回提供的消息,並die()
允許合約的創建者自毀合約並清理狀態。讓我們在集成的 Solidity 開發環境中創建 Greeter 合約。請注意,這
pragma solidity ^0.4.11
需要 0.4.11 或更高版本的 Solidity 編譯器版本,可以在右側選擇。在編譯合約之前,讓我們保存 Greeter “Hello, world!” 契約供以後使用。
現在,Parity Solidity 環境允許編譯部署在區塊鏈上所需的字節碼,並生成合約互動所需的應用程序二進制介面 (ABI)。
一個方便的功能是程式碼優化和自動編譯,在您編碼時重新生成字節碼和 ABI。
完成這些步驟後,Parity Ethereum 為我們提供了以下 JSON 介面 (ABI)、字節碼、元數據和 swarm 散列。
應用程序二進制介面 (ABI):
[ { "constant":false, "inputs":[ ], "name":"die", "outputs":[ ], "payable":false, "type":"function" }, { "constant":true, "inputs":[ ], "name":"say", "outputs":[ { "name":"", "type":"string" } ], "payable":false, "type":"function" }, { "inputs":[ { "name":"_message", "type":"string" } ], "payable":false, "type":"constructor" } ]
編譯後的字節碼:
0x6060604052341561000c57fe5b604051610307380380610307833981016040528051015b60008054600160a060020a03191633600160a060020a03161790558051610051906001906020840190610059565b505b506100f9565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061009a57805160ff19168380011785556100c7565b828001600101855582156100c7579182015b828111156100c75782518255916020019190600101906100ac565b5b506100d49291506100d8565b5090565b6100f691905b808211156100d457600081556001016100de565b5090565b90565b6101ff806101086000396000f300606060405263ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166335f469948114610045578063954ab4b214610057575bfe5b341561004d57fe5b6100556100e7565b005b341561005f57fe5b610067610129565b6040805160208082528351818301528351919283929083019185019080838382156100ad575b8051825260208311156100ad57601f19909201916020918201910161008d565b505050905090810190601f1680156100d95780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000543373ffffffffffffffffffffffffffffffffffffffff908116911614156101265760005473ffffffffffffffffffffffffffffffffffffffff16ff5b5b565b6101316101c1565b60018054604080516020600284861615610100026000190190941693909304601f810184900484028201840190925281815292918301828280156101b65780601f1061018b576101008083540402835291602001916101b6565b820191906000526020600020905b81548152906001019060200180831161019957829003601f168201915b505050505090505b90565b604080516020810190915260008152905600a165627a7a72305820bcb6dac55886f2319f381c72002482234ee43206c088e0eea076260a472d83230029
契約元數據:
{ "compiler":{ "version":"0.4.11+commit.68ef5810" }, "language":"Solidity", "output":{ "abi":[ { "constant":false, "inputs":[ ], "name":"die", "outputs":[ ], "payable":false, "type":"function" }, { "constant":true, "inputs":[ ], "name":"say", "outputs":[ { "name":"", "type":"string" } ], "payable":false, "type":"function" }, { "inputs":[ { "name":"_message", "type":"string" } ], "payable":false, "type":"constructor" } ], "devdoc":{ "methods":{ } }, "userdoc":{ "methods":{ } } }, "settings":{ "compilationTarget":{ "GreeterHelloWorld":"greeter" }, "libraries":{ }, "optimizer":{ "enabled":true, "runs":200 }, "remappings":[ ] }, "sources":{ "GreeterHelloWorld":{ "keccak256":"0xe8f8f27c4b8e16a9d3b488f689b9e3018d25bdee6a67734444c9bbbf7a59c731", "urls":[ "bzzr://ed1e3e9a69578ad5ccc4bb33539f802ee94b243163491f6ee3f9456b0ae4d281" ] } }, "version":1 }
群雜湊:
bcb6dac55886f2319f381c72002482234ee43206c088e0eea076260a472d8323
這些資訊對於將您的智能合約部署到乙太坊區塊鏈並集成到您的去中心化應用程序中非常重要。保留它們以供進一步參考。
順便說一句,如果您更喜歡使用命令行,並且安裝了 Solidity 編譯器,則可以通過發出以下命令來實現相同的目的,假設您將合約程式碼儲存到名為 的文件中
greeter.sol
:solc --optimize --abi --bin --metadata greeter.sol ======= greeter.sol:greeter ======= Binary: 6060604052341561000c57fe5b604051610307380380610307833981016040528051015b60008054600160a060020a03191633600160a060020a03161790558051610051906001906020840190610059565b505b506100f9565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061009a57805160ff19168380011785556100c7565b828001600101855582156100c7579182015b828111156100c75782518255916020019190600101906100ac565b5b506100d49291506100d8565b5090565b6100f691905b808211156100d457600081556001016100de565b5090565b90565b6101ff806101086000396000f300606060405263ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166335f469948114610045578063954ab4b214610057575bfe5b341561004d57fe5b6100556100e7565b005b341561005f57fe5b610067610129565b6040805160208082528351818301528351919283929083019185019080838382156100ad575b8051825260208311156100ad57601f19909201916020918201910161008d565b505050905090810190601f1680156100d95780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000543373ffffffffffffffffffffffffffffffffffffffff908116911614156101265760005473ffffffffffffffffffffffffffffffffffffffff16ff5b5b565b6101316101c1565b60018054604080516020600284861615610100026000190190941693909304601f810184900484028201840190925281815292918301828280156101b65780601f1061018b576101008083540402835291602001916101b6565b820191906000526020600020905b81548152906001019060200180831161019957829003601f168201915b505050505090505b90565b604080516020810190915260008152905600a165627a7a7230582033ca9aeef924a814a80886e9f27b539e8c3b71780e6da527ebcd4df6076c45000029 Metadata: {"compiler":{"version":"0.4.11+commit.68ef5810"},"language":"Solidity","output":{"abi":[{"constant":false,"inputs":[],"name":"die","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"say","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"inputs":[{"name":"_message","type":"string"}],"payable":false,"type":"constructor"}],"devdoc":{"methods":{}},"userdoc":{"methods":{}}},"settings":{"compilationTarget":{"greeter.sol":"greeter"},"libraries":{},"optimizer":{"enabled":true,"runs":200},"remappings":[]},"sources":{"greeter.sol":{"keccak256":"0x2914014e6c5ef4ffd0de42461548cfb78b006af718a3ddf18c9fb851ba457854","urls":["bzzr://622e562f0f51d94c6bd395063133657d178c6c5c7bfd43db6c10470f59eb34ac"]}},"version":1} Contract JSON ABI [{"constant":false,"inputs":[],"name":"die","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"say","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"inputs":[{"name":"_message","type":"string"}],"payable":false,"type":"constructor"}]
請注意,
urls
當使用solc
.現在,我們終於要部署我們的合約了,點擊Deploy按鈕。
可以從 Parity 中直接部署歡迎契約。
選擇一個問候你的字元串。
一旦部署和開採,…
…合約在您的 Parity Ethereum 錢包中可用,並允許基本互動。
使用合約介面查詢
say
函式並立即查看結果。要通過 JSON-RPC API 部署合約,我們首先必須
eth_estimateGas
通過傳入編譯後的字節碼來估計 RPC 呼叫的 gas 需求。curl --data '{"jsonrpc":"2.0","method": "eth_estimateGas", "params": [{"from": "0x00a329c0648769a73afac7f9381e08fb43dbea72", "data": "0x6060604052341561000c57fe5b6040516103aa3803806103aa833981016040528080518201919050505b33600060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508060019080519060200190610080929190610088565b505b5061012d565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100c957805160ff19168380011785556100f7565b828001600101855582156100f7579182015b828111156100f65782518255916020019190600101906100db565b5b5090506101049190610108565b5090565b61012a91905b8082111561012657600081600090555060010161010e565b5090565b90565b61026e8061013c6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806335f4699414610046578063954ab4b214610058575bfe5b341561004e57fe5b6100566100f1565b005b341561006057fe5b610068610185565b60405180806020018281038252838181518152602001915080519060200190808383600083146100b7575b8051825260208311156100b757602082019150602081019050602083039250610093565b505050905090810190601f1680156100e35780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16141561018257600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16ff5b5b565b61018d61022e565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102235780601f106101f857610100808354040283529160200191610223565b820191906000526020600020905b81548152906001019060200180831161020657829003601f168201915b505050505090505b90565b6020604051908101604052806000815250905600a165627a7a72305820d7c4be18c8e86dd70b00aa0b072cde72893fcaafc766ff7bb86debf4d9357cac00290000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001048656c6c6f2c20457468657265756d2100000000000000000000000000000000"}], "id": 1}' -H "Content-Type: application/json" -X POST localhost:8545 {"jsonrpc":"2.0","result":"0x4414b","id":1}
現在我們知道,合約部署將花費 278859 gas (hex
0x4414b
),我們可以廣播交易:curl --data '{"method":"eth_sendTransaction","params":[{"from":"0x00a329c0648769a73afac7f9381e08fb43dbea72","gas":"0x4414b","data":"0x6060604052341561000c57fe5b6040516103aa3803806103aa833981016040528080518201919050505b33600060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508060019080519060200190610080929190610088565b505b5061012d565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100c957805160ff19168380011785556100f7565b828001600101855582156100f7579182015b828111156100f65782518255916020019190600101906100db565b5b5090506101049190610108565b5090565b61012a91905b8082111561012657600081600090555060010161010e565b5090565b90565b61026e8061013c6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806335f4699414610046578063954ab4b214610058575bfe5b341561004e57fe5b6100566100f1565b005b341561006057fe5b610068610185565b60405180806020018281038252838181518152602001915080519060200190808383600083146100b7575b8051825260208311156100b757602082019150602081019050602083039250610093565b505050905090810190601f1680156100e35780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16141561018257600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16ff5b5b565b61018d61022e565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102235780601f106101f857610100808354040283529160200191610223565b820191906000526020600020905b81548152906001019060200180831161020657829003601f168201915b505050505090505b90565b6020604051908101604052806000815250905600a165627a7a72305820d7c4be18c8e86dd70b00aa0b072cde72893fcaafc766ff7bb86debf4d9357cac00290000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001048656c6c6f2c20457468657265756d2100000000000000000000000000000000"}],"id":1,"jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST localhost:8545 {"jsonrpc":"2.0","result":"0x9f79dbaeab3f0e24bbe2c885432dae3ff4350e357a497e7bf8820a64c219e011","id":1}
請注意,如果賬戶尚未解鎖,您必須切換到錢包使用者界面並與受信任的簽名者確認交易。
現在您了解了編寫、編譯和部署合約到乙太坊鏈的要點,並知道如何呼叫
say()
我們部署的 Greeter 合約的函式。與乙太坊區塊鏈上的智能合約互動有兩種不同的方式。您可以呼叫(查詢)或呼叫(執行)合約。呼叫合約是一個只讀過程,通常是免費的。呼叫合約需要廣播交易以執行所需合約的功能,並且必須支付費用。所以,讓我們執行我們的 Greeter 合約。只有一個功能可以執行:
die()
.在開發之後進行清理通常是一個好習慣。現在我們知道我們開發的 Greeter 合約有效,我們將通過呼叫自毀合約的die函式將其從區塊鏈中刪除。
一旦被開採,自毀功能將從目前的區塊鏈狀態中釋放空間。
由於無法與已終止的合約進行互動,因此讓我們通過點擊“忘記”按鈕將其刪除。
要通過 RPC 執行
die()
函式,我們首先要查找函式簽名。這可以通過以下方式完成solc --hash
:$ solc --hash greeter.sol ======= greeter.sol:greeter ======= Function signatures: 35f46994: die() 954ab4b2: say()
現在,我們所要做的就是
0x1A3E28181B5043aC359B1146a8CCcce2a950212b
使用數據載入為我們的 Greeter 合約創建一個事務0x35f46994
。自毀呼叫通常不會花費太多費用,因此我們只假設少量0xc350
。curl --data '{"method":"eth_sendTransaction","params":[{"from":"0x00a329c0648769a73afac7f9381e08fb43dbea72",to:"0x1A3E28181B5043aC359B1146a8CCcce2a950212b","gas":"0xc350","data":"0x35f46994"}],"id":1,"jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST localhost:8545 {"jsonrpc":"2.0","result":"0x8423e6ed270f92621c14302f0d3db647eaf9677ef8f2ba0dd3374b79d2567c78","id":1}
我希望這能直接回答你的問題。我更喜歡 Greeter 契約,因為它可能會死掉。但是你會明白這個想法,並且可以調整它以使你的HelloWorld契約生效。