JSON RPC 与eth节点沟通的桥梁
与eth节点沟通的桥梁, 下面是使用curl访问节点数据的例子:
web3_clientVersion获取eth节点客户端的版本. 例如Geth v.1.11.6、Erigon等
curl --location --request POST 'https://rpc.ankr.com/eth' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc": "2.0",
"method": "web3_clientVersion",
"params": [],
"id": 1
}'
eth_gasPrice获取gas的单价(猜测是最新块的baseFee+MaxPriorityFee)
curl --location --request POST 'https://rpc.ankr.com/eth' \
--header 'Content-Type: application/json' \
--data-raw '[
{
"jsonrpc": "2.0",
"method": "eth_gasPrice",
"params": [],
"id": 1
}
]'
curl --location --request POST 'https://rpc.ankr.com/eth' \
--header 'Content-Type: application/json' \
--data-raw '[
{
"jsonrpc": "2.0",
"method": "eth_accounts",
"params": [],
"id": 1
}
]'
curl --location --request POST 'https://rpc.ankr.com/eth' \
--header 'Content-Type: application/json' \
--data-raw '[
{
"jsonrpc": "2.0",
"method": "eth_blockNumber",
"params": [],
"id": 1
}
]'
curl --location --request POST 'https://rpc.ankr.com/eth' \
--header 'Content-Type: application/json' \
--data-raw '[
{
"jsonrpc": "2.0",
"method": "eth_getBalance",
"params": [
"0x407d73d8a49eeb85d32cf465507dd71d507100c1",
"latest"
],
"id": 1
}
]'
eth_sign对数据进行签名(已过期)
curl --location --request POST 'https://rpc.ankr.com/eth' \
--header 'Content-Type: application/json' \
--data-raw '[
{
"jsonrpc": "2.0",
"method": "eth_sign",
"params": [
"0x9b2055d370f73ec7d8a03e965129118dc8f5bf83",
"0xdeadbeaf"
],
"id": 1
}
]'
eth_signTransaction对一个交易进行签名(已过期, 没有用到私钥, 猜测是在eth客户端使用已解锁的账户进行签名)
curl --location --request POST 'https://rpc.ankr.com/eth' \
--header 'Content-Type: application/json' \
--data-raw '[
{
"jsonrpc": "2.0",
"method": "eth_signTransaction",
"params": [
{
"data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675",
"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
"gas": "0x76c0",
"gasPrice": "0x9184e72a000",
"to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
"value": "0x9184e72a"
}
],
"id": 1
}
]'
eth_sendTransaction发送交易信息(签名问题同上)
curl --location --request POST 'https://rpc.ankr.com/eth' \
--header 'Content-Type: application/json' \
--data-raw '[
{
"jsonrpc": "2.0",
"method": "eth_sendTransaction",
"params": [
{
"data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675",
"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
"gas": "0x76c0",
"gasPrice": "0x9184e72a000",
"to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
"value": "0x9184e72a"
}
],
"id": 1
}
]'
eth_sendRawTransaction发送签过名的交易, 返回hash需要使用eth_getTransactionReceipt获取详情数据
curl --location --request POST 'https://rpc.ankr.com/eth' \
--header 'Content-Type: application/json' \
--data-raw '[
{
"jsonrpc": "2.0",
"method": "eth_sendRawTransaction",
"params": [ "0xa3f20717a250c2b0b729b7e5becbff67fdaef7e0699da4de7ca5895b02a170a12d887fd3b17bfdce3481f10bea41f45ba9f709d39ce8325427b57afcfc994cee1b"
],
"id": 1
}
]'
eth_call仅在客户端执行,并不发送交易到区块链网络, 虽然是在本地执行在发送tx时也要注意账户余额是否充足保证能执行完毕.
curl --location --request POST 'https://rpc.ankr.com/eth' \
--header 'Content-Type: application/json' \
--data-raw '[
{
"jsonrpc": "2.0",
"method": "eth_call",
"params": [
{
"data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675",
"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
"gas": "0x76c0",
"gasPrice": "0x9184e72a000",
"to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
"value": "0x0"
},
"latest"
],
"id": 1
}
]'
curl --location --request POST 'https://rpc.ankr.com/eth' \
--header 'Content-Type: application/json' \
--data-raw '[
{
"jsonrpc": "2.0",
"method": "eth_estimateGas",
"params": [
{
"data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675",
"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
"gas": "0x76c0",
"gasPrice": "0x9184e72a000",
"to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
"value": "0x0"
},
"latest"
],
"id": 1
}
]'
curl --location --request POST 'https://rpc.ankr.com/eth' \
--header 'Content-Type: application/json' \
--data-raw '[
{
"jsonrpc": "2.0",
"method": "eth_getTransactionReceipt",
"params": [
"0x85d995eba9763907fdf35cd2034144dd9d53ce32cbec21349d4b12823c6860c5"
],
"id": 1
}
]'
参考: https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_estimategas
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!