-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
getInvocationDelays for tx util and test
- Loading branch information
1 parent
806352d
commit afd580d
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
packages/bridge-ui/src/libs/bridge/getInvocationDelayForTx.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
56 changes: 56 additions & 0 deletions
56
packages/bridge-ui/src/tests/libs/bridge/getInvocationDelayForTx.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}); | ||
}); | ||
}); |