Skip to content

Commit

Permalink
Change verifyPayersBalanceForTransactionExecution
Browse files Browse the repository at this point in the history
  • Loading branch information
janezpodhostnik committed Dec 15, 2022
1 parent 7d617b6 commit fb2d0f0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
16 changes: 9 additions & 7 deletions contracts/FlowFees.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -106,25 +106,27 @@ pub contract FlowFees {
var maxTransactionFee = self.computeFees(inclusionEffort: inclusionEffort, executionEffort: maxExecutionEffort)

// Get the minimum required payer's balance for the transaction.
let minimumRequiredBalance = maxTransactionFee + FlowStorageFees.minimumStorageReservation
let minimumRequiredBalance = FlowStorageFees.defaultTokenReservedBalance(payerAcct.address)

if minimumRequiredBalance == UFix64(0) {
// If there are no fees to deduct, then the payer can pay for the transaction.
// This is a shortcut for the case where the fees and minimum account balance are set to 0.
// If the required balance is zero exit early.
return VerifyPayerBalanceResult(
canExecuteTransaction: true,
requiredBalance: minimumRequiredBalance,
maximumTransactionFees: maxTransactionFee,
)
}

// get the balance of the payers default vault
let tokenVault = payerAcct.borrow<&FlowToken.Vault>(from: /storage/flowTokenVault)
?? panic("Unable to borrow reference to the default token vault")
// Get the balance of the payers default vault.
// In the edge case where the payer doesnt have a vault, treat the balance as 0.
var balance = 0.0
if let tokenVault = payerAcct.borrow<&FlowToken.Vault>(from: /storage/flowTokenVault) {
balance = tokenVault.balance
}

return VerifyPayerBalanceResult(
// The transaction can be executed it the payers balance is greater than the minimum required balance.
canExecuteTransaction: tokenVault.balance >= minimumRequiredBalance,
canExecuteTransaction: balance >= minimumRequiredBalance,
requiredBalance: minimumRequiredBalance,
maximumTransactionFees: maxTransactionFee)
}
Expand Down
15 changes: 13 additions & 2 deletions contracts/FlowStorageFees.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub contract FlowStorageFees {

/// Gets "available" balance of an account
///
/// The available balance is its default token balance minus what is reserved for storage.
/// The available balance of an account is its default token balance minus what is reserved for storage.
/// If the account has no default balance it is counted as a balance of 0.0 FLOW
pub fun defaultTokenAvailableBalance(_ accountAddress: Address): UFix64 {
//get balance of account
Expand All @@ -162,13 +162,24 @@ pub contract FlowStorageFees {
}

// get how much should be reserved for storage
var reserved = self.defaultTokenReservedBalance(accountAddress)

return balance.saturatingSubtract(reserved)
}

/// Gets "reserved" balance of an account
///
/// The reserved balance of an account is its storage used multiplied by the storage cost per flow token.
/// The reserved balance is at least the minimum storage reservation.
pub fun defaultTokenReservedBalance(_ accountAddress: Address): UFix64 {
let acct = getAccount(accountAddress)
var reserved = self.storageCapacityToFlow(self.convertUInt64StorageBytesToUFix64Megabytes(acct.storageUsed))
// at least self.minimumStorageReservation should be reserved
if reserved < self.minimumStorageReservation {
reserved = self.minimumStorageReservation
}

return balance.saturatingSubtract(reserved)
return reserved
}

init() {
Expand Down
Loading

0 comments on commit fb2d0f0

Please sign in to comment.