一旦您拥有了 Ethereum 网关 — 即您 创建了一个新网关 且 target 为 Ethereum — 您就可以通过为查询指定正确的 JSON blob 来与 不同的 Ethereum 网络 进行交互。
Cloudflare Ethereum 网关允许发出 HTTP 请求,该请求的主体被设置为您希望发出请求的 JSON 主体。例如,如果您想读取编号为 0x2244 的区块,那么您的 JSON blob 采用以下格式:
{
"jsonrpc": "2.0",
"method": "eth_getBlockByNumber",
"params": ["0x2244", true],
"id": 1
}每个 blob 都使用有效的 method 参数。此处的 params 数组包含我们要定位的区块号,以及一个布尔值,表示区块中的各个交易是应完整显示 (true) 还是作为存根显示 (false)。
要将此查询发送到您的 自定义 Ethereum 网关,您可以使用 cURL 命令:
curl https://web3-trial.cloudflare-eth.com/v1/mainnet -H 'Content-Type: application/json' --data '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["0x2244", true],"id":1}'您还可以使用 JS Fetch API 编写相同的查询:
await fetch(
new Request("https://web3-trial.cloudflare-eth.com/v1/mainnet", {
method: "POST",
body: JSON.stringify({
jsonrpc: "2.0",
method: "eth_getBlockByNumber",
params: ["0x2244", true],
id: 1,
}),
headers: {
"Content-Type": "application/json",
},
}),
).then((resp) => {
return resp.json();
});在这两种情况下,响应都将是以下形式的 JSON blob:
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"difficulty": "0x746ef15b66",
"extraData": "0x476574682f76312e302e302f6c696e75782f676f312e342e32",
"gasLimit": "0x1388",
"gasUsed": "0x0",
"hash": "0xd6bb42034740c5d728e774e43a01f26222e0fcc279c504ca5963dc34fe70f392",
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"miner": "0xf927a40c8b7f6e07c5af7fa2155b4864a4112b13",
"mixHash": "0x975da446e302e6da6cedb3fbaa763c3c203ae88d6fab4924e2a3d34a568c4361",
"nonce": "0x88a7f12f49151c83",
"number": "0x2244",
"parentHash": "0x067fd84ecdbc7491bf5ec7d5d4ead361b1f590eec74797a7f90b4a7d7004a48d",
"receiptsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"size": "0x21b",
"stateRoot": "0x828dade2067283e370993ec6a1bda0e65c1310e404a6d5bbb030b596eb80017c",
"timestamp": "0x55bb040f",
"totalDifficulty": "0x5c328da43525d",
"transactions": [],
"transactionsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"uncles": []
}
}目前,Ethereum 网关允许您使用 eth_sendRawTransaction RPC 方法写入网络。这将创建新的消息调用交易或用于签名交易的合约创建。使用对应于您自己的 Ethereum 钱包 ↗ 的密钥对交易进行签名。
一旦您设置了钱包并拥有了签署您自己的交易的方法,您就可以通过 Cloudflare Ethereum 网关 将该交易写入 Ethereum 网络。已签名的交易使用如下形式的十六进制字符串:
"0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"然后,您可以使用您的 自定义网关 通过 cURL 命令将交易发送到网络:
curl https://web3-trial.cloudflare-eth.com/v1/mainnet -H 'Content-Type: application/json' --data '{"jsonrpc":"2.0","method":"eth_sendRawTransaction","params":["0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"],"id":1}'您也可以使用 JS Fetch API 请求:
await fetch(
new Request("https://web3-trial.cloudflare-eth.com/v1/mainnet", {
method: "POST",
body: JSON.stringify({
jsonrpc: "2.0",
method: "eth_sendRawTransaction",
params: [
"0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675",
],
id: 1,
}),
headers: {
"Content-Type": "application/json",
},
}),
).then((resp) => {
return resp.json();
});(上面的实际命令将不起作用 — 您需要提供自己的签名交易。)