Skip to content

Commit

Permalink
update token bridge fee approximation
Browse files Browse the repository at this point in the history
  • Loading branch information
sisyphusSmiling committed Oct 25, 2024
1 parent 3df1914 commit 2e871cb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
6 changes: 4 additions & 2 deletions cadence/transactions/bridge/tokens/bridge_tokens_from_evm.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ transaction(vaultIdentifier: String, amount: UInt256) {

/* --- Configure a ScopedFTProvider --- */
//
// Calculate the bridge fee - bridging from EVM consumes no storage, so flat fee
let approxFee = FlowEVMBridgeUtils.calculateBridgeFee(bytes: 0)
// Set a cap on the withdrawable bridge fee
var approxFee = FlowEVMBridgeUtils.calculateBridgeFee(
bytes: 200_000 // 200 kB as upper bound on movable storage used in a single transaction
)
// Issue and store bridge-dedicated Provider Capability in storage if necessary
if signer.storage.type(at: FlowEVMBridgeConfig.providerCapabilityStoragePath) == nil {
let providerCap = signer.capabilities.storage.issue<auth(FungibleToken.Withdraw) &{FungibleToken.Provider}>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ transaction(vaultIdentifier: String, amount: UInt256, recipient: Address) {

/* --- Configure a ScopedFTProvider --- */
//
// Calculate the bridge fee - bridging from EVM consumes no storage, so flat fee
let approxFee = FlowEVMBridgeUtils.calculateBridgeFee(bytes: 0)
// Set a cap on the withdrawable bridge fee
var approxFee = FlowEVMBridgeUtils.calculateBridgeFee(
bytes: 200_000 // 200 kB as upper bound on movable storage used in a single transaction
)
// Issue and store bridge-dedicated Provider Capability in storage if necessary
if signer.storage.type(at: FlowEVMBridgeConfig.providerCapabilityStoragePath) == nil {
let providerCap = signer.capabilities.storage.issue<auth(FungibleToken.Withdraw) &{FungibleToken.Provider}>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,11 @@ transaction(vaultIdentifier: String, amount: UFix64, recipient: String) {
from: vaultData.storagePath
) ?? panic("Could not access signer's FungibleToken Vault")

// Withdraw the requested balance & calculate the approximate bridge fee based on storage usage
let currentStorageUsage = signer.storage.used
// Withdraw the requested balance & set a cap on the withdrawable bridge fee
self.sentVault <- vault.withdraw(amount: amount)
let withdrawnStorageUsage = signer.storage.used
// Approximate the bridge fee based on the difference in storage usage with some buffer
var approxFee = FlowEVMBridgeUtils.calculateBridgeFee(
bytes: currentStorageUsage - withdrawnStorageUsage
) * 1.10
bytes: 200_000 // 200 kB as upper bound on movable storage used in a single transaction
)
// Determine if the Vault requires onboarding - this impacts the fee required
self.requiresOnboarding = FlowEVMBridge.typeRequiresOnboarding(self.sentVault.getType())
?? panic("Bridge does not support this asset type")
Expand Down
9 changes: 3 additions & 6 deletions cadence/transactions/bridge/tokens/bridge_tokens_to_evm.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,11 @@ transaction(vaultIdentifier: String, amount: UFix64) {
from: vaultData.storagePath
) ?? panic("Could not access signer's FungibleToken Vault")

// Withdraw the requested balance & calculate the approximate bridge fee based on storage usage
let currentStorageUsage = signer.storage.used
// Withdraw the requested balance & set a cap on the withdrawable bridge fee
self.sentVault <- vault.withdraw(amount: amount)
let withdrawnStorageUsage = signer.storage.used
// Approximate the bridge fee based on the difference in storage usage with some buffer
var approxFee = FlowEVMBridgeUtils.calculateBridgeFee(
bytes: currentStorageUsage - withdrawnStorageUsage
) * 1.10
bytes: 200_000 // 200 kB as upper bound on movable storage used in a single transaction
)
// Determine if the Vault requires onboarding - this impacts the fee required
self.requiresOnboarding = FlowEVMBridge.typeRequiresOnboarding(self.sentVault.getType())
?? panic("Bridge does not support this asset type")
Expand Down

0 comments on commit 2e871cb

Please sign in to comment.