Skip to content

Commit

Permalink
fix: correct address calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
vbrvk committed Oct 31, 2024
1 parent cac7ad6 commit 740a09e
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 178 deletions.
107 changes: 47 additions & 60 deletions src/escrow-factory/escrow-factory-facade.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
import {Address, NetworkEnum} from '@1inch/fusion-sdk'
import {Address, Interaction, NetworkEnum} from '@1inch/fusion-sdk'
import {EscrowFactory} from './escrow-factory'
import {EscrowFactoryZksync} from './escrow-factory-zksync'
import {DstImmutablesComplement, Immutables} from '../immutables'
import {MerkleLeaf} from '../cross-chain-order/hash-lock/hash-lock'

export class EscrowFactoryFacade {
export class EscrowFactoryFacade implements EscrowFactory {
private factory: EscrowFactory

constructor(factoryAddress: Address) {
this.factory = new EscrowFactory(factoryAddress)
constructor(chainId: NetworkEnum, factoryAddress: Address) {
this.factory = EscrowFactoryFacade.getFactory(chainId, factoryAddress)
}

public getEscrowAddressByChain(
/**
* chain id
*/
get address(): Address {
return this.factory.address
}

public static getFactory(
chainId: NetworkEnum,
factoryAddress: Address
): EscrowFactory {
switch (chainId) {
case NetworkEnum.ZKSYNC:
return new EscrowFactoryZksync(factoryAddress)
default:
return new EscrowFactory(factoryAddress)
}
}

public getEscrowAddress(
/**
* @see Immutables.hash
*/
Expand All @@ -23,25 +37,13 @@ export class EscrowFactoryFacade {
*/
implementationAddress: Address
): Address {
switch (chainId) {
case NetworkEnum.ZKSYNC:
return this.factory.getZkEscrowAddress(
immutablesHash,
implementationAddress
)
default:
return this.factory.getEscrowAddress(
immutablesHash,
implementationAddress
)
}
return this.factory.getEscrowAddress(
immutablesHash,
implementationAddress
)
}

public getSrcEscrowAddressByChain(
/**
* chain id
*/
chainId: NetworkEnum,
public getSrcEscrowAddress(
/**
* From `SrcEscrowCreated` event (with correct timeLock.deployedAt)
*/
Expand All @@ -51,25 +53,13 @@ export class EscrowFactoryFacade {
*/
implementationAddress: Address
): Address {
switch (chainId) {
case NetworkEnum.ZKSYNC:
return this.factory.getZkSrcEscrowAddress(
srcImmutables,
implementationAddress
)
default:
return this.factory.getSrcEscrowAddress(
srcImmutables,
implementationAddress
)
}
return this.factory.getSrcEscrowAddress(
srcImmutables,
implementationAddress
)
}

public getDstEscrowAddressByChain(
/**
* chain id
*/
chainId: NetworkEnum,
public getDstEscrowAddress(
/**
* From `SrcEscrowCreated` event
*/
Expand All @@ -91,23 +81,20 @@ export class EscrowFactoryFacade {
*/
implementationAddress: Address
): Address {
switch (chainId) {
case NetworkEnum.ZKSYNC:
return this.factory.getZkDstEscrowAddress(
srcImmutables,
complement,
blockTime,
taker,
implementationAddress
)
default:
return this.factory.getDstEscrowAddress(
srcImmutables,
complement,
blockTime,
taker,
implementationAddress
)
}
return this.getDstEscrowAddress(
srcImmutables,
complement,
blockTime,
taker,
implementationAddress
)
}

public getMultipleFillInteraction(
proof: MerkleLeaf[],
idx: number,
secretHash: string
): Interaction {
return this.factory.getMultipleFillInteraction(proof, idx, secretHash)
}
}
56 changes: 56 additions & 0 deletions src/escrow-factory/escrow-factory-zksync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {Address} from '@1inch/fusion-sdk'
import {AbiCoder, concat, keccak256} from 'ethers'
import {add0x, getBytesCount, isHexBytes, trim0x} from '@1inch/byte-utils'
import assert from 'assert'
import {EscrowFactory} from './escrow-factory'

export class EscrowFactoryZksync extends EscrowFactory {
private static create2Prefix =
'0x2020dba91b30cc0006188af794c2fb30dd8520db7e2c088b7fc7c103c00ca494'

/**
* ZkSync proxy bytecode do not depends on implementation address
*
* @see proxy example - https://explorer.zksync.io/address/0xd5317Ded4FBb98526AdD35A15d63cFBFB929efc7
*/
private static minimalProxyBytecodeHash =
'0x01000035492ceb24a47d861a8fd7e65b117f2eb5bf6453e191ba770c70ca7f43'

/**
* Calculate address of escrow contract in ZkSync Era
*
* @return escrow address at same the chain as `this.address`
*/
public override getEscrowAddress(
/**
* @see Immutables.hash
*/
immutablesHash: string,
/**
* Address of escrow implementation at the same chain as `this.address`
*/
implementationAddress: Address
): Address {
assert(
isHexBytes(immutablesHash) && getBytesCount(immutablesHash) === 32n,
'invalid hash'
)

const inputHash = keccak256(
AbiCoder.defaultAbiCoder().encode(
['address'],
[implementationAddress.toString()]
)
)

const concatenatedData = concat([
EscrowFactoryZksync.create2Prefix,
add0x(trim0x(this.address.toString()).padStart(64, '0')),
immutablesHash,
EscrowFactoryZksync.minimalProxyBytecodeHash,
inputHash
])

return new Address(add0x(keccak256(concatenatedData).slice(-40)))
}
}
132 changes: 105 additions & 27 deletions src/escrow-factory/escrow-factory.spec.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import {Address} from '@1inch/fusion-sdk'
import {Address, NetworkEnum} from '@1inch/fusion-sdk'
import {keccak256} from 'ethers'
import {EscrowFactoryFacade} from './escrow-factory-facade'
import {EscrowFactoryZksync} from './escrow-factory-zksync'
import {Immutables} from '../immutables'
import {HashLock, TimeLocks} from '../cross-chain-order'

describe('EscrowAddressFacade', () => {
it('Should correct calc src/dst address for Ethereum', () => {
const facade = new EscrowFactoryFacade(Address.fromBigInt(1n))
const facade = new EscrowFactoryFacade(
NetworkEnum.ETHEREUM,
Address.fromBigInt(1n)
)
const immutablesHash = keccak256('0x')
const srcImplAddress = Address.fromBigInt(1n)
const dstImplAddress = Address.fromBigInt(2n)

const srcAddress = facade.getEscrowAddressByChain(
1,
const srcAddress = facade.getEscrowAddress(
immutablesHash,
srcImplAddress
)
const dstAddress = facade.getEscrowAddressByChain(
1,
const dstAddress = facade.getEscrowAddress(
immutablesHash,
dstImplAddress
)
Expand All @@ -29,29 +33,103 @@ describe('EscrowAddressFacade', () => {
)
})

it('Should correct calc zk src/dst address for zkSync', () => {
const facade = new EscrowFactoryFacade(Address.fromBigInt(1n))
const immutablesHash = keccak256('0x')
const srcImplAddress = Address.fromBigInt(1n)
const dstImplAddress = Address.fromBigInt(2n)

const srcAddress = facade.getEscrowAddressByChain(
324,
immutablesHash,
srcImplAddress
)
const dstAddress = facade.getEscrowAddressByChain(
324,
immutablesHash,
dstImplAddress
describe('zkSync', () => {
const facade = new EscrowFactoryFacade(
NetworkEnum.ZKSYNC,
Address.fromBigInt(1n)
)
it('Should calc correct address by immutables hash', () => {
const immutablesHash = keccak256('0x')
const srcImplAddress = Address.fromBigInt(1n)
const dstImplAddress = Address.fromBigInt(2n)

expect(srcAddress).toEqual(
new Address('0x48f33ed21a3ab24b699fc2f709266f86b0c5714e')
)
const srcAddress = facade.getEscrowAddress(
immutablesHash,
srcImplAddress
)
const dstAddress = facade.getEscrowAddress(
immutablesHash,
dstImplAddress
)

expect(dstAddress).toEqual(
new Address('0x68460df9d1f08e7eaff280cb880d9be59b2143b7')
)
expect(srcAddress).toEqual(
new Address('0x48f33ed21a3ab24b699fc2f709266f86b0c5714e')
)

expect(dstAddress).toEqual(
new Address('0x68460df9d1f08e7eaff280cb880d9be59b2143b7')
)
})

it('Should calc correct src address from immutables', () => {
const immutables = Immutables.new({
maker: new Address(
'0x04d3b2c70208f3fb196affef78080b3cc05ee1cb'
),
taker: new Address(
'0x9c4dffb4f7e8217a8ac0555d67e125f8769284ba'
),
token: new Address(
'0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'
),
amount: 99345341n,
hashLock: HashLock.fromString(
'0x939dbeb956ac9369de8b1acaaa78a173620c2275fba501f8340b91cbceabdbf1'
),
orderHash:
'0x8ed7de0668228e00103038a7fc6a19c933d56fca27b154a3e532ebeb4a5c07bd',
timeLocks:
TimeLocks.fromBigInt(
46545443980783778519496226194286874737220106048808714334632784681655911579684n
),
safetyDeposit: 2474844692460000n
})

const factory = new EscrowFactoryZksync(
new Address('0x584aEaB186D81dbB52a8a14820c573480c3d4773')
)

expect(
factory.getSrcEscrowAddress(
immutables,
new Address('0xddc60c7babfc55d8030f51910b157e179f7a41fc')
)
).toEqual(new Address('0x98f0a945348c031f85164562bff61eb08a0629df'))
})
it('Should calc correct dst address from immutables', () => {
const immutables = Immutables.new({
maker: new Address(
'0x04d3b2c70208f3fb196affef78080b3cc05ee1cb'
),
taker: new Address(
'0x9c4dffb4f7e8217a8ac0555d67e125f8769284ba'
),
token: new Address(
'0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'
),
amount: 99345341n,
hashLock: HashLock.fromString(
'0x939dbeb956ac9369de8b1acaaa78a173620c2275fba501f8340b91cbceabdbf1'
),
orderHash:
'0x8ed7de0668228e00103038a7fc6a19c933d56fca27b154a3e532ebeb4a5c07bd',
timeLocks:
TimeLocks.fromBigInt(
46545443980783778519496226194286874737220106048808714334632784681655911579684n
),
safetyDeposit: 2474844692460000n
})

const factory = new EscrowFactoryZksync(
new Address('0x584aEaB186D81dbB52a8a14820c573480c3d4773')
)

expect(
factory.getEscrowAddress(
immutables.hash(),
new Address('0xdc4ccc2fc2475d0ed3fddd563c44f2bf6a3900c9')
)
).toEqual(new Address('0x93d8a039c23cffd1067e02edbdd28189ac679a39'))
})
})
})
Loading

0 comments on commit 740a09e

Please sign in to comment.