-
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.
- Loading branch information
1 parent
87f0318
commit 49e872a
Showing
7 changed files
with
135 additions
and
89 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
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
3 changes: 2 additions & 1 deletion
3
packages/bridge-ui/src/components/Dialogs/RetryDialog/types.ts
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,63 @@ | ||
import { readContract } from '@wagmi/core'; | ||
import { type Address, zeroAddress } from 'viem'; | ||
|
||
import { quotaManagerAbi } from '$abi'; | ||
import { isL2Chain } from '$libs/chain'; | ||
import { getLogger } from '$libs/util/logger'; | ||
import { config } from '$libs/wagmi'; | ||
|
||
import { type BridgeTransaction, ContractType } from '.'; | ||
import { getContractAddressByType } from './getContractAddressByType'; | ||
|
||
const log = getLogger('bridge:checkBridgeQuota'); | ||
|
||
export const checkBridgeQuota = async ({ | ||
transaction, | ||
amount, | ||
}: { | ||
transaction: BridgeTransaction; | ||
tokenAddress?: Address; | ||
amount: bigint; | ||
}) => { | ||
log( | ||
'Checking bridge quota', | ||
transaction.canonicalTokenAddress, | ||
amount, | ||
isL2Chain(Number(transaction.destChainId)), | ||
transaction.destChainId, | ||
transaction.srcChainId, | ||
); | ||
|
||
const tokenAddress = | ||
transaction.canonicalTokenAddress && (transaction.canonicalTokenAddress as string) !== '' | ||
? transaction.canonicalTokenAddress | ||
: zeroAddress; | ||
|
||
if (isL2Chain(Number(transaction.destChainId))) { | ||
// Quota only applies for transactions from L2-L1. | ||
// So if the destination chain is an L2 chain, we can skip this check. | ||
log('Skipping quota check for L2 chain'); | ||
return true; | ||
} | ||
|
||
const quotaManagerAddress = getContractAddressByType({ | ||
srcChainId: Number(transaction.destChainId), | ||
destChainId: Number(transaction.srcChainId), | ||
contractType: ContractType.QUOTAMANAGER, | ||
}); | ||
|
||
const quota = await readContract(config, { | ||
address: quotaManagerAddress, | ||
abi: quotaManagerAbi, | ||
chainId: Number(transaction.destChainId), | ||
functionName: 'availableQuota', | ||
args: [tokenAddress, 0n], | ||
}); | ||
|
||
if (amount > quota) { | ||
log('Not enough quota', quota, amount); | ||
return false; | ||
} | ||
log('Quota:', quota, 'Amount:', amount, 'Has enough quota:', amount <= quota); | ||
return true; | ||
}; |