Skip to content

Commit

Permalink
refactor FlowEVMBridgeUtils.deposit to .depositFee to ease internal f…
Browse files Browse the repository at this point in the history
…ee deposits
  • Loading branch information
sisyphusSmiling committed Apr 23, 2024
1 parent d2c8366 commit 2338351
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 26 deletions.
35 changes: 10 additions & 25 deletions cadence/contracts/bridge/FlowEVMBridge.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ contract FlowEVMBridge : IFlowEVMNFTBridge, IFlowEVMTokenBridge {
message: "This type is not supported as defined by the project's development team"
)
// Withdraw from feeProvider and deposit to self
let feeVault <-feeProvider.withdraw(amount: FlowEVMBridgeConfig.onboardFee) as! @FlowToken.Vault
FlowEVMBridgeUtils.deposit(<-feeVault)
FlowEVMBridgeUtils.depositFee(feeProvider, feeAmount: FlowEVMBridgeConfig.onboardFee)
// Deploy an EVM defining contract via the FlowBridgeFactory.sol contract
let onboardingValues = self.deployEVMContract(forAssetType: type)
// Initialize bridge escrow for the asset
Expand Down Expand Up @@ -171,9 +170,8 @@ contract FlowEVMBridge : IFlowEVMNFTBridge, IFlowEVMTokenBridge {
self.evmAddressRequiresOnboarding(address) == true,
message: "Onboarding is not needed for this contract"
)
// Withdraw from feeProvider and deposit to self
let feeVault <-feeProvider.withdraw(amount: FlowEVMBridgeConfig.onboardFee) as! @FlowToken.Vault
FlowEVMBridgeUtils.deposit(<-feeVault)
// Withdraw fee from feeProvider and deposit
FlowEVMBridgeUtils.depositFee(feeProvider, feeAmount: FlowEVMBridgeConfig.onboardFee)
// Deploy a defining Cadence contract to the bridge account
self.deployDefiningContract(evmContractAddress: address)
}
Expand Down Expand Up @@ -216,13 +214,8 @@ contract FlowEVMBridge : IFlowEVMNFTBridge, IFlowEVMTokenBridge {
let storageUsed = FlowEVMBridgeNFTEscrow.lockNFT(<-token)
// Calculate the bridge fee on current rates
let feeAmount = FlowEVMBridgeUtils.calculateBridgeFee(bytes: storageUsed)
assert(
feeProvider.isAvailableToWithdraw(amount: feeAmount),
message: "Fee provider does not have balance to cover the bridge fee of ".concat(feeAmount.toString())
)
// Withdraw from feeProvider and deposit to self
let feeVault <-feeProvider.withdraw(amount: feeAmount) as! @FlowToken.Vault
FlowEVMBridgeUtils.deposit(<-feeVault)
// Withdraw fee from feeProvider and deposit
FlowEVMBridgeUtils.depositFee(feeProvider, feeAmount: feeAmount)

// Does the bridge control the EVM contract associated with this type?
let associatedAddress = FlowEVMBridgeConfig.getEVMAddressAssociated(with: tokenType)
Expand Down Expand Up @@ -317,8 +310,7 @@ contract FlowEVMBridge : IFlowEVMNFTBridge, IFlowEVMTokenBridge {
}
// Withdraw from feeProvider and deposit to self
let feeAmount = FlowEVMBridgeUtils.calculateBridgeFee(bytes: 0)
let feeVault <-feeProvider.withdraw(amount: feeAmount) as! @FlowToken.Vault
FlowEVMBridgeUtils.deposit(<-feeVault)
FlowEVMBridgeUtils.depositFee(feeProvider, feeAmount: feeAmount)

// Get the EVMAddress of the ERC721 contract associated with the type
let associatedAddress = FlowEVMBridgeConfig.getEVMAddressAssociated(with: type)
Expand Down Expand Up @@ -407,11 +399,7 @@ contract FlowEVMBridge : IFlowEVMNFTBridge, IFlowEVMTokenBridge {

// Here we assume burning Vault in Cadence which doesn't require storage consumption
feeAmount = FlowEVMBridgeUtils.calculateBridgeFee(bytes: 0)
let feeVault <-feeProvider.withdraw(amount: feeAmount) as! @FlowToken.Vault
assert(feeVault.balance == feeAmount, message: "Fee provider did not return the requested fee")

// Deposit fee and return early
FlowEVMBridgeUtils.deposit(<-feeVault)
FlowEVMBridgeUtils.depositFee(feeProvider, feeAmount: feeAmount)
return
}

Expand All @@ -427,10 +415,8 @@ contract FlowEVMBridge : IFlowEVMNFTBridge, IFlowEVMTokenBridge {
feeAmount = FlowEVMBridgeUtils.calculateBridgeFee(bytes: 0)
}

// Withdraw from feeProvider and deposit to self
let feeVault <-feeProvider.withdraw(amount: feeAmount) as! @FlowToken.Vault
assert(feeVault.balance == feeAmount, message: "Fee provider did not return the requested fee")
FlowEVMBridgeUtils.deposit(<-feeVault)
// Withdraw fee amount from feeProvider and deposit
FlowEVMBridgeUtils.depositFee(feeProvider, feeAmount: feeAmount)

// Does the bridge control the EVM contract associated with this type?
let associatedAddress = FlowEVMBridgeConfig.getEVMAddressAssociated(with: vaultType)
Expand Down Expand Up @@ -494,8 +480,7 @@ contract FlowEVMBridge : IFlowEVMNFTBridge, IFlowEVMTokenBridge {
}
// Withdraw from feeProvider and deposit to self
let feeAmount = FlowEVMBridgeUtils.calculateBridgeFee(bytes: 0)
let feeVault <-feeProvider.withdraw(amount: feeAmount) as! @FlowToken.Vault
FlowEVMBridgeUtils.deposit(<-feeVault)
FlowEVMBridgeUtils.depositFee(feeProvider, feeAmount: feeAmount)

// Get the EVMAddress of the ERC20 contract associated with the type
let associatedAddress = FlowEVMBridgeConfig.getEVMAddressAssociated(with: type)
Expand Down
6 changes: 5 additions & 1 deletion cadence/contracts/bridge/FlowEVMBridgeUtils.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -666,9 +666,13 @@ contract FlowEVMBridgeUtils {
/// Deposits fees to the bridge account's FlowToken Vault - helps fund asset storage
///
access(account)
fun deposit(_ feeVault: @FlowToken.Vault) {
fun depositFee(_ feeProvider: auth(FungibleToken.Withdraw) &{FungibleToken.Provider}, feeAmount: UFix64) {
let vault = self.account.storage.borrow<&FlowToken.Vault>(from: /storage/flowTokenVault)
?? panic("Could not borrow FlowToken.Vault reference")

let feeVault <-feeProvider.withdraw(amount: feeAmount) as! @FlowToken.Vault
assert(feeVault.balance == feeAmount, message: "Fee provider did not return the requested fee")

vault.deposit(from: <-feeVault)
}

Expand Down

0 comments on commit 2338351

Please sign in to comment.