Skip to content

Commit

Permalink
Update fungible token interface to check the type deposited in the de…
Browse files Browse the repository at this point in the history
…posit function (#51)

* type check in deposit

* update dependencies

* use isInstance and readme

* update versions
  • Loading branch information
joshuahannan authored Apr 20, 2021
1 parent 2ef3c89 commit 54218d0
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ Right now we are using unsigned 64-bit fixed point numbers `UFix64` as the type
- Users could create custom `Receiver`s to trigger special code when transfers to them happen, like forwarding the tokens
to another account, splitting them up, and much more.

- **ATTENTION**: It is VITALLY important that if you are making your own implementation of the fungible token interface that
- It is important that if you are making your own implementation of the fungible token interface that
you cast the input to `deposit` as the type of your token.
`let vault <- from as! @ExampleToken.Vault`
Because the interface specifies the argument as `@FungibleToken.Vault`, any resource that satisfies this can be sent to the deposit function. If you do not cast it as the type of your token, others could deposit different tokens into your Vault maliciously to change the balance.
The interface specifies the argument as `@FungibleToken.Vault`, any resource that satisfies this can be sent to the deposit function. The interface checks that the concrete types match, but you'll still need to cast the `Vault` before storing it.

5 - Creating an empty Vault resource

Expand Down
6 changes: 6 additions & 0 deletions contracts/FungibleToken.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ pub contract interface FungibleToken {
/// deposit takes a Vault and adds its balance to the balance of this Vault
///
pub fun deposit(from: @Vault) {
// Assert that the concrete type of the deposited vault is the same
// as the vault that is accepting the deposit
pre {
from.isInstance(self.getType()):
"Cannot deposit an incompatible token type"
}
post {
self.balance == before(self.balance) + before(from.balance):
"New Vault balance must be the sum of the previous balance and the deposited Vault"
Expand Down
6 changes: 3 additions & 3 deletions lib/go/contracts/internal/assets/assets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/go/templates/transaction_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func GenerateBurnTokensScript(fungibleAddr, tokenAddr flow.Address, tokenName st
func GenerateTransferInvalidVaultScript(fungibleAddr, tokenAddr, otherTokenAddr, receiverAddr flow.Address, tokenName, otherTokenName string, amount int) []byte {
storageName := MakeFirstLowerCase(tokenName)

otherStorageName := MakeFirstLowerCase(tokenName)
otherStorageName := MakeFirstLowerCase(otherTokenName)

template := `
import FungibleToken from 0x%s
Expand Down
Loading

0 comments on commit 54218d0

Please sign in to comment.