Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: integrate v1 endpoints with fe #73

Open
wants to merge 4 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 25 additions & 37 deletions hooks/useAppInit.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,47 @@
/* eslint-disable react-hooks/exhaustive-deps */
import { substrateConfig } from "@/config/walletConfig";
import { useAvailAccount } from "@/stores/availWalletHook";
import { useCommonStore } from "@/stores/common";
import { pollWithDelay } from "@/utils/poller";
import { ApiPromise, disconnect, initialize } from "avail-js-sdk";
import { useEffect } from "react";
import useTransactions from "./useTransactions";
import { useAccount } from "wagmi";
import { appConfig } from "@/config/default";
import { Chain, TransactionStatus } from "@/types/common";
import { _getBalance, initApi, sleep } from "@/utils/common";
import { Logger } from "@/utils/logger";
import {
fetchAvlHead,
fetchEthHead,
fetchLatestBlockhash,
} from "@/services/api";
import { useLatestBlockInfo } from "@/stores/lastestBlockInfo";
import { substrateConfig } from '@/config/walletConfig';
import { useAvailAccount } from '@/stores/availWalletHook';
import { useCommonStore } from '@/stores/common';
import { pollWithDelay } from '@/utils/poller';
import { ApiPromise } from 'avail-js-sdk';
import { useEffect } from 'react';
import useTransactions from './useTransactions';
import { useAccount } from 'wagmi';
import { appConfig } from '@/config/default';
import { Chain, TransactionStatus } from '@/types/common';
import { _getBalance, initApi } from '@/utils/common';
import { Logger } from '@/utils/logger';
import { fetchAvlHead, fetchEthHead } from '@/services/api';
import { useLatestBlockInfo } from '@/stores/lastestBlockInfo';

