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

fix(bridge-ui): fix ETH self claiming #16344

Merged
merged 8 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@
<svelte:component this={Erc20} size={20} />
</i>
{/if}
<span class={textClass}>{truncateString(value.symbol, 5)}</span>
<span class={textClass}>{truncateString(value.symbol, 6)}</span>
</div>
{/if}
</div>
Expand Down
19 changes: 17 additions & 2 deletions packages/bridge-ui/src/libs/bridge/ETHBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { bridgeService } from '$config';
import { BridgePausedError, ProcessMessageError, ReleaseError, SendMessageError } from '$libs/error';
import type { BridgeProver } from '$libs/proof';
import { isBridgePaused } from '$libs/util/checkForPausedContracts';
import { getConnectedWallet } from '$libs/util/getConnectedWallet';
import { getLogger } from '$libs/util/logger';
import { config } from '$libs/wagmi';

Expand Down Expand Up @@ -141,16 +142,30 @@ export class ETHBridge extends Bridge {
const srcChainId = Number(message.srcChainId);
const destChainId = Number(message.destChainId);

const client = await getConnectedWallet();
if (!client) throw new Error('Client not found');

const bridgeContract = await getContract({
client,
abi: bridgeABI,
address: destBridgeAddress,
});

if (messageStatus === MessageStatus.NEW) {
const proof = await this._prover.encodedSignalProof(msgHash, srcChainId, destChainId);

const estimatedGas = await bridgeContract.estimateGas.processMessage([message, proof], {
account: client.account,
});
log('Estimated gas', estimatedGas);

try {
const { request } = await simulateContract(config, {
address: destBridgeAddress,
abi: bridgeABI,
functionName: 'processMessage',
args: [message, proof],
gas: message.gasLimit,
gas: estimatedGas,
});
log('Simulate contract', request);

Expand All @@ -159,7 +174,7 @@ export class ETHBridge extends Bridge {
abi: bridgeABI,
functionName: 'processMessage',
args: [message, proof],
gas: message.gasLimit,
gas: estimatedGas,
});
return txHash;
log('Transaction hash for processMessage call', txHash);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getBlock, readContract } from '@wagmi/core';
import { hexToBigInt } from 'viem';

import { crossChainSyncABI } from '$abi';
import { routingContractsMap } from '$bridgeConfig';
Expand All @@ -18,6 +19,7 @@ export async function isTransactionProcessable(bridgeTx: BridgeTransaction) {
// Any other status that's not NEW we assume this bridge tx
// has already been processed (was processable)
// TODO: do better job here as this is to make the UI happy

if (status !== MessageStatus.NEW) return true;

const destCrossChainSyncAddress = routingContractsMap[Number(destChainId)][Number(srcChainId)].crossChainSyncAddress;
Expand All @@ -31,15 +33,15 @@ export async function isTransactionProcessable(bridgeTx: BridgeTransaction) {
abi: crossChainSyncABI,
functionName: 'getSyncedSnippet',
args: [BigInt(0)],
chainId: Number(srcChainId),
chainId: Number(destChainId),
});

const srcBlock = await getBlock(config, {
blockHash,
chainId: Number(srcChainId),
});

return srcBlock.number !== null && receipt.blockNumber <= srcBlock.number;
return srcBlock.number !== null && hexToBigInt(receipt.blockNumber) <= srcBlock.number;
} catch (error) {
return false;
}
Expand Down
5 changes: 4 additions & 1 deletion packages/bridge-ui/src/libs/bridge/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ export type RelayerMessage = {
Memo: string;
};

// viem expects a bigint, but the receipt.blockNumber is a hex string
export type ModifiedTransactionReceipt = Omit<TransactionReceipt, 'blockNumber'> & { blockNumber: Hex };

export type BridgeTransaction = {
hash: Hash;
from: Address;
Expand All @@ -71,7 +74,7 @@ export type BridgeTransaction = {
timestamp?: number;

status?: MessageStatus;
receipt?: TransactionReceipt;
receipt?: ModifiedTransactionReceipt;
msgHash?: Hash;
message?: Message;
};
Expand Down
12 changes: 6 additions & 6 deletions packages/bridge-ui/src/libs/storage/BridgeTxService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getPublicClient, waitForTransactionReceipt } from '@wagmi/core';
import { type Address, getContract, type Hash, type TransactionReceipt } from 'viem';
import { type Address, getContract, type Hash, hexToBigInt } from 'viem';

import { bridgeABI } from '$abi';
import { routingContractsMap } from '$bridgeConfig';
import { pendingTransaction, storageService } from '$config';
import { type BridgeTransaction, MessageStatus } from '$libs/bridge';
import { type BridgeTransaction, MessageStatus, type ModifiedTransactionReceipt } from '$libs/bridge';
import { isSupportedChain } from '$libs/chain';
import { FilterLogsError } from '$libs/error';
import { fetchTransactionReceipt } from '$libs/util/fetchTransactionReceipt';
Expand Down Expand Up @@ -123,15 +123,15 @@ export class BridgeTxService {
// Ignore transactions from chains not supported by the bridge
if (!isSupportedChain(Number(srcChainId))) return;

let receipt: TransactionReceipt | null = null;
let receipt: ModifiedTransactionReceipt | null = null;

if (waitForTx) {
// We might want to wait for the transaction to be mined
receipt = await waitForTransactionReceipt(config, {
receipt = (await waitForTransactionReceipt(config, {
hash,
chainId: Number(srcChainId),
timeout: pendingTransaction.waitTimeout,
});
})) as unknown as ModifiedTransactionReceipt;
} else {
// Returns the transaction receipt for hash or null
// if the transaction has not been mined.
Expand All @@ -152,7 +152,7 @@ export class BridgeTxService {
userAddress: address,
srcChainId: Number(srcChainId),
destChainId: Number(destChainId),
blockNumber: Number(receipt.blockNumber),
blockNumber: Number(hexToBigInt(receipt.blockNumber)),
});
} catch (error) {
//TODO: handle error
Expand Down
2 changes: 1 addition & 1 deletion packages/bridge-ui/src/libs/util/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function renderBalance(balance: Maybe<GetBalanceReturnType>) {
if (!balance) return '0.00';
// if (typeof balance === 'bigint') return balance.toString();
const maxlength = Number(balance.formatted) < 0.000001 ? balance.decimals : 6;
return `${truncateString(balance.formatted, maxlength, '')} ${truncateString(balance.symbol, 5)}`;
return `${truncateString(balance.formatted, maxlength, '')} ${truncateString(balance.symbol, 7)}`;
}

export function renderEthBalance(balance: bigint, maxlength = 8): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { chains } from '$libs/chain';

export async function fetchTransactionReceipt(transactionHash: Hash, chainId: number) {
try {
const nodeUrl = chains.find((c) => c.id === chainId)?.rpcUrls?.public?.http[0];
const nodeUrl = chains.find((c) => c.id === chainId)?.rpcUrls?.default?.http[0];
if (!nodeUrl) {
throw new Error('Node URL not found');
}
Expand Down
Loading