📜 ⬆️ ⬇️

Work with smart contracts through Ethereum RPC API



Hello. In this article, we will look at the basic techniques for publishing smart contracts and interacting with them using the Ethereum RPC API. The discussed API methods allow solving such tasks:

  1. Create an account.
  2. Creating the correct smart contract.
  3. Retrieving information from a smart contract.
  4. Changing the status of a smart contract.


Contents :
')

Description of API used:


Some general notes



  1. All proposed actions are illustrated with real data from the Rinkeby test network (at the time of this writing).
  2. The status of transactions, accounts and smart contracts in Rinkeby can be monitored at https://rinkeby.etherscan.io/ (for the Ropsten subnet, https://ropsten.etherscan.io/ will be, respectively).

Packing Parameters and Returned Data


This part is also of a general nature and is therefore put into a separate chapter, but its study can be postponed until the moment when you directly need to use the parameters or parse the answer of the smart contract.

Incoming (parameters) or outgoing data packet for a contract is formed according to the following principle:


Consider, for example, a block in which the following data is transmitted: address , string , uint32 , address[] (at the beginning of each 32-byte string, for convenience, its hexadecimal address is relative to the beginning of the block).

 000:000000000000000000000000570f5d143ee469d12dc29bf8b3345fa5536476d9 020:0000000000000000000000000000000000000000000000000000000000000080 040:0000000000000000000000000000000000000000000000000000000000001234 060:00000000000000000000000000000000000000000000000000000000000000c0 080:0000000000000000000000000000000000000000000000000000000000000003 0a0:4e65770000000000000000000000000000000000000000000000000000000000 0c0:0000000000000000000000000000000000000000000000000000000000000002 0e0:000000000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 100:000000000000000000000000bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb 

In line 000, the address 0570f5d143ee469d12dc29bf8b3345fa5536476d9 is transmitted.
In line 020, a link is sent to the block describing a variable of type string - 0x80 bytes from the beginning of the block.
In line 040, an integer number 01234 .
In line 060, a link is sent to the block describing the array address[] - 0xc0 bytes from the beginning of the block.
In line 080, the character counter of a variable of type string - 3 is transmitted.
In line 0a0, the characters themselves are transferred to a variable of type string - the word New .
In line 0c0, the count of the elements of the array address[] - 2 is transmitted.
In line 0e0, the first element of the array address[] is 0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa - 0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa .
In line 100, the second element of the array address[] - 0bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb is 0bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb .

Attention! The whole block is transmitted in one continuous array:

Creating an account and working with it


To create a new account, use the personal_newAccount method, as a parameter to which the password is transmitted, which will later be used to unlock the account:

 {"jsonrpc":"2.0","method":"personal_newAccount","params":["PASSWORD"],"id":1} 

In response, an account identifier arrives, in this case, 0xfbeda9914b78b58a0f0e810298f9d545f8379f8d .

 {"jsonrpc":"2.0","id":1,"result":"0xfbeda9914b78b58a0f0e810298f9d545f8379f8d"} 

We will produce all further manipulations from this account - 0xfbeda9914b78b58a0f0e810298f9d545f8379f8d .

Now we need to put on it a certain amount to pay for the execution of transactions. Since it is impossible for a person to participate in the mining network of Rinkeby from outside, a special “tap” is provided for replenishing accounts, the procedure for using which is described here - https://faucet.rinkeby.io/ .

To replenish the account you need:


In order to use the funds from the account - to carry out transactions of any kind - the account must be unblocked . To unlock the account, use the personal_unlockAccount method. The parameters are the account identifier, password and unlock duration in seconds:

 {"jsonrpc":"2.0","method":"personal_unlockAccount","params":["0xfbeda9914b78b58a0f0e810298f9d 545f8379f8d", "PASSWORD", 600],"id":1} {"jsonrpc":"2.0","id":1,"result":true} 

Creating a smart contract


Technically, a smart contract is created by a transaction containing in the data field a contract byte-code and a block of initialization parameters, if such are provided for by a smart contract.

The contract byte code for creating a creation transaction can be obtained in the following ways:


