-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add assertions to generic transfer and update docs
- Loading branch information
1 parent
637ead0
commit 3d667c0
Showing
14 changed files
with
394 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
import "FungibleToken" | ||
import "MetadataViews" | ||
import "FungibleTokenMetadataViews" | ||
import "ExampleToken" | ||
|
||
/// This is a contract that returns metadata for a different token | ||
/// type to try to get a user who is transferring tokens | ||
/// via the generic transactions to transfer the wrong tokens | ||
access(all) contract MaliciousToken: FungibleToken { | ||
|
||
/// The event that is emitted when new tokens are minted | ||
access(all) event TokensMinted(amount: UFix64, type: String) | ||
|
||
/// Total supply of MaliciousTokens in existence | ||
access(all) var totalSupply: UFix64 | ||
|
||
/// Storage and Public Paths | ||
access(all) let VaultStoragePath: StoragePath | ||
access(all) let VaultPublicPath: PublicPath | ||
access(all) let ReceiverPublicPath: PublicPath | ||
access(all) let AdminStoragePath: StoragePath | ||
|
||
access(all) view fun getContractViews(resourceType: Type?): [Type] { | ||
return [ | ||
Type<FungibleTokenMetadataViews.FTView>(), | ||
Type<FungibleTokenMetadataViews.FTVaultData>(), | ||
Type<FungibleTokenMetadataViews.TotalSupply>() | ||
] | ||
} | ||
|
||
access(all) fun resolveContractView(resourceType: Type?, viewType: Type): AnyStruct? { | ||
switch viewType { | ||
case Type<FungibleTokenMetadataViews.FTView>(): | ||
return FungibleTokenMetadataViews.FTView( | ||
ftDisplay: self.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTDisplay>()) as! FungibleTokenMetadataViews.FTDisplay?, | ||
ftVaultData: self.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTVaultData>()) as! FungibleTokenMetadataViews.FTVaultData? | ||
) | ||
case Type<FungibleTokenMetadataViews.FTVaultData>(): | ||
return FungibleTokenMetadataViews.FTVaultData( | ||
storagePath: /storage/exampleTokenVault, | ||
receiverPath: /public/exampleTokenReceiver, | ||
metadataPath: /public/exampleTokenVault, | ||
receiverLinkedType: Type<&ExampleToken.Vault>(), | ||
metadataLinkedType: Type<&ExampleToken.Vault>(), | ||
createEmptyVaultFunction: (fun(): @{FungibleToken.Vault} { | ||
return <-MaliciousToken.createEmptyVault(vaultType: Type<@MaliciousToken.Vault>()) | ||
}) | ||
) | ||
case Type<FungibleTokenMetadataViews.TotalSupply>(): | ||
return FungibleTokenMetadataViews.TotalSupply( | ||
totalSupply: MaliciousToken.totalSupply | ||
) | ||
} | ||
return nil | ||
} | ||
|
||
/// 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. | ||
/// | ||
access(all) resource Vault: FungibleToken.Vault { | ||
|
||
/// The total balance of this vault | ||
access(all) var balance: UFix64 | ||
|
||
// initialize the balance at resource creation time | ||
init(balance: UFix64) { | ||
self.balance = balance | ||
} | ||
|
||
/// Called when a fungible token is burned via the `Burner.burn()` method | ||
access(contract) fun burnCallback() { | ||
if self.balance > 0.0 { | ||
MaliciousToken.totalSupply = MaliciousToken.totalSupply - self.balance | ||
} | ||
self.balance = 0.0 | ||
} | ||
|
||
/// In fungible tokens, there are no specific views for specific vaults, | ||
/// So we can route calls to view functions to the contract views functions | ||
access(all) view fun getViews(): [Type] { | ||
return MaliciousToken.getContractViews(resourceType: nil) | ||
} | ||
|
||
access(all) fun resolveView(_ view: Type): AnyStruct? { | ||
return MaliciousToken.resolveContractView(resourceType: nil, viewType: view) | ||
} | ||
|
||
/// getSupportedVaultTypes optionally returns a list of vault types that this receiver accepts | ||
access(all) view fun getSupportedVaultTypes(): {Type: Bool} { | ||
let supportedTypes: {Type: Bool} = {} | ||
supportedTypes[self.getType()] = true | ||
return supportedTypes | ||
} | ||
|
||
access(all) view fun isSupportedVaultType(type: Type): Bool { | ||
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 | ||
/// and withdraws that amount from the Vault. | ||
/// | ||
/// It creates a new temporary Vault that is used to hold | ||
/// the tokens that are being transferred. It returns the newly | ||
/// created Vault to the context that called so it can be deposited | ||
/// elsewhere. | ||
/// | ||
access(FungibleToken.Withdraw) fun withdraw(amount: UFix64): @MaliciousToken.Vault { | ||
self.balance = self.balance - amount | ||
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. | ||
/// | ||
access(all) fun deposit(from: @{FungibleToken.Vault}) { | ||
let vault <- from as! @MaliciousToken.Vault | ||
self.balance = self.balance + vault.balance | ||
destroy vault | ||
} | ||
|
||
/// 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. | ||
/// | ||
access(all) fun createEmptyVault(): @MaliciousToken.Vault { | ||
return <-create Vault(balance: 0.0) | ||
} | ||
} | ||
|
||
/// Minter | ||
/// | ||
/// Resource object that token admin accounts can hold to mint new tokens. | ||
/// | ||
access(all) resource Minter { | ||
/// mintTokens | ||
/// | ||
/// Function that mints new tokens, adds them to the total supply, | ||
/// and returns them to the calling context. | ||
/// | ||
access(all) fun mintTokens(amount: UFix64): @MaliciousToken.Vault { | ||
MaliciousToken.totalSupply = MaliciousToken.totalSupply + amount | ||
let vault <-create Vault(balance: amount) | ||
emit TokensMinted(amount: amount, type: vault.getType().identifier) | ||
return <-vault | ||
} | ||
} | ||
|
||
/// 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. | ||
/// | ||
access(all) fun createEmptyVault(vaultType: Type): @MaliciousToken.Vault { | ||
return <- create Vault(balance: 0.0) | ||
} | ||
|
||
init() { | ||
self.totalSupply = 1000.0 | ||
|
||
self.VaultStoragePath = /storage/maliciousTokenVault | ||
self.VaultPublicPath = /public/maliciousTokenVault | ||
self.ReceiverPublicPath = /public/maliciousTokenReceiver | ||
self.AdminStoragePath = /storage/maliciousTokenAdmin | ||
|
||
// Create the Vault with the total supply of tokens and save it in storage | ||
// | ||
let vault <- create Vault(balance: self.totalSupply) | ||
emit TokensMinted(amount: vault.balance, type: vault.getType().identifier) | ||
|
||
// Create a public capability to the stored Vault that exposes | ||
// the `deposit` method and getAcceptedTypes method through the `Receiver` interface | ||
// and the `balance` method through the `Balance` interface | ||
// | ||
let maliciousTokenCap = self.account.capabilities.storage.issue<&MaliciousToken.Vault>(self.VaultStoragePath) | ||
self.account.capabilities.publish(maliciousTokenCap, at: self.VaultPublicPath) | ||
let receiverCap = self.account.capabilities.storage.issue<&MaliciousToken.Vault>(self.VaultStoragePath) | ||
self.account.capabilities.publish(receiverCap, at: self.ReceiverPublicPath) | ||
|
||
self.account.storage.save(<-vault, to: /storage/maliciousTokenVault) | ||
|
||
let admin <- create Minter() | ||
self.account.storage.save(<-admin, to: self.AdminStoragePath) | ||
} | ||
} |
Oops, something went wrong.