-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bridge-ui-v2): Claim & Release (#14267)
- Loading branch information
1 parent
723eb60
commit 6c6089e
Showing
22 changed files
with
555 additions
and
51 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 |
---|---|---|
@@ -1,19 +1,21 @@ | ||
export const recommentProcessingFee = { | ||
ethGasLimit: BigInt(900000), | ||
erc20NotDeployedGasLimit: BigInt(3100000), | ||
erc20DeployedGasLimit: BigInt(1100000), | ||
ethGasLimit: BigInt(900_000), | ||
erc20NotDeployedGasLimit: BigInt(3_100_000), | ||
erc20DeployedGasLimit: BigInt(1_100_000), | ||
}; | ||
|
||
export const processingFeeComponent = { | ||
closingDelayOptionClick: 300, | ||
intervalComputeRecommendedFee: 20000, | ||
intervalComputeRecommendedFee: 20_000, | ||
}; | ||
|
||
export const bridge = { | ||
noOwnerGasLimit: BigInt(140000), | ||
noTokenDeployedGasLimit: BigInt(3000000), | ||
export const bridgeService = { | ||
noOwnerGasLimit: BigInt(140_000), | ||
noTokenDeployedGasLimit: BigInt(3_000_000), | ||
erc20GasLimitThreshold: BigInt(2_500_000), | ||
unpredictableGasLimit: BigInt(1_000_000), | ||
}; | ||
|
||
export const pendingTransaction = { | ||
waitTimeout: 300000, | ||
waitTimeout: 300_000, | ||
}; |
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
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
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
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,115 @@ | ||
import { getContract } from '@wagmi/core'; | ||
import type { Hash } from 'viem'; | ||
|
||
import { bridgeABI } from '$abi'; | ||
import { chainContractsMap } from '$libs/chain'; | ||
import { MessageStatusError, WrongChainError, WrongOwnerError } from '$libs/error'; | ||
import type { BridgeProver } from '$libs/proof'; | ||
import { getLogger } from '$libs/util/logger'; | ||
|
||
import { type BridgeArgs, type ClaimArgs, MessageStatus, type ReleaseArgs } from './types'; | ||
|
||
const log = getLogger('bridge:Bridge'); | ||
|
||
export abstract class Bridge { | ||
protected readonly _prover: BridgeProver; | ||
|
||
constructor(prover: BridgeProver) { | ||
this._prover = prover; | ||
} | ||
|
||
/** | ||
* We are gonna run some common checks here: | ||
* 1. Check that the wallet is connected to the destination chain | ||
* 2. Check that the message is owned by the user | ||
* 3. Check that the message has not been claimed already | ||
* 4. Check that the message has not failed | ||
* | ||
* Important: wallet must be connected to the destination chain | ||
*/ | ||
protected async beforeClaiming({ msgHash, message, wallet }: ClaimArgs) { | ||
const destChainId = Number(message.destChainId); | ||
// Are we connected to the destination chain? | ||
if (wallet.chain.id !== destChainId) { | ||
throw new WrongChainError('wallet must be connected to the destination chain'); | ||
} | ||
|
||
const { owner } = message; | ||
const userAddress = wallet.account.address; | ||
// Are we the owner of the message? | ||
if (owner.toLowerCase() !== userAddress.toLowerCase()) { | ||
throw new WrongOwnerError('user cannot process this as it is not their message'); | ||
} | ||
|
||
const destBridgeAddress = chainContractsMap[wallet.chain.id].bridgeAddress; | ||
|
||
const destBridgeContract = getContract({ | ||
address: destBridgeAddress, | ||
abi: bridgeABI, | ||
|
||
// We are gonna resuse this contract to actually process the message | ||
// so we'll need to sign the transaction | ||
walletClient: wallet, | ||
}); | ||
|
||
const messageStatus: MessageStatus = await destBridgeContract.read.getMessageStatus([msgHash]); | ||
|
||
log(`Claiming message with status ${messageStatus}`); | ||
|
||
// Has it been claimed already? | ||
if (messageStatus === MessageStatus.DONE) { | ||
throw new MessageStatusError('message already processed'); | ||
} | ||
|
||
// Has it failed? | ||
if (messageStatus === MessageStatus.FAILED) { | ||
throw new MessageStatusError('user can not process this as message has failed'); | ||
} | ||
|
||
return { messageStatus, destBridgeContract }; | ||
} | ||
|
||
/** | ||
* We are gonna run the following checks here: | ||
* 1. Check that the wallet is connected to the source chain | ||
* 2. Check that the message is owned by the user | ||
* 3. Check that the message has failed | ||
*/ | ||
protected async beforeReleasing({ msgHash, message, wallet }: ClaimArgs) { | ||
const srcChainId = Number(message.srcChainId); | ||
// Are we connected to the source chain? | ||
if (wallet.chain.id !== srcChainId) { | ||
throw new WrongChainError('wallet must be connected to the source chain'); | ||
} | ||
|
||
const { owner } = message; | ||
const userAddress = wallet.account.address; | ||
// Are we the owner of the message? | ||
if (owner.toLowerCase() !== userAddress.toLowerCase()) { | ||
throw new WrongOwnerError('user cannot process this as it is not their message'); | ||
} | ||
|
||
// Before releasing we need to make sure the message has failed | ||
const destChainId = Number(message.destChainId); | ||
const destBridgeAddress = chainContractsMap[destChainId].bridgeAddress; | ||
|
||
const destBridgeContract = getContract({ | ||
address: destBridgeAddress, | ||
abi: bridgeABI, | ||
chainId: destChainId, | ||
}); | ||
|
||
const messageStatus: MessageStatus = await destBridgeContract.read.getMessageStatus([msgHash]); | ||
|
||
log(`Releasing message with status ${messageStatus}`); | ||
|
||
if (messageStatus !== MessageStatus.FAILED) { | ||
throw new MessageStatusError('message must fail to release funds'); | ||
} | ||
} | ||
|
||
abstract estimateGas(args: BridgeArgs): Promise<bigint>; | ||
abstract bridge(args: BridgeArgs): Promise<Hash>; | ||
abstract claim(args: ClaimArgs): Promise<Hash>; | ||
abstract release(args: ReleaseArgs): Promise<Hash>; | ||
} |
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 |
---|---|---|
@@ -1,13 +1,21 @@ | ||
import type { Hex } from 'viem'; | ||
import type { Hash, Hex } from 'viem'; | ||
|
||
import type { Bridge } from './types'; | ||
import { Bridge } from './Bridge'; | ||
|
||
export class ERC1155Bridge implements Bridge { | ||
export class ERC1155Bridge extends Bridge { | ||
async estimateGas(): Promise<bigint> { | ||
return Promise.resolve(BigInt(0)); | ||
} | ||
|
||
async bridge(): Promise<Hex> { | ||
return Promise.resolve('0x'); | ||
} | ||
|
||
async claim() { | ||
return Promise.resolve('0x' as Hash); | ||
} | ||
|
||
async release() { | ||
return Promise.resolve('0x' as Hash); | ||
} | ||
} |
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
Oops, something went wrong.