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 15 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
18 changes: 9 additions & 9 deletions contracts/FlowFees.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ access(all) contract FlowFees {
// 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,
_ payerAcct: auth(BorrowValue) &Account,
inclusionEffort: UFix64,
maxExecutionEffort: UFix64,
): VerifyPayerBalanceResult {
Expand All @@ -120,7 +120,7 @@ access(all) contract FlowFees {
// 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()
}

Expand All @@ -133,7 +133,7 @@ access(all) contract FlowFees {

/// 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) {
Expand All @@ -142,7 +142,7 @@ access(all) contract FlowFees {
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")


Expand All @@ -163,13 +163,13 @@ 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)
}

Expand All @@ -182,11 +182,11 @@ access(all) contract FlowFees {
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)
}
}
183 changes: 103 additions & 80 deletions contracts/FlowIDTableStaking.cdc

Large diffs are not rendered by default.

52 changes: 25 additions & 27 deletions contracts/FlowServiceAccount.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -25,50 +25,44 @@ access(all) contract FlowServiceAccount {
access(contract) var accountCreators: {Address: Bool}

/// Initialize an account with a FlowToken Vault and publish capabilities.
access(all) fun initDefaultToken(_ acct: AuthAccount) {
access(all) fun initDefaultToken(_ acct: auth(SaveValue, Capabilities) &Account) {
// Create a new FlowToken Vault and save it in storage
acct.save(<-FlowToken.createEmptyVault(), to: /storage/flowTokenVault)
acct.storage.save(<-FlowToken.createEmptyVault(), to: /storage/flowTokenVault)

// Create a public capability to the Vault that only exposes
// the deposit function through the Receiver interface
acct.link<&FlowToken.Vault>(
/public/flowTokenReceiver,
target: /storage/flowTokenVault
)
let receiverCapability = acct.capabilities.storage.issue<&FlowToken.Vault>(/storage/flowTokenVault)
acct.capabilities.publish(receiverCapability, at: /public/flowTokenReceiver)

// Create a public capability to the Vault that only exposes
// the balance field through the Balance interface
acct.link<&FlowToken.Vault>(
/public/flowTokenBalance,
target: /storage/flowTokenVault
)
let balanceCapability = acct.capabilities.storage.issue<&FlowToken.Vault>(/storage/flowTokenVault)
acct.capabilities.publish(balanceCapability, at: /public/flowTokenBalance)
}

/// Get the default token balance on an account
///
/// Returns 0 if the account has no default balance
access(all) fun defaultTokenBalance(_ acct: PublicAccount): UFix64 {
access(all) fun defaultTokenBalance(_ acct: &Account): UFix64 {
var balance = 0.0
if let balanceRef = acct
.getCapability(/public/flowTokenBalance)
.borrow<&FlowToken.Vault>(){
balance = balanceRef.getBalance()
}
if let balanceRef = acct.capabilities.borrow<&FlowToken.Vault>(/public/flowTokenBalance) {
balance = balanceRef.getBalance()
}

return balance
}

/// Return a reference to the default token vault on an account
access(all) fun defaultTokenVault(_ acct: AuthAccount): auth(FungibleToken.Withdrawable) &FlowToken.Vault {
return acct.borrow<auth(FungibleToken.Withdrawable) &FlowToken.Vault>(from: /storage/flowTokenVault)
access(all) fun defaultTokenVault(_ acct: auth(BorrowValue) &Account): auth(FungibleToken.Withdrawable) &FlowToken.Vault {
return acct.storage.borrow<auth(FungibleToken.Withdrawable) &FlowToken.Vault>(from: /storage/flowTokenVault)
?? panic("Unable to borrow reference to the default token vault")
}

/// Will be deprecated and can be deleted after the switchover to FlowFees.deductTransactionFee
///
/// Called when a transaction is submitted to deduct the fee
/// from the AuthAccount that submitted it
access(all) fun deductTransactionFee(_ acct: AuthAccount) {
access(all) fun deductTransactionFee(_ acct: auth(BorrowValue) &Account) {
if self.transactionFee == UFix64(0) {
return
}
Expand All @@ -86,7 +80,11 @@ access(all) contract FlowServiceAccount {
/// - Deducts the account creation fee from a payer account.
/// - Inits the default token.
/// - Inits account storage capacity.
access(all) fun setupNewAccount(newAccount: AuthAccount, payer: AuthAccount) {
access(all) fun setupNewAccount(
newAccount: auth(SaveValue, BorrowValue, Capabilities) &Account,
payer: auth(BorrowValue) &Account
) {

if !FlowServiceAccount.isAccountCreator(payer.address) {
panic("Account not authorized to create accounts")
}
Expand Down Expand Up @@ -119,7 +117,7 @@ access(all) contract FlowServiceAccount {

/// Is true if new acconts can only be created by approved accounts `self.accountCreators`
access(all) fun isAccountCreationRestricted(): Bool {
return self.account.copy<Bool>(from: /storage/isAccountCreationRestricted) ?? false
return self.account.storage.copy<Bool>(from: /storage/isAccountCreationRestricted) ?? false
}

// Authorization resource to change the fields of the contract
Expand All @@ -130,19 +128,19 @@ access(all) contract FlowServiceAccount {

// Gets Execution Effort Weights from the service account's storage
access(all) fun getExecutionEffortWeights(): {UInt64: UInt64} {
return self.account.copy<{UInt64: UInt64}>(from: /storage/executionEffortWeights)
return self.account.storage.copy<{UInt64: UInt64}>(from: /storage/executionEffortWeights)
?? panic("execution effort weights not set yet")
}

// Gets Execution Memory Weights from the service account's storage
access(all) fun getExecutionMemoryWeights(): {UInt64: UInt64} {
return self.account.copy<{UInt64: UInt64}>(from: /storage/executionMemoryWeights)
return self.account.storage.copy<{UInt64: UInt64}>(from: /storage/executionMemoryWeights)
?? panic("execution memory weights not set yet")
}

// Gets Execution Memory Limit from the service account's storage
access(all) fun getExecutionMemoryLimit(): UInt64 {
return self.account.copy<UInt64>(from: /storage/executionMemoryLimit)
return self.account.storage.copy<UInt64>(from: /storage/executionMemoryLimit)
?? panic("execution memory limit not set yet")
}

Expand Down Expand Up @@ -183,8 +181,8 @@ access(all) contract FlowServiceAccount {

access(all) fun setIsAccountCreationRestricted(_ enabled: Bool) {
let path = /storage/isAccountCreationRestricted
let oldValue = FlowServiceAccount.account.load<Bool>(from: path)
FlowServiceAccount.account.save<Bool>(enabled, to: path)
let oldValue = FlowServiceAccount.account.storage.load<Bool>(from: path)
FlowServiceAccount.account.storage.save<Bool>(enabled, to: path)
if enabled != oldValue {
emit IsAccountCreationRestrictedUpdated(isRestricted: enabled)
}
Expand All @@ -200,6 +198,6 @@ access(all) contract FlowServiceAccount {
let admin <- create Administrator()
admin.addAccountCreator(self.account.address)

self.account.save(<-admin, to: /storage/flowServiceAdmin)
self.account.storage.save(<-admin, to: /storage/flowServiceAdmin)
}
}
Loading