Skip to content

Commit

Permalink
feat: helpers for multiple fills
Browse files Browse the repository at this point in the history
  • Loading branch information
vbrvk committed Aug 12, 2024
1 parent 77fc07f commit a842953
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
15 changes: 15 additions & 0 deletions src/cross-chain-order/hash-lock/hash-lock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,19 @@ describe('HashLock', () => {
getBytesCount(HashLock.forMultipleFills(leaves).toString())
).toEqual(32n)
})

it('should return proof', () => {
const secrets = [
'0x6466643931343237333333313437633162386632316365646666323931643738',
'0x3131353932633266343034343466363562333230313837353438356463616130',
'0x6634376135663837653765303462346261616566383430303662303336386635'
]

const leaves = HashLock.getMerkleLeaves(secrets)

expect(HashLock.getProof(leaves, 0)).toEqual([
'0x3009d711fa0454e4a4f0d553fcfa67f20e0d67571d7e06f2105814c6b123ba55',
'0x70e897a17a55b03df83541f6420507b3482da14f2489ef7ea4d9a94cf30d1c06'
])
})
})
6 changes: 5 additions & 1 deletion src/cross-chain-order/hash-lock/hash-lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export class HashLock {
)
}

public static getProof(leaves: MerkleLeaf[], idx: number): MerkleLeaf[] {
return SimpleMerkleTree.of(leaves).getProof(idx) as MerkleLeaf[]
}

public static fromString(value: string): HashLock {
assert(
isHexBytes(value) && getBytesCount(value) === 32n,
Expand Down Expand Up @@ -70,4 +74,4 @@ export class HashLock {
}
}

type MerkleLeaf = string & {_tag: 'MerkleLeaf'}
export type MerkleLeaf = string & {_tag: 'MerkleLeaf'}
28 changes: 24 additions & 4 deletions src/escrow-factory/escrow-factory.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {Address} from '@1inch/fusion-sdk'
import {getCreate2Address, keccak256} from 'ethers'
import {Address, Interaction} from '@1inch/fusion-sdk'
import {AbiCoder, getCreate2Address, keccak256} from 'ethers'
import {getBytesCount, isHexBytes, trim0x} from '@1inch/byte-utils'
import assert from 'assert'
import {Immutables} from '../immutables'
import {DstImmutablesComplement} from '../immutables/dst-immutables-complement'
import {Immutables, DstImmutablesComplement} from '../immutables'
import {MerkleLeaf} from '../cross-chain-order/hash-lock/hash-lock'

export class EscrowFactory {
constructor(public readonly address: Address) {}
Expand Down Expand Up @@ -103,4 +103,24 @@ export class EscrowFactory {
implementationAddress
)
}

public getMultipleFillInteraction(
proof: MerkleLeaf[],
idx: number,
secretHash: string
): Interaction {
return new Interaction(
this.address,
AbiCoder.defaultAbiCoder().encode(
[
`(
bytes32[] proof,
uint256 idx,
bytes32 secretHash,
)`
],
[{proof, idx, secretHash}]
)
)
}
}

0 comments on commit a842953

Please sign in to comment.