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

Add upgrade plan for FlowToken metadata #252

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
286 changes: 286 additions & 0 deletions transactions/update-contract/2023/aug-flowtoken/FlowToken.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
import FungibleToken from 0xf233dcee88fe0abe
import MetadataViews from 0x1d7e57aa55817448
import FungibleTokenMetadataViews from 0xf233dcee88fe0abe
import ViewResolver from 0x1d7e57aa55817448

pub contract FlowToken: FungibleToken, ViewResolver {

// Total supply of Flow tokens in existence
pub var totalSupply: UFix64

// Event that is emitted when the contract is created
pub event TokensInitialized(initialSupply: UFix64)

// Event that is emitted when tokens are withdrawn from a Vault
pub event TokensWithdrawn(amount: UFix64, from: Address?)

// Event that is emitted when tokens are deposited to a Vault
pub event TokensDeposited(amount: UFix64, to: Address?)

// Event that is emitted when new tokens are minted
pub event TokensMinted(amount: UFix64)

// Event that is emitted when tokens are destroyed
pub event TokensBurned(amount: UFix64)

// Event that is emitted when a new minter resource is created
pub event MinterCreated(allowedAmount: UFix64)

// Event that is emitted when a new burner resource is created
pub event BurnerCreated()

// Vault
//
// Each user stores an instance of only the Vault in their storage
// The functions in the Vault and governed by the pre and post conditions
// in FungibleToken when they are called.
// The checks happen at runtime whenever a function is called.
//
// Resources can only be created in the context of the contract that they
// are defined in, so there is no way for a malicious user to create Vaults
// out of thin air. A special Minter resource needs to be defined to mint
// new tokens.
//
pub resource Vault: FungibleToken.Provider, FungibleToken.Receiver, FungibleToken.Balance, MetadataViews.Resolver {

// holds the balance of a users tokens
pub var balance: UFix64

// initialize the balance at resource creation time
init(balance: UFix64) {
self.balance = balance
}

// withdraw
//
// Function that takes an integer amount as an argument
// and withdraws that amount from the Vault.
// It creates a new temporary Vault that is used to hold
// the money that is being transferred. It returns the newly
// created Vault to the context that called so it can be deposited
// elsewhere.
//
pub fun withdraw(amount: UFix64): @FungibleToken.Vault {
self.balance = self.balance - amount
emit TokensWithdrawn(amount: amount, from: self.owner?.address)
return <-create Vault(balance: amount)
}

// deposit
//
// Function that takes a Vault object as an argument and adds
// its balance to the balance of the owners Vault.
// It is allowed to destroy the sent Vault because the Vault
// was a temporary holder of the tokens. The Vault's balance has
// been consumed and therefore can be destroyed.
pub fun deposit(from: @FungibleToken.Vault) {
let vault <- from as! @FlowToken.Vault
self.balance = self.balance + vault.balance
emit TokensDeposited(amount: vault.balance, to: self.owner?.address)
vault.balance = 0.0
destroy vault
}

destroy() {
if self.balance > 0.0 {
FlowToken.totalSupply = FlowToken.totalSupply - self.balance
}
}

/// Get all the Metadata Views implemented by FlowToken
///
/// @return An array of Types defining the implemented views. This value will be used by
/// developers to know which parameter to pass to the resolveView() method.
///
pub fun getViews(): [Type]{
return FlowToken.getViews()
}

/// Get a Metadata View from FlowToken
///
/// @param view: The Type of the desired view.
/// @return A structure representing the requested view.
///
pub fun resolveView(_ view: Type): AnyStruct? {
return FlowToken.resolveView(view)
}
}

// createEmptyVault
//
// Function that creates a new Vault with a balance of zero
// and returns it to the calling context. A user must call this function
// and store the returned Vault in their storage in order to allow their
// account to be able to receive deposits of this token type.
//
pub fun createEmptyVault(): @FungibleToken.Vault {
return <-create Vault(balance: 0.0)
}

pub fun getViews(): [Type] {
return [Type<FungibleTokenMetadataViews.FTView>(),
Type<FungibleTokenMetadataViews.FTDisplay>(),
Type<FungibleTokenMetadataViews.FTVaultData>(),
Type<FungibleTokenMetadataViews.TotalSupply>()]
}

/// Get a Metadata View from FlowToken
///
/// @param view: The Type of the desired view.
/// @return A structure representing the requested view.
///
pub fun resolveView(_ view: Type): AnyStruct? {
switch view {
case Type<FungibleTokenMetadataViews.FTView>():
return FungibleTokenMetadataViews.FTView(
ftDisplay: self.resolveView(Type<FungibleTokenMetadataViews.FTDisplay>()) as! FungibleTokenMetadataViews.FTDisplay?,
ftVaultData: self.resolveView(Type<FungibleTokenMetadataViews.FTVaultData>()) as! FungibleTokenMetadataViews.FTVaultData?
)
case Type<FungibleTokenMetadataViews.FTDisplay>():
let media = MetadataViews.Media(
file: MetadataViews.HTTPFile(
url: FlowToken.getLogoURI()
),
mediaType: "image/svg+xml"
)
let medias = MetadataViews.Medias([media])
return FungibleTokenMetadataViews.FTDisplay(
name: "FLOW Network Token",
symbol: "FLOW",
description: "FLOW is the native token for the Flow blockchain. It is required for securing the network, transaction fees, storage fees, staking, FLIP voting and may be used by applications built on the Flow Blockchain",
externalURL: MetadataViews.ExternalURL("https://flow.com"),
logos: medias,
socials: {
"twitter": MetadataViews.ExternalURL("https://twitter.com/flow_blockchain")
}
)
case Type<FungibleTokenMetadataViews.FTVaultData>():
return FungibleTokenMetadataViews.FTVaultData(
storagePath: /storage/flowTokenVault,
receiverPath: /public/flowTokenReceiver,
metadataPath: /public/flowTokenBalance,
providerPath: /private/flowTokenVault,
receiverLinkedType: Type<&FlowToken.Vault{FungibleToken.Receiver, FungibleToken.Balance, MetadataViews.Resolver}>(),
metadataLinkedType: Type<&FlowToken.Vault{FungibleToken.Balance, MetadataViews.Resolver}>(),
providerLinkedType: Type<&FlowToken.Vault{FungibleToken.Provider}>(),
createEmptyVaultFunction: (fun (): @FungibleToken.Vault {
return <-FlowToken.createEmptyVault()
})
)
case Type<FungibleTokenMetadataViews.TotalSupply>():
return FungibleTokenMetadataViews.TotalSupply(totalSupply: FlowToken.totalSupply)
}
return nil
}

pub resource Administrator {
// createNewMinter
//
// Function that creates and returns a new minter resource
//
pub fun createNewMinter(allowedAmount: UFix64): @Minter {
emit MinterCreated(allowedAmount: allowedAmount)
return <-create Minter(allowedAmount: allowedAmount)
}

// createNewBurner
//
// Function that creates and returns a new burner resource
//
pub fun createNewBurner(): @Burner {
emit BurnerCreated()
return <-create Burner()
}
}

// Minter
//
// Resource object that token admin accounts can hold to mint new tokens.
//
pub resource Minter {

// the amount of tokens that the minter is allowed to mint
pub var allowedAmount: UFix64

// mintTokens
//
// Function that mints new tokens, adds them to the total supply,
// and returns them to the calling context.
//
pub fun mintTokens(amount: UFix64): @FlowToken.Vault {
pre {
amount > UFix64(0): "Amount minted must be greater than zero"
amount <= self.allowedAmount: "Amount minted must be less than the allowed amount"
}
FlowToken.totalSupply = FlowToken.totalSupply + amount
self.allowedAmount = self.allowedAmount - amount
emit TokensMinted(amount: amount)
return <-create Vault(balance: amount)
}

init(allowedAmount: UFix64) {
self.allowedAmount = allowedAmount
}
}

// Burner
//
// Resource object that token admin accounts can hold to burn tokens.
//
pub resource Burner {

// burnTokens
//
// Function that destroys a Vault instance, effectively burning the tokens.
//
// Note: the burned tokens are automatically subtracted from the
// total supply in the Vault destructor.
//
pub fun burnTokens(from: @FungibleToken.Vault) {
let vault <- from as! @FlowToken.Vault
let amount = vault.balance
destroy vault
emit TokensBurned(amount: amount)
}
}

/// Gets the Flow Logo XML URI from storage
pub fun getLogoURI(): String {
return FlowToken.account.copy<String>(from: /storage/flowTokenLogoURI) ?? ""
}

init() {
self.totalSupply = 0.0

// Create the Vault with the total supply of tokens and save it in storage
//
let vault <- create Vault(balance: self.totalSupply)

// Example of how to resolve a metadata view for a Vault
let ftView = vault.resolveView(Type<FungibleTokenMetadataViews.FTView>())

self.account.save(<-vault, to: /storage/flowTokenVault)

// Create a public capability to the stored Vault that only exposes
// the `deposit` method through the `Receiver` interface
//
self.account.link<&FlowToken.Vault{FungibleToken.Receiver, FungibleToken.Balance, MetadataViews.Resolver}>(
/public/flowTokenReceiver,
target: /storage/flowTokenVault
)

// Create a public capability to the stored Vault that only exposes
// the `balance` field through the `Balance` interface
//
self.account.link<&FlowToken.Vault{FungibleToken.Balance, MetadataViews.Resolver}>(
/public/flowTokenBalance,
target: /storage/flowTokenVault
)

let admin <- create Administrator()
self.account.save(<-admin, to: /storage/flowTokenAdmin)

// Emit an event that shows that the contract was initialized
emit TokensInitialized(initialSupply: self.totalSupply)
}
}
Loading