Skip to content

Commit

Permalink
ton access v4 (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
shaharyakir committed Jul 30, 2024
1 parent 9f11096 commit 85100d2
Show file tree
Hide file tree
Showing 5 changed files with 609 additions and 794 deletions.
15 changes: 8 additions & 7 deletions src/lib/findUnfreezeBlock.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Address, TonClient4 } from "ton";
import { Address } from "ton";
import { AccountDetails } from "types";
import { executeV4Function } from "./getClientV4";

export async function findUnfreezeBlock(
tc4: TonClient4,
lastKnownAccountDetails: AccountDetails,
account: Address,
overrideBlock: number | undefined,
Expand All @@ -18,12 +18,12 @@ export async function findUnfreezeBlock(
);
}

let nextSeqno;
let nextSeqno: number;

if (!overrideBlock) {
// Shards from all chains at timestamp
const { shards: shardLastPaid } = await tc4.getBlockByUtime(
lastKnownAccountDetails.storageStat!.lastPaid
const { shards: shardLastPaid } = await executeV4Function((tc4) =>
tc4.getBlockByUtime(lastKnownAccountDetails.storageStat!.lastPaid)
);

// Get masterchain seqno (which we need to query v4)
Expand All @@ -34,11 +34,12 @@ export async function findUnfreezeBlock(

// From this -> https://github.com/ton-community/ton-api-v4/blob/main/src/api/handlers/handleAccountGetLite.ts#L21
// we understand that v4 is always to be queried by a masterchain seqno
const { account: accountDetails } = await tc4.getAccount(nextSeqno, account);
const { account: accountDetails } = await executeV4Function((tc4) =>
tc4.getAccount(nextSeqno, account)
);

if (accountDetails.state.type !== "active" && !!accountDetails.storageStat) {
return findUnfreezeBlock(
tc4,
accountDetails,
account,
overrideBlock,
Expand Down
26 changes: 22 additions & 4 deletions src/lib/getClientV4.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
import { TonClient4 } from "ton";
import { getHttpV4Endpoint } from "@orbs-network/ton-access";

export async function getClientV4() {
// TODO clientV4 ton access dont work
// const endpoint = await getHttpV4Endpoint();
return new TonClient4({ endpoint: "https://mainnet-v4.tonhubapi.com" });
const clients: TonClient4[] = [];

export async function getClientsV4() {
if (clients.length === 0) {
const tonAccessEndpoint = await getHttpV4Endpoint();
clients.push(
...[
new TonClient4({ endpoint: "https://mainnet-v4.tonhubapi.com" }),
new TonClient4({ endpoint: tonAccessEndpoint }),
]
);
}

return clients;
}

export async function executeV4Function<T>(
func: (tc: TonClient4) => Promise<T>
) {
const clients = await getClientsV4();
return Promise.any(clients.map(func));
}
15 changes: 7 additions & 8 deletions src/lib/useAccountDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { configParse18 } from "./configParse18";
import { calculateAmountForDelta } from "./calculateAmountForDelta";
import { findUnfreezeBlock } from "./findUnfreezeBlock";
import { MONTH_SEC } from "./useUnfreezeCallback";
import { getClientV4 } from "./getClientV4";
import { executeV4Function, getClientsV4 } from "./getClientV4";

export function useAccountDetails(
accountStr: string,
Expand All @@ -17,21 +17,21 @@ export function useAccountDetails(
async () => {
const account = Address.parse(accountStr);

const tc4 = await getClientV4();
let {
last: { seqno },
} = await tc4.getLastBlock();
} = await executeV4Function((tc4) => tc4.getLastBlock());

// Fetch current account details (under the assumption it's frozen)
const { account: frozenAccountDetails } = await tc4.getAccountLite(
seqno,
account
const { account: frozenAccountDetails } = await executeV4Function((tc4) =>
tc4.getAccountLite(seqno, account)
);

const balance = fromNano(frozenAccountDetails.balance.coins);

// Fetch config param 18 which specifies storage prices / sec
const config18Raw = await tc4.getConfig(seqno, [18]);
const config18Raw = await executeV4Function((tc4) =>
tc4.getConfig(seqno, [18])
);
const config18 = configParse18(
Cell.fromBoc(
Buffer.from(config18Raw.config.cell, "base64")
Expand Down Expand Up @@ -61,7 +61,6 @@ export function useAccountDetails(
// Fetch the block number to unfreeze from
({ unfreezeBlock, lastPaid, activeAccountDetails } =
await findUnfreezeBlock(
tc4,
frozenAccountDetails,
account,
overrideBlockToReviveFrom
Expand Down
11 changes: 4 additions & 7 deletions src/lib/useUnfreezeTxn.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { useQuery } from "@tanstack/react-query";
import { Address, Cell } from "ton";
import { StateInit } from "ton";
import { Address, Cell, StateInit } from "ton";
import { useNotification } from "../components";
import { getClientV4 } from "./getClientV4";
import { executeV4Function } from "./getClientV4";

export function useUnfreezeTxn(
accountStr: string,
Expand All @@ -16,12 +15,10 @@ export function useUnfreezeTxn(
async () => {
const account = Address.parse(accountStr);

const tc4 = await getClientV4();
let error;

const { account: accountDetails } = await tc4.getAccount(
unfreezeBlock!,
account
const { account: accountDetails } = await executeV4Function((tc4) =>
tc4.getAccount(unfreezeBlock!, account)
);

if (accountDetails.state.type !== "active") {
Expand Down
Loading

0 comments on commit 85100d2

Please sign in to comment.