Skip to content

Commit

Permalink
Fix account creators (#185)
Browse files Browse the repository at this point in the history
* make function comments docstrings

* make the account creator set non-public and add a function to query the set

* update generated file

* update generated file for script
  • Loading branch information
turbolent authored May 24, 2021
1 parent e5f6198 commit 513a463
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
41 changes: 23 additions & 18 deletions contracts/FlowServiceAccount.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ pub contract FlowServiceAccount {

pub event AccountCreatorRemoved(accountCreator: Address)

// A fixed-rate fee charged to execute a transaction
/// A fixed-rate fee charged to execute a transaction
pub var transactionFee: UFix64

// A fixed-rate fee charged to create a new account
/// A fixed-rate fee charged to create a new account
pub var accountCreationFee: UFix64

// The list of account addresses that have permission to create accounts
pub var accountCreators: {Address: Bool}
/// The list of account addresses that have permission to create accounts
access(contract) var accountCreators: {Address: Bool}

// Initialize an account with a FlowToken Vault and publish capabilities.
/// Initialize an account with a FlowToken Vault and publish capabilities.
pub fun initDefaultToken(_ acct: AuthAccount) {
// Create a new FlowToken Vault and save it in storage
acct.save(<-FlowToken.createEmptyVault(), to: /storage/flowTokenVault)
Expand All @@ -42,7 +42,7 @@ pub contract FlowServiceAccount {
)
}

// Get the default token balance on an account
/// Get the default token balance on an account
pub fun defaultTokenBalance(_ acct: PublicAccount): UFix64 {
let balanceRef = acct
.getCapability(/public/flowTokenBalance)
Expand All @@ -51,14 +51,14 @@ pub contract FlowServiceAccount {
return balanceRef.balance
}

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

// Called when a transaction is submitted to deduct the fee
// from the AuthAccount that submitted it
/// Called when a transaction is submitted to deduct the fee
/// from the AuthAccount that submitted it
pub fun deductTransactionFee(_ acct: AuthAccount) {
if self.transactionFee == UFix64(0) {
return
Expand All @@ -70,9 +70,9 @@ pub contract FlowServiceAccount {
FlowFees.deposit(from: <-feeVault)
}

// - Deducts the account creation fee from a payer account.
// - Inits the default token.
// - Inits account storage capacity.
/// - Deducts the account creation fee from a payer account.
/// - Inits the default token.
/// - Inits account storage capacity.
pub fun setupNewAccount(newAccount: AuthAccount, payer: AuthAccount) {
if self.accountCreationFee < FlowStorageFees.minimumStorageReservation {
panic("Account creation fees setup incorrectly")
Expand All @@ -90,33 +90,38 @@ pub contract FlowServiceAccount {
vaultRef.deposit(from: <-storageFeeVault)
}

// Returns true if the given address is permitted to create accounts, false otherwise
/// Returns true if the given address is permitted to create accounts, false otherwise
pub fun isAccountCreator(_ address: Address): Bool {
return self.accountCreators[address] ?? false
}

// Authorization resource to change the fields of the contract
/// Returns all addresses permitted to create accounts
pub fun getAccountCreators(): [Address] {
return self.accountCreators.keys
}

/// Authorization resource to change the fields of the contract
pub resource Administrator {

// sets the transaction fee
/// Sets the transaction fee
pub fun setTransactionFee(_ newFee: UFix64) {
FlowServiceAccount.transactionFee = newFee
emit TransactionFeeUpdated(newFee: newFee)
}

// sets the account creation fee
/// Sets the account creation fee
pub fun setAccountCreationFee(_ newFee: UFix64) {
FlowServiceAccount.accountCreationFee = newFee
emit AccountCreationFeeUpdated(newFee: newFee)
}

// adds an account address as an authorized account creator
/// Adds an account address as an authorized account creator
pub fun addAccountCreator(_ accountCreator: Address) {
FlowServiceAccount.accountCreators[accountCreator] = true
emit AccountCreatorAdded(accountCreator: accountCreator)
}

// removes an account address as an authorized account creator
/// Removes an account address as an authorized account creator
pub fun removeAccountCreator(_ accountCreator: Address) {
FlowServiceAccount.accountCreators.remove(key: accountCreator)
emit AccountCreatorRemoved(accountCreator: accountCreator)
Expand Down
6 changes: 3 additions & 3 deletions lib/go/contracts/internal/assets/assets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions lib/go/templates/internal/assets/assets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions transactions/FlowServiceAccount/get_account_creators.cdc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import FlowServiceAccount from 0xSERVICEADDRESS

pub fun main(): UFix64 {
return FlowServiceAccount.accountCreators.keys
pub fun main(): [Address] {
return FlowServiceAccount.getAccountCreators()
}

0 comments on commit 513a463

Please sign in to comment.