-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from 1inch/feat/zksync-escrow-factory
feat: zksync address generation for escrow factory
- Loading branch information
Showing
4 changed files
with
244 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
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 implements EscrowFactory { | ||
private factory: EscrowFactory | ||
|
||
constructor(chainId: NetworkEnum, factoryAddress: Address) { | ||
this.factory = EscrowFactoryFacade.getFactory(chainId, factoryAddress) | ||
} | ||
|
||
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 | ||
*/ | ||
immutablesHash: string, | ||
/** | ||
* Address of escrow implementation at the same chain as `this.address` | ||
*/ | ||
implementationAddress: Address | ||
): Address { | ||
return this.factory.getEscrowAddress( | ||
immutablesHash, | ||
implementationAddress | ||
) | ||
} | ||
|
||
public getSrcEscrowAddress( | ||
/** | ||
* From `SrcEscrowCreated` event (with correct timeLock.deployedAt) | ||
*/ | ||
srcImmutables: Immutables, | ||
/** | ||
* Address of escrow implementation at the same chain as `this.address` | ||
*/ | ||
implementationAddress: Address | ||
): Address { | ||
return this.factory.getSrcEscrowAddress( | ||
srcImmutables, | ||
implementationAddress | ||
) | ||
} | ||
|
||
public getDstEscrowAddress( | ||
/** | ||
* From `SrcEscrowCreated` event | ||
*/ | ||
srcImmutables: Immutables, | ||
/** | ||
* From `SrcEscrowCreated` event | ||
*/ | ||
complement: DstImmutablesComplement, | ||
/** | ||
* Block time when event `DstEscrowCreated` produced | ||
*/ | ||
blockTime: bigint, | ||
/** | ||
* Taker from `DstEscrowCreated` event | ||
*/ | ||
taker: Address, | ||
/** | ||
* Address of escrow implementation at the same chain as `this.address` | ||
*/ | ||
implementationAddress: Address | ||
): Address { | ||
return this.getDstEscrowAddress( | ||
srcImmutables, | ||
complement, | ||
blockTime, | ||
taker, | ||
implementationAddress | ||
) | ||
} | ||
|
||
public getMultipleFillInteraction( | ||
proof: MerkleLeaf[], | ||
idx: number, | ||
secretHash: string | ||
): Interaction { | ||
return this.factory.getMultipleFillInteraction(proof, idx, secretHash) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters