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

Adds better error messages for FlowStakingCollection and ledger transactions #455

Merged
merged 5 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
194 changes: 145 additions & 49 deletions contracts/FlowStakingCollection.cdc

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

143 changes: 83 additions & 60 deletions lib/go/templates/internal/assets/assets.go

Large diffs are not rendered by default.

68 changes: 34 additions & 34 deletions lib/go/templates/manifest.mainnet.json

Large diffs are not rendered by default.

68 changes: 34 additions & 34 deletions lib/go/templates/manifest.testnet.json

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions transactions/idTableStaking/scripts/get_moves_pending.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import FlowIDTableStaking from "FlowIDTableStaking"

// This script returns the current moves pending list

access(all) fun main(): {String: {UInt32: Bool}} {
return FlowIDTableStaking.getMovesPendingList()!
}
8 changes: 6 additions & 2 deletions transactions/lockedTokens/user/deposit_tokens.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ transaction(amount: UFix64) {

prepare(acct: auth(BorrowValue) &Account) {
self.holderRef = acct.storage.borrow<&LockedTokens.TokenHolder>(from: LockedTokens.TokenHolderStoragePath)
?? panic("The primary user account does not have an associated locked account")
?? panic("Cannot deposit tokens to a locked account! The signer of the transaction "
.concat("does not have an associated locked account, ")
.concat("so there is nowhere to deposit the tokens."))

self.vaultRef = acct.storage.borrow<auth(FungibleToken.Withdraw) &FlowToken.Vault>(from: /storage/flowTokenVault)
?? panic("Could not borrow flow token vault ref")
?? panic("The signer does not store a FlowToken Vault object at the path "
.concat("/storage/flowTokenVault. ")
.concat("The signer must initialize their account with this vault first!"))
}

execute {
Expand Down
8 changes: 6 additions & 2 deletions transactions/lockedTokens/user/withdraw_tokens.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ transaction(amount: UFix64) {

prepare(acct: auth(BorrowValue) &Account) {
self.holderRef = acct.storage.borrow<auth(LockedTokens.TokenOperations, FungibleToken.Withdraw) &LockedTokens.TokenHolder>(from: LockedTokens.TokenHolderStoragePath)
?? panic("The primary user account does not have an associated locked account")
?? panic("Cannot withdraw locked tokens! The signer of the transaction "
.concat("does not have an associated locked account, ")
.concat("so there are no locked tokens to withdraw."))

self.vaultRef = acct.storage.borrow<auth(FungibleToken.Withdraw) &FlowToken.Vault>(from: /storage/flowTokenVault)
?? panic("Could not borrow flow token vault ref")
?? panic("The signer does not store a FlowToken Vault object at the path "
.concat("/storage/flowTokenVault. ")
.concat("The signer must initialize their account with this vault first!"))
}

execute {
Expand Down
2 changes: 1 addition & 1 deletion transactions/stakingCollection/close_stake.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ transaction(nodeID: String, delegatorID: UInt32?) {

prepare(account: auth(BorrowValue) &Account) {
self.stakingCollectionRef = account.storage.borrow<auth(FlowStakingCollection.CollectionOwner) &FlowStakingCollection.StakingCollection>(from: FlowStakingCollection.StakingCollectionStoragePath)
?? panic("Could not borrow a reference to a StakingCollection in the primary user's account")
?? panic(FlowStakingCollection.getCollectionMissingError(nil))
}

execute {
Expand Down
13 changes: 10 additions & 3 deletions transactions/stakingCollection/create_machine_account.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,21 @@ transaction(nodeID: String,

prepare(account: auth(BorrowValue) &Account) {
self.stakingCollectionRef = account.storage.borrow<auth(FlowStakingCollection.CollectionOwner) &FlowStakingCollection.StakingCollection>(from: FlowStakingCollection.StakingCollectionStoragePath)
?? panic("Could not borrow a reference to a StakingCollection in the primary user's account")
?? panic(FlowStakingCollection.getCollectionMissingError(nil))

if let machineAccount = self.stakingCollectionRef.createMachineAccountForExistingNode(nodeID: nodeID, payer: account) {
let sigAlgo = SignatureAlgorithm(rawValue: machineAccountKeySignatureAlgorithm)
?? panic("Could not get a signature algorithm from the raw enum value provided")
?? panic("Cannot create machine account with provided key: Must provide a signature algorithm raw value that corresponds to "
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I do want to put this error message construction in a contract somewhere. Any ideas about where it could go? Potentially in the Crypto contract? Or maybe just in the Service Account contract? Another option would be to create a new contract in the service account that just contains multiple different common error message constructions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first glance, the Crypto contract makes the most sense to me. A version of the enum constructor that panics on invalid input with an error message like this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, Crypto makes the most sense, maybe adding a getter for these enums which reverts with the appropriate message

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.concat("one of the available signature algorithms for Flow keys.")
.concat("You provided ").concat(machineAccountKeySignatureAlgorithm.toString())
.concat(" but the options are either 1 (ECDSA_P256), 2 (ECDSA_secp256k1), or 3 (BLS_BLS12_381)."))

let hashAlgo = HashAlgorithm(rawValue: machineAccountKeyHashAlgorithm)
?? panic("Could not get a hash algorithm from the raw enum value provided")
?? panic("Cannot create machine account with provided key: Must provide a hash algorithm raw value that corresponds to "
.concat("one of of the available hash algorithms for Flow keys.")
.concat("You provided ").concat(machineAccountKeyHashAlgorithm.toString())
.concat(" but the options are 1 (SHA2_256), 2 (SHA2_384), 3 (SHA3_256), ")
.concat("4 (SHA3_384), 5 (KMAC128_BLS_BLS12_381), or 6 (KECCAK_256)."))

let publicKey = PublicKey(
publicKey: machineAccountKey.decodeHex(),
Expand Down
2 changes: 1 addition & 1 deletion transactions/stakingCollection/register_delegator.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ transaction(id: String, amount: UFix64) {

prepare(account: auth(BorrowValue) &Account) {
self.stakingCollectionRef = account.storage.borrow<auth(FlowStakingCollection.CollectionOwner) &FlowStakingCollection.StakingCollection>(from: FlowStakingCollection.StakingCollectionStoragePath)
?? panic("Could not borrow a reference to a StakingCollection in the primary user's account")
?? panic(FlowStakingCollection.getCollectionMissingError(nil))
}

execute {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ transaction(ids: [String], amounts: [UFix64]) {

prepare(account: auth(BorrowValue) &Account) {
self.stakingCollectionRef = account.storage.borrow<auth(FlowStakingCollection.CollectionOwner) &FlowStakingCollection.StakingCollection>(from: FlowStakingCollection.StakingCollectionStoragePath)
?? panic("Could not borrow ref to StakingCollection")
?? panic(FlowStakingCollection.getCollectionMissingError(nil))
}

execute {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ transaction(ids: [String],

prepare(account: auth(BorrowValue) &Account) {
self.stakingCollectionRef = account.storage.borrow<auth(FlowStakingCollection.CollectionOwner) &FlowStakingCollection.StakingCollection>(from: FlowStakingCollection.StakingCollectionStoragePath)
?? panic("Could not borrow ref to StakingCollection")
?? panic(FlowStakingCollection.getCollectionMissingError(nil))

var i = 0

Expand Down
13 changes: 10 additions & 3 deletions transactions/stakingCollection/register_node.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ transaction(id: String,

prepare(account: auth(BorrowValue) &Account) {
self.stakingCollectionRef = account.storage.borrow<auth(FlowStakingCollection.CollectionOwner) &FlowStakingCollection.StakingCollection>(from: FlowStakingCollection.StakingCollectionStoragePath)
?? panic("Could not borrow a reference to a StakingCollection in the primary user's account")
?? panic(FlowStakingCollection.getCollectionMissingError(nil))

if let machineAccount = self.stakingCollectionRef.registerNode(
id: id,
Expand All @@ -30,10 +30,17 @@ transaction(id: String,
payer: account
) {
let sigAlgo = SignatureAlgorithm(rawValue: machineAccountKeySignatureAlgorithm)
?? panic("Could not get a signature algorithm from the raw enum value provided")
?? panic("Cannot register node with provided machine account key: Must provide a signature algorithm raw value that corresponds to "
.concat("one of the available signature algorithms for Flow keys.")
.concat("You provided ").concat(machineAccountKeySignatureAlgorithm.toString())
.concat(" but the options are either 1 (ECDSA_P256), 2 (ECDSA_secp256k1), or 3 (BLS_BLS12_381)."))

let hashAlgo = HashAlgorithm(rawValue: machineAccountKeyHashAlgorithm)
?? panic("Could not get a hash algorithm from the raw enum value provided")
?? panic("Cannot register node with the provided machine account key: Must provide a hash algorithm raw value that corresponds to "
.concat("one of of the available hash algorithms for Flow keys.")
.concat("You provided ").concat(machineAccountKeyHashAlgorithm.toString())
.concat(" but the options are 1 (SHA2_256), 2 (SHA2_384), 3 (SHA3_256), ")
.concat("4 (SHA3_384), 5 (KMAC128_BLS_BLS12_381), or 6 (KECCAK_256)."))

let publicKey = PublicKey(
publicKey: machineAccountKey.decodeHex(),
Expand Down
2 changes: 1 addition & 1 deletion transactions/stakingCollection/request_unstaking.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ transaction(nodeID: String, delegatorID: UInt32?, amount: UFix64) {

prepare(account: auth(BorrowValue) &Account) {
self.stakingCollectionRef = account.storage.borrow<auth(FlowStakingCollection.CollectionOwner) &FlowStakingCollection.StakingCollection>(from: FlowStakingCollection.StakingCollectionStoragePath)
?? panic("Could not borrow a reference to a StakingCollection in the primary user's account")
?? panic(FlowStakingCollection.getCollectionMissingError(nil))
}

execute {
Expand Down
2 changes: 1 addition & 1 deletion transactions/stakingCollection/restake_all_stakers.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ transaction {

prepare(account: auth(BorrowValue) &Account) {
self.stakingCollectionRef = account.storage.borrow<auth(FlowStakingCollection.CollectionOwner) &FlowStakingCollection.StakingCollection>(from: FlowStakingCollection.StakingCollectionStoragePath)
?? panic("Could not borrow ref to StakingCollection")
?? panic(FlowStakingCollection.getCollectionMissingError(nil))
}

execute {
Expand Down
2 changes: 1 addition & 1 deletion transactions/stakingCollection/stake_new_tokens.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ transaction(nodeID: String, delegatorID: UInt32?, amount: UFix64) {

prepare(account: auth(BorrowValue) &Account) {
self.stakingCollectionRef = account.storage.borrow<auth(FlowStakingCollection.CollectionOwner) &FlowStakingCollection.StakingCollection>(from: FlowStakingCollection.StakingCollectionStoragePath)
?? panic("Could not borrow a reference to a StakingCollection in the primary user's account")
?? panic(FlowStakingCollection.getCollectionMissingError(nil))
}

execute {
Expand Down
2 changes: 1 addition & 1 deletion transactions/stakingCollection/stake_rewarded_tokens.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ transaction(nodeID: String, delegatorID: UInt32?, amount: UFix64) {

prepare(account: auth(BorrowValue) &Account) {
self.stakingCollectionRef = account.storage.borrow<auth(FlowStakingCollection.CollectionOwner) &FlowStakingCollection.StakingCollection>(from: FlowStakingCollection.StakingCollectionStoragePath)
?? panic("Could not borrow a reference to a StakingCollection in the primary user's account")
?? panic(FlowStakingCollection.getCollectionMissingError(nil))
}

execute {
Expand Down
2 changes: 1 addition & 1 deletion transactions/stakingCollection/stake_unstaked_tokens.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ transaction(nodeID: String, delegatorID: UInt32?, amount: UFix64) {

prepare(account: auth(BorrowValue) &Account) {
self.stakingCollectionRef = account.storage.borrow<auth(FlowStakingCollection.CollectionOwner) &FlowStakingCollection.StakingCollection>(from: FlowStakingCollection.StakingCollectionStoragePath)
?? panic("Could not borrow a reference to a StakingCollection in the primary user's account")
?? panic(FlowStakingCollection.getCollectionMissingError(nil))
}

execute {
Expand Down
8 changes: 5 additions & 3 deletions transactions/stakingCollection/transfer_delegator.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@ transaction(nodeID: String, delegatorID: UInt32, to: Address) {
prepare(account: auth(BorrowValue) &Account) {
// The account to transfer the NodeDelegator object to must have a valid Staking Collection in order to receive the NodeDelegator.
if (!FlowStakingCollection.doesAccountHaveStakingCollection(address: to)) {
panic("Destination account must have a Staking Collection set up.")
panic("The desination account does not store a Staking Collection object at the path "
.concat(FlowStakingCollection.StakingCollectionStoragePath.toString())
.concat(". The destination account must initialize their account with this object first!"))
}

// Get a reference to the authorizers StakingCollection
self.fromStakingCollectionRef = account.storage.borrow<auth(FlowStakingCollection.CollectionOwner) &FlowStakingCollection.StakingCollection>(from: FlowStakingCollection.StakingCollectionStoragePath)
?? panic("Could not borrow a reference to a StakingCollection in the primary user's account")
?? panic(FlowStakingCollection.getCollectionMissingError(nil))

// Get the PublicAccount of the account to transfer the NodeDelegator to.
let toAccount = getAccount(to)

// Borrow a capability to the public methods available on the receivers StakingCollection.
self.toStakingCollectionCap = toAccount.capabilities
.borrow<&FlowStakingCollection.StakingCollection>(FlowStakingCollection.StakingCollectionPublicPath)
?? panic("Could not borrow a referamce to a StakingCollection in the receiver's account")
?? panic("Could not borrow a reference to a StakingCollection in the receiver's account")
}

execute {
Expand Down
10 changes: 7 additions & 3 deletions transactions/stakingCollection/transfer_node.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ transaction(nodeID: String, to: Address) {
prepare(account: auth(BorrowValue) &Account) {
// The account to transfer the NodeStaker object to must have a valid Staking Collection in order to receive the NodeStaker.
if (!FlowStakingCollection.doesAccountHaveStakingCollection(address: to)) {
panic("Destination account must have a Staking Collection set up.")
panic("The desination account does not store a Staking Collection object at the path "
.concat(FlowStakingCollection.StakingCollectionStoragePath.toString())
.concat(". The destination account must initialize their account with this object first!"))
}

// Get a reference to the authorizers StakingCollection
self.fromStakingCollectionRef = account.storage.borrow<auth(FlowStakingCollection.CollectionOwner) &FlowStakingCollection.StakingCollection>(from: FlowStakingCollection.StakingCollectionStoragePath)
?? panic("Could not borrow a reference to a StakingCollection in the primary user's account")
?? panic(FlowStakingCollection.getCollectionMissingError(nil))

// Get the PublicAccount of the account to transfer the NodeStaker to.
let toAccount = getAccount(to)
Expand All @@ -27,7 +29,9 @@ transaction(nodeID: String, to: Address) {
?? panic("Could not borrow a reference to a StakingCollection in the receiver's account")

let machineAccountInfo = self.fromStakingCollectionRef.getMachineAccounts()[nodeID]
?? panic("Could not get machine account info for the specified node ID")
?? panic("Could not get machine account info from the signer's account for the node ID "
.concat(nodeID).concat(". Make sure that the node has configured a machine account ")
.concat("and has it registered in the staking collection."))

// Remove the NodeStaker from the authorizers StakingCollection.
let nodeStaker <- self.fromStakingCollectionRef.removeNode(nodeID: nodeID)
Expand Down
2 changes: 1 addition & 1 deletion transactions/stakingCollection/unstake_all.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ transaction(nodeID: String) {

prepare(account: auth(BorrowValue) &Account) {
self.stakingCollectionRef = account.storage.borrow<auth(FlowStakingCollection.CollectionOwner) &FlowStakingCollection.StakingCollection>(from: FlowStakingCollection.StakingCollectionStoragePath)
?? panic("Could not borrow a reference to a StakingCollection in the primary user's account")
?? panic(FlowStakingCollection.getCollectionMissingError(nil))
}

execute {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ transaction(nodeID: String, newAddress: String) {

prepare(account: auth(BorrowValue) &Account) {
self.stakingCollectionRef = account.storage.borrow<auth(FlowStakingCollection.CollectionOwner) &FlowStakingCollection.StakingCollection>(from: FlowStakingCollection.StakingCollectionStoragePath)
?? panic("Could not borrow a reference to a StakingCollection in the primary user's account")
?? panic(FlowStakingCollection.getCollectionMissingError(nil))
}

execute {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ transaction(nodeID: String, amount: UFix64) {

prepare(account: auth(BorrowValue) &Account) {
self.stakingCollectionRef = account.storage.borrow<auth(FlowStakingCollection.CollectionOwner) &FlowStakingCollection.StakingCollection>(from: FlowStakingCollection.StakingCollectionStoragePath)
?? panic("Could not borrow a reference to a StakingCollection in the primary user's account")
?? panic(FlowStakingCollection.getCollectionMissingError(nil))
}

execute {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ transaction(nodeID: String, delegatorID: UInt32?, amount: UFix64) {

prepare(account: auth(BorrowValue) &Account) {
self.stakingCollectionRef = account.storage.borrow<auth(FlowStakingCollection.CollectionOwner) &FlowStakingCollection.StakingCollection>(from: FlowStakingCollection.StakingCollectionStoragePath)
?? panic("Could not borrow a reference to a StakingCollection in the primary user's account")
?? panic(FlowStakingCollection.getCollectionMissingError(nil))
}

execute {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ transaction(nodeID: String, delegatorID: UInt32?, amount: UFix64) {

prepare(account: auth(BorrowValue) &Account) {
self.stakingCollectionRef = account.storage.borrow<auth(FlowStakingCollection.CollectionOwner) &FlowStakingCollection.StakingCollection>(from: FlowStakingCollection.StakingCollectionStoragePath)
?? panic("Could not borrow a reference to a StakingCollection in the primary user's account")
?? panic(FlowStakingCollection.getCollectionMissingError(nil))
}

execute {
Expand Down
Loading