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

Nialexsan/cadence 1.0 destroy #584

Merged
merged 4 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
55 changes: 45 additions & 10 deletions docs/build/guides/fungible-token.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ access(all) contract FooToken: FungibleToken {
}
```

The destroy call is an important thing to handle though since if anyone ever does destroy their vault, we'll want to change the total supply of the token. You can add this inside of your `Vault` resource as well.
The destroy event is an important thing to handle though since if anyone ever does destroy their vault, we'll want to change the total supply of the token. You can add this inside of your `Vault` resource as well.

```cadence
import "FungibleToken"
Expand All @@ -230,19 +230,57 @@ access(all) contract FooToken: FungibleToken {
// ...previous code

access(all) resource Vault: FungibleToken.Provider, FungibleToken.Receiver, FungibleToken.Balance {
access(all) event ResourceDestroyed(balance: UFix64 = self.balance)

// ...other vault code

destroy() {
FooToken.totalSupply = FooToken.totalSupply - self.balance
}

}

// ...additional code
}
```

And add follow this exapmle to handle the emitted event:

```go

package main

import (
"context"
"fmt"
"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/access/http"
)

func main() {
ctx := context.Background()
// Initialize flow client
flowClient, err := http.NewClient(http.EmulatorHost)
if err != nil {
panic(err)
}

// Assuming the contract is deployed at address 0x1
contractAddress := "0x1"
eventType := fmt.Sprintf("A.%s.FooToken.Vault.ResourceDestroyed", contractAddress)

// Replace startHeight and endHeight with the actual block height range you're interested in
startHeight := uint64(0)
endHeight := uint64(100)
events, err := flowClient.GetEventsForHeightRange(ctx, eventType, startHeight, endHeight)
if err != nil {
panic(err)
}

for _, blockEvent := range events {
for _, event := range blockEvent.Events {
// run a transaction to update FooToken.totalSupply
}
}
}

```

### Creating a Minter

Let's create a minter resource which is used to mint vaults that have tokens in them. We can keep track of tokens we are minting with totalSupply
Expand Down Expand Up @@ -295,6 +333,7 @@ access(all) contract FooToken: FungibleToken {
access(all) event TokensInitialized(initialSupply: UFix64)
access(all) event TokensWithdrawn(amount: UFix64, from: Address?)
access(all) event TokensDeposited(amount: UFix64, to: Address?)
access(all) event ResourceDestroyed(balance: UFix64 = self.balance)
access(all) var totalSupply: UFix64

access(all) resource Vault: FungibleToken.Provider, FungibleToken.Receiver, FungibleToken.Balance {
Expand All @@ -314,10 +353,6 @@ access(all) contract FooToken: FungibleToken {
return <- create Vault(balance: amount)
}

destroy() {
FooToken.totalSupply = FooToken.totalSupply - self.balance
}

init(balance: UFix64) {
self.balance = balance
}
Expand Down
4 changes: 1 addition & 3 deletions docs/build/guides/nft.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,13 @@ access(all) contract FooBar {
// ...[NFT resource code]...

access(all) resource Collection {
access(all) event ResourceDestroyed(ownedNFTs: @{UInt64: NFT} = self.ownedNFTs)
access(all) var ownedNFTs: @{UInt64: NFT}

init() {
self.ownedNFTs <- {}
}

destroy () {
destroy self.ownedNFTs
}
}

// ...[NFTMinter code]...
Expand Down