Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dynamic vm transfer transaction #138

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
}
}
Loading