Ethereum development strategy 1: preparation part I: installation of various related tools and environment configuration
Ethereum Development Strategy II: preparation Part II: Compilation and installation of Ethereum
Ethereum development strategy 3: building Ethereum private chain (Mining Based on ethash consensus)
Ethereum development strategy 4: operation of Ethereum private chain (Mining Based on ethash consensus))
Ethereum development strategy 5: Ethereum's programming interface Web3 JS API: transfer
Ethereum development strategy 6: Compilation and deployment of smart contracts
my Personal blog Update faster~
Chapter 5 has introduced Web3 The sixth chapter introduces the compilation, deployment and call of smart contract. We can also use Web3 JS completes the call of smart contract. This chapter only explains how to call smart contract through transaction. We continue to use the smart contract in the previous chapter and deploy it to Ethereum network according to the method in the previous chapter. After successful deployment, we will get the contract address, the interface description file of the contract and the signature of the contract function.
1. The account invokes the contract in the case of connected Ethereum nodes
(the node has been started, enter node in the new terminal to enter the nodejs environment) web3.0 should be used JS interacts with the blockchain. First, you need to create a web3 object, and then connect to the Ethereum node.
var Web3 = require("web3"); //Create web3 object var web3 = new Web3(); //Connect to Ethereum node web3.setProvider(new Web3.providers.HttpProvider("http://localhost:8545"));
The signature acquisition method of contract function is as follows:
//After the get() method of the contract is first calculated by sha3, the first 8 bits except 0x are taken var get_func_sign = web3.sha3('get()').substr(2,8);//6d4ce63c //After the set(uint256) method of the contract is calculated by sha3, the first 8 bits except 0x are taken var set_func_sign = web3.sha3('set(uint256)').substr(2,8);//60fe47b1
Note: you can enter get separately in nodejs environment_ func_ Sign and set_func_sign gets signed.
There are two ways to call the contract. The first way is to add the contract interface description file through the contract address. The transfer method has been introduced in the previous chapter. Here is the second way: add the function signature of the called contract through the contract address.
First, we build a transaction that calls the get method:
var tx_get = { "from": "0x05eb64320ca48ad8176da4970ce9ec3ac0eefad6", "to": "0x9602d219514a41472757853b0a0e890eaca41145", "data": "0x6d4ce63c0000000000000000000000000000000000000000000000000000000000000000" };
Note: the sender address from here is the account address in the same node (the connected one), and to is the contract address. data is the parameter passed to the contract, which is composed of ox, 6d4ce63c and a 32byte parameter. 0 is passed in because the called get method does not require parameters.
Execute transaction:
var result = web3.eth.call(tx_get); console.info(result);
After execution, return to 2a, because we have assigned 42 in the previous chapter. 2a is the hexadecimal of 42.
Next, call the set method to set a value for storedData. First, build a set method transaction:
var tx_set = { "from": "0x05eb64320ca48ad8176da4970ce9ec3ac0eefad6", "to": "0x9602d219514a41472757853b0a0e890eaca41145", "gasLimit": web3.toHex("50000"), "data": "0x60fe47b10000000000000000000000000000000000000000000000000000000000000001" };
Note: data is the parameter passed to the contract, which is composed of ox, 60fe47b and a 32byte parameter. The called set method passes in parameter 1.
Execute transaction:
var txId = web3.eth.sendTransaction(tx_set);
Note that you need to unlock the account here:
personal.unlockAccount(eth.accounts[0], "123456")
We need to start mining and write transactions into the blockchain:
miner.start(1);admin.sleepBlocks(1);miner.stop();
After successful execution, calling the get method to see the return value can be seen that it has changed from 42 to 1:
var result = web3.eth.call(tx_get); console.info(result);
2. The account does not send the signed transaction call contract when it is connected to the Ethereum node
Like the account transfer problem in Chapter 5, if the note of the contract we want to call is not in the connected Ethereum node (i.e. node0), we can use Web3 eth. The sendrawtransaction method completes the signing and sending of the call contract transaction.
First, we should start node1 node and connect it with node0 according to the method introduced in Chapter 4. In order to ensure that the account of node1 node has a balance to call the contract, we should also set node1 account 0 as the mining reward address and mine to obtain some balance. Next, we can continue the operation in the web3js started in the previous section (node0 connected).
Create transaction to be sent:
var address = "0x528db84a61df8c6b2e0db61e5edf69aec84021ab"; var nonce = web3.eth.getTransactionCount(address, 'pending'); var rawTx = { "from": address, "to": '0x9602d219514a41472757853b0a0e890eaca41145', "gas": web3.toHex(50000), "value": '0x00', "nonce": web3.toHex(nonce), "gaslimit": web3.toHex(50000), "data": "0x60fe47b10000000000000000000000000000000000000000000000000000000000000003" }
Where from is the account 0 address of node1, to is the contract address, and data passes parameter 3 to the set method.
Add a signature to the transaction (we need to obtain the private key of account 0 in node1 in advance by using the method described in Chapter 5):
//Introduction package var Tx = require('ethereumjs-tx'); //Create raw transaction var tx = new Tx(rawTx); //Private key of from address var privateKey = new Buffer('484f879d34fe5d0aa6185107f7b319b0995162c15c2d93f8de42924a868632bf', 'hex'); //Sign rawTx with private key tx.sign(privateKey); //Signed transaction var serializedTx = tx.serialize(); //The signed transaction information is output in the console console.log(serializedTx.toString('hex'));
Send signed transaction:
web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash){ if (!err){ console.log(hash); } else { console.log(err); } });
Then perform mining on the js console of node0:
miner.start(1);admin.sleepBlocks(1);miner.stop();
Call the get method to check whether the storedData value is successfully set to 3:
var result = web3.eth.call(tx_get); console.info(result);