Skip to content

Commit

Permalink
Merge pull request #138 from onflow/gio/fix-dynamic-flow-transfer
Browse files Browse the repository at this point in the history
Fix dynamic vm transfer transaction
  • Loading branch information
sisyphusSmiling authored Dec 20, 2024
2 parents fcef52b + b8f3e08 commit 1d66a43
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions cadence/transactions/flow-token/dynamic_vm_transfer.cdc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import "FlowStorageFees"
import "FungibleToken"
import "FlowToken"

Expand All @@ -18,16 +19,17 @@ transaction(addressString: String, amount: UFix64) {
let sentVault: @FlowToken.Vault
let evmRecipient: EVM.EVMAddress?
var receiver: &{FungibleToken.Receiver}?

prepare(signer: auth(BorrowValue, SaveValue) &Account) {
// Reference signer's COA if one exists
let coa = signer.storage.borrow<auth(EVM.Withdraw) &EVM.CadenceOwnedAccount>(from: /storage/evm)

// Reference signer's FlowToken Vault
let sourceVault = signer.storage.borrow<auth(FungibleToken.Withdraw) &FlowToken.Vault>(from: /storage/flowTokenVault)
?? panic("Could not borrow signer's FlowToken.Vault")
let cadenceBalance = sourceVault.balance

// Ensure we don't withdraw more than required for storage
let cadenceBalance = FlowStorageFees.defaultTokenAvailableBalance(signer.address)

// Define optional recipients for both VMs
self.receiver = nil
let cadenceRecipient = Address.fromString(addressString)
Expand Down Expand Up @@ -56,7 +58,7 @@ transaction(addressString: String, amount: UFix64) {
if amount > self.sentVault.balance {
let difference = amount - cadenceBalance
// Revert if the signer doesn't have an EVM account or EVM balance is insufficient
if coa == nil || difference < coa!.balance().inFLOW() {
if coa == nil || difference > coa!.balance().inFLOW() {
panic("Insufficient balance across Flow and EVM accounts")
}

Expand Down Expand Up @@ -101,4 +103,4 @@ transaction(addressString: String, amount: UFix64) {
self.evmRecipient!.deposit(from: <-self.sentVault)
}
}
}
}

0 comments on commit 1d66a43

Please sign in to comment.