Skip to content

Commit

Permalink
fix: TimeLock serialization/deserialization (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
vbrvk authored Aug 10, 2024
1 parent ccd6d61 commit 0063a52
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 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
16 changes: 10 additions & 6 deletions src/immutables/immutables.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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'
)
})

Expand Down

0 comments on commit 0063a52

Please sign in to comment.