Skip to content

Commit

Permalink
feat(bridge-ui): fetch transactions from relayer api (#13244)
Browse files Browse the repository at this point in the history
  • Loading branch information
shadab-taiko authored Mar 4, 2023
1 parent 5920b7e commit 9732cc0
Show file tree
Hide file tree
Showing 12 changed files with 299 additions and 100 deletions.
12 changes: 7 additions & 5 deletions packages/bridge-ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@ You can use the following values in the `.env` file to spin up the Bridge UI loc
VITE_NODE_ENV=dev
VITE_L1_RPC_URL="https://l1rpc.internal.taiko.xyz/"
VITE_L2_RPC_URL="https://l2rpc.internal.taiko.xyz/"
VITE_L1_EXPLORER_URL="https://l1explorer.internal.taiko.xyz/"
VITE_L2_EXPLORER_URL="https://l2explorer.internal.taiko.xyz/"
VITE_RELAYER_URL="https://relayer.internal.taiko.xyz/"
VITE_TEST_ERC20_ADDRESS_MAINNET="0x1B5Ccd66cc2408A0084047720167F6234Dc5498A"
VITE_TEST_ERC20_ADDRESS_MAINNET="0x3435A6180fBB1BAEc87bDC49915282BfBC328C70"
VITE_TEST_ERC20_SYMBOL_MAINNET="BULL"
VITE_TEST_ERC20_NAME_MAINNET="Bull Token"
VITE_MAINNET_CHAIN_ID=31336
VITE_TAIKO_CHAIN_ID=167001
VITE_MAINNET_CHAIN_NAME="Ethereum A2"
VITE_TAIKO_CHAIN_NAME="Taiko A2"
VITE_MAINNET_TOKEN_VAULT_ADDRESS="0xAE4C9bD0f7AE5398Df05043079596E2BF0079CE9"
VITE_MAINNET_TOKEN_VAULT_ADDRESS="0x5E506e2E0EaD3Ff9d93859A5879cAA02582f77c3"
VITE_TAIKO_TOKEN_VAULT_ADDRESS="0x0000777700000000000000000000000000000002"
VITE_MAINNET_HEADER_SYNC_ADDRESS="0xa6421A7f48498cee3aEb6428a8A2DD5fAA3AcE2f"
VITE_MAINNET_HEADER_SYNC_ADDRESS="0x9b557777Be33A8A2fE6aF93E017A0d139B439E5D"
VITE_TAIKO_HEADER_SYNC_ADDRESS="0x0000777700000000000000000000000000000001"
VITE_MAINNET_BRIDGE_ADDRESS="0x0237443359aB0b11EcDC41A7aF1C90226a88c70f"
VITE_MAINNET_BRIDGE_ADDRESS="0xAE4C9bD0f7AE5398Df05043079596E2BF0079CE9"
VITE_TAIKO_BRIDGE_ADDRESS="0x0000777700000000000000000000000000000004"
VITE_MAINNET_SIGNAL_SERVICE_ADDRESS="0x403cc7802725928652a3d116Bb1781005e2e76d3"
VITE_MAINNET_SIGNAL_SERVICE_ADDRESS="0x162A36c9821eadeCFF9669A3940b7f72d055Cd1c"
VITE_TAIKO_SIGNAL_SERVICE_ADDRESS="0x0000777700000000000000000000000000000007"
```
4 changes: 2 additions & 2 deletions packages/bridge-ui/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ export default {
],
coverageThreshold: {
global: {
statements: 95,
statements: 94,
branches: 72,
functions: 89,
functions: 88,
lines: 95,
},
},
Expand Down
54 changes: 41 additions & 13 deletions packages/bridge-ui/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
} from "./store/transactions";
import Navbar from "./components/Navbar.svelte";
import { signer } from "./store/signer";
import type { Transactioner } from "./domain/transactions";
import type { BridgeTransaction, Transactioner } from "./domain/transactions";
import { wagmiClient } from "./store/wagmi";
setupI18n({ withLocale: "en" });
Expand All @@ -53,6 +53,9 @@
import type { TokenService } from "./domain/token";
import { CustomTokenService } from "./storage/customTokenService";
import { userTokens, tokenService } from "./store/userToken";
import RelayerAPIService from "./relayer-api/service";
import type { RelayerAPI } from "./domain/relayerApi";
import { relayerApi, relayerBlockInfoMap } from "./store/relayerApi";
const providerMap: Map<number, ethers.providers.JsonRpcProvider> = new Map<
number,
Expand Down Expand Up @@ -106,8 +109,6 @@
],
});
providers.set(providerMap);
const prover: Prover = new ProofService(providerMap);
const ethBridge = new ETHBridge(prover);
Expand All @@ -133,22 +134,47 @@
providerMap
);
const relayerApiService: RelayerAPI = new RelayerAPIService(providerMap, import.meta.env.VITE_RELAYER_URL);
const tokenStore: TokenService = new CustomTokenService(
window.localStorage,
);
tokenService.set(tokenStore);
transactioner.set(storageTransactioner);
relayerApi.set(relayerApiService);
signer.subscribe(async (store) => {
if (store) {
const userAddress = await store.getAddress();
const txs = await $transactioner.GetAllByAddress(
const apiTxs = await $relayerApi.GetAllByAddress(
userAddress
);
transactions.set(txs);
const blockInfoMap = await $relayerApi.GetBlockInfo();
relayerBlockInfoMap.set(blockInfoMap)
const txs = await $transactioner.GetAllByAddress(
userAddress,
);
// const hashToApiTxsMap = new Map(apiTxs.map((tx) => {
// return [tx.hash, tx];
// }))
const updatedStorageTxs: BridgeTransaction[] = txs.filter((tx) => {
const blockInfo = blockInfoMap.get(tx.fromChainId);
if(blockInfo?.latestProcessedBlock >= tx.receipt.blockNumber) {
return false;
}
return true;
});
$transactioner.UpdateStorageByAddress(userAddress, updatedStorageTxs);
transactions.set([...updatedStorageTxs, ...apiTxs]);
const tokens = await $tokenService.GetTokens(userAddress)
userTokens.set(tokens);
Expand All @@ -160,10 +186,13 @@
store.forEach(async (tx) => {
await $signer.provider.waitForTransaction(tx.hash, 1);
successToast("Transaction completed!");
// TODO: Fix, .pop() removes the last tx but the confirmed tx is not necessarily the last one in the pendingTransactions array.
const s = store;
s.pop();
pendingTransactions.set(s);
// TODO: Do we need this?
transactions.set(
await $transactioner.GetAllByAddress(await $signer.getAddress())
);
Expand All @@ -175,24 +204,23 @@
transactions.subscribe((store) => {
if (store) {
store.forEach(async (tx) => {
const txInterval = transactionToIntervalMap.get(tx.ethersTx.hash);
const txInterval = transactionToIntervalMap.get(tx.hash);
if (txInterval) {
clearInterval(txInterval);
transactionToIntervalMap.delete(tx.ethersTx.hash);
transactionToIntervalMap.delete(tx.hash);
}
if (tx.status === MessageStatus.New) {
const provider = providerMap.get(tx.toChainId);
const interval = setInterval(async () => {
const txInterval = transactionToIntervalMap.get(tx.ethersTx.hash);
const txInterval = transactionToIntervalMap.get(tx.hash);
if (txInterval !== interval) {
clearInterval(txInterval);
transactionToIntervalMap.delete(tx.ethersTx.hash);
transactionToIntervalMap.delete(tx.hash);
}
transactionToIntervalMap.set(tx.ethersTx.hash, interval);
transactionToIntervalMap.set(tx.hash, interval);
if (!tx.msgHash) return;
const contract = new ethers.Contract(
Expand All @@ -206,9 +234,9 @@
if (messageStatus === MessageStatus.Done) {
successToast("Bridge message processed successfully");
const txOngoingInterval = transactionToIntervalMap.get(tx.ethersTx.hash);
const txOngoingInterval = transactionToIntervalMap.get(tx.hash);
clearInterval(txOngoingInterval);
transactionToIntervalMap.delete(tx.ethersTx.hash);
transactionToIntervalMap.delete(tx.hash);
}
}, 20 * 1000);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/bridge-ui/src/components/TransactionDetail.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<tr>
<td>Tx Hash</td>
<td class="text-right">
<a class="link flex items-center justify-end" target="_blank" rel="noreferrer" href={`${chains[transaction.fromChainId].explorerUrl}/tx/${transaction.ethersTx.hash}`}>
<span class="mr-2">{truncateString(transaction.ethersTx.hash)}</span>
<a class="link flex items-center justify-end" target="_blank" rel="noreferrer" href={`${chains[transaction.fromChainId].explorerUrl}/tx/${transaction.hash}`}>
<span class="mr-2">{truncateString(transaction.hash)}</span>
<ArrowTopRightOnSquare />
</a>
</td>
Expand Down
3 changes: 2 additions & 1 deletion packages/bridge-ui/src/components/form/BridgeForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@
toChainId: $toChain.id,
symbol: $token.symbol,
amountInWei: amountInWei,
ethersTx: tx,
from: tx.from,
hash: tx.hash,
status: MessageStatus.New,
};
if (!transactions) {
Expand Down
6 changes: 3 additions & 3 deletions packages/bridge-ui/src/domain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ const L1_RPC = import.meta?.env?.VITE_L1_RPC_URL ?? "https://l1rpc.internal.taik

const L2_RPC = import.meta?.env?.VITE_L2_RPC_URL ?? "https://l2rpc.internal.taiko.xyz/";

const L1_BRIDGE_ADDRESS = import.meta?.env?.VITE_MAINNET_BRIDGE_ADDRESS ?? "0x0237443359aB0b11EcDC41A7aF1C90226a88c70f";
const L1_BRIDGE_ADDRESS = import.meta?.env?.VITE_MAINNET_BRIDGE_ADDRESS ?? "0xAE4C9bD0f7AE5398Df05043079596E2BF0079CE9";

const L2_BRIDGE_ADDRESS = import.meta?.env?.VITE_TAIKO_BRIDGE_ADDRESS ?? "0x0000777700000000000000000000000000000004";

const L1_HEADER_SYNC_ADDRESS = import.meta?.env?.VITE_MAINNET_HEADER_SYNC_ADDRESS ?? "0xa6421A7f48498cee3aEb6428a8A2DD5fAA3AcE2f";
const L1_HEADER_SYNC_ADDRESS = import.meta?.env?.VITE_MAINNET_HEADER_SYNC_ADDRESS ?? "0x9b557777Be33A8A2fE6aF93E017A0d139B439E5D";

const L2_HEADER_SYNC_ADDRESS = import.meta?.env?.VITE_TAIKO_HEADER_SYNC_ADDRESS ?? "0x0000777700000000000000000000000000000001";

const L1_SIGNAL_SERVICE_ADDRESS = import.meta?.env?.VITE_MAINNET_SIGNAL_SERVICE_ADDRESS ?? "0x403cc7802725928652a3d116Bb1781005e2e76d3";
const L1_SIGNAL_SERVICE_ADDRESS = import.meta?.env?.VITE_MAINNET_SIGNAL_SERVICE_ADDRESS ?? "0x162A36c9821eadeCFF9669A3940b7f72d055Cd1c";

const L2_SIGNAL_SERVICE_ADDRESS = import.meta?.env?.VITE_TAIKO_SIGNAL_SERVICE_ADDRESS ?? "0x0000777700000000000000000000000000000007";

Expand Down
16 changes: 16 additions & 0 deletions packages/bridge-ui/src/domain/relayerApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { BridgeTransaction } from "./transactions";

export interface RelayerAPI {
GetAllByAddress(
address: string,
chainID?: number
): Promise<BridgeTransaction[]>;

GetBlockInfo(): Promise<Map<number, RelayerBlockInfo>>;
}

export type RelayerBlockInfo = {
chainId: number;
latestProcessedBlock: number;
latestBlock: number;
}
8 changes: 7 additions & 1 deletion packages/bridge-ui/src/domain/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import type { BigNumber, ethers } from "ethers";
import type { Message, MessageStatus } from "./message";

export type BridgeTransaction = {
ethersTx: ethers.Transaction;
hash: string;
from: string;
receipt?: ethers.providers.TransactionReceipt;
status: MessageStatus;
msgHash?: string;
Expand All @@ -18,4 +19,9 @@ export interface Transactioner {
address: string,
chainID?: number
): Promise<BridgeTransaction[]>;

UpdateStorageByAddress(
address: string,
txs: BridgeTransaction[]
): void;
}
Loading

0 comments on commit 9732cc0

Please sign in to comment.