Back

blockchain - 使用RPC方法直接调用 fuji / polka / eth 智能合约

发布时间: 2022-01-07 06:47:00

参考:https://ethereum.stackexchange.com/questions/78857/infura-json-rpc-how-to-call-a-function-passing-parameters
和 https://docs.avax.network/build/avalanchego-apis/
以及 https://eth.wiki/json-rpc/API
我在fuji上发布了一个智能 合约(remix + metamask )的方式,
该合约地址为: 0x140a09c157d956C6b90Ad8c4B120F0bdFEf538e7
我的地址:0xa97D4D83Bb3EFE403E1e02B079A21B89947cE7A6
方法为 store(uint256),
查看

查询: 调用retrieve 方法

curl https://api.avax-test.network/ext/bc/C/rpc -H 'content-type:application/json;' -X POST --data '{"jsonrpc":"2.0", "method":"eth_call", "params":[{"from": "0xa97D4D83Bb3EFE403E1e02B079A21B89947cE7A6", "to": "0x3d27fbCBACBb0a0134Bf2CEF88ee75A27A28d1d3", "data": "0x2e64cec10000000000000000000000000000000000000000000000000000000000000000"}, "latest"], "id":1}'
{"jsonrpc":"2.0","id":1,"result":"0x000000000000000000000000000000000000000000000000000000000000014d"}

设置: 调用store(uint256) 方法

这里涉及到转账,
from: 同上
to:  同上
data:  0x6057361d000000000000000000000000000000000000000000000000000000000000014d
这里会用到签名。 sign tx, 然后这样那样的。(需要private key)
思路:
1. get private key
2. sign tx
3. 发送上面的signed tx

调用 方法:该方法的参数包含数组

这里参考:https://docs.soliditylang.org/en/v0.5.3/abi-spec.html#formal-specification-of-the-encoding

貌似不通形式的参数还不一样.

如果只有一个参数,该参数为数组的话,就很简单:

如果该方法包含多个参数,例如 (true, 1, ["foo", "bar", 33, false]) 的话,就特别复杂。

参考:https://medium.com/@hayeah/how-to-decipher-a-smart-contract-method-call-8ee980311603

下面也是一个解读:

所以,0x60, 0xa0 ,   0xe0 分别是第一个,第二个,第三个参数出现的位置。

这就明白了。

Back