Skip to content

Commit

Permalink
update bridge fee approximation in nft bridging txns
Browse files Browse the repository at this point in the history
  • Loading branch information
sisyphusSmiling committed Oct 25, 2024
1 parent a452f91 commit 47f8732
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
6 changes: 4 additions & 2 deletions cadence/transactions/bridge/nft/bridge_nft_from_evm.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ transaction(nftIdentifier: String, id: 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 @@ -67,8 +67,10 @@ transaction(nftIdentifier: String, id: 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 @@ -52,15 +52,14 @@ transaction(nftIdentifier: String, id: UInt64, recipient: String) {
) ?? panic("Could not access signer's NFT Collection")

// Withdraw the requested NFT & calculate the approximate bridge fee based on NFT storage usage
let currentStorageUsage = signer.storage.used
self.nft <- collection.withdraw(withdrawID: id)
let withdrawnStorageUsage = signer.storage.used
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 NFT requires onboarding - this impacts the fee required
self.requiresOnboarding = FlowEVMBridge.typeRequiresOnboarding(self.nft.getType())
?? panic("Bridge does not support this asset type")
// Add the onboarding fee if onboarding is necessary
if self.requiresOnboarding {
approxFee = approxFee + FlowEVMBridgeConfig.onboardFee
}
Expand Down Expand Up @@ -100,7 +99,7 @@ transaction(nftIdentifier: String, id: UInt64, recipient: String) {
feeProvider: &self.scopedProvider as auth(FungibleToken.Withdraw) &{FungibleToken.Provider}
)
}
// Execute the bridge transaction
// Execute the bridge to EVM
let recipientEVMAddress = EVM.addressFromString(recipient)
FlowEVMBridge.bridgeNFTToEVM(
token: <-self.nft,
Expand Down
9 changes: 4 additions & 5 deletions cadence/transactions/bridge/nft/bridge_nft_to_evm.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,15 @@ transaction(nftIdentifier: String, id: UInt64) {
from: collectionData.storagePath
) ?? panic("Could not access signer's NFT Collection")

// Withdraw the requested NFT & calculate the approximate bridge fee based on NFT storage usage
let currentStorageUsage = signer.storage.used
// Withdraw the requested NFT & set a cap on the withdrawable bridge fee
self.nft <- collection.withdraw(withdrawID: id)
let withdrawnStorageUsage = signer.storage.used
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 NFT requires onboarding - this impacts the fee required
self.requiresOnboarding = FlowEVMBridge.typeRequiresOnboarding(self.nft.getType())
?? panic("Bridge does not support this asset type")
// Add the onboarding fee if onboarding is necessary
if self.requiresOnboarding {
approxFee = approxFee + FlowEVMBridgeConfig.onboardFee
}
Expand Down

0 comments on commit 47f8732

Please sign in to comment.