Skip to content

Commit

Permalink
add better error messages for all the transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuahannan committed Sep 23, 2024
1 parent 0256cd7 commit 800a43d
Show file tree
Hide file tree
Showing 32 changed files with 271 additions and 169 deletions.
186 changes: 93 additions & 93 deletions lib/go/templates/internal/assets/assets.go

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions transactions/burn_tokens.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ transaction(amount: UFix64) {
self.supplyBefore = ExampleToken.totalSupply

let vaultData = ExampleToken.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTVaultData>()) as! FungibleTokenMetadataViews.FTVaultData?
?? panic("Could not get vault data view for the contract")
?? panic("Could not resolve FTVaultData view. The ExampleToken"
.concat(" contract needs to implement the FTVaultData Metadata view in order to execute this transaction"))

// Withdraw tokens from the signer's vault in storage
let sourceVault = signer.storage.borrow<auth(FungibleToken.Withdraw) &ExampleToken.Vault>(
from: vaultData.storagePath
) ?? panic("Could not borrow a reference to the signer's ExampleToken vault")
from: vaultData.storagePath)
?? panic("The signer does not store a ExampleToken Vault object at the path "
.concat(vaultData.storagePath.toString())
.concat("The signer must initialize their account with this object first!"))

self.burnVault <- sourceVault.withdraw(amount: amount) as! @ExampleToken.Vault
}

Expand Down
16 changes: 12 additions & 4 deletions transactions/generic_transfer_with_address.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,20 @@ transaction(amount: UFix64, to: Address, contractAddress: Address, contractName:
// Borrow a reference to the vault stored on the passed account at the passed publicPath
let resolverRef = getAccount(contractAddress)
.contracts.borrow<&{FungibleToken}>(name: contractName)
?? panic("Could not borrow a reference to the fungible token contract")
?? panic("Could not borrow FungibleToken reference to the contract. Make sure the provided contract name ("
.concat(contractName).concat(") and address (").concat(contractAddress.toString()).concat(") are correct!"))

// Use that reference to retrieve the FTView
self.vaultData = resolverRef.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTVaultData>()) as! FungibleTokenMetadataViews.FTVaultData?
?? panic("Could not resolve the FTVaultData view for the given Fungible token contract")
?? panic("Could not resolve FTVaultData view. The ".concat(contractName)
.concat(" contract needs to implement the FTVaultData Metadata view in order to execute this transaction"))

// Get a reference to the signer's stored vault
let vaultRef = signer.storage.borrow<auth(FungibleToken.Withdraw) &{FungibleToken.Provider}>(from: self.vaultData.storagePath)
?? panic("Could not borrow reference to the owner's Vault!")
?? panic("The signer does not store a FungibleToken.Provider object at the path "
.concat(self.vaultData.storagePath.toString()).concat("For the ").concat(contractName)
.concat(" contract at address ").concat(contractAddress.toString())
.concat(". The signer must initialize their account with this object first!"))

self.tempVault <- vaultRef.withdraw(amount: amount)

