eth区块链相关知识

JSON RPC 与eth节点沟通的桥梁

JSON RPC

与eth节点沟通的桥梁, 下面是使用curl访问节点数据的例子:

  1. 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
    }'
  2. 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
    }
]'
  1. eth_accounts在客户端解锁的账户
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
    }
]'
  1. eth_blockNumber返回客户端区块的高度, 有可能会落后因为还没同步最新的
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
    }
]'
  1. eth_getBalance获取账户余额, 可以指定blockNumber
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
    }
]'
  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
    }
    ]'
  2. 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
    }
    ]'
  3. 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
    }
    ]'
  4. 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
    }
    ]'
  5. 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
    }
]'
  1. eth_estimateGas估算执行合约代码需要的gas数量
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
    }
]'
  1. eth_getTransactionReceipt根据交易hash获取交易详情
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

点赞 0
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
打野工程师
打野工程师
江湖只有他的大名,没有他的介绍。