Skip to content

Commit

Permalink
use solana rpc directly
Browse files Browse the repository at this point in the history
  • Loading branch information
vrtnd committed Jul 28, 2024
1 parent d520a27 commit 888f634
Showing 1 changed file with 31 additions and 31 deletions.
62 changes: 31 additions & 31 deletions src/utils/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,42 @@ import {
} from "../adapters/ibc";
const retry = require("async-retry");

async function getLatestSlot(rpcUrl = "https://api.mainnet-beta.solana.com") {
const response = await fetch(rpcUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
jsonrpc: "2.0",
id: 1,
method: "getSlot",
params: [
{
commitment: "finalized",
},
],
}),
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const data = await response.json();

if (data.error) {
throw new Error(`RPC error: ${data.error.message}`);
}
return data.result;
}

export async function getLatestBlockNumber(chain: string, bridge?: string): Promise<number> {
if (chain === "sui") {
// const client = getClient();
// return Number(await client.getLatestCheckpointSequenceNumber());
} else if (chain === "solana") {
const connection = getConnection();
return await connection.getSlot();
return await getLatestSlot();
} else if (chain === "tron") {
return (await tronGetLatestBlock()).number;
} else if (bridge && bridge === "ibc") {
Expand Down Expand Up @@ -53,35 +82,6 @@ const lookupBlock = async (timestamp: number, { chain }: { chain: Chain }) => {
return blockRes;
}
};
async function getLatestSlot(rpcUrl = "https://api.mainnet-beta.solana.com") {
const response = await fetch(rpcUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
jsonrpc: "2.0",
id: 1,
method: "getSlot",
params: [
{
commitment: "finalized",
},
],
}),
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const data = await response.json();

if (data.error) {
throw new Error(`RPC error: ${data.error.message}`);
}
return data.result;
}

async function getBlockTime(slotNumber: number, rpcUrl = "https://api.mainnet-beta.solana.com") {
const response = await fetch(rpcUrl, {
Expand Down

0 comments on commit 888f634

Please sign in to comment.