const useAppInit = () => {
const { selected } = useAvailAccount();
const account = useAccount();
const {
api,
setApi,
pendingTransactionsNumber,
setPendingTransactionsNumber,
readyToClaimTransactionsNumber,
setReadyToClaimTransactionsNumber,
fromChain,
dollarAmount,
setDollarAmount,
toChain,
fromAmount,
toAddress,
ethBalance,
setEthBalance,
availBalance,
setAvailBalance,
} = useCommonStore();
const { fetchTransactions } = useTransactions();
const { pendingTransactions } = useTransactions();
const { setAvlHead, setEthHead, setLatestBlockhash } = useLatestBlockInfo();
const { setAvlHead, setEthHead } = useLatestBlockInfo();

const fetchHeads = async (api: ApiPromise) => {
try {
Logger.info("FETCH_HEADS");
const ethHead = await fetchEthHead();
if(!ethHead.data) throw new Error("Failed to fetch ETH head");
setEthHead(ethHead.data);
const LatestBlockhash = await fetchLatestBlockhash(ethHead.data.slot);
setLatestBlockhash(LatestBlockhash.data);
const avlHead = await fetchAvlHead(api);
if(!avlHead.data) throw new Error("Failed to fetch AVAIL head");
setAvlHead(avlHead.data);
} catch (error) {
Logger.error(`ERROR_FETCH_HEADS: ${error}`);

}
};

Expand Down Expand Up @@ -109,12 +97,7 @@ const useAppInit = () => {
const fetchBalances = async () => {
if (account.address && api) {
try {
const result = await _getBalance(
Chain.ETH,
api,
undefined,
account.address
);
const result = await _getBalance(Chain.ETH, api, undefined, account.address);
setEthBalance(result);
} catch (error) {
console.error("Failed to fetch ETH balance:", error);
Expand All @@ -141,6 +124,7 @@ const useAppInit = () => {
fetchBalances();
}, [account.address, selected?.address, api]);


/**
* @description get the price of the token
*
Expand All @@ -161,6 +145,10 @@ const useAppInit = () => {
}

return { fetchBalances, getTokenPrice };
};
}





export default useAppInit;
5 changes: 2 additions & 3 deletions hooks/useClaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import ethereumBridgeTuring from "@/constants/abis/ethereumBridgeTuring.json";

import { config } from "@/app/providers";
import {
fetchLatestBlockhash,
getAccountStorageProofs,
getMerkleProof,
} from "@/services/api";
Expand All @@ -27,7 +26,7 @@ import { initApi } from "@/utils/common";
import { ApiPromise } from "avail-js-sdk";

export default function useClaim() {
const { ethHead, latestBlockhash } = useLatestBlockInfo();
const { ethHead } = useLatestBlockInfo();
const { switchNetwork, activeNetworkId, activeUserAddress } = useEthWallet();
const { selected } = useAvailAccount();
const { address } = useAccount();
Expand Down Expand Up @@ -197,7 +196,7 @@ export default function useClaim() {
}

const proofs = await getAccountStorageProofs(
latestBlockhash.blockHash,
ethHead.blockHash,
executeParams.messageid,
);

Expand Down
25 changes: 4 additions & 21 deletions services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const JSONBigInt = jsonbigint({ useNativeBigInt: true });
* @returns merkleProof
*/
export const getMerkleProof = async (blockhash: string, index: number) => {
const response = await axios.get(`${appConfig.bridgeApiBaseUrl}/eth/proof/${blockhash}`, {
const response = await axios.get(`${appConfig.bridgeApiBaseUrl}/v1/eth/proof/${blockhash}`, {
params: { index },
transformResponse: [data => data]
});
Expand All @@ -35,7 +35,7 @@ export const getMerkleProof = async (blockhash: string, index: number) => {
export async function fetchAvlHead(api: ApiPromise): Promise<{
data: LatestBlockInfo["avlHead"];
}> {
const response = await fetch(`${appConfig.bridgeApiBaseUrl}/avl/head`);
const response = await fetch(`${appConfig.bridgeApiBaseUrl}/v1/avl/head`);
//TODO: Change below as response.json() does not contain endTimestamp.
const avlHead: LatestBlockInfo["avlHead"] = await response.json();
const blockHash = await api.rpc.chain.getBlockHash(avlHead.data.end);
Expand All @@ -55,28 +55,11 @@ export async function fetchAvlHead(api: ApiPromise): Promise<{
export async function fetchEthHead(): Promise<{
data: LatestBlockInfo["ethHead"];
}> {
const response = await fetch(`${appConfig.bridgeApiBaseUrl}/eth/head`);
const response = await fetch(`${appConfig.bridgeApiBaseUrl}/v1/eth/head`);
const ethHead: LatestBlockInfo["ethHead"] = await response.json();
return { data: ethHead };
}


/**
* @description Fetches the latest blockhash for a given slot
* @param slot
* @returns LatestBlockInfo["latestBlockhash"]
*/
export async function fetchLatestBlockhash(
slot: LatestBlockInfo["ethHead"]["slot"]
): Promise<{ data: LatestBlockInfo["latestBlockhash"] }> {
const response = await fetch(
`${appConfig.bridgeApiBaseUrl}/beacon/slot/${slot}`
);
const latestBlockhash: LatestBlockInfo["latestBlockhash"] =
await response.json();
return { data: latestBlockhash };
}

/**
* @description Fetches the account storage proofs for a given blockhash and messageid
* @flow ETH -> AVAIL
Expand All @@ -89,7 +72,7 @@ export async function getAccountStorageProofs(
blockhash: string,
messageid: number
) {
const response = await fetch(`${appConfig.bridgeApiBaseUrl}/avl/proof/${blockhash}/${messageid}`)
const response = await fetch(`${appConfig.bridgeApiBaseUrl}/v1/avl/proof/${blockhash}/${messageid}`)
.catch((e: any) => {
Logger.error(e);
return Response.error();
Expand Down
10 changes: 4 additions & 6 deletions stores/lastestBlockInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ export interface LatestBlockInfo {
slot: number;
timestamp: number;
timestampDiff: number;
blockNumber: number,
blockHash: string,
Comment on lines +9 to +10
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: consider using consistent semicolon usage (lines 9 and 10 have commas, others use semicolons)

};
latestBlockhash: {
blockHash: string
};
setLatestBlockhash: (latestBlockhash: LatestBlockInfo["latestBlockhash"]) => void;
setEthHead: (ethHead: LatestBlockInfo["ethHead"]) => void;
avlHead: {
data: {
Expand All @@ -27,9 +25,9 @@ export const useLatestBlockInfo = create<LatestBlockInfo>((set) => ({
slot: 0,
timestamp: 0,
timestampDiff: 0,
blockNumber: 0,
blockHash: "",
},
latestBlockhash: { blockHash: "" },
setLatestBlockhash: (latestBlockhash) => set({ latestBlockhash }),
setEthHead: (ethHead) => set({ ethHead }),
avlHead: {
data: {
Expand Down