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

Update core contracts to latest stable cadence #382

Merged
merged 23 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
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
50 changes: 25 additions & 25 deletions contracts/FlowFees.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ access(all) contract FlowFees {
access(all) struct VerifyPayerBalanceResult {
// True if the payer has sufficient balance for the transaction execution to continue
access(all) let canExecuteTransaction: Bool
// The minimum payer balance required for the transaction execution to continue.
// The minimum payer balance required for the transaction execution to continue.
// This value is defined by verifyPayersBalanceForTransactionExecution.
access(all) let requiredBalance: UFix64
// The maximum transaction fees (inclusion fees + execution fees) the transaction can incur
// The maximum transaction fees (inclusion fees + execution fees) the transaction can incur
// (if all available execution effort is used)
access(all) let maximumTransactionFees: UFix64

Expand All @@ -95,12 +95,12 @@ access(all) contract FlowFees {
// and returns the maximum possible transaction fees.
// (according to the inclusion effort and maximum execution effort of the transaction).
//
// The requiredBalance balance is defined as the minimum account balance +
// The requiredBalance balance is defined as the minimum account balance +
// maximum transaction fees (inclusion fees + execution fees at max execution effort).
access(all) fun verifyPayersBalanceForTransactionExecution(
_ payerAcct: AuthAccount,
inclusionEffort: UFix64,
maxExecutionEffort: UFix64,
_ payerAcct: auth(BorrowValue) &Account,
inclusionEffort: UFix64,
maxExecutionEffort: UFix64
): VerifyPayerBalanceResult {
// Get the maximum fees required for the transaction.
var maxTransactionFee = self.computeFees(inclusionEffort: inclusionEffort, executionEffort: maxExecutionEffort)
Expand All @@ -111,50 +111,50 @@ access(all) contract FlowFees {
if minimumRequiredBalance == UFix64(0) {
// If the required balance is zero exit early.
return VerifyPayerBalanceResult(
canExecuteTransaction: true,
canExecuteTransaction: true,
requiredBalance: minimumRequiredBalance,
maximumTransactionFees: maxTransactionFee,
maximumTransactionFees: maxTransactionFee
)
}

// 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) {
if let tokenVault = payerAcct.storage.borrow<&FlowToken.Vault>(from: /storage/flowTokenVault) {
balance = tokenVault.getBalance()
}

return VerifyPayerBalanceResult(
// The transaction can be executed it the payers balance is greater than the minimum required balance.
// The transaction can be executed it the payers balance is greater than the minimum required balance.
canExecuteTransaction: balance >= minimumRequiredBalance,
requiredBalance: minimumRequiredBalance,
maximumTransactionFees: maxTransactionFee)
}

/// Called when a transaction is submitted to deduct the fee
/// from the AuthAccount that submitted it
access(all) fun deductTransactionFee(_ acct: AuthAccount, inclusionEffort: UFix64, executionEffort: UFix64) {
access(all) fun deductTransactionFee(_ acct: auth(BorrowValue) &Account, inclusionEffort: UFix64, executionEffort: UFix64) {
var feeAmount = self.computeFees(inclusionEffort: inclusionEffort, executionEffort: executionEffort)

if feeAmount == UFix64(0) {
// If there are no fees to deduct, do not continue,
// If there are no fees to deduct, do not continue,
// so that there are no unnecessarily emitted events
return
}

let tokenVault = acct.borrow<auth(FungibleToken.Withdrawable) &FlowToken.Vault>(from: /storage/flowTokenVault)
let tokenVault = acct.storage.borrow<auth(FungibleToken.Withdrawable) &FlowToken.Vault>(from: /storage/flowTokenVault)
?? panic("Unable to borrow reference to the default token vault")


if feeAmount > tokenVault.getBalance() {
// In the future this code path will never be reached,
// In the future this code path will never be reached,
// as payers that are under account minimum balance will not have their transactions included in a collection
//
// Currently this is not used to fail the transaction (as that is the responsibility of the minimum account balance logic),
// But is used to reduce the balance of the vault to 0.0, if the vault has less available balance than the transaction fees.
// But is used to reduce the balance of the vault to 0.0, if the vault has less available balance than the transaction fees.
feeAmount = tokenVault.getBalance()
}

let feeVault <- tokenVault.withdraw(amount: feeAmount)
self.vault.deposit(from: <-feeVault)

Expand All @@ -163,30 +163,30 @@ access(all) contract FlowFees {
}

access(all) fun getFeeParameters(): FeeParameters {
return self.account.copy<FeeParameters>(from: /storage/FlowTxFeeParameters) ?? panic("Error getting tx fee parameters. They need to be initialized first!")
return self.account.storage.copy<FeeParameters>(from: /storage/FlowTxFeeParameters) ?? panic("Error getting tx fee parameters. They need to be initialized first!")
}

access(self) fun setFeeParameters(_ feeParameters: FeeParameters) {
// empty storage before writing new FeeParameters to it
self.account.load<FeeParameters>(from: /storage/FlowTxFeeParameters)
self.account.save(feeParameters,to: /storage/FlowTxFeeParameters)
self.account.storage.load<FeeParameters>(from: /storage/FlowTxFeeParameters)
self.account.storage.save(feeParameters,to: /storage/FlowTxFeeParameters)
emit FeeParametersChanged(surgeFactor: feeParameters.surgeFactor, inclusionEffortCost: feeParameters.inclusionEffortCost, executionEffortCost: feeParameters.executionEffortCost)
}


// compute the transaction fees with the current fee parameters and the given inclusionEffort and executionEffort
access(all) fun computeFees(inclusionEffort: UFix64, executionEffort: UFix64): UFix64 {
let params = self.getFeeParameters()

let totalFees = params.surgeFactor * ( inclusionEffort * params.inclusionEffortCost + executionEffort * params.executionEffortCost )
return totalFees
}

init(adminAccount: AuthAccount) {
init(adminAccount: auth(SaveValue) &Account) {
// Create a new FlowToken Vault and save it in storage
self.vault <- FlowToken.createEmptyVault() as! @FlowToken.Vault

let admin <- create Administrator()
adminAccount.save(<-admin, to: /storage/flowFeesAdmin)
adminAccount.storage.save(<-admin, to: /storage/flowFeesAdmin)
}
}
Loading