The second method can be used to store the contract creation template directly on the blockchain — then it is enough to know the transaction identifier to extract it and “clone” it.

Technically, you can extract the transaction code directly by the identifier of an already existing smart contract (the eth_getCode method), but DO NOT , since it issues a byte code that has undergone special processing when creating a smart contract (in particular, initialization blocks are removed from it).

Compiling smart contract source code


To compile the source code for Solidity, I use Remix - https://remix.ethereum.org/ .



Enter the text of the contract, if there are no errors - the Bytecode itself will be in the Bytecode field, in our case:

 6060604052341561000f57600080fd5b5b6040805190810160405280600381526020017f4e657700000 000000000000000000000000000000000000000000000000000008152506000908051906020019061 005b929190610062565b505b610107565b82805460018160011615610100020316600290049060005 2602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b 828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906 100b5565b5b5090506100de91906100e2565b5090565b61010491905b8082111561010057600081600 09055506001016100e8565b5090565b90565b6101b6806101166000396000f30060606040526000357 c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063abd95b 951461003e575b600080fd5b341561004957600080fd5b6100516100cd565b604051808060200182810 3825283818151815260200191508051906020019080838360005b8381101561009257808201518184 01525b602081019050610076565b50505050905090810190601f1680156100bf578082038051600183 6020036101000a031916815260200191505b509250505060405180910390f35b6100d5610176565b60 008054600181600116156101000203166002900480601f016020809104026020016040519081016040 52809291908181526020018280546001816001161561010002031660029004801561016b5780601f10 6101405761010080835404028352916020019161016b565b820191906000526020600020905b81548 152906001019060200180831161014e57829003601f168201915b505050505090505b90565b6020604 051908101604052806000815250905600a165627a7a723058208ce8782af87268eb0fb543832301a980 e57bdb35f0a3cdbfe8f2ece20d9a3eaa0029 

Extract code from transaction


To extract code from a transaction, use the eth_getTransactionByHash method. In our example, the “cloned” smart contract was created by the transaction 0xc4d20bb8f9eede45968fc6bc850397e92b9f263eeb11200615cc08562d46c2e7 .

 {"jsonrpc":"2.0","method":"eth_getTransactionByHash","params":["0xc4d20bb8f9eede45968fc6bc850 397e92b9f263eeb11200615cc08562d46c2e7"],"id":1} 

