Skip to content

Commit

Permalink
fix example token, remove getBalance, add isAvailableToWithdraw
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuahannan committed Jan 25, 2024
1 parent 01706d1 commit 6d66d29
Show file tree
Hide file tree
Showing 26 changed files with 288 additions and 716 deletions.
14 changes: 7 additions & 7 deletions contracts/ExampleToken.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ access(all) contract ExampleToken: FungibleToken {
let vaultRef = self.account.capabilities.borrow<&ExampleToken.Vault>(/public/exampleTokenVault)
?? panic("Could not borrow a reference to the vault resolver")

return vaultRef.resolveView(view)
return vaultRef.resolveView(viewType)
}

/// Vault
Expand Down Expand Up @@ -58,11 +58,6 @@ access(all) contract ExampleToken: FungibleToken {
self.receiverPath = PublicPath(identifier: "exampleTokenReceiver")!
}

/// Get the balance of the vault
access(all) view fun getBalance(): UFix64 {
return self.balance
}

/// Called when a fungible token is burned via the `Burner.burn()` method
access(contract) fun burnCallback() {
if self.balance > 0.0 {
Expand Down Expand Up @@ -137,6 +132,11 @@ access(all) contract ExampleToken: FungibleToken {
return self.getSupportedVaultTypes()[type] ?? false
}

/// Asks if the amount can be withdrawn from this vault
access(all) view fun isAvailableToWithdraw(amount: UFix64): Bool {
return amount <= self.balance
}

/// withdraw
///
/// Function that takes an amount as an argument
Expand Down Expand Up @@ -219,7 +219,7 @@ access(all) contract ExampleToken: FungibleToken {

// Create a public capability to the stored Vault that exposes
// the `deposit` method and getAcceptedTypes method through the `Receiver` interface
// and the `getBalance()` method through the `Balance` interface
// and the `balance` method through the `Balance` interface
//
let exampleTokenCap = self.account.capabilities.storage.issue<&Vault>(vault.storagePath)
self.account.capabilities.publish(exampleTokenCap, at: vault.publicPath)
Expand Down
51 changes: 18 additions & 33 deletions contracts/FungibleToken.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -48,39 +48,20 @@ access(all) contract interface FungibleToken: ViewResolver {

/// The event that is emitted when tokens are withdrawn from a Vault
access(all) event Withdrawn(type: String, amount: UFix64, from: Address?, fromUUID: UInt64, withdrawnUUID: UInt64)
// access(contract) view fun emitWithdrawnEvent(type: String, amount: UFix64, from: Address?, fromUUID: UInt64, withdrawnUUID: UInt64): Bool {
// if (amount > 0.0) && (from != nil) && (from != 0x8624b52f9ddcd04a) && (from != 0x9eca2b38b18b5dfe) {
// emit Withdrawn(type: type, amount: amount, from: from, fromUUID: fromUUID, withdrawnUUID: withdrawnUUID)
// }
// return true
// }

/// The event that is emitted when tokens are deposited to a Vault
access(all) event Deposited(type: String, amount: UFix64, to: Address?, toUUID: UInt64, depositedUUID: UInt64)
// access(contract) view fun emitDepositedEvent(type: String, amount: UFix64, to: Address?, toUUID: UInt64, depositedUUID: UInt64): Bool {
// if (amount > 0.0) && (to != nil) && (to != 0x8624b52f9ddcd04a) && (to != 0x9eca2b38b18b5dfe) {
// emit Deposited(type: type, amount: amount, to: to, toUUID: toUUID, depositedUUID: depositedUUID)
// }
// return true
// }

/// Event that is emitted when the global burn method is called with a non-zero balance
access(all) event Burned(type: String, amount: UFix64, fromUUID: UInt64)
// access(contract) view fun emitBurnedEvent(type: String, amount: UFix64, fromUUID: UInt64): Bool {
// if amount > 0.0 {
// emit Burned(type: type, amount: amount, fromUUID: fromUUID)
// }
// return true
// }

/// Balance
///
/// The interface that provides standard functions\
/// for getting balance information
///
access(all) resource interface Balance {
/// Get the balance of the vault
access(all) view fun getBalance(): UFix64
access(all) var balance: UFix64
}

/// Provider
Expand All @@ -94,6 +75,16 @@ access(all) contract interface FungibleToken: ViewResolver {
///
access(all) resource interface Provider {

/// Function to ask a provider if a specific amount of tokens
/// is available to be withdrawn
/// This could be useful to avoid panicing when calling withdraw
/// when the balance is unknown
/// Additionally, if the provider is pulling from multiple vaults
/// it only needs to check some of the vaults until the desired amount
/// is reached, potentially helping with performance.
///
access(all) view fun isAvailableToWithdraw(amount: UFix64): Bool

/// withdraw subtracts tokens from the implementing resource
/// and returns a Vault with the removed tokens.
///
Expand All @@ -104,9 +95,8 @@ access(all) contract interface FungibleToken: ViewResolver {
access(Withdraw) fun withdraw(amount: UFix64): @{Vault} {
post {
// `result` refers to the return value
result.getBalance() == amount:
result.balance == amount:
"Withdrawal amount must be the same as the balance of the withdrawn Vault"
//FungibleToken.emitWithdrawnEvent(type: self.getType().identifier, amount: amount, from: self.owner?.address, fromUUID: self.uuid, withdrawnUUID: result.uuid)
emit Withdrawn(type: self.getType().identifier, amount: amount, from: self.owner?.address, fromUUID: self.uuid, withdrawnUUID: result.uuid)
}
}
Expand Down Expand Up @@ -146,9 +136,6 @@ access(all) contract interface FungibleToken: ViewResolver {
/// Field that tracks the balance of a vault
access(all) var balance: UFix64

/// Get the balance of the vault
access(all) view fun getBalance(): UFix64

/// Called when a fungible token is burned via the `Burner.burn()` method
/// Implementations can do any bookkeeping or emit any events
/// that should be emitted when a vault is destroyed.
Expand All @@ -158,7 +145,6 @@ access(all) contract interface FungibleToken: ViewResolver {
/// This is to prevent vault owners from spamming fake Burned events.
access(contract) fun burnCallback() {
pre {
//FungibleToken.emitBurnedEvent(type: self.getType().identifier, amount: self.balance, fromUUID: self.uuid)
emit Burned(type: self.getType().identifier, amount: self.balance, fromUUID: self.uuid)
}
post {
Expand Down Expand Up @@ -192,14 +178,14 @@ access(all) contract interface FungibleToken: ViewResolver {
///
access(Withdraw) fun withdraw(amount: UFix64): @{Vault} {
pre {
self.getBalance() >= amount:
self.balance >= amount:
"Amount withdrawn must be less than or equal than the balance of the Vault"
}
post {
// use the special function `before` to get the value of the `balance` field
// at the beginning of the function execution
//
self.getBalance() == before(self.getBalance()) - amount:
self.balance == before(self.balance) - amount:
"New Vault balance must be the difference of the previous balance and the withdrawn Vault balance"
}
}
Expand All @@ -212,11 +198,10 @@ access(all) contract interface FungibleToken: ViewResolver {
pre {
from.isInstance(self.getType()):
"Cannot deposit an incompatible token type"
//FungibleToken.emitDepositedEvent(type: from.getType().identifier, amount: from.getBalance(), to: self.owner?.address, toUUID: self.uuid, depositedUUID: from.uuid)
emit Deposited(type: from.getType().identifier, amount: from.getBalance(), to: self.owner?.address, toUUID: self.uuid, depositedUUID: from.uuid)
emit Deposited(type: from.getType().identifier, amount: from.balance, to: self.owner?.address, toUUID: self.uuid, depositedUUID: from.uuid)
}
post {
self.getBalance() == before(self.getBalance()) + before(from.getBalance()):
self.balance == before(self.balance) + before(from.balance):
"New Vault balance must be the sum of the previous balance and the deposited Vault"
}
}
Expand All @@ -225,7 +210,7 @@ access(all) contract interface FungibleToken: ViewResolver {
///
access(all) fun createEmptyVault(): @{Vault} {
post {
result.getBalance() == 0.0: "The newly created Vault must have zero balance"
result.balance == 0.0: "The newly created Vault must have zero balance"
}
}
}
Expand All @@ -235,7 +220,7 @@ access(all) contract interface FungibleToken: ViewResolver {
access(all) fun createEmptyVault(vaultType: Type): @{FungibleToken.Vault} {
post {
result.getType() == vaultType: "The returned vault does not match the desired type"
result.getBalance() == 0.0: "The newly created Vault must have zero balance"
result.balance == 0.0: "The newly created Vault must have zero balance"
}
}
}
6 changes: 3 additions & 3 deletions contracts/FungibleTokenSwitchboard.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,14 @@ access(all) contract FungibleTokenSwitchboard {
// If we can borrow a reference to the vault...
if let vaultRef = depositedVaultCapability.borrow() {
// We deposit the funds on said vault
vaultRef.deposit(from: <-from.withdraw(amount: from.getBalance()))
vaultRef.deposit(from: <-from.withdraw(amount: from.balance))
}
}
// if deposit failed for some reason
if from.getBalance() > 0.0 {
if from.balance > 0.0 {
emit NotCompletedDeposit(
type: from.getType(),
amount: from.getBalance(),
amount: from.balance,
switchboardOwner: self.owner?.address,
)
return <-from
Expand Down
2 changes: 1 addition & 1 deletion contracts/utility/PrivateReceiverForwarder.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ access(all) contract PrivateReceiverForwarder {
access(contract) fun deposit(from: @{FungibleToken.Vault}) {
let receiverRef = self.recipient.borrow()!

let balance = from.getBalance()
let balance = from.balance

let uuid = from.uuid

Expand Down
2 changes: 1 addition & 1 deletion contracts/utility/TokenForwarding.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ access(all) contract TokenForwarding {
access(all) fun deposit(from: @{FungibleToken.Vault}) {
let receiverRef = self.recipient.borrow<&{FungibleToken.Receiver}>()!

let balance = from.getBalance()
let balance = from.balance

let uuid = from.uuid

Expand Down
Loading

0 comments on commit 6d66d29

Please sign in to comment.