Skip to content

非公式本サイトは非公式の日本語ドキュメントであり、Cloudflare 公式サイトではありません。最新情報はdevelopers.cloudflare.comをご確認ください。

Ethereum ゲートウェイを使う

最終更新 Markdown で表示Agent セットアップ

Ethereum ゲートウェイを用意したら(targetEthereum にして 新しいゲートウェイを作成 した状態)、クエリ用の正しい JSON を指定して さまざまな Ethereum ネットワーク とやり取りできます。

ネットワークから読み取る

Cloudflare Ethereum Gateway では、送りたいリクエストの JSON 本文を HTTP リクエストの本文に設定できます。たとえば、番号 0x2244 のブロックを読み取る場合、JSON は次の形式になります。

{
	"jsonrpc": "2.0",
	"method": "eth_getBlockByNumber",
	"params": ["0x2244", true],
	"id": 1
}

各 JSON には、有効な method パラメーター を使います。ここでの params 配列には、探したいブロック番号と、ブロック内の各トランザクションを全文で返すか(true)、スタブで返すか(false)を示す真偽値が入ります。

このクエリを カスタム Ethereum Gateway に送るには、次の 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 になります。

{
	"jsonrpc": "2.0",
	"id": 1,
	"result": {
		"difficulty": "0x746ef15b66",
		"extraData": "0x476574682f76312e302e302f6c696e75782f676f312e342e32",
		"gasLimit": "0x1388",
		"gasUsed": "0x0",
		"hash": "0xd6bb42034740c5d728e774e43a01f26222e0fcc279c504ca5963dc34fe70f392",
		"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
		"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 Gateway は eth_sendRawTransaction RPC メソッドでネットワークへ書き込めます。署名済みトランザクションに対して、新しいメッセージコールトランザクション、またはコントラクト作成を行います。トランザクションは、自分の Ethereum ウォレット に対応する秘密鍵で署名します。

ウォレットを用意し、自分でトランザクションに署名できる状態になったら、Cloudflare Ethereum Gateway 経由で Ethereum ネットワークに書き込めます。署名済みトランザクションは、次の形式の 16 進数文字列です。

"0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"

その後、カスタム Gateway を使い、次の 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();
});

(上のコマンドはそのままでは動きません。自分で署名したトランザクションを指定してください。)

役に立ちましたか?