The response input tag contains the contract bytecode.

 {"jsonrpc":"2.0","id":1,"result":{"blockHash":"0x731f202b0232de8c474c71677b29868f65802c068d0d e31b17bec09f3e31144c", "blockNumber":"0xbad3f", "from":"0x17eafa57fd812968d90aecc5a51e330e2e1c11a3", "gas":"0x31b2e", "gasPrice":"0x9c7652400", "hash":"0xc4d20bb8f9eede45968fc6bc850397e92b9f263eeb11200615cc08562d46c2e7", "input":"0x6060604052341561000f57600080fd5b5b6040805190810160405280600381526020017f4e 657700000000000000000000000000000000000000000000000000000000008152506000908051906 020019061005b929190610062565b505b610107565b82805460018160011615610100020316600290 0490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556 100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190 600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b8082111561010057 60008160009055506001016100e8565b5090565b90565b6101b6806101166000396000f30060606040 526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168 063abd95b951461003e575b600080fd5b341561004957600080fd5b6100516100cd565b604051808060 2001828103825283818151815260200191508051906020019080838360005b8381101561009257808 20151818401525b602081019050610076565b50505050905090810190601f1680156100bf578082038 0516001836020036101000a031916815260200191505b509250505060405180910390f35b6100d5610 176565b60008054600181600116156101000203166002900480601f016020809104026020016040519 08101604052809291908181526020018280546001816001161561010002031660029004801561016b 5780601f106101405761010080835404028352916020019161016b565b820191906000526020600020 905b81548152906001019060200180831161014e57829003601f168201915b505050505090505b9056 5b6020604051908101604052806000815250905600a165627a7a723058208ce8782af87268eb0fb5438 32301a980e57bdb35f0a3cdbfe8f2ece20d9a3eaa0029", "nonce":"0x30", "to":null, "transactionIndex":"0x2", "value":"0x0", "v":"0x2c", "r":"0x577865931aac644a3eefb83b59344caeab9e2970cfeb1bef02837b1bb4bccca0", "s":"0x48013e0c5ca3bff66fa7e19ebbcd9eaed63bc0a45004041c310f9fc4dfc5e5e8"}} 0x9c7652400 "," hash ":" 0xc4d20bb8f9eede45968fc6bc850397e92b9f263eeb11200615cc08562d46c2e7 "," input ":" 0x6060604052341561000f57600080fd5b5b6040805190810160405280600381526020017f4e {"jsonrpc":"2.0","id":1,"result":{"blockHash":"0x731f202b0232de8c474c71677b29868f65802c068d0d e31b17bec09f3e31144c", "blockNumber":"0xbad3f", "from":"0x17eafa57fd812968d90aecc5a51e330e2e1c11a3", "gas":"0x31b2e", "gasPrice":"0x9c7652400", "hash":"0xc4d20bb8f9eede45968fc6bc850397e92b9f263eeb11200615cc08562d46c2e7", "input":"0x6060604052341561000f57600080fd5b5b6040805190810160405280600381526020017f4e 657700000000000000000000000000000000000000000000000000000000008152506000908051906 020019061005b929190610062565b505b610107565b82805460018160011615610100020316600290 0490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556 100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190 600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b8082111561010057 60008160009055506001016100e8565b5090565b90565b6101b6806101166000396000f30060606040 526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168 063abd95b951461003e575b600080fd5b341561004957600080fd5b6100516100cd565b604051808060 2001828103825283818151815260200191508051906020019080838360005b8381101561009257808 20151818401525b602081019050610076565b50505050905090810190601f1680156100bf578082038 0516001836020036101000a031916815260200191505b509250505060405180910390f35b6100d5610 176565b60008054600181600116156101000203166002900480601f016020809104026020016040519 08101604052809291908181526020018280546001816001161561010002031660029004801561016b 5780601f106101405761010080835404028352916020019161016b565b820191906000526020600020 905b81548152906001019060200180831161014e57829003601f168201915b505050505090505b9056 5b6020604051908101604052806000815250905600a165627a7a723058208ce8782af87268eb0fb5438 32301a980e57bdb35f0a3cdbfe8f2ece20d9a3eaa0029", "nonce":"0x30", "to":null, "transactionIndex":"0x2", "value":"0x0", "v":"0x2c", "r":"0x577865931aac644a3eefb83b59344caeab9e2970cfeb1bef02837b1bb4bccca0", "s":"0x48013e0c5ca3bff66fa7e19ebbcd9eaed63bc0a45004041c310f9fc4dfc5e5e8"}} 

Please note that if the designer of the smart contract provides for the use of parameters, then in addition to the bytecode, the input tag will contain the values ​​of the parameters immediately following the bytecode. When creating a smart contract template for subsequent cloning, you can use zero values ​​of the parameters.

Calculating the cost of publishing a contract


To calculate the cost of publishing a contract, the eth_estimateGas method is eth_estimateGas , in the parameters of which you must specify the account number ( from tag) from which the contract will be created, as well as the contract byte code ( data tag). The answer will indicate the required number of Gas.

