Skip to content

Commit

Permalink
fix: TimeLock serialization/deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
vbrvk committed Aug 10, 2024
1 parent ccd6d61 commit 8e74b58
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
18 changes: 18 additions & 0 deletions src/cross-chain-order/time-locks/time-locks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
})
})
24 changes: 12 additions & 12 deletions src/cross-chain-order/time-locks/time-locks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
)
}

Expand All @@ -146,22 +146,22 @@ 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)
}

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 {
Expand Down

0 comments on commit 8e74b58

Please sign in to comment.