Skip to content

Commit

Permalink
getInvocationDelays for tx util and test
Browse files Browse the repository at this point in the history
  • Loading branch information
KorbinianK committed Mar 12, 2024
1 parent 806352d commit afd580d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
41 changes: 41 additions & 0 deletions packages/bridge-ui/src/libs/bridge/getInvocationDelayForTx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { BridgeTransaction } from '$libs/bridge/types';
import { getLatestBlockTimestamp } from '$libs/util/getLatestBlockTimestamp';
import { getLogger } from '$libs/util/logger';

import { getInvocationDelaysForDestBridge } from './getInvocationDelaysForDestBridge';
import { getProofReceiptForMsgHash } from './getProofReceiptForMsgHash';

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

export const getInvoationDelayForTx = async (tx: BridgeTransaction) => {
log('getInvoationDelayForTx', 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);

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

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

log('time since last claim', latestBlockTimestamp - provenAt);
const delays = {
preferredDelay: delayForPreferred - (latestBlockTimestamp - provenAt),
notPreferredDelay: delayForNotPreferred - (latestBlockTimestamp - provenAt),
};
log('remaining delays', delays);
return delays;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { getInvoationDelayForTx } from '$libs/bridge/getInvocationDelayForTx';
import { getInvocationDelaysForDestBridge } from '$libs/bridge/getInvocationDelaysForDestBridge';
import { getProofReceiptForMsgHash } from '$libs/bridge/getProofReceiptForMsgHash';
import { getLatestBlockTimestamp } from '$libs/util/getLatestBlockTimestamp';
import { ALICE, L1_CHAIN_ID, L2_CHAIN_ID, MOCK_BRIDGE_TX_1 } from '$mocks';

vi.mock('$customToken', () => {
const mockERC20 = {
name: 'MockERC20',
addresses: { '1': '0x123' },
symbol: 'MTF',
decimals: 18,
type: 'ERC20',
};
return {
customToken: [mockERC20],
};
});

vi.mock('$libs/bridge/getInvocationDelaysForDestBridge');
vi.mock('$libs/bridge/getProofReceiptForMsgHash');
vi.mock('$libs/util/getLatestBlockTimestamp');

describe('getInvocationDelayForTx()', () => {
it('should return the invocation delays for the transaction', async () => {
const MOCK_BLOCK_TIMESTAMP = 1632787200n;
const MOCK_RECIEPT_TIMESTAMP = 1632787200n - 200n;
const PREFERRED_CLAIMER_DELAY = 100n;
const NOT_PREFERRED_CLAIMER_DELAY = 200n;
const MOCK_DELAYS = [PREFERRED_CLAIMER_DELAY, NOT_PREFERRED_CLAIMER_DELAY] as const;

//Given
vi.mocked(getInvocationDelaysForDestBridge).mockResolvedValue(MOCK_DELAYS);
vi.mocked(getLatestBlockTimestamp).mockResolvedValue(MOCK_BLOCK_TIMESTAMP);
vi.mocked(getProofReceiptForMsgHash).mockResolvedValue([MOCK_RECIEPT_TIMESTAMP, ALICE]);

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

//Then
expect(result).toStrictEqual({
preferredDelay: PREFERRED_CLAIMER_DELAY - (MOCK_BLOCK_TIMESTAMP - MOCK_RECIEPT_TIMESTAMP),
notPreferredDelay: NOT_PREFERRED_CLAIMER_DELAY - (MOCK_BLOCK_TIMESTAMP - MOCK_RECIEPT_TIMESTAMP),
});
expect(getInvocationDelaysForDestBridge).toHaveBeenCalledWith({
srcChainId: BigInt(L1_CHAIN_ID),
destChainId: BigInt(L2_CHAIN_ID),
});
expect(getLatestBlockTimestamp).toHaveBeenCalledWith(BigInt(L2_CHAIN_ID));
expect(getProofReceiptForMsgHash).toHaveBeenCalledWith({
msgHash: MOCK_BRIDGE_TX_1.msgHash,
destChainId: BigInt(L2_CHAIN_ID),
srcChainId: BigInt(L1_CHAIN_ID),
});
});
});

0 comments on commit afd580d

Please sign in to comment.