If the designer of a smart contract involves the use of parameters, then they should be included in the request in the data tag immediately after the bytecode. Otherwise, the calculation of Gas will be incorrect.

 {"jsonrpc":"2.0","method":"eth_estimateGas","params":[{ "from":"0xfbeda9914b78b58a0f0e810298f9d545f8379f8d", "data":"0x6060604052341561000f57600080fd5b5b6040805190810160405280600381526020017f4e6 577000000000000000000000000000000000000000000000000000000000081525060009080519060 20019061005b929190610062565b505b610107565b828054600181600116156101000203166002900 490600052602060002090601f016020900481019282601f106100a357805160ff191683800117855561 00d1565b828001600101855582156100d1579182015b828111156100d057825182559160200191906 00101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b80821115610100576 0008160009055506001016100e8565b5090565b90565b6101b6806101166000396000f300606060405 26000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680 63abd95b951461003e575b600080fd5b341561004957600080fd5b6100516100cd565b6040518080602 001828103825283818151815260200191508051906020019080838360005b83811015610092578082 0151818401525b602081019050610076565b50505050905090810190601f1680156100bf5780820380 516001836020036101000a031916815260200191505b509250505060405180910390f35b6100d56101 76565b60008054600181600116156101000203166002900480601f0160208091040260200160405190 8101604052809291908181526020018280546001816001161561010002031660029004801561016b5 780601f106101405761010080835404028352916020019161016b565b8201919060005260206000209 05b81548152906001019060200180831161014e57829003601f168201915b505050505090505b90565 b6020604051908101604052806000815250905600a165627a7a723058208ce8782af87268eb0fb54383 2301a980e57bdb35f0a3cdbfe8f2ece20d9a3eaa0029" }], "id":1} {"jsonrpc":"2.0","id":1,"result":"0x31b2e"} , "method": "eth_estimateGas", "params": [{ "from": "0xfbeda9914b78b58a0f0e810298f9d545f8379f8d", "data": "0x6060604052341561000f57600080fd5b5b6040805190810160405280600381526020017f4e6 {"jsonrpc":"2.0","method":"eth_estimateGas","params":[{ "from":"0xfbeda9914b78b58a0f0e810298f9d545f8379f8d", "data":"0x6060604052341561000f57600080fd5b5b6040805190810160405280600381526020017f4e6 577000000000000000000000000000000000000000000000000000000000081525060009080519060 20019061005b929190610062565b505b610107565b828054600181600116156101000203166002900 490600052602060002090601f016020900481019282601f106100a357805160ff191683800117855561 00d1565b828001600101855582156100d1579182015b828111156100d057825182559160200191906 00101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b80821115610100576 0008160009055506001016100e8565b5090565b90565b6101b6806101166000396000f300606060405 26000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680 63abd95b951461003e575b600080fd5b341561004957600080fd5b6100516100cd565b6040518080602 001828103825283818151815260200191508051906020019080838360005b83811015610092578082 0151818401525b602081019050610076565b50505050905090810190601f1680156100bf5780820380 516001836020036101000a031916815260200191505b509250505060405180910390f35b6100d56101 76565b60008054600181600116156101000203166002900480601f0160208091040260200160405190 8101604052809291908181526020018280546001816001161561010002031660029004801561016b5 780601f106101405761010080835404028352916020019161016b565b8201919060005260206000209 05b81548152906001019060200180831161014e57829003601f168201915b505050505090505b90565 b6020604051908101604052806000815250905600a165627a7a723058208ce8782af87268eb0fb54383 2301a980e57bdb35f0a3cdbfe8f2ece20d9a3eaa0029" }], "id":1} {"jsonrpc":"2.0","id":1,"result":"0x31b2e"} 

Note that in some cases smart contracts contain semantic (non-syntactic) errors, which are skipped by the compiler, but lead to the inoperability of the contract. One of the signs of the occurrence of such an error will be a sharp - at times, increase in the cost of creating a contract.

Execution of a transaction for the publication of a contract


To publish a contract, use the eth_sendTransaction method. The following parameters are passed to the method:


 {"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{ "from":"0xfbeda9914b78b58a0f0e810298f9d545f8379f8d", "gas":"0x31b2e", "data":"0x6060604052341561000f57600080fd5b5b6040805190810160405280600381526020017f4e6 577000000000000000000000000000000000000000000000000000000000081525060009080519060 20019061005b929190610062565b505b610107565b828054600181600116156101000203166002900 490600052602060002090601f016020900481019282601f106100a357805160ff191683800117855561 00d1565b828001600101855582156100d1579182015b828111156100d057825182559160200191906 00101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b80821115610100576 0008160009055506001016100e8565b5090565b90565b6101b6806101166000396000f300606060405 26000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680 63abd95b951461003e575b600080fd5b341561004957600080fd5b6100516100cd565b6040518080602 001828103825283818151815260200191508051906020019080838360005b83811015610092578082 0151818401525b602081019050610076565b50505050905090810190601f1680156100bf5780820380 516001836020036101000a031916815260200191505b509250505060405180910390f35b6100d56101 76565b60008054600181600116156101000203166002900480601f0160208091040260200160405190 8101604052809291908181526020018280546001816001161561010002031660029004801561016b5 780601f106101405761010080835404028352916020019161016b565b8201919060005260206000209 05b81548152906001019060200180831161014e57829003601f168201915b505050505090505b90565 b6020604051908101604052806000815250905600a165627a7a723058208ce8782af87268eb0fb54383 2301a980e57bdb35f0a3cdbfe8f2ece20d9a3eaa0029" }], "id":1} , "method": "eth_sendTransaction", "params": [{ "from": "0xfbeda9914b78b58a0f0e810298f9d545f8379f8d", "gas": "0x31b2e", "data": "0x6060604052341561000f57600080fd5b5b6040805190810160405280600381526020017f4e6 {"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{ "from":"0xfbeda9914b78b58a0f0e810298f9d545f8379f8d", "gas":"0x31b2e", "data":"0x6060604052341561000f57600080fd5b5b6040805190810160405280600381526020017f4e6 577000000000000000000000000000000000000000000000000000000000081525060009080519060 20019061005b929190610062565b505b610107565b828054600181600116156101000203166002900 490600052602060002090601f016020900481019282601f106100a357805160ff191683800117855561 00d1565b828001600101855582156100d1579182015b828111156100d057825182559160200191906 00101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b80821115610100576 0008160009055506001016100e8565b5090565b90565b6101b6806101166000396000f300606060405 26000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680 63abd95b951461003e575b600080fd5b341561004957600080fd5b6100516100cd565b6040518080602 001828103825283818151815260200191508051906020019080838360005b83811015610092578082 0151818401525b602081019050610076565b50505050905090810190601f1680156100bf5780820380 516001836020036101000a031916815260200191505b509250505060405180910390f35b6100d56101 76565b60008054600181600116156101000203166002900480601f0160208091040260200160405190 8101604052809291908181526020018280546001816001161561010002031660029004801561016b5 780601f106101405761010080835404028352916020019161016b565b8201919060005260206000209 05b81548152906001019060200180831161014e57829003601f168201915b505050505090505b90565 b6020604051908101604052806000815250905600a165627a7a723058208ce8782af87268eb0fb54383 2301a980e57bdb35f0a3cdbfe8f2ece20d9a3eaa0029" }], "id":1} 

In response, we get the transaction number:

 {"jsonrpc":"2.0","id":1,"result":"0x26cd429a43bc2f706f206fa6a536374cc7bf0e5090f0ed9b8f30ded71 73529f5"} 

or error message:

 {"jsonrpc":"2.0","id":1,"error":{"code":-32000,"message":"authentication needed: password or unlock"}} 

Now you need to wait for the completion of the transaction and get the result of its execution - a contract is created or not. The eth_getTransactionReceipt method is used for eth_getTransactionReceipt :

 {"jsonrpc":"2.0","method":"eth_getTransactionReceipt","params":["0x26cd429a43bc2f706f206fa6a53 6374cc7bf0e5090f0ed9b8f30ded7173529f5"],"id":1} 

While the transaction is in the Pending Txn, the following response will be issued:

 {"jsonrpc":"2.0","id":1,"result":null} 

After completing the transaction, we will receive a full “receipt”, in the contractAddress tag which will contain the address of the created smart contract:

 {"jsonrpc":"2.0", "id":1, "result":{"blockHash":"0x3afdc600435caebebb91497f01372c3ad6ac712c37fe9b1028445d8b41a58fca" , "blockNumber":"0xc4b16", "contractAddress":"0x2af49c8a413ea4b66ca8fd872befa9d1c8d22562", "cumulativeGasUsed":"0x31b2d", "from":"0xfbeda9914b78b58a0f0e810298f9d545f8379f8d", "gasUsed":"0x31b2d","logs":[], "logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000", "root":"0x47b6297fef6ba21c45ea845a6fba19ddb144d957f600cfe7230e05f0f711ac12", "to":null, "transactionHash":"0x26cd429a43bc2f706f206fa6a536374cc7bf0e5090f0ed9b8f30ded7173529f5", "transactionIndex":"0x0"}} 

If a receipt is not received within 5 minutes, then either there is a problem on the network or your node has not sent the network wiring. To understand the true cause, you should look through the Pending Txn queue on etherscan.io ( https://rinkeby.etherscan.io/txsPending ). If there is no transaction there, it means that you need to restart the Ethereum client and repeat the publication again.

Now it is necessary to check whether the smart contract was created correctly. To do this, you can use the eth_getCode method — getting the contract code by its address:

 {"jsonrpc":"2.0","method":"eth_getCode","params":["0x2af49c8a413ea4b66ca8fd872befa9d1c8d2256 2", "latest" ],"id":1} 

If the contract was created incorrectly, the answer will be received:

 {"jsonrpc":"2.0","id":1,"result":"0x"} 

If the result tag gives out some data other than 0x , then the contract is created successfully.

Interaction with a smart contract


To demonstrate interaction with a smart contract, we will use the following exemplary smart contract:

 pragma solidity ^0.4.9; contract Test { address Owner ; address Seller ; address Bank ; address BankCert ; string Status ; function Test(address seller_, address bank_) { Owner=msg.sender ; Seller=seller_ ; Bank=bank_ ; Status="New" ; } // 0xabd95b95 function GetStatus() constant returns (address, address, string retVal) { return(Seller, Bank, Status) ; } // 0x1bb71149 function SetBankCert(address cert) { if(msg.sender!=Bank) return ; BankCert=cert ; Status="Confirmed" ; } } 

When you create a contract (constructor - function Test ), the address of the Seller ( seller_ ) and the address of the Bank ( bank_ ) are bank_ . The GetStatus method returns the address of the Seller, the Bank and the current status of the contract. The SetBankCert method SetBankCert intended to store in the contract some digital identifier with a transfer to the “Confirmed” status.

Contract Byte Code:

 6060604052341561000f57600080fd5b60405160408061064383398101604052808051906020019091 9080519060200190919050505b336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908 373ffffffffffffffffffffffffffffffffffffffff16021790555081600160006101000a81548173fffffffffffffffffffffffffffffffff fffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555080600260006101000a81548173ffffffffff ffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550604080519081016040 5280600381526020017f4e657700000000000000000000000000000000000000000000000000000000 008152506004908051906020019061014292919061014b565b505b50506101f0565b82805460018160 0116156101000203166002900490600052602060002090601f016020900481019282601f1061018c57 805160ff19168380011785556101ba565b828001600101855582156101ba579182015b828111156101 b957825182559160200191906001019061019e565b5b5090506101c791906101cb565b5090565b6101 ed91905b808211156101e95760008160009055506001016101d1565b5090565b90565b610444806101 ff6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000 000000000900463ffffffff1680631bb7114914610049578063abd95b9514610082575b600080fd5b34156 1005457600080fd5b610080600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190505061017 7565b005b341561008d57600080fd5b610095610264565b604051808473ffffffffffffffffffffffffffffffffffffffff1 673ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff1673fffffffffffffffffffffffff fffffffffffffff1681526020018060200182810382528381815181526020019150805190602001908083836 0005b8381101561013a5780820151818401525b60208101905061011e565b505050509050908101906 01f1680156101675780820380516001836020036101000a031916815260200191505b5094505050505 060405180910390f35b600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffff ffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415156101d357610261565b80600360006101 000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506 040805190810160405280600981526020017f436f6e6669726d6564000000000000000000000000000 00000000000000000008152506004908051906020019061025f92919061035f565b505b50565b60008 061026f6103df565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff166002600090549 06101000a900473ffffffffffffffffffffffffffffffffffffffff16600480805460018160011615610100020316600290 0480601f01602080910402602001604051908101604052809291908181526020018280546001816001 161561010002031660029004801561034c5780601f1061032157610100808354040283529160200191 61034c565b820191906000526020600020905b81548152906001019060200180831161032f57829003 601f168201915b505050505090509250925092505b909192565b828054600181600116156101000203 166002900490600052602060002090601f016020900481019282601f106103a057805160ff191683800 11785556103ce565b828001600101855582156103ce579182015b828111156103cd578251825591602 0019190600101906103b2565b5b5090506103db91906103f3565b5090565b602060405190810160405 280600081525090565b61041591905b808211156104115760008160009055506001016103f9565b509 0565b905600a165627a7a72305820554a2d34ed0a4ad399f7464c8b29ba8c619e7f5de89746021f5aded 9516dd1860029 

Creating a contract with parameters


If the designer of a smart contract uses parameters (as in our demo example), then they should be “packed” in accordance with the description “Packing of parameters and returned data” and attached to the contract byte code.

In our case, the transfer of the address of the Seller 0x794ce6de39fa2d274438cc1692db04dfb5bea836 and the address of the Bank 0xfbeda9914 b78b58a0f0e810298f9d545f8379f8d when creating a smart contract will look like this (the contract byte code ends with 0029 ):

 60606040...94000029000000000000000000000000794ce6de39fa2d274438cc1692db04dfb5bea8360 00000000000000000000000fbeda9914b78b58a0f0e810298f9d545f8379f8d 

Once again, I note that the Gas calculation must be performed for the entire block. Bytecode + Parameters, otherwise the contract will either not be created or will not work.

Our test demo contract was created at 0x3d20e579f5befdc7d3f589adb6155f684d9a751c .

Identification of contract methods


The first 4 bytes (8 hexadecimal digits) of the method description hash are used to identify the smart contract method to which we refer.

For example, for the GetStatus method of a demo contract, the description will be GetStatus() , and for the SetBankCert method - SetBankCert(address) . Special attention should be paid to the absence of gaps in the description - there were sad precedents: (.

To determine the hash, the web3_sha3 method is web3_sha3 , and the string value should be given in hexadecimal representation (for GetStatus() this will be 0x4765745374617475732829 ):

 {"jsonrpc":"2.0","method":"web3_sha3","params":["0x4765745374617475732829"],"id":1} {"jsonrpc":"2.0","id":1,"result":"0xabd95b950242a279866243fa2b8fec5adddf6560d4e1b4f8745cfe7b5 7786865"} 

Accordingly, the identifier of the GetStatus method will be 0xabd95b95 , for the SetBankCert method SetBankCert identifier is 0x1bb71149 .

Calling Information Request Methods


To call methods that are not related to changing the state of a contract (for example, to get information about its current status), the API method eth_call can be used.

The method request has the following structure:

 {"jsonrpc":"2.0","method":"eth_call","params":[{"to":< >, "data":< >},"latest"],"id":1} 

The block < > is formed as follows:

< >< >

where < > are formed as indicated in the “Package of parameters and return data” section.

If the method does not assume the presence of parameters, the < > block consists only of the method identifier.

For example, to call the GetStatus method of the GetStatus Demo Contract, use the query:

 {"jsonrpc":"2.0", "method":"eth_call", "params":[{"to":"0x3d20e579f5befdc7d3f589adb6155f684d9a751c", "data":"0xabd95b95"}, "latest"],"id":1} 

which will be answered:

 {"jsonrpc":"2.0", "id":1, "result":"0x000000000000000000000000794ce6de39fa2d274438cc1692db04dfb5bea8360000000000 00000000000000fbeda9914b78b58a0f0e810298f9d545f8379f8d000000000000000000000000000000 000000000000000000000000000000006000000000000000000000000000000000000000000000000 000000000000000034e65770000000000000000000000000000000000000000000000000000000000" } 

Let us analyze the received answer in accordance with the rules of the “Packing of Parameters and Returned Data” clause and taking into account the description of the GetStatus method - function getStatus (), returns (address, address, string retVal).

For convenience of analysis, decompose the answer into 32-byte words:

 000:000000000000000000000000794ce6de39fa2d274438cc1692db04dfb5bea836 020:000000000000000000000000fbeda9914b78b58a0f0e810298f9d545f8379f8d 040:0000000000000000000000000000000000000000000000000000000000000060 060:0000000000000000000000000000000000000000000000000000000000000003 080:4e65770000000000000000000000000000000000000000000000000000000000 

Based on the description, we expect to receive the following set of variables: address, string. In this way:


Calling methods that change the state of the contract


To call methods that change the state of a contract, the API method eth_sendTransaction must be used.

The method request has the following structure:

 {"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{ "from":< >, "to":< >, "gas":< >, "data":< > }], "id":1} 

< > must have a balance sufficient to pay < > . In addition, it should be borne in mind that the contract may contain internal conditions for controlling the < > , as, for example, in the SetBankCert method of our demo contract.
The block < > is formed as follows:

< >< >

where < > are formed as specified in the paragraph “Packing of parameters and returned data”.

If the method does not assume the presence of parameters, the < > block consists only of the method identifier.

, SetBankCert("0f7b0f8870a5596a7b57dd3e035550aeb5af16607") -, < > :

0x1bb71149000000000000000000000000f7b0f8870a5596a7b57dd3e035550aeb5af16607 .

, -, eth_estimateGas , , eth_sendTransaction .

 {"jsonrpc":"2.0","method":"eth_estimateGas","params":[{ "from":"0xfbeda9914b78b58a0f0e810298f9d545f8379f8d", "to":"0x3d20e579f5befdc7d3f589adb6155f684d9a751c", "data":"0x1bb71149000000000000000000000000f7b0f8870a5596a7b57dd3e035550aeb5af16607" }],"id":1} {"jsonrpc":"2.0","id":1,"result":"0xd312"} 

, -, Gas . Gas, , , . Gas, — 0200000 .

eth_sendTransaction :

 {"jsonrpc":"2.0", "method":"eth_sendTransaction", "params":[{ "from":"0xfbeda9914b78b58a0f0e810298f9d545f8379f8d", "to":"0x3d20e579f5befdc7d3f589adb6155f684d9a751c", "gas":"0xd312", "data":"0x1bb71149000000000000000000000000f7b0f8870a5596a7b57dd3e035550aeb5af16607" }],"id":1} 

:

 {"jsonrpc":"2.0","id":1,"result":"0xe55c9fe8f816f5730053fc491ea27acfd83c615b6623d06f25fb281fea 750f3c"} 

-, , ( eth_getTran sactionReceipt):

 {"jsonrpc":"2.0","method":"eth_getTransactionReceipt","params":["0xe55c9fe8f816f5730053fc491ea2 7acfd83c615b6623d06f25fb281fea750f3c"],"id":1} 

— :

 {"jsonrpc":"2.0", "id":1, "result":{"blockHash":"0xd8bb4a0b0ca3a598a69786f2f40876d547a672044fc8d961ec22da60606fa2fb" , "blockNumber":"0xc4b6f", "contractAddress":null, "cumulativeGasUsed":"0x459e5", "from":"0xfbeda9914b78b58a0f0e810298f9d545f8379f8d", "gasUsed":"0xd311", "logs":[], "logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000", "root":"0x75f6823221a4f0e4f80dfa6c7ccfc961c7522833aac959d6a398537730a62041", "to":"0x3d20e579f5befdc7d3f589adb6155f684d9a751c", "transactionHash":"0xe55c9fe8f816f5730053fc491ea27acfd83c615b6623d06f25fb281fea750f3c", "transactionIndex":"0x1"}} 

, , - , , -.

, , — - , « ».

 000:000000000000000000000000794ce6de39fa2d274438cc1692db04dfb5bea836 020:000000000000000000000000fbeda9914b78b58a0f0e810298f9d545f8379f8d 040:0000000000000000000000000000000000000000000000000000000000000060 050:0000000000000000000000000000000000000000000000000000000000000009 060:436f6e6669726d65640000000000000000000000000000000000000000000000 

, , «Confirmed».

Summary


, - RPC API - Ethereum. , , -, , , - .

, /.

Source: https://habr.com/ru/post/338172/


All Articles