Add full Chain 138 integration: 8 steps, chain spec, app-ethereum config, docs
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
75
step-03-coin-module/network-explorer.ts
Normal file
75
step-03-coin-module/network-explorer.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* Step 3 — Coin module: network layer (explorer/RPC wrapper)
|
||||
* Target: ledger-live libs/coin-modules/coin-ethereum (or coin-defi_oracle_meta) src/network/
|
||||
*
|
||||
* Wraps Chain 138 RPC and Blockscout for sync, history, fees. Use in bridge.
|
||||
*/
|
||||
|
||||
const RPC_URL = "https://rpc-http-pub.d-bis.org";
|
||||
const EXPLORER_API = "https://explorer.d-bis.org/api";
|
||||
|
||||
export async function getLastBlock(): Promise<number> {
|
||||
const res = await fetch(RPC_URL, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "eth_blockNumber", params: [] }),
|
||||
});
|
||||
const data = await res.json();
|
||||
return parseInt(data.result, 16);
|
||||
}
|
||||
|
||||
export async function getBalance(address: string): Promise<string> {
|
||||
const res = await fetch(RPC_URL, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
jsonrpc: "2.0",
|
||||
id: 1,
|
||||
method: "eth_getBalance",
|
||||
params: [address, "latest"],
|
||||
}),
|
||||
});
|
||||
const data = await res.json();
|
||||
return data.result ?? "0x0";
|
||||
}
|
||||
|
||||
export async function getTransactionCount(address: string): Promise<number> {
|
||||
const res = await fetch(RPC_URL, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
jsonrpc: "2.0",
|
||||
id: 1,
|
||||
method: "eth_getTransactionCount",
|
||||
params: [address, "latest"],
|
||||
}),
|
||||
});
|
||||
const data = await res.json();
|
||||
return parseInt(data.result ?? "0x0", 16);
|
||||
}
|
||||
|
||||
/**
|
||||
* Explorer: get transactions for address.
|
||||
* Blockscout REST v2: GET /api/v2/addresses/{address_hash}/transactions
|
||||
* Response: { items: Tx[], next_page_params?: { block_number, index, items_count } }
|
||||
*/
|
||||
const EXPLORER_API_V2 = "https://explorer.d-bis.org/api/v2";
|
||||
|
||||
export async function getAddressTransactions(
|
||||
address: string,
|
||||
params?: { block_number?: number; index?: number; items_count?: number }
|
||||
): Promise<{ items: unknown[]; next_page_params?: { block_number: number; index: number; items_count: number } }> {
|
||||
let url = `${EXPLORER_API_V2}/addresses/${encodeURIComponent(address)}/transactions`;
|
||||
if (params?.block_number != null && params?.index != null && params?.items_count != null) {
|
||||
url += `?block_number=${params.block_number}&index=${params.index}&items_count=${params.items_count}`;
|
||||
}
|
||||
const res = await fetch(url);
|
||||
if (!res.ok) return { items: [] };
|
||||
const data = await res.json();
|
||||
return {
|
||||
items: data.items ?? [],
|
||||
next_page_params: data.next_page_params,
|
||||
};
|
||||
}
|
||||
|
||||
export { RPC_URL, EXPLORER_API, EXPLORER_API_V2 };
|
||||
Reference in New Issue
Block a user