Skip to content

Commit

Permalink
small util updates and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
KorbinianK committed Mar 17, 2024
1 parent 370458a commit e1bfe5d
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 32 deletions.
30 changes: 22 additions & 8 deletions packages/bridge-ui/src/libs/bridge/getInvocationDelayForTx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { BridgeTransaction } from '$libs/bridge/types';
import { NoDelaysForBridgeError } from '$libs/error';
import { getLatestBlockTimestamp } from '$libs/util/getLatestBlockTimestamp';
import { getLogger } from '$libs/util/logger';

Expand All @@ -7,27 +8,40 @@ import { getProofReceiptForMsgHash } from './getProofReceiptForMsgHash';

const log = getLogger('bridge:getInvocationDelayForTx');

export const getInvoationDelayForTx = async (tx: BridgeTransaction) => {
log('getInvoationDelayForTx', tx);
export const getInvocationDelayForTx = async (tx: BridgeTransaction) => {
log('getInvocationDelayForTx', tx);

const invocationDelays = await getInvocationDelaysForDestBridge({
srcChainId: tx.srcChainId,
destChainId: tx.destChainId,
});
const delayForPreferred = invocationDelays[0];
const delayForNotPreferred = invocationDelays[1];

log('invocationDelays', invocationDelays);

const latestBlockTimestamp = await getLatestBlockTimestamp(tx.destChainId);
log('latestBlockTimestamp', latestBlockTimestamp);
if (invocationDelays[0] === 0n) {
throw new NoDelaysForBridgeError('Destination chain does not have delays');
}

const proofReciept = await getProofReceiptForMsgHash({
msgHash: tx.msgHash,
destChainId: tx.destChainId,
srcChainId: tx.srcChainId,
});

if (proofReciept[0] === 0n) {
// No proof receipt found, no delay (yet)
return {
preferredDelay: 0n,
notPreferredDelay: 0n,
};
}

const delayForPreferred = invocationDelays[0];
const delayForNotPreferred = invocationDelays[1];

log('invocationDelays', invocationDelays);

const latestBlockTimestamp = await getLatestBlockTimestamp(tx.destChainId);
log('latestBlockTimestamp', latestBlockTimestamp);

const provenAt = proofReciept[0];
// const provenBy = proofReciept[1];

Expand Down
10 changes: 9 additions & 1 deletion packages/bridge-ui/src/libs/bridge/isTransactionProcessable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { hexToBigInt, keccak256, toBytes } from 'viem';
import { signalServiceAbi } from '$abi';
import { routingContractsMap } from '$bridgeConfig';
import { chains } from '$libs/chain';
import { getLogger } from '$libs/util/logger';
import { config } from '$libs/wagmi';

import { type BridgeTransaction, MessageStatus } from './types';
const log = getLogger('libs:bridge:isTransactionProcessable');

export async function isTransactionProcessable(bridgeTx: BridgeTransaction) {
const { receipt, message, srcChainId, destChainId, msgStatus } = bridgeTx;
Expand Down Expand Up @@ -39,7 +41,13 @@ export async function isTransactionProcessable(bridgeTx: BridgeTransaction) {
const latestSyncedblock = syncedChainData[0];

const synced = latestSyncedblock >= hexToBigInt(receipt.blockNumber);

log('isTransactionProcessable', {
from: srcChainId,
to: destChainId,
latestSyncedblock,
receiptBlockNumber: hexToBigInt(receipt.blockNumber),
synced,
});
return synced;
} catch (error) {
console.error('Error checking if transaction is processable', error);
Expand Down
14 changes: 12 additions & 2 deletions packages/bridge-ui/src/libs/bridge/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Address, Hash, Hex, TransactionReceipt, WalletClient } from 'viem';
import type { Address, GetContractReturnType, Hash, Hex, TransactionReceipt, WalletClient } from 'viem';

import type { bridgeAbi } from '$abi';
import type { ChainID } from '$libs/chain';
import type { TokenType } from '$libs/token';

Expand Down Expand Up @@ -186,9 +187,18 @@ export type RequireApprovalArgs = {
export type ClaimArgs = {
bridgeTx: BridgeTransaction;
wallet: WalletClient;
lastAttempt?: boolean; // used for retrying
};

export type ReleaseArgs = ClaimArgs;
export type ProcessMessageType = ClaimArgs & {
bridgeContract: GetContractReturnType<typeof bridgeAbi, WalletClient>;
client: WalletClient;
};

export type RetryMessageArgs = ProcessMessageType;

export type ReleaseArgs = ProcessMessageType;

export interface Bridge {
estimateGas(args: BridgeArgs): Promise<bigint>;
bridge(args: BridgeArgs): Promise<Hex>;
Expand Down
4 changes: 4 additions & 0 deletions packages/bridge-ui/src/libs/error/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,7 @@ export class IpfsError extends Error {
export class ClientError extends Error {
name = 'ClientError';
}

export class NoDelaysForBridgeError extends Error {
name = 'NoDelaysForBridgeError';
}
19 changes: 0 additions & 19 deletions packages/bridge-ui/src/libs/relayer/RelayerAPIService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,22 +291,3 @@ const _eventToTokenType = (eventType: RelayerEventType): TokenType => {
return TokenType.ETH;
}
};

// function _checkType(bridgeTx: BridgeTransaction): TokenType {
// const to = bridgeTx.message?.to;

// switch (to?.toLowerCase()) {
// case routingContractsMap[Number(bridgeTx.destChainId)][Number(bridgeTx.srcChainId)].erc20VaultAddress.toLowerCase():
// return TokenType.ERC20;
// case routingContractsMap[Number(bridgeTx.destChainId)][
// Number(bridgeTx.srcChainId)
// ].erc721VaultAddress.toLowerCase():
// return TokenType.ERC721;
// case routingContractsMap[Number(bridgeTx.destChainId)][
// Number(bridgeTx.srcChainId)
// ].erc1155VaultAddress.toLowerCase():
// return TokenType.ERC1155;
// default:
// return TokenType.ETH;
// }
// }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getInvoationDelayForTx } from '$libs/bridge/getInvocationDelayForTx';
import { getInvocationDelayForTx } from '$libs/bridge/getInvocationDelayForTx';
import { getInvocationDelaysForDestBridge } from '$libs/bridge/getInvocationDelaysForDestBridge';
import { getProofReceiptForMsgHash } from '$libs/bridge/getProofReceiptForMsgHash';
import { getLatestBlockTimestamp } from '$libs/util/getLatestBlockTimestamp';
Expand Down Expand Up @@ -35,7 +35,7 @@ describe('getInvocationDelayForTx()', () => {
vi.mocked(getProofReceiptForMsgHash).mockResolvedValue([MOCK_RECIEPT_TIMESTAMP, ALICE]);

//When
const result = await getInvoationDelayForTx(MOCK_BRIDGE_TX_1);
const result = await getInvocationDelayForTx(MOCK_BRIDGE_TX_1);

//Then
expect(result).toStrictEqual({
Expand Down

0 comments on commit e1bfe5d

Please sign in to comment.