In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what is the method of deployment and interaction of intelligent contract in Ethernet Square". In daily operation, I believe that many people have doubts about the method of deployment and interaction of intelligent contract in Ethernet Square. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "what is the method of deployment and interaction of intelligent contract in Ethernet Square". Next, please follow the editor to study!
Start the container to execute the geth command
Root@ubu-blockchain2:~# docker run-I blockchain101/ethereum-geth:1.6.5 geth attach http://45.32.252.88:8201Welcome to the Geth JavaScript consoled instance: Geth/01/v1.6.5-stable/linux-amd64/go1.8coinbase: 0x4c57e7e9c2f728046ddc6e96052056a241bdbd0aat block: 6064 (Wed, 02 Aug 2017 01:13:50 UTC) datadir: / ethcluster/779977/data/01 modules: admin:1.0 eth:1.0 net:1.0 rpc:1.0 web3:1.0
Check our account and balance
Eth.getBalance (eth.accounts [0]) 11000000000000000000eth.getBalance (eth.accounts [1]) 0eth.accounts [0] "0x4c57e7e9c2f728046ddc6e96052056a241bdbd0a" > eth.accounts [1] "0xe82e2f0a5abd8774767b9751659976f9c4f59181"
Initiate a deal
Eth.sendTransaction ({from:eth.accounts [0], to:eth.accounts [1], value:web3.toWei (3 recordings)}) "0x0075da712d26aea17d6647035107f509e13eaf3d113c1577db14d4cc4216caec"
View transaction details
> eth.getTransaction ("0x0075da712d26aea17d6647035107f509e13eaf3d113c1577db14d4cc4216caec") {blockHash: "0x3115703894dc6015c96ef4de3e5615f416498ca1f985902b38cd70e27dab8871", blockNumber: 1250, from: "0x4c57e7e9c2f728046ddc6e96052056a241bdbd0a", gas: 90000, gasPrice: 18000000000, hash: "0x0075da712d26aea17d6647035107f509e13eaf3d113c1577db14d4cc4216caec", input: "0x", nonce: 0, r: "0x2aef2c1fa03a0fa4172d21e3383d8c0431d45ec855b9d16efdd5eb2de90c414c", s: "0xc4d530fb7902bf509fe56bfbea4861bf6cc16791afc9c9103c1a18f77407d1f", to: "0xe82e2f0a5abd8774767b9751659976f9c4f59181", transactionIndex: 0, v: "0x17cdb6", value: 3000000000000000} > eth.getBalance (eth.accounts [1]) 30000000000000000
Verify the balance of user 0
> eth.getBalance (eth.accounts [0]) 7999622000000000000
Write a simple contract
Contract Sample {uint public value; function Sample (uint v) {value=v;} function set (uint v) {value=v;} function get () constant returns (uint) {return value;}}
Compile the ABI interface and contract binaries in the remix web page,
Abi= [{"constant": true, "inputs": [], "name": "value", "outputs": [{"name": "," type ":" uint256 "}]," payable ": false," type ":" function "}, {" constant ": false," inputs ": [{" name ":" v "," type ":" uint256 "}]," name ":" set "," outputs ": []," payable ": false," type ":" function "}, {" constant ": true "inputs": [], "name": "get", "outputs": [{"name": "," type ":" uint256 "}]," payable ": false," type ":" function "}, {" inputs ": [{" name ":" v "," type ":" uint256 "}]," payable ": false," type ":" constructor "}] [{constant: true, inputs: [], name:" value " Outputs: [{name: ", type:" uint256 "}], payable: false, type:" function "}, {constant: false, inputs: [{name:" v ", type:" uint256 "}], name:" set ", outputs: [], payable: false, type:" function "}, {constant: true Inputs: [], name: "get", outputs: [{name: ", type:" uint256 "}], payable: false, type:" function "}, {inputs: [{name:" v ", type:" uint256 "}], payable: false, type:" constructor "}]
Need to use eth.contract to define a contract class
> sample=eth.contract (abi) {abi: [{constant: true, inputs: [], name: "value", outputs: [{...}], payable: false, type: "function"}, {constant: false, inputs: [{...}], name: "set", outputs: [], payable: false Type: "function"}, {constant: true, inputs: [], name: "get", outputs: [{...}], payable: false, type: "function"}, {inputs: [{...}], payable: false, type: "constructor"}], eth: {accounts: ["0x4c57e7e9c2f728046ddc6e96052056a241bdbd0a", "0xe82e2f0a5abd8774767b9751659976f9c4f59181"] BlockNumber: 6225, coinbase: "0x4c57e7e9c2f728046ddc6e96052056a241bdbd0a", compile: {lll: function (), serpent: function (), solidity: function ()}, defaultAccount: undefined, defaultBlock: "latest", gasPrice: 1800000000, hashrate: 0, mining: false, pendingTransactions: [], protocolVersion: "0x3f", syncing: false, call: function (), contract: function (abi), estimateGas: function () Filter: function (fil, callback), getAccounts: function (callback), getBalance: function (), getBlock: function (), getBlockNumber: function (callback), getBlockTransactionCount: function (), getBlockUncleCount: function (), getCode: function (), getCoinbase: function (callback), getCompilers: function (), getGasPrice: function (callback), getHashrate: function (callback), getMining: function (callback), getPendingTransactions: function (function), callback: callback (callback) GetRawTransaction: function (), getRawTransactionFromBlock: function (), getStorageAt: function (), getSyncing: function (callback), getTransaction: function (), getTransactionCount: function (), getTransactionFromBlock: function (), getTransactionReceipt: function (), getUncle: function (), getWork: function (), iban: function (iban), icapNamereg: function (), isSyncing: function (callback), namereg: function (), resend: function (), sendIBANTransaction: function () SendRawTransaction: function (), sendTransaction: function (), sign: function (), signTransaction: function (), submitTransaction: function (), submitWork: function ()}, at: function (address, callback), getData: function (), new: function ()}
The binary code of the contract is assigned to SampleHEX for ease of use
SampleHEX= "0x6060604052341561000c57fe5b60405160208061013a833981016040528080519060200190919050505b806000819055505b505b60f9806100416000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633fa4f24514604e57806360fe47b11460715780636d4ce63c14608e575bfe5b3415605557fe5b605b60b1565b6040518082815260200191505060405180910390f35b3415607857fe5b608c600480803590602001909190505060b7565b005b3415609557fe5b609b60c2565b6040518082815260200191505060405180910390f35b60005481565b806000819055505b50565b600060005490505b905600a165627a7a72305820208c8101070c8ba5a9b32db2bf4b8062a9ba50bc2869c39ac2297938756540e80029"
Deploy the contract code into the chain
> thesample=sample.new (1, {from:eth.accounts [0], data:SampleHEX,gas:3000000}) {abi: [{constant: true, inputs: [], name: "value", outputs: [{...}], payable: false, type: "function"}, {constant: false, inputs: [{...}], name: "set" Outputs: [], payable: false, type: "function"}, {constant: true, inputs: [], name: "get", outputs: [{...}], payable: false, type: "function"}, {inputs: [{...}], payable: false, type: "constructor"}], address: undefined TransactionHash: "0xee74bcb4461c9712ec9aca96a5a3a4c3c64be1213854d519fc8e5432b554f7a1"}
View transaction details
> samplerecpt=eth.getTransactionReceipt ("0xee74bcb4461c9712ec9aca96a5a3a4c3c64be1213854d519fc8e5432b554f7a1") {blockHash: "0xddba16545af882835fb9a69a0e5f3b9287c61664837d5ea0068b38575cb665c5", blockNumber: 6246, contractAddress: "0x7504fa9d64ab290844b82660d43b310f8fba0276", cumulativeGasUsed: 141836, from: "0x4c57e7e9c2f728046ddc6e96052056a241bdbd0a", gasUsed: 141836, logs: [], logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", root: "0xd1093ecaca9cc0d10e82a533a15feccedf7ff5c79fb3ebd9366ec0b35dbef478", to: null, transactionHash: "0xee74bcb4461c9712ec9aca96a5a3a4c3c64be1213854d519fc8e5432b554f7a1", transactionIndex: 0}
Contract naming
> samplecontract=sample.at ("0x7504fa9d64ab290844b82660d43b310f8fba0276") {abi: [{constant: true, inputs: [], name: "value", outputs: [{...}], payable: false, type: "function"}, {constant: false, inputs: [{...}], name: "set", outputs: [], payable: false Type: "function"}, {constant: true, inputs: [], name: "get", outputs: [{...}], payable: false, type: "function"}, {inputs: [{...}], payable: false, type: "constructor"}], address: "0x7504fa9d64ab290844b82660d43b310f8fba0276", transactionHash: null, allEvents: function () Get: function (), set: function (), value: function ()}
The contract view function get (), then calls the set () function, which has changed when get () looks at it.
Samplecontract.get.call () 1 > samplecontract.set.sendTransaction (9, {from:eth.accounts [0], gas:3000000}) "0x822ee6fb4caceb7e844c533f7f3bc57806f7cb3676fb3066eb848cca46b2f38a" > samplecontract.get.call () 9
We open another terminal, open the console of cluster1's peer02, directly at to the intelligent contract address deployed by the last terminal, and perform set operation.
Root@ubu-blockchain2:~/ethereum-docker/ethereum-docker/ethereum-testnet-docker/dockercomposefiles# docker run-I blockchain101/ethereum-geth:1.6.5 geth attach http://45.32.252.88:9201Welcome to the Geth JavaScript console! > abi= [{"constant": true, "inputs": [], "name": "value", "outputs": [{"name": "type": "uint256"}], "payable": false, "type": "function"}, {"constant": false "inputs": [{"name": "v", "type": "uint256"}], "name": "set", "outputs": [], "payable": false, "type": "function"}, {"constant": true, "inputs": [], "name": "get", "outputs": [{"name": "," type ":" uint256 "}]," payable ": false," type ":" function "}, {" inputs ": [{" name ":" v " "type": "uint256"}], "payable": false, "type": "constructor"}] > sample=eth.contract (abi) SampleHEX= "0x6060604052341561000c57fe5b60405160208061013a833981016040528080519060200190919050505b806000819055505b505b60f9806100416000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633fa4f24514604e57806360fe47b11460715780636d4ce63c14608e575bfe5b3415605557fe5b605b60b1565b6040518082815260200191505060405180910390f35b3415607857fe5b608c600480803590602001909190505060b7565b005b3415609557fe5b609b60c2565b6040518082815260200191505060405180910390f35b60005481565b806000819055505b50565b600060005490505b905600a165627a7a72305820208c8101070c8ba5a9b32db2bf4b8062a9ba50bc2869c39ac2297938756540e80029"
Directly assign the contract address and perform the set operation
Samplecontract=sample.at ("0x7504fa9d64ab290844b82660d43b310f8fba0276") > samplecontract.get.call () 9
Here comes the easy way. The deployment of a smart contract requires compilation, which is compiled online:
Https://ethereum.github.io/browser-solidity/#version=soljson-v0.4.14+commit.c2215d46.js
Modify the compiled abi and object name:
Here, you can only find a token contract on the Internet, which can be recharged, transferred and queried. The issue function can recharge the ether to the contract account, and the transfer function can send the token,getBalance function to other accounts to obtain the token balance of a certain account. The code is as follows: pragma solidity ^ 0.4.2 th contract {address issuer; mapping (address = > uint) balances; event Issue (address account, uint amount) Event Transfer (address from, address to,uint amount); function Token () {issuer = msg.sender;} function issue (address account, uintamount) {if (msg.sender! = issuer) throw; balances [account] + = amount } function transfer (address to, uint amount) {if (balances [msg.sender])
< amount)throw; balances[msg.sender] -= amount; balances[to] += amount; Transfer(msg.sender, to, amount); } function getBalance(address account)constant returns (uint) { return balances[account]; } }修改编译好的gas和对象名称:varbrowser_untitled_sol_tokenContract =web3.eth.contract([{"constant":false,"inputs":[{"name":"account","type":"address"},{"name":"amount","type":"uint256"}],"name":"issue","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"amount","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"account","type":"address"}],"name":"getBalance","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"account","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"Issue","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"from","type":"address"},{"indexed":false,"name":"to","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"Transfer","type":"event"}]);var token =browser_untitled_sol_tokenContract.new( { from: web3.eth.accounts[0], data:'0x6060604052341561000f57600080fd5b5b336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505b5b6103d2806100616000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063867904b414610054578063a9059cbb14610096578063f8b2cb4f146100d8575b600080fd5b341561005f57600080fd5b610094600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610125565b005b34156100a157600080fd5b6100d6600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919080359060200190919050506101d2565b005b34156100e357600080fd5b61010f600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190505061035c565b6040518082815260200191505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614151561018057600080fd5b80600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055505b5050565b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101561021e57600080fd5b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555080600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055507fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef338383604051808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001828152602001935050505060405180910390a15b5050565b6000600160008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205490505b9190505600a165627a7a723058204afe007a03446d43d13ac892e6dba9d032f540a11ff427d26c22560727cbea2f0029', gas: '4300000' }, function (e, contract){ console.log(e, contract);if (typeof contract.address !=='undefined') { console.log('Contract mined! address:' + contract.address + ' transactionHash: ' + contract.transactionHash); } }) 此时输入合约部署的实例a_demotypes, 可以看到a_demotypes的详情。 控制台调用 >A_demotypes {abi: [{constant: false, inputs: [{...}], name: "f", outputs: [{...}], payable: false, type: "function"}], address: "0x54ed7a5f5a63ddada3bfe83b3e632adabaa5fc2f", transactionHash: "0x69cde62bcd6458e14f40497f4840f422911d63f5dea2b3a9833e6810db64a1c9", allEvents: function (), f: function ()}
The point here is that address represents the address of the contract, and you will find that this is the same as the information of the account. In fact, you can also think of the contract address as an account address, which is the contract address that we call externally.
Recharge token.issue.sendTransaction (eth.accounts [0], 100,100, {from:eth.accounts [0]}); send tokentoken.transfer (eth.accounts [1], 30, {from:eth.accounts [0]}) to check the balance token.getBalance () console call is the same as Java object call, it can be called directly.
External interface interacts with intelligent contract
There are many interfaces provided by Yi Tai Fang, JSON RPC interface, web3 interface, here we use JSON RPC interface.
The principle of contract interaction
The interaction of the contract is a transaction, and what we need to do is to request a transaction from the block in the form of parameters according to the method and parameters to be called by api. After receiving our request, ethereum parses the passed parameters to execute the relevant contract code.
The RPC interface gives us two methods: eth_sendTransaction and eth_call.
Eth_sendTransactionCreatesnew message call transaction or a contract creation, if the data field containscode.ParametersObject-The transaction objectfrom: DATA, 20 Bytes-The address the transaction is send from.to: DATA, 20 Bytes-(optional when creating new contract) The address the transaction is directed to.gas: QUANTITY-(optional, default: 90000) Integer of the gas provided for the transaction execution. It will return unused gas.gasPrice: QUANTITY-(optional, default: To-Be-Determined) Integer of the gasPrice used for each paid gasvalue: QUANTITY-(optional) Integer of the value send with this transactiondata: DATA-The compiled code of a contract OR the hash of the invoked method signature and encoded parameters. For details see Ethereum Contract ABInonce: QUANTITY-(optional) Integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce.
As you can see, if we create a contract, we only need from,to (optional in the document, but the contract cannot be executed normally without to null in practice. It is recommended that this value is the contract address generated after we deployed the contract), data. The value of the attribute of Data can be found in Contract ABI. Here's something like this:
The value of Data is relatively not fixed, and how it is generated is related to the parameter type and number of parameters of the contract. Here we take three methods of deploying a token contract as an example:
Recharge issue (address account, uint amount)
This method has two parameters, address recharge account and uint recharge quantity.
According to Contract ABI, the data value should be the first 8 bytes of the sha3 of the method name + 64 bytes of the parameter, which is not enough to add to 0.
Here the name of the method is not issue (address account, uint amount) but the SHA03 value of issue (address,uint256).
Web3.sha3 ("issue (address,uint256)") "0x867904b44b606800f6f10498e11292d04ea19bfc7fe4bc0f1695aa516381f73d"
We recharge 10 to the first account, and the data here is not etheric coins, but tokens we have created.
Eth.accounts [0] "0x0cc9684af605bae10de801218321c1336bb62946"
Convert 10 to hexadecimal
000000000000000000000000000000000000000000000000000000000000000a
Then the data of data are:
0x867904b4000000000000000000000000fdf57e81872562a6112656f961944ce821fdf7eb000000000000000000000000000000000000000000000000000000000000000a
Then finally we call the eth_sendTransaction method to pass the parameter JSON object as follows:
{from:0xfdf57e81872562a6112656f961944ce821fdf7eb, to:0x7fe133950fc010ce41322d88f64f1918b9abb3a3, data: 0x867904b4000000000000000000000000fdf57e81872562a6112656f961944ce821fdf7eb000000000000000000000000000000000000000000000000000000000000000a}
Return: the hash value of this transaction, which has not been executed at this time, but is just created and needs to be completed by the miner through mining.
Call the API method:
JsonRpcHttpClient client = newJsonRpcHttpClient (new URL ("http://127.0.0.1:8545"));Object result = client.invoke (methodName,params, Object.class))
Check the existing token of the first account through the console:
Token.getbalance (eth.accounts [0])
Execute the calling interface code. Returns the transaction hash value:
0x2013764d1c3fea680f9015353497aa5f9f8d61580a3bd0524b3613b34329c095
At this point, the console enters:
Trading is the same as recharging. Note that the value of the token transfer account is the value of the from attribute, the token transfer account is the value of the data attribute, and the to corresponds to the contract address.
Eth_call
Executes anew message call immediately without creating a transaction on the block chain.ParametersObject-The transaction call objectfrom: DATA, 20 Bytes-(optional) The address the transaction is sent from.to: DATA, 20 Bytes-The address the transaction is directed to.gas: QUANTITY-(optional) Integer of the gas provided for the transaction execution. Eth_call consumes zero gas, but this parameter may be needed by some executions.gasPrice: QUANTITY-(optional) Integer of the gasPrice used for each paid gasvalue: QUANTITY-(optional) Integer of the value send with this transactiondata: DATA-(optional) Hash of the method signature and encoded parameters. For details seeEthereum Contract ABIQUANTITY | TAG-integer block number, or the string "latest", "earliest" or "pending", see the default block parameter
This method returns a piece of information to us, which is equivalent to the query function of the database, the parameters are also three, from,to,data, the data format is all the same.
Query getBalance (address account)
Query method hash code:
Web3.sha3 ("getBalance (address)") "0xf8b2cb4f3943230388faeee074f1503714bff212640051caba01411868d14ae3"
If you query the account we topped up in the previous step, the parameter data passed is:
0xf8b2cb4f000000000000000000000000fdf57e81872562a6112656f961944ce821fdf7eb
The final parameters of the eth_call method are:
{from: 0xfdf57e81872562a6112656f961944ce821fdf7eb, to:0x7fe133950fc010ce41322d88f64f1918b9abb3a3, data:0xf8b2cb4f000000000000000000000000fdf57e81872562a6112656f961944ce821fdf7eb}
Note that this method requires two parameters. In addition to processing a JSONobject, there is also a string parameter, which can be "" or "latest", "earliest" or "pending".
The calling interface returns a hexadecimal string:
0x0000000000000000000000000000000000000000000000000000000000000071 is the number of tokens of the account, converted to decimal: 113, which is consistent with the control query.
This is the interactive process of an intelligent contract. Isn't it easy.
At this point, the study on "what is the method of intelligent contract deployment and interaction in Ethernet Square" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.