Skip to content

Commit

Permalink
Update to newest Stable Cadence Preview (#371)
Browse files Browse the repository at this point in the history
* update to view functions for stable cadence

* remove unreachable code

* update templates to Stable Cadence

* update to Stable Cadence preview 4

* update for stable cadence

* fix parse error

---------

Co-authored-by: Josh Hannan <[email protected]>
Co-authored-by: Bastian Müller <[email protected]>
  • Loading branch information
3 people authored Jul 5, 2023
1 parent 18e5b36 commit 0e0a35d
Show file tree
Hide file tree
Showing 22 changed files with 1,034 additions and 1,038 deletions.
64 changes: 32 additions & 32 deletions contracts/FlowContractAudits.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,47 @@
/// deployed to the Service Account, so it is documented here
/// for reference
pub contract FlowContractAudits {
access(all) contract FlowContractAudits {

// Event that is emitted when a new Auditor resource is created
pub event AuditorCreated()
access(all) event AuditorCreated()

// Event that is emitted when a new contract audit voucher is created
pub event VoucherCreated(address: Address?, recurrent: Bool, expiryBlockHeight: UInt64?, codeHash: String)
access(all) event VoucherCreated(address: Address?, recurrent: Bool, expiryBlockHeight: UInt64?, codeHash: String)

// Event that is emitted when a contract audit voucher is used
pub event VoucherUsed(address: Address, key: String, recurrent: Bool, expiryBlockHeight: UInt64?)
access(all) event VoucherUsed(address: Address, key: String, recurrent: Bool, expiryBlockHeight: UInt64?)

// Event that is emitted when a contract audit voucher is removed
pub event VoucherRemoved(key: String, recurrent: Bool, expiryBlockHeight: UInt64?)
access(all) event VoucherRemoved(key: String, recurrent: Bool, expiryBlockHeight: UInt64?)

// Dictionary of all vouchers
access(contract) var vouchers: {String: AuditVoucher}

// The storage path for the admin resource
pub let AdminStoragePath: StoragePath
access(all) let AdminStoragePath: StoragePath

// The storage Path for auditors' AuditorProxy
pub let AuditorProxyStoragePath: StoragePath
access(all) let AuditorProxyStoragePath: StoragePath

// The public path for auditors' AuditorProxy capability
pub let AuditorProxyPublicPath: PublicPath
access(all) let AuditorProxyPublicPath: PublicPath

// Single audit voucher that is used for contract deployment
pub struct AuditVoucher {
access(all) struct AuditVoucher {

// Address of the account the voucher is intended for
// If nil, the contract can be deployed to any account
pub let address: Address?
access(all) let address: Address?

// If false, the voucher will be removed after first use
pub let recurrent: Bool
access(all) let recurrent: Bool

// If non-nil, the voucher won't be valid after the expiry block height
pub let expiryBlockHeight: UInt64?
access(all) let expiryBlockHeight: UInt64?

// Hash of contract code
pub let codeHash: String
access(all) let codeHash: String

init(address: Address?, recurrent: Bool, expiryBlockHeight: UInt64?, codeHash: String) {
self.address = address
Expand All @@ -55,33 +55,33 @@ pub contract FlowContractAudits {
}

// Returns all current vouchers
pub fun getAllVouchers(): {String: AuditVoucher} {
access(all) fun getAllVouchers(): {String: AuditVoucher} {
return self.vouchers
}

// Get the associated dictionary key for given address and codeHash
pub fun generateVoucherKey(address: Address?, codeHash: String): String {
access(all) fun generateVoucherKey(address: Address?, codeHash: String): String {
if address != nil {
return address!.toString().concat("-").concat(codeHash)
}
return "any-".concat(codeHash)
}

pub fun hashContractCode(_ code: String): String {
access(all) fun hashContractCode(_ code: String): String {
return String.encodeHex(HashAlgorithm.SHA3_256.hash(code.utf8))
}

// Auditors can create new vouchers and remove them
pub resource Auditor {
access(all) resource Auditor {

// Create new voucher with contract code
pub fun addVoucher(address: Address?, recurrent: Bool, expiryOffset: UInt64?, code: String) {
access(all) fun addVoucher(address: Address?, recurrent: Bool, expiryOffset: UInt64?, code: String) {
let codeHash = FlowContractAudits.hashContractCode(code)
self.addVoucherHashed(address: address, recurrent: recurrent, expiryOffset: expiryOffset, codeHash: codeHash)
}

// Create new voucher with hashed contract code
pub fun addVoucherHashed(address: Address?, recurrent: Bool, expiryOffset: UInt64?, codeHash: String) {
access(all) fun addVoucherHashed(address: Address?, recurrent: Bool, expiryOffset: UInt64?, codeHash: String) {

// calculate expiry block height based on expiryOffset
var expiryBlockHeight: UInt64? = nil
Expand All @@ -102,35 +102,35 @@ pub contract FlowContractAudits {
}

// Remove a voucher with given key
pub fun deleteVoucher(key: String) {
access(all) fun deleteVoucher(key: String) {
FlowContractAudits.deleteVoucher(key)
}
}

// Used by admin to set the Auditor capability
pub resource interface AuditorProxyPublic {
pub fun setAuditorCapability(_ cap: Capability<&Auditor>)
access(all) resource interface AuditorProxyPublic {
access(all) fun setAuditorCapability(_ cap: Capability<&Auditor>)
}

// The auditor account will have audit access through AuditorProxy
// This enables the admin account to revoke access
// See https://docs.onflow.org/cadence/design-patterns/#capability-revocation
pub resource AuditorProxy: AuditorProxyPublic {
access(all) resource AuditorProxy: AuditorProxyPublic {
access(self) var auditorCapability: Capability<&Auditor>?

pub fun setAuditorCapability(_ cap: Capability<&Auditor>) {
access(all) fun setAuditorCapability(_ cap: Capability<&Auditor>) {
self.auditorCapability = cap
}

pub fun addVoucher(address: Address?, recurrent: Bool, expiryOffset: UInt64?, code: String) {
access(all) fun addVoucher(address: Address?, recurrent: Bool, expiryOffset: UInt64?, code: String) {
self.auditorCapability!.borrow()!.addVoucher(address: address, recurrent: recurrent, expiryOffset: expiryOffset, code: code)
}

pub fun addVoucherHashed(address: Address?, recurrent: Bool, expiryOffset: UInt64?, codeHash: String) {
access(all) fun addVoucherHashed(address: Address?, recurrent: Bool, expiryOffset: UInt64?, codeHash: String) {
self.auditorCapability!.borrow()!.addVoucherHashed(address: address, recurrent: recurrent, expiryOffset: expiryOffset, codeHash: codeHash)
}

pub fun deleteVoucher(key: String) {
access(all) fun deleteVoucher(key: String) {
self.auditorCapability!.borrow()!.deleteVoucher(key: key)
}

Expand All @@ -141,20 +141,20 @@ pub contract FlowContractAudits {
}

// Can be called by anyone but needs a capability to function
pub fun createAuditorProxy(): @AuditorProxy {
access(all) fun createAuditorProxy(): @AuditorProxy {
return <- create AuditorProxy()
}

pub resource Administrator {
access(all) resource Administrator {

// Creates new Auditor
pub fun createNewAuditor(): @Auditor {
access(all) fun createNewAuditor(): @Auditor {
emit AuditorCreated()
return <-create Auditor()
}

// Checks all vouchers and removes expired ones
pub fun cleanupExpiredVouchers() {
access(all) fun cleanupExpiredVouchers() {
for key in FlowContractAudits.vouchers.keys {
let v = FlowContractAudits.vouchers[key]!
if v.expiryBlockHeight != nil {
Expand All @@ -166,7 +166,7 @@ pub contract FlowContractAudits {
}

// For testing
pub fun useVoucherForDeploy(address: Address, code: String): Bool {
access(all) fun useVoucherForDeploy(address: Address, code: String): Bool {
return FlowContractAudits.useVoucherForDeploy(address: address, code: code)
}
}
Expand Down
48 changes: 24 additions & 24 deletions contracts/FlowFees.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,67 @@ import FungibleToken from 0xFUNGIBLETOKENADDRESS
import FlowToken from 0xFLOWTOKENADDRESS
import FlowStorageFees from 0xFLOWSTORAGEFEESADDRESS

pub contract FlowFees {
access(all) contract FlowFees {

// Event that is emitted when tokens are deposited to the fee vault
pub event TokensDeposited(amount: UFix64)
access(all) event TokensDeposited(amount: UFix64)

// Event that is emitted when tokens are withdrawn from the fee vault
pub event TokensWithdrawn(amount: UFix64)
access(all) event TokensWithdrawn(amount: UFix64)

// Event that is emitted when fees are deducted
pub event FeesDeducted(amount: UFix64, inclusionEffort: UFix64, executionEffort: UFix64)
access(all) event FeesDeducted(amount: UFix64, inclusionEffort: UFix64, executionEffort: UFix64)

// Event that is emitted when fee parameters change
pub event FeeParametersChanged(surgeFactor: UFix64, inclusionEffortCost: UFix64, executionEffortCost: UFix64)
access(all) event FeeParametersChanged(surgeFactor: UFix64, inclusionEffortCost: UFix64, executionEffortCost: UFix64)

// Private vault with public deposit function
access(self) var vault: @FlowToken.Vault

pub fun deposit(from: @FungibleToken.Vault) {
access(all) fun deposit(from: @FungibleToken.Vault) {
let from <- from as! @FlowToken.Vault
let balance = from.balance
self.vault.deposit(from: <-from)
emit TokensDeposited(amount: balance)
}

/// Get the balance of the Fees Vault
pub fun getFeeBalance(): UFix64 {
access(all) fun getFeeBalance(): UFix64 {
return self.vault.balance
}

pub resource Administrator {
access(all) resource Administrator {
// withdraw
//
// Allows the administrator to withdraw tokens from the fee vault
pub fun withdrawTokensFromFeeVault(amount: UFix64): @FungibleToken.Vault {
access(all) fun withdrawTokensFromFeeVault(amount: UFix64): @FungibleToken.Vault {
let vault <- FlowFees.vault.withdraw(amount: amount)
emit TokensWithdrawn(amount: amount)
return <-vault
}

/// Allows the administrator to change all the fee parameters at once
pub fun setFeeParameters(surgeFactor: UFix64, inclusionEffortCost: UFix64, executionEffortCost: UFix64) {
access(all) fun setFeeParameters(surgeFactor: UFix64, inclusionEffortCost: UFix64, executionEffortCost: UFix64) {
let newParameters = FeeParameters(surgeFactor: surgeFactor, inclusionEffortCost: inclusionEffortCost, executionEffortCost: executionEffortCost)
FlowFees.setFeeParameters(newParameters)
}

/// Allows the administrator to change the fee surge factor
pub fun setFeeSurgeFactor(_ surgeFactor: UFix64) {
access(all) fun setFeeSurgeFactor(_ surgeFactor: UFix64) {
let oldParameters = FlowFees.getFeeParameters()
let newParameters = FeeParameters(surgeFactor: surgeFactor, inclusionEffortCost: oldParameters.inclusionEffortCost, executionEffortCost: oldParameters.executionEffortCost)
FlowFees.setFeeParameters(newParameters)
}
}

/// A struct holding the fee parameters needed to calculate the fees
pub struct FeeParameters {
access(all) struct FeeParameters {
/// The surge factor is used to make transaction fees respond to high loads on the network
pub var surgeFactor: UFix64
access(all) var surgeFactor: UFix64
/// The FLOW cost of one unit of inclusion effort. The FVM is responsible for metering inclusion effort.
pub var inclusionEffortCost: UFix64
access(all) var inclusionEffortCost: UFix64
/// The FLOW cost of one unit of execution effort. The FVM is responsible for metering execution effort.
pub var executionEffortCost: UFix64
access(all) var executionEffortCost: UFix64

init(surgeFactor: UFix64, inclusionEffortCost: UFix64, executionEffortCost: UFix64){
self.surgeFactor = surgeFactor
Expand All @@ -72,15 +72,15 @@ pub contract FlowFees {
}

// VerifyPayerBalanceResult is returned by the verifyPayersBalanceForTransactionExecution function
pub struct VerifyPayerBalanceResult {
access(all) struct VerifyPayerBalanceResult {
// True if the payer has sufficient balance for the transaction execution to continue
pub let canExecuteTransaction: Bool
access(all) let canExecuteTransaction: Bool
// The minimum payer balance required for the transaction execution to continue.
// This value is defined by verifyPayersBalanceForTransactionExecution.
pub let requiredBalance: UFix64
access(all) let requiredBalance: UFix64
// The maximum transaction fees (inclusion fees + execution fees) the transaction can incur
// (if all available execution effort is used)
pub let maximumTransactionFees: UFix64
access(all) let maximumTransactionFees: UFix64

init(canExecuteTransaction: Bool, requiredBalance: UFix64, maximumTransactionFees: UFix64){
self.canExecuteTransaction = canExecuteTransaction
Expand All @@ -97,7 +97,7 @@ pub contract FlowFees {
//
// The requiredBalance balance is defined as the minimum account balance +
// maximum transaction fees (inclusion fees + execution fees at max execution effort).
pub fun verifyPayersBalanceForTransactionExecution(
access(all) fun verifyPayersBalanceForTransactionExecution(
_ payerAcct: AuthAccount,
inclusionEffort: UFix64,
maxExecutionEffort: UFix64,
Expand Down Expand Up @@ -133,7 +133,7 @@ pub contract FlowFees {

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

if feeAmount == UFix64(0) {
Expand All @@ -142,7 +142,7 @@ pub contract FlowFees {
return
}

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


Expand All @@ -162,7 +162,7 @@ pub contract FlowFees {
emit FeesDeducted(amount: feeAmount, inclusionEffort: inclusionEffort, executionEffort: executionEffort)
}

pub fun getFeeParameters(): FeeParameters {
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!")
}

Expand All @@ -175,7 +175,7 @@ pub contract FlowFees {


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

let totalFees = params.surgeFactor * ( inclusionEffort * params.inclusionEffortCost + executionEffort * params.executionEffortCost )
Expand Down
Loading

0 comments on commit 0e0a35d

Please sign in to comment.