Skip to content

Commit

Permalink
Adds custom example token contract Getters (#22)
Browse files Browse the repository at this point in the history
* adding custom example token

* fixing PR comments

* replaced Address templates
  • Loading branch information
joshuahannan authored Jun 5, 2020
1 parent 9f92bdb commit 755c0dd
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 19 deletions.
48 changes: 45 additions & 3 deletions contracts/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const (
fungibleTokenFilename = "FungibleToken.cdc"
flowTokenFilename = "FlowToken.cdc"
exampleTokenFilename = "ExampleToken.cdc"
defaultFungibleTokenAddress = "02"
defaultFungibleTokenAddress = "FUNGIBLETOKENADDRESS"
tokenForwardingFilename = "TokenForwarding.cdc"
)

Expand All @@ -23,7 +23,7 @@ func FungibleToken() []byte {

// FlowToken returns the FlowToken contract.
//
// The returned contract will import the FungibleToken contract from the specified address.
// The returned contract will import the FungibleToken interface from the specified address.
func FlowToken(fungibleTokenAddr string) []byte {
code := assets.MustAssetString(flowTokenFilename)

Expand All @@ -38,7 +38,7 @@ func FlowToken(fungibleTokenAddr string) []byte {

// ExampleToken returns the ExampleToken contract.
//
// The returned contract will import the FungibleToken contract from the specified address.
// The returned contract will import the FungibleToken interface from the specified address.
func ExampleToken(fungibleTokenAddr string) []byte {
code := assets.MustAssetString(exampleTokenFilename)

Expand All @@ -51,6 +51,27 @@ func ExampleToken(fungibleTokenAddr string) []byte {
return []byte(code)
}

// CustomToken returns the ExampleToken contract with a custom name.
//
// The returned contract will import the FungibleToken interface from the specified address.
func CustomToken(fungibleTokenAddr, tokenName string) []byte {
code := assets.MustAssetString(exampleTokenFilename)

code = strings.ReplaceAll(
code,
"0x"+defaultFungibleTokenAddress,
"0x"+fungibleTokenAddr,
)

code = strings.ReplaceAll(
code,
"ExampleToken",
tokenName,
)

return []byte(code)
}

// TokenForwarding returns the TokenForwarding contract.
//
// The returned contract will import the FungibleToken contract from the specified address.
Expand All @@ -65,3 +86,24 @@ func TokenForwarding(fungibleTokenAddr string) []byte {

return []byte(code)
}

// CustomTokenForwarding returns the TokenForwarding contract for a custom token
//
// The returned contract will import the FungibleToken interface from the specified address.
func CustomTokenForwarding(fungibleTokenAddr, tokenName string) []byte {
code := assets.MustAssetString(tokenForwardingFilename)

code = strings.ReplaceAll(
code,
"0x"+defaultFungibleTokenAddress,
"0x"+fungibleTokenAddr,
)

code = strings.ReplaceAll(
code,
"ExampleToken",
tokenName,
)

return []byte(code)
}
12 changes: 12 additions & 0 deletions contracts/contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,20 @@ func TestExampleTokenContract(t *testing.T) {
assert.Contains(t, string(contract), addrA.Hex())
}

func TestCustomExampleTokenContract(t *testing.T) {
contract := contracts.CustomToken(addrA.Hex(), "UtilityCoin")
assert.NotNil(t, contract)
assert.Contains(t, string(contract), addrA.Hex())
}

func TestTokenForwardingContract(t *testing.T) {
contract := contracts.TokenForwarding(addrA.Hex())
assert.NotNil(t, contract)
assert.Contains(t, string(contract), addrA.Hex())
}

func TestCustomTokenForwardingContract(t *testing.T) {
contract := contracts.CustomTokenForwarding(addrA.Hex(), "UtilityCoin")
assert.NotNil(t, contract)
assert.Contains(t, string(contract), addrA.Hex())
}
18 changes: 9 additions & 9 deletions contracts/internal/assets/assets.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/contracts/ExampleToken.cdc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import FungibleToken from 0x02
import FungibleToken from 0xFUNGIBLETOKENADDRESS

pub contract ExampleToken: FungibleToken {

Expand Down
2 changes: 1 addition & 1 deletion src/contracts/FlowToken.cdc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import FungibleToken from 0x02
import FungibleToken from 0xFUNGIBLETOKENADDRESS

pub contract FlowToken: FungibleToken {

Expand Down
2 changes: 1 addition & 1 deletion src/contracts/TokenForwarding.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ their tokens to.
*/

import FungibleToken from 0x02
import FungibleToken from 0xFUNGIBLETOKENADDRESS

pub contract TokenForwarding {

Expand Down
2 changes: 2 additions & 0 deletions test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ require (
github.com/onflow/flow-go-sdk v0.4.0
github.com/stretchr/testify v1.5.1
)

replace github.com/onflow/flow-ft/contracts => ../contracts
52 changes: 48 additions & 4 deletions test/token_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package test

import (
"strings"
"testing"

emulator "github.com/dapperlabs/flow-emulator"
Expand Down Expand Up @@ -366,14 +365,59 @@ func DeployTokenContracts(b *emulator.Blockchain, t *testing.T, key []*flow.Acco
_, err = b.CommitBlock()
assert.NoError(t, err)

exampleTokenCode := readFile("../src/contracts/ExampleToken.cdc")
codeWithFTAddr := strings.ReplaceAll(string(exampleTokenCode), "02", fungibleAddr.String())
exampleTokenCode := contracts.ExampleToken(fungibleAddr.String())

tokenAddr, err := b.CreateAccount(key, []byte(codeWithFTAddr))
tokenAddr, err := b.CreateAccount(key, []byte(exampleTokenCode))
assert.NoError(t, err)

_, err = b.CommitBlock()
assert.NoError(t, err)

return fungibleAddr, tokenAddr
}

func TestCreateCustomToken(t *testing.T) {
b := newEmulator()

accountKeys := test.AccountKeyGenerator()

exampleTokenAccountKey, _ := accountKeys.NewWithSigner()
// Should be able to deploy a contract as a new account with no keys.
fungibleTokenCode := contracts.FungibleToken()
fungibleAddr, err := b.CreateAccount(nil, fungibleTokenCode)
assert.NoError(t, err)

_, err = b.CommitBlock()
assert.NoError(t, err)

exampleTokenCode := contracts.CustomToken(fungibleAddr.String(), "UtilityCoin")

tokenAddr, err := b.CreateAccount([]*flow.AccountKey{exampleTokenAccountKey}, exampleTokenCode)
assert.NoError(t, err)

_, err = b.CommitBlock()
assert.NoError(t, err)

joshAccountKey, joshSigner := accountKeys.NewWithSigner()
joshAddress, _ := b.CreateAccount([]*flow.AccountKey{joshAccountKey}, nil)

t.Run("Should be able to create empty Vault that doesn't affect supply", func(t *testing.T) {
tx := flow.NewTransaction().
SetScript(GenerateCreateTokenScript(fungibleAddr, tokenAddr, "UtilityCoin", "utilityCoin")).
SetGasLimit(100).
SetProposalKey(b.ServiceKey().Address, b.ServiceKey().ID, b.ServiceKey().SequenceNumber).
SetPayer(b.ServiceKey().Address).
AddAuthorizer(joshAddress)

signAndSubmit(
t, b, tx,
[]flow.Address{b.ServiceKey().Address, joshAddress},
[]crypto.Signer{b.ServiceKey().Signer(), joshSigner},
false,
)

executeScriptAndCheck(t, b, GenerateInspectVaultScript(fungibleAddr, tokenAddr, joshAddress, "UtilityCoin", "utilityCoin", 0))

executeScriptAndCheck(t, b, GenerateInspectSupplyScript(fungibleAddr, tokenAddr, "UtilityCoin", 1000))
})
}

0 comments on commit 755c0dd

Please sign in to comment.