Expand All @@ -71,7 +76,10 @@ transaction(amount: UFix64, to: Address, contractAddress: Address, contractName:
execute {
let recipient = getAccount(to)
let receiverRef = recipient.capabilities.borrow<&{FungibleToken.Receiver}>(self.vaultData.receiverPath)
?? panic("Could not borrow reference to the recipient's Receiver!")
?? panic("Could not borrow a Receiver reference to the FungibleToken Vault in account "
.concat(to.toString()).concat(" at path ").concat(self.vaultData.receiverPath.toString())
.concat(". Make sure you are sending to an address that has ")
.concat("a FungibleToken Vault set up properly at the specified path."))

// Transfer tokens from the signer's stored vault to the receiver capability
receiverRef.deposit(from: <-self.tempVault)
Expand Down
9 changes: 7 additions & 2 deletions transactions/generic_transfer_with_paths.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ transaction(amount: UFix64, to: Address, senderPathIdentifier: String, receiverP

// Get a reference to the signer's stored vault
let vaultRef = signer.storage.borrow<auth(FungibleToken.Withdraw) &{FungibleToken.Provider}>(from: storagePath)
?? panic("Could not borrow reference to the owner's Vault!")
?? panic("The signer does not store a FungibleToken.Provider object at the path "
.concat(storagePath.toString())
.concat("The signer must initialize their account with this object first!"))

self.tempVault <- vaultRef.withdraw(amount: amount)

Expand All @@ -48,7 +50,10 @@ transaction(amount: UFix64, to: Address, senderPathIdentifier: String, receiverP

let recipient = getAccount(to)
let receiverRef = recipient.capabilities.borrow<&{FungibleToken.Receiver}>(publicPath)
?? panic("Could not borrow reference to the recipient's Receiver!")
?? panic("Could not borrow a Receiver reference to the FungibleToken Vault in account "
.concat(to.toString()).concat(" at path ").concat(publicPath.toString())
.concat(". Make sure you are sending to an address that has ")
.concat("a FungibleToken Vault set up properly at the specified path."))

// Transfer tokens from the signer's stored vault to the receiver capability
receiverRef.deposit(from: <-self.tempVault)
Expand Down
8 changes: 6 additions & 2 deletions transactions/metadata/scripts/get_token_metadata.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ access(all) fun main(address: Address): FungibleTokenMetadataViews.FTView {
let account = getAccount(address)

let vaultData = ExampleToken.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTVaultData>()) as! FungibleTokenMetadataViews.FTVaultData?
?? panic("Could not get vault data view for the contract")
?? panic("Could not resolve FTVaultData view. The ExampleToken"
.concat(" contract needs to implement the FTVaultData Metadata view in order to execute this transaction"))


let vaultRef = account.capabilities.borrow<&ExampleToken.Vault>(vaultData.metadataPath)
?? panic("Could not borrow a reference to the vault resolver")
?? panic("Could not borrow a reference to the ExampleToken Vault in account "
.concat(address.toString()).concat(" at path ").concat(vaultData.metadataPath.toString())
.concat(". Make sure you are querying an address that has an ExampleToken Vault set up properly."))

let ftView = FungibleTokenMetadataViews.getFTView(viewResolver: vaultRef)

Expand Down
9 changes: 2 additions & 7 deletions transactions/metadata/scripts/get_vault_data.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@ access(all) fun main(address: Address): FungibleTokenMetadataViews.FTVaultData {
let account = getAccount(address)

let vaultData = ExampleToken.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTVaultData>()) as! FungibleTokenMetadataViews.FTVaultData?
?? panic("Could not get vault data view for the contract")

let vaultRef = account.capabilities.borrow<&ExampleToken.Vault>(vaultData.metadataPath)
?? panic("Could not borrow a reference to the vault resolver")

let vaultData = FungibleTokenMetadataViews.getFTVaultData(vaultRef)
?? panic("Token does not implement FTVaultData view")
?? panic("Could not resolve FTVaultData view. The ExampleToken"
.concat(" contract needs to implement the FTVaultData Metadata view in order to execute this transaction"))

return vaultData
}
7 changes: 5 additions & 2 deletions transactions/metadata/scripts/get_vault_display.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ access(all) fun main(address: Address): FungibleTokenMetadataViews.FTDisplay {
let account = getAccount(address)

let vaultData = ExampleToken.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTVaultData>()) as! FungibleTokenMetadataViews.FTVaultData?
?? panic("Could not get vault data view for the contract")
?? panic("Could not resolve FTVaultData view. The ExampleToken"
.concat(" contract needs to implement the FTVaultData Metadata view in order to execute this transaction"))

let vaultRef = account.capabilities.borrow<&ExampleToken.Vault>(vaultData.metadataPath)
?? panic("Could not borrow a reference to the vault resolver")
?? panic("Could not borrow a reference to the ExampleToken Vault in account "
.concat(address.toString()).concat(" at path ").concat(vaultData.metadataPath.toString())
.concat(". Make sure you are querying an address that has an ExampleToken Vault set up properly."))

let ftDisplay = FungibleTokenMetadataViews.getFTDisplay(vaultRef)
?? panic("Token does not implement FTDisplay view")
Expand Down
7 changes: 5 additions & 2 deletions transactions/metadata/scripts/get_vault_supply_view.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ access(all) fun main(address: Address): UFix64 {
let account = getAccount(address)

let vaultData = ExampleToken.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTVaultData>()) as! FungibleTokenMetadataViews.FTVaultData?
?? panic("Could not get vault data view for the contract")
?? panic("Could not resolve FTVaultData view. The ExampleToken"
.concat(" contract needs to implement the FTVaultData Metadata view in order to execute this transaction"))

let vaultRef = account.capabilities.borrow<&ExampleToken.Vault>(vaultData.metadataPath)
?? panic("Could not borrow a reference to the vault resolver")
?? panic("Could not borrow a reference to the ExampleToken Vault in account "
.concat(address.toString()).concat(" at path ").concat(vaultData.metadataPath.toString())
.concat(". Make sure you are querying an address that has an ExampleToken Vault set up properly."))

let ftSupply = vaultRef.resolveView(Type<FungibleTokenMetadataViews.TotalSupply>())!

Expand Down
7 changes: 5 additions & 2 deletions transactions/metadata/scripts/get_views.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ access(all) fun main(address: Address): [Type] {
let account = getAccount(address)

let vaultData = ExampleToken.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTVaultData>()) as! FungibleTokenMetadataViews.FTVaultData?
?? panic("Could not get vault data view for the contract")
?? panic("Could not resolve FTVaultData view. The ExampleToken"
.concat(" contract needs to implement the FTVaultData Metadata view in order to execute this transaction"))

let vaultRef = account.capabilities.borrow<&ExampleToken.Vault>(vaultData.metadataPath)
?? panic("Could not borrow Balance reference to the Vault")
?? panic("Could not borrow a reference to the ExampleToken Vault in account "
.concat(address.toString()).concat(" at path ").concat(vaultData.metadataPath.toString())
.concat(". Make sure you are querying an address that has an ExampleToken Vault set up properly."))

return vaultRef.getViews()
}
6 changes: 4 additions & 2 deletions transactions/metadata/setup_account_from_address.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ transaction(contractAddress: Address, contractName: String) {
// Borrow a reference to the vault stored on the passed account at the passed publicPath
let resolverRef = getAccount(contractAddress)
.contracts.borrow<&{FungibleToken}>(name: contractName)
?? panic("Could not borrow a reference to the fungible token contract")
?? panic("Could not borrow FungibleToken reference to the contract. Make sure the provided contract name ("
.concat(contractName).concat(") and address (").concat(contractAddress.toString()).concat(") are correct!"))

// Use that reference to retrieve the FTView
let ftVaultData = resolverRef.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTVaultData>()) as! FungibleTokenMetadataViews.FTVaultData?
?? panic("Could not resolve the FTVaultData view for the given Fungible token contract")
?? panic("Could not resolve FTVaultData view. The ".concat(contractName)
.concat(" contract needs to implement the FTVaultData Metadata view in order to execute this transaction"))

// Create a new empty vault using the createEmptyVault function inside the FTVaultData
let emptyVault <-ftVaultData.createEmptyVault()
Expand Down
4 changes: 3 additions & 1 deletion transactions/metadata/setup_account_from_vault_reference.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ transaction(address: Address, publicPath: PublicPath) {
// Borrow a reference to the vault stored on the passed account at the passed publicPath
let resolverRef = getAccount(address)
.capabilities.borrow<&{ViewResolver.Resolver}>(publicPath)
?? panic("Could not borrow a reference to the vault view resolver ")
?? panic("Could not borrow a reference to the ViewResolver in account "
.concat(address.toString()).concat(" at path ").concat(publicPath.toString())
.concat(". Make sure you are querying an address that has a Vault set up properly."))

// Use that reference to retrieve the FTView
let ftView = FungibleTokenMetadataViews.getFTView(viewResolver: resolverRef)
Expand Down
10 changes: 7 additions & 3 deletions transactions/mint_tokens.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ transaction(recipient: Address, amount: UFix64) {

// Borrow a reference to the admin object
self.tokenMinter = signer.storage.borrow<&ExampleToken.Minter>(from: ExampleToken.AdminStoragePath)
?? panic("Signer is not the token admin")
?? panic("Cannot mint: Signer does not store the ExampleToken Minter in their account!")

let vaultData = ExampleToken.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTVaultData>()) as! FungibleTokenMetadataViews.FTVaultData?
?? panic("Could not get vault data view for the contract")
?? panic("Could not resolve FTVaultData view. The ExampleToken"
.concat(" contract needs to implement the FTVaultData Metadata view in order to execute this transaction"))

self.tokenReceiver = getAccount(recipient).capabilities.borrow<&{FungibleToken.Receiver}>(vaultData.receiverPath)
?? panic("Could not borrow receiver reference to the Vault")
?? panic("Could not borrow a Receiver reference to the FungibleToken Vault in account "
.concat(recipient.toString()).concat(" at path ").concat(vaultData.receiverPath.toString())
.concat(". Make sure you are sending to an address that has ")
.concat("a FungibleToken Vault set up properly at the specified path."))
}

execute {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ transaction {
execute {

let vaultData = ExampleToken.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTVaultData>()) as! FungibleTokenMetadataViews.FTVaultData?
?? panic("Could not get vault data view for the contract")
?? panic("Could not resolve FTVaultData view. The ExampleToken"
.concat(" contract needs to implement the FTVaultData Metadata view in order to execute this transaction"))

// Save a regular vault to the new account
self.newAccount.storage.save(<-ExampleToken.createEmptyVault(vaultType: Type<@ExampleToken.Vault>()), to: vaultData.storagePath)
Expand Down
3 changes: 2 additions & 1 deletion transactions/privateForwarder/create_private_forwarder.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ transaction {
prepare(signer: auth(IssueStorageCapabilityController, PublishCapability, SaveValue) &Account) {

let vaultData = ExampleToken.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTVaultData>()) as! FungibleTokenMetadataViews.FTVaultData?
?? panic("Could not get vault data view for the contract")
?? panic("Could not resolve FTVaultData view. The ExampleToken"
.concat(" contract needs to implement the FTVaultData Metadata view in order to execute this transaction"))

// Issue a Receiver Capability targetting the ExampleToken Vault
let receiverCapability = signer.capabilities.storage.issue<&{FungibleToken.Receiver}>(
Expand Down
3 changes: 2 additions & 1 deletion transactions/privateForwarder/setup_and_create_forwarder.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ transaction {

prepare(signer: auth(IssueStorageCapabilityController, PublishCapability, SaveValue) &Account) {
let vaultData = ExampleToken.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTVaultData>()) as! FungibleTokenMetadataViews.FTVaultData?
?? panic("Could not get vault data view for the contract")
?? panic("Could not resolve FTVaultData view. The ExampleToken"
.concat(" contract needs to implement the FTVaultData Metadata view in order to execute this transaction"))

if signer.capabilities.get<&PrivateReceiverForwarder.Forwarder>(PrivateReceiverForwarder.PrivateReceiverPublicPath).check() {
// private forwarder was already set up
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ transaction(addressAmountMap: {Address: UFix64}) {
prepare(signer: auth(BorrowValue) &Account) {

let vaultData = ExampleToken.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTVaultData>()) as! FungibleTokenMetadataViews.FTVaultData?
?? panic("Could not get vault data view for the contract")
?? panic("Could not resolve FTVaultData view. The ExampleToken"
.concat(" contract needs to implement the FTVaultData Metadata view in order to execute this transaction"))

// Get a reference to the signer's stored vault
self.vaultRef = signer.storage.borrow<auth(FungibleToken.Withdraw) &ExampleToken.Vault>(from: vaultData.storagePath)
?? panic("Could not borrow reference to the owner's Vault!")
?? panic("The signer does not store an ExampleToken.Vault object at the path "
.concat(vaultData.storagePath.toString())
.concat("The signer must initialize their account with this vault first!"))

self.privateForwardingSender = signer.storage.borrow<&PrivateReceiverForwarder.Sender>(from: PrivateReceiverForwarder.SenderStoragePath)
?? panic("Could not borrow reference to the owner's Vault!")
Expand Down
13 changes: 10 additions & 3 deletions transactions/safe_generic_transfer.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,23 @@ transaction(amount: UFix64, to: Address, senderPath: StoragePath, receiverPath:

// Get a reference to the signer's stored vault
let vaultRef = signer.storage.borrow<auth(FungibleToken.Withdraw) &{FungibleToken.Provider}>(from: senderPath)
?? panic("Could not borrow reference to the owner's Vault!")
?? panic("The signer does not store a FungibleToken.Provider object at the path "
.concat(senderPath.toString())
.concat("The signer must initialize their account with this object first!"))

self.senderReceiverRef = signer.storage.borrow<&{FungibleToken.Receiver}>(from: senderPath)
?? panic("Could not borrow {FungibleToken.Receiver} reference to the owner's Vault!")
?? panic("Could not borrow {FungibleToken.Receiver} reference to the signer's Vault!")

self.tempVault <- vaultRef.withdraw(amount: amount)
}

execute {
let receiverRef = getAccount(to).capabilities.borrow<&{FungibleToken.Receiver}>(receiverPath)!
let receiverRef = getAccount(to).capabilities.borrow<&{FungibleToken.Receiver}>(receiverPath)
?? panic("Could not borrow a Receiver reference to the FungibleToken Vault in account "
.concat(to.toString()).concat(" at path ").concat(receiverPath.toString())
.concat(". Make sure you are sending to an address that has ")
.concat("a FungibleToken Vault set up properly at the specified path."))

let supportedVaultTypes = receiverRef.getSupportedVaultTypes()
// Only transfer tokens when the receiver is willing to receive the targeted FT.
if supportedVaultTypes.containsKey(self.tempVault.getType()) {
Expand Down
Loading

0 comments on commit 800a43d

Please sign in to comment.