|
1 | 1 | import { ETH_DEVNET_URL, STARKNET_DEVNET_URL } from './constants' |
2 | 2 |
|
3 | 3 | // |
4 | | -// Docs: https://github.com/0xSpaceShard/starknet-devnet-rs/blob/main/contracts/l1-l2-messaging/README.md#ethereum-setup |
| 4 | +// Docs: https://0xspaceshard.github.io/starknet-devnet/docs/postman |
5 | 5 | // |
6 | 6 |
|
7 | 7 | /* |
8 | | - * https://github.com/0xSpaceShard/starknet-devnet-rs/blob/7e5ff351198f799816c1857c1048bf8ee7f89428/crates/starknet-devnet-server/src/api/http/models.rs#L23 |
| 8 | + * https://github.com/0xSpaceShard/starknet-devnet-rs/blob/main/crates/starknet-devnet-server/src/api/http/models.rs#L23 |
9 | 9 | */ |
10 | 10 | export type PostmanLoadL1MessagingContract = Readonly<{ |
11 | 11 | networkUrl?: string |
12 | 12 | address?: string |
13 | 13 | }> |
14 | 14 |
|
15 | 15 | /* |
16 | | - * https://github.com/0xSpaceShard/starknet-devnet-rs/blob/7e5ff351198f799816c1857c1048bf8ee7f89428/crates/starknet-devnet-server/src/api/http/models.rs#L132 |
| 16 | + * https://github.com/0xSpaceShard/starknet-devnet-rs/blob/main/crates/starknet-devnet-server/src/api/http/models.rs#L132 |
17 | 17 | */ |
18 | 18 | export type MessagingLoadAddress = Readonly<{ |
19 | 19 | messaging_contract_address: string |
20 | 20 | }> |
21 | 21 |
|
22 | | -/* |
23 | | - * https://github.com/0xSpaceShard/starknet-devnet-rs/blob/7e5ff351198f799816c1857c1048bf8ee7f89428/crates/starknet-devnet-server/src/api/http/endpoints/postman.rs#L12 |
24 | | - */ |
25 | | -export const loadL1MessagingContract = async ( |
26 | | - params?: PostmanLoadL1MessagingContract, |
27 | | -): Promise<MessagingLoadAddress> => { |
28 | | - const res = await fetch(`${STARKNET_DEVNET_URL}/postman/load_l1_messaging_contract`, { |
| 22 | +type JsonRpcResponse<T> = { |
| 23 | + result?: T |
| 24 | + error?: { message: string } |
| 25 | +} |
| 26 | + |
| 27 | +const devnetRpc = async <T>(method: string, params: Record<string, unknown>): Promise<T> => { |
| 28 | + const res = await fetch(`${STARKNET_DEVNET_URL}/rpc`, { |
29 | 29 | method: 'POST', |
30 | 30 | headers: { |
31 | 31 | 'Content-Type': 'application/json', |
32 | 32 | }, |
33 | 33 | body: JSON.stringify({ |
34 | | - network_url: params?.networkUrl ?? ETH_DEVNET_URL, |
35 | | - address: params?.address, |
| 34 | + jsonrpc: '2.0', |
| 35 | + id: 1, |
| 36 | + method, |
| 37 | + params, |
36 | 38 | }), |
37 | 39 | }) |
38 | 40 |
|
39 | | - const result = await res.json() |
40 | | - if (result.error != null) { |
41 | | - throw new Error(result.error) |
| 41 | + const data = (await res.json()) as JsonRpcResponse<T> |
| 42 | + if (data.error != null) { |
| 43 | + throw new Error(data.error.message) |
| 44 | + } |
| 45 | + if (data.result == null) { |
| 46 | + throw new Error(`${method} returned no result`) |
42 | 47 | } |
43 | | - return result |
| 48 | + return data.result |
| 49 | +} |
| 50 | + |
| 51 | +/* |
| 52 | + * starknet-devnet-rs 0.8+ removed HTTP /postman/load_l1_messaging_contract; use devnet_postmanLoad. |
| 53 | + */ |
| 54 | +export const loadL1MessagingContract = async ( |
| 55 | + params?: PostmanLoadL1MessagingContract, |
| 56 | +): Promise<MessagingLoadAddress> => { |
| 57 | + return devnetRpc<MessagingLoadAddress>('devnet_postmanLoad', { |
| 58 | + network_url: params?.networkUrl ?? ETH_DEVNET_URL, |
| 59 | + ...(params?.address != null && { messaging_contract_address: params.address }), |
| 60 | + }) |
44 | 61 | } |
45 | 62 |
|
46 | 63 | /* |
@@ -82,22 +99,10 @@ export type FlushedMessages = Readonly<{ |
82 | 99 | }> |
83 | 100 |
|
84 | 101 | /* |
85 | | - * https://github.com/0xSpaceShard/starknet-devnet-rs/blob/7e5ff351198f799816c1857c1048bf8ee7f89428/crates/starknet-devnet-server/src/api/http/endpoints/postman.rs#L26 |
| 102 | + * starknet-devnet-rs 0.8+ removed HTTP /postman/flush; use devnet_postmanFlush. |
86 | 103 | */ |
87 | 104 | export const flush = async (params?: FlushParameters): Promise<FlushedMessages> => { |
88 | | - const res = await fetch(`${STARKNET_DEVNET_URL}/postman/flush`, { |
89 | | - method: 'POST', |
90 | | - headers: { |
91 | | - 'Content-Type': 'application/json', |
92 | | - }, |
93 | | - body: JSON.stringify({ |
94 | | - dry_run: params?.dryRun ?? false, |
95 | | - }), |
| 105 | + return devnetRpc<FlushedMessages>('devnet_postmanFlush', { |
| 106 | + dry_run: params?.dryRun ?? false, |
96 | 107 | }) |
97 | | - |
98 | | - const result = await res.json() |
99 | | - if (result.error != null) { |
100 | | - throw new Error(result.error) |
101 | | - } |
102 | | - return result |
103 | 108 | } |
0 commit comments