From 0063a52a298f1df89581569fdcc11cd6bcb68903 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Sat, 10 Aug 2024 13:01:21 +0300 Subject: [PATCH] fix: TimeLock serialization/deserialization (#11) --- .../time-locks/time-locks.spec.ts | 18 ++++++++++++++ .../time-locks/time-locks.ts | 24 +++++++++---------- src/immutables/immutables.spec.ts | 16 ++++++++----- 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/cross-chain-order/time-locks/time-locks.spec.ts b/src/cross-chain-order/time-locks/time-locks.spec.ts index d29c21e..f045fc6 100644 --- a/src/cross-chain-order/time-locks/time-locks.spec.ts +++ b/src/cross-chain-order/time-locks/time-locks.spec.ts @@ -110,4 +110,22 @@ describe('TimeLocks', () => { DstStage.PrivateCancellation ) }) + + it('Should set deployedAt', () => { + const timeLock = TimeLocks.new({ + srcWithdrawal: 0n, // no finality lock for test + srcPublicWithdrawal: 120n, // 2m for private withdrawal + srcCancellation: 121n, // 1sec public withdrawal + srcPublicCancellation: 122n, // 1sec private cancellation + dstWithdrawal: 0n, // no finality lock for test + dstPublicWithdrawal: 120n, // 2m private withdrawal + dstCancellation: 121n // 1sec public withdrawal + }) + + timeLock.setDeployedAt(0x66b5e815n) + + expect(timeLock.build().toString(16)).toEqual( + '66b5e8150000007900000078000000000000007a000000790000007800000000' + ) + }) }) diff --git a/src/cross-chain-order/time-locks/time-locks.ts b/src/cross-chain-order/time-locks/time-locks.ts index afc3c84..ea04d3e 100644 --- a/src/cross-chain-order/time-locks/time-locks.ts +++ b/src/cross-chain-order/time-locks/time-locks.ts @@ -23,14 +23,14 @@ export class TimeLocks { static Web3Type = 'uint256' protected constructor( - private _deployedAt: bigint, private readonly _srcWithdrawal: bigint, private readonly _srcPublicWithdrawal: bigint, private readonly _srcCancellation: bigint, private readonly _srcPublicCancellation: bigint, private readonly _dstWithdrawal: bigint, private readonly _dstPublicWithdrawal: bigint, - private readonly _dstCancellation: bigint + private readonly _dstCancellation: bigint, + private _deployedAt: bigint ) { assert( _deployedAt <= UINT_32_MAX, @@ -128,14 +128,14 @@ export class TimeLocks { dstCancellation: bigint }): TimeLocks { return new TimeLocks( - 0n, params.srcWithdrawal, params.srcPublicWithdrawal, params.srcCancellation, params.srcPublicCancellation, params.dstWithdrawal, params.dstPublicWithdrawal, - params.dstCancellation + params.dstCancellation, + 0n ) } @@ -146,7 +146,7 @@ export class TimeLocks { return valBN.getMask( new BitMask(BigInt(i) * 32n, BigInt(i + 1) * 32n) ).value - }) as [bigint, bigint, bigint, bigint, bigint, bigint, bigint, bigint] // ts cannot infer that we have exactly 8 elements in the array after map + }) as [bigint, bigint, bigint, bigint, bigint, bigint, bigint, bigint] // ts can not infer that result is 8 elements return new TimeLocks(...params) } @@ -154,14 +154,14 @@ export class TimeLocks { public build(): bigint { return [ this.deployedAt, - this._srcWithdrawal, - this._srcPublicWithdrawal, - this._srcCancellation, - this._srcPublicCancellation, - this._dstWithdrawal, + this._dstCancellation, this._dstPublicWithdrawal, - this._dstCancellation - ].reduceRight((acc, el) => (acc << 32n) | el) + this._dstWithdrawal, + this._srcPublicCancellation, + this._srcCancellation, + this._srcPublicWithdrawal, + this._srcWithdrawal + ].reduce((acc, el) => (acc << 32n) | el) } public setDeployedAt(time: bigint): this { diff --git a/src/immutables/immutables.spec.ts b/src/immutables/immutables.spec.ts index 3c77a8b..7a909eb 100644 --- a/src/immutables/immutables.spec.ts +++ b/src/immutables/immutables.spec.ts @@ -4,7 +4,6 @@ import {TimeLocks} from '../cross-chain-order/time-locks/time-locks' import {HashLock} from '../cross-chain-order/hash-lock' describe('Immutables', function () { - // values from contract tests const immutables = Immutables.new({ orderHash: '0x47bb61560b511b196788026f8de50c213051732f2c4abfeb855f1bdf0825aa1f', @@ -22,14 +21,19 @@ describe('Immutables', function () { ), amount: 150000000000000000n, safetyDeposit: 30000000000000000n, - timeLocks: - TimeLocks.fromBigInt( - 24263952003825210752747571682508896791736523869669408224653997988904961n - ) + timeLocks: TimeLocks.new({ + srcWithdrawal: 120n, + srcPublicWithdrawal: 500n, + srcCancellation: 1020n, + srcPublicCancellation: 1530n, + dstWithdrawal: 300n, + dstPublicWithdrawal: 540n, + dstCancellation: 900n + }).setDeployedAt(1n) }) it('Should calc correct hash of immutables', function () { expect(immutables.hash()).toEqual( - '0xa0064b1f7adf195756a63a5df5d7ce6dc9fc327e7a6b0ecdd6758ac791460abd' + '0x447dfe500c4f14a12f9f7d7029c2f0db155248ea905cfea12296ea6f05e6fe58' ) })