Skip to content

Commit

Permalink
remove custom destructors
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuahannan committed Dec 12, 2023
1 parent eb1dd95 commit 24d5952
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 87 deletions.
17 changes: 1 addition & 16 deletions contracts/NonFungibleToken-v2.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -84,28 +84,13 @@ access(all) contract NonFungibleToken {
///
access(all) event Deposit(id: UInt64, uuid: UInt64, to: Address?, type: String)

/// Destroy
///
/// The event that should be emitted when an NFT is destroyed
access(all) event Destroy(id: UInt64, uuid: UInt64, type: String)

access(self) view fun emitNFTDestroy(id: UInt64, uuid: UInt64, type: String): Bool
{
emit Destroy(id: id, uuid: uuid, type: type)
return true
}

/// Interface that the NFTs must conform to
///
access(all) resource interface NFT: ViewResolver.Resolver {
/// The unique ID that each NFT has
access(all) view fun getID(): UInt64

destroy() {
pre {
NonFungibleToken.emitNFTDestroy(id: self.getID(), uuid: self.uuid, type: self.getType().identifier)
}
}
// access(all) event ResourceDestroyed(uuid: UInt64 = self.uuid, type: self.getType().identifier)
/// Get a reference to an NFT that this NFT owns
/// Both arguments are optional to allow the NFT to choose
Expand Down
74 changes: 7 additions & 67 deletions contracts/utility/FungibleToken.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,9 @@ access(all) contract FungibleToken {

/// The event that is emitted when tokens are withdrawn from a Vault
access(all) event Withdraw(amount: UFix64, from: Address?, type: String)
access(self) fun emitWithdrawEvent(amount: UFix64, from: Address?, type: String): Bool {
emit Withdraw(amount: amount, from: from, type: type)
return true
}

/// The event that is emitted when tokens are deposited to a Vault
access(all) event Deposit(amount: UFix64, to: Address?, type: String)
access(self) fun emitDepositEvent(amount: UFix64, to: Address?, type: String): Bool {
emit Deposit(amount: amount, to: to, type: type)
return true
}

/// The event that is emitted when tokens are transferred from one account to another
access(all) event Transfer(amount: UFix64, from: Address?, to: Address?, type: String)
access(self) fun emitTransferEvent(amount: UFix64, from: Address?, to: Address?, type: String): Bool {
emit Transfer(amount: amount, from: from, to: to, type: type)
return true
}

/// Event emitted when tokens are destroyed
access(all) event Burn(amount: UFix64, type: String)

access(self) fun emitBurnEvent(amount: UFix64, type: String): Bool {
if amount >= 0.0 {
emit Burn(amount: amount, type: type)
}
return true
}

/// Provider
///
Expand Down Expand Up @@ -107,7 +82,7 @@ access(all) contract FungibleToken {
// `result` refers to the return value
result.getBalance() == amount:
"Withdrawal amount must be the same as the balance of the withdrawn Vault"
//FungibleToken.emitWithdrawEvent(amount: amount, from: self.owner?.address, type: self.getType().identifier)
emit Withdraw(amount: amount, from: self.owner?.address, type: self.getType().identifier)
}
}
}
Expand Down Expand Up @@ -140,22 +115,14 @@ access(all) contract FungibleToken {
}
}

access(all) resource interface Transferor {
/// Function for a direct transfer instead of having to do a deposit and withdrawal
///
access(Withdrawable) fun transfer(amount: UFix64, receiver: Capability<&{FungibleToken.Receiver}>) {
pre {
receiver.check(): "Could not borrow a reference to the NFT receiver"
}
}
}

/// Vault
///
/// Ideally, this interface would also conform to Receiver, Balance, Transferor, Provider, and Resolver
/// but that is not supported yet
///
access(all) resource interface Vault: Receiver, Transferor, Provider, ViewResolver.Resolver { //,Balance {
access(all) resource interface Vault: Receiver, Provider, ViewResolver.Resolver {

//access(all) event ResourceDestroyed(balance: UFix64 = self.getBalance(), type: Type = self.getType().identifier)
/// Get the balance of the vault
access(all) view fun getBalance(): UFix64
Expand All @@ -178,14 +145,10 @@ access(all) contract FungibleToken {
}

/// Returns the storage path where the vault should typically be stored
access(all) view fun getDefaultStoragePath(): StoragePath? {
return nil
}
access(all) view fun getDefaultStoragePath(): StoragePath?

/// Returns the public path where this vault should have a public capability
access(all) view fun getDefaultPublicPath(): PublicPath? {
return nil
}
access(all) view fun getDefaultPublicPath(): PublicPath?

/// Returns the public path where this vault's Receiver should have a public capability
/// Publishing a Receiver Capability at a different path enables alternate Receiver implementations to be used
Expand All @@ -194,13 +157,6 @@ access(all) contract FungibleToken {
return nil
}

// access(all) view fun getViews(): [Type] {
// pre { true: "dummy" }
// }
// access(all) fun resolveView(_ view: Type): AnyStruct? {
// pre { true: "dummy" }
// }
/// withdraw subtracts `amount` from the Vault's balance
/// and returns a new Vault with the subtracted balance
///
Expand All @@ -226,36 +182,20 @@ access(all) contract FungibleToken {
pre {
from.isInstance(self.getType()):
"Cannot deposit an incompatible token type"
//FungibleToken.emitDepositEvent(amount: from.getBalance(), to: self.owner?.address, type: from.getType().identifier)
emit Deposit(amount: from.getBalance(), to: self.owner?.address, type: from.getType().identifier)
}
post {
self.getBalance() == before(self.getBalance()) + before(from.getBalance()):
"New Vault balance must be the sum of the previous balance and the deposited Vault"
}
}

/// Function for a direct transfer instead of having to do a deposit and withdrawal
///
access(Withdrawable) fun transfer(amount: UFix64, receiver: Capability<&{FungibleToken.Receiver}>) {
post {
self.getBalance() == before(self.getBalance()) - amount:
"New Vault balance from the sender must be the difference of the previous balance and the withdrawn Vault balance"
//FungibleToken.emitTransferEvent(amount: amount, from: self.owner?.address, to: receiver.borrow()?.owner?.address, type: self.getType().identifier)
}
}

/// createEmptyVault allows any user to create a new Vault that has a zero balance
///
access(all) fun createEmptyVault(): @{Vault} {
post {
result.getBalance() == 0.0: "The newly created Vault must have zero balance"
}
}

destroy() {
pre {
//FungibleToken.emitBurnEvent(amount: self.getBalance(), type: self.getType().identifier)
}
}
}
}
Loading

0 comments on commit 24d5952

Please sign in to comment.