Skip to content

Commit

Permalink
harden balance state assertions for token bridging
Browse files Browse the repository at this point in the history
  • Loading branch information
sisyphusSmiling committed Apr 24, 2024
1 parent 669a80f commit 306640a
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions cadence/contracts/bridge/FlowEVMBridge.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ contract FlowEVMBridge : IFlowEVMNFTBridge, IFlowEVMTokenBridge {
let decimals = FlowEVMBridgeUtils.getTokenDecimals(evmContractAddress: associatedAddress)
let bridgeAmount = FlowEVMBridgeUtils.ufix64ToUInt256(value: vaultBalance, decimals: decimals)

let toPreBalance = FlowEVMBridgeUtils.balanceOf(owner: to, evmContractAddress: associatedAddress)

let isFactoryDeployed = FlowEVMBridgeUtils.isEVMContractBridgeOwned(evmContractAddress: associatedAddress)
// Controlled by the bridge - mint or transfer based on the bridge's EVM contract authority
if isFactoryDeployed {
Expand All @@ -448,6 +450,13 @@ contract FlowEVMBridge : IFlowEVMNFTBridge, IFlowEVMTokenBridge {
)
assert(callResult.status == EVM.Status.successful, message: "Tranfer to bridge recipient failed")
}

// Ensure bridge to recipient was succcessful
let toPostBalance = FlowEVMBridgeUtils.balanceOf(owner: to, evmContractAddress: associatedAddress)
assert(
toPostBalance == toPreBalance + bridgeAmount,
message: "Transfer to bridge recipient failed"
)
}

/// Public entrypoint to bridge FTs from EVM to Cadence
Expand Down Expand Up @@ -508,21 +517,28 @@ contract FlowEVMBridge : IFlowEVMNFTBridge, IFlowEVMTokenBridge {
)
assert(hasSufficientBalance, message: "Caller does not have sufficient balance to bridge requested tokens")

// Get the bridge COA's balance of the token before executing the protected transfer call
// Get the owner and escrow balance of the token before executing the protected transfer call
let bridgeCOAAddress = self.getBridgeCOAEVMAddress()
let ownerBalanceBefore = FlowEVMBridgeUtils.balanceOf(owner: owner, evmContractAddress: associatedAddress)
let bridgeBalanceBefore = FlowEVMBridgeUtils.balanceOf(
owner: self.getBridgeCOAEVMAddress(),
owner: bridgeCOAAddress,
evmContractAddress: associatedAddress
)

// Execute the transfer from the calling owner to the bridge's COA, escrowing the tokens in EVM
let callResult = protectedTransferCall()
assert(callResult.status == EVM.Status.successful, message: "Transfer to bridge COA failed")

// Get the bridge COA's balance of the token before executing the protected transfer call
// Confirm the transfer of the expected was successful in both sending owner and recipient escrow
let ownerBalanceAfter = FlowEVMBridgeUtils.balanceOf(owner: owner, evmContractAddress: associatedAddress)
let bridgeBalanceAfter = FlowEVMBridgeUtils.balanceOf(
owner: self.getBridgeCOAEVMAddress(),
owner: bridgeCOAAddress,
evmContractAddress: associatedAddress
)
assert(
ownerBalanceAfter == ownerBalanceBefore - amount,
message: "Transfer to bridge COA failed - cannot bridge FT without bridge escrow"
)
assert(
bridgeBalanceAfter == bridgeBalanceBefore + amount,
message: "Transfer to bridge COA failed - cannot bridge FT without bridge escrow"
Expand Down

0 comments on commit 306640a

Please sign in to comment.