Skip to content

Commit 13a3da6

Browse files
authored
update dependencies and use example token instead of flow token (#20)
* updating dependencies and using example token instead of flow token * updating dependencies * fixing README and example transactions * using better string templating * go mod tidy * make generate contracts
1 parent 0e8024a commit 13a3da6

19 files changed

+634
-633
lines changed

README.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@ The feedback we are looking for is:
4545

4646
## Basics of the Standard:
4747

48-
The code for the standard is in `src/contracts/FungibleToken.cdc`. An example implementation of the standard that simulates what a simple FlowToken would be like is in `src/contracts/FlowToken.cdc`.
48+
The code for the standard is in `src/contracts/FungibleToken.cdc`. An example implementation of the standard that simulates what a simple token would be like is in `src/contracts/ExampleToken.cdc`.
49+
50+
The exact smart contract that is used for the official Flow Network Token is in `src/contracts/FlowToken.cdc`
4951

5052
Example transactions that users could use to interact with fungible tokens are located in the `src/transactions/` directory.
53+
Go transaction templates are in the `test/templates.go` file. These templates are mostly generic and can be used with any fungible token implementation by providing the correct addresses, names, and values.
5154

5255
The standard consists of a contract interface called `FungibleToken` that requires implementing contracts to define a `Vault` resource that represents the tokens that an account owns. Each account that owns tokens will have a `Vault` stored in its account storage. Users call functions on each other's `Vault`s to send and receive tokens.
5356

@@ -60,7 +63,7 @@ Right now we are using unsigned 64-bit fixed point numbers `UFix64` as the type
6063
- `pub var totalSupply: UFix64`
6164
- The only required field of the contract. It would be incremented when new tokens are minted and decremented when they are destroyed.
6265
- Event that gets emitted when the contract is initialized
63-
- `pub event FungibleTokenInitialized(initialSupply: UFix64)`
66+
- `pub event TokensInitialized(initialSupply: UFix64)`
6467

6568
2- Retrieving the token fields of a `Vault` in an account that owns tokens.
6669

@@ -71,7 +74,7 @@ Right now we are using unsigned 64-bit fixed point numbers `UFix64` as the type
7174
3- Withdrawing a specific amount of tokens *amount* using the *withdraw* function of the owner's `Vault`
7275

7376
- Provider interface
74-
- `pub fun withdraw(amount: UFix64): @Vault`
77+
- `pub fun withdraw(amount: UFix64): @FungibleToken.Vault`
7578
- Conditions
7679
- the returned Vault's balance must equal the amount withdrawn
7780
- The amount withdrawn must be less than or equal to the balance
@@ -81,29 +84,33 @@ Right now we are using unsigned 64-bit fixed point numbers `UFix64` as the type
8184
- Indicates how much was withdrawn and from what account the `Vault` is stored in.
8285
If the `Vault` is not in account storage when the event is emitted,
8386
`from` will be `nil`.
84-
- `pub event withdraw(amount: UFix64, from: Address?)`
87+
- `pub event TokensWithdrawn(amount: UFix64, from: Address?)`
8588

8689
4 - Depositing a specific amount of tokens *from* using the *deposit* function of the recipient's `Vault`
8790

8891
- `Receiver` interface
89-
- `pub fun deposit(from: @Vault)`
92+
- `pub fun deposit(from: @FungibleToken.Vault)`
9093
- Conditions
9194
- `from` balance must be non-zero
9295
- The resulting balance must be equal to the initial balance + the balance of `from`
9396
- deposit event
9497
- Indicates how much was deposited and to what account the `Vault` is stored in.
9598
If the `Vault` is not in account storage when the event is emitted,
9699
`to` will be `nil`.
97-
- `pub event Deposit(amount: UFix64, to: Address?)`
98-
- Users could create custom `Receiver`s to trigger special code when transfers to them happen.
100+
- `pub event TokensDeposited(amount: UFix64, to: Address?)`
101+
- Users could create custom `Receiver`s to trigger special code when transfers to them happen, like forwarding the tokens
102+
to another account, splitting them up, and much more.
103+
104+
- **ATTENTION**: It is VITALLY important that if you are making your own implementation of the fungible token interface that
105+
you cast the input to `deposit` as the type of your token.
106+
`let vault <- from as! @ExampleToken.Vault`
107+
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.
99108

100109
5 - Creating an empty Vault resource
101110

102-
- `pub fun createEmptyVault(): @Vault`
103-
- Currently have no event
104-
- Defined in the contract, but not in the `Vault` resource.
105-
This means that to create an empty `Vault`,
106-
the caller would always have to call the function in the contract.
111+
- `pub fun createEmptyVault(): @FungibleToken.Vault`
112+
- Defined in the contract
113+
To create an empty `Vault`, the caller calls the function in the contract and stores the Vault in their storage.
107114
- Conditions:
108115
- the balance of the returned Vault must be 0
109116

@@ -173,7 +180,7 @@ A standard for token metadata is still an unsolved problem in the general blockc
173180
To use the Flow Token contract as is, you need to follow these steps:
174181

175182
1. Deploy the `FungibleToken` definition to account `0x02`
176-
2. Deploy the `FlowToken` definition to account `0x03`
183+
2. Deploy the `ExampleToken` definition to account `0x03`
177184
3. You can use the `get_balance.cdc` or `get_supply.cdc` scripts to read the
178185
balance of a user's `Vault` or the total supply of all tokens, respectively.
179186
4. Use the `setupAccount.cdc` on any account to set up the account to be able to

contracts/contracts.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import (
1111
const (
1212
fungibleTokenFilename = "FungibleToken.cdc"
1313
flowTokenFilename = "FlowToken.cdc"
14+
exampleTokenFilename = "ExampleToken.cdc"
1415
defaultFungibleTokenAddress = "02"
16+
tokenForwardingFilename = "TokenForwarding.cdc"
1517
)
1618

1719
// FungibleToken returns the FungibleToken contract interface.
@@ -33,3 +35,33 @@ func FlowToken(fungibleTokenAddr string) []byte {
3335

3436
return []byte(code)
3537
}
38+
39+
// ExampleToken returns the ExampleToken contract.
40+
//
41+
// The returned contract will import the FungibleToken contract from the specified address.
42+
func ExampleToken(fungibleTokenAddr string) []byte {
43+
code := assets.MustAssetString(exampleTokenFilename)
44+
45+
code = strings.ReplaceAll(
46+
code,
47+
"0x"+defaultFungibleTokenAddress,
48+
"0x"+fungibleTokenAddr,
49+
)
50+
51+
return []byte(code)
52+
}
53+
54+
// TokenForwarding returns the TokenForwarding contract.
55+
//
56+
// The returned contract will import the FungibleToken contract from the specified address.
57+
func TokenForwarding(fungibleTokenAddr string) []byte {
58+
code := assets.MustAssetString(tokenForwardingFilename)
59+
60+
code = strings.ReplaceAll(
61+
code,
62+
"0x"+defaultFungibleTokenAddress,
63+
"0x"+fungibleTokenAddr,
64+
)
65+
66+
return []byte(code)
67+
}

contracts/contracts_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,15 @@ func TestFlowTokenContract(t *testing.T) {
2121
assert.NotNil(t, contract)
2222
assert.Contains(t, string(contract), addrA.Hex())
2323
}
24+
25+
func TestExampleTokenContract(t *testing.T) {
26+
contract := contracts.ExampleToken(addrA.Hex())
27+
assert.NotNil(t, contract)
28+
assert.Contains(t, string(contract), addrA.Hex())
29+
}
30+
31+
func TestTokenForwardingContract(t *testing.T) {
32+
contract := contracts.TokenForwarding(addrA.Hex())
33+
assert.NotNil(t, contract)
34+
assert.Contains(t, string(contract), addrA.Hex())
35+
}

contracts/go.mod

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@ module github.com/onflow/flow-ft/contracts
33
go 1.14
44

55
require (
6+
github.com/ethereum/go-ethereum v1.9.13 // indirect
67
github.com/kevinburke/go-bindata v3.21.0+incompatible // indirect
7-
github.com/onflow/flow-go-sdk v0.3.0-beta1
8+
github.com/onflow/cadence v0.4.0 // indirect
9+
github.com/onflow/flow-go-sdk v0.4.0
10+
github.com/pkg/errors v0.9.1 // indirect
811
github.com/stretchr/testify v1.5.1
12+
golang.org/x/crypto v0.0.0-20200429183012-4b2356b1ed79 // indirect
13+
golang.org/x/lint v0.0.0-20200130185559-910be7a94367 // indirect
14+
golang.org/x/sys v0.0.0-20200501145240-bc7a7d42d5c3 // indirect
15+
golang.org/x/tools v0.0.0-20200323144430-8dcfad9e016e // indirect
16+
gopkg.in/yaml.v2 v2.2.8 // indirect
917
)

0 commit comments

Comments
 (0)