76 lines
2.5 KiB
TypeScript
76 lines
2.5 KiB
TypeScript
/**
|
|
* 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 };
|