Skip to content

Commit

Permalink
update generic setup and transfer transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuahannan committed Feb 9, 2024
1 parent 320aa3c commit 2ab8303
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 36 deletions.
64 changes: 32 additions & 32 deletions coverage.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"186": 1,
"187": 1,
"188": 1,
"200": 8,
"200": 9,
"204": 4,
"206": 4,
"210": 4,
Expand All @@ -34,20 +34,20 @@
"221": 4,
"223": 4,
"224": 4,
"27": 45,
"27": 47,
"29": 2,
"34": 4,
"40": 4,
"41": 4,
"52": 38,
"59": 2,
"52": 40,
"59": 3,
"63": 0,
"67": 1,
"93": 20,
"94": 20,
"95": 20,
"96": 20,
"97": 20
"93": 21,
"94": 21,
"95": 21,
"96": 21,
"97": 21
},
"missed_lines": [
63,
Expand Down Expand Up @@ -77,8 +77,8 @@
"202": 6,
"205": 18,
"214": 0,
"223": 8,
"224": 8,
"223": 9,
"224": 9,
"98": 7
},
"missed_lines": [
Expand All @@ -97,14 +97,14 @@
"103": 2,
"104": 2,
"107": 0,
"145": 38,
"146": 38,
"148": 38,
"149": 38,
"150": 38,
"151": 38,
"152": 38,
"153": 38,
"145": 40,
"146": 40,
"148": 40,
"149": 40,
"150": 40,
"151": 40,
"152": 40,
"153": 40,
"163": 0,
"164": 0,
"165": 0,
Expand Down Expand Up @@ -589,27 +589,27 @@
},
"excluded_locations": [
"A.0000000000000001.NonFungibleToken",
"A.0000000000000001.FlowServiceAccount",
"A.0000000000000001.FlowStorageFees",
"A.0000000000000001.FlowClusterQC",
"A.0000000000000002.FungibleToken",
"A.0000000000000001.ExampleNFT",
"I.Test",
"I.BlockchainHelpers",
"A.0000000000000002.FungibleTokenMetadataViews",
"A.0000000000000001.StakingProxy",
"A.0000000000000001.FlowDKG",
"A.0000000000000004.FlowFees",
"A.0000000000000001.NodeVersionBeacon",
"A.0000000000000002.FungibleToken",
"A.0000000000000002.FungibleTokenMetadataViews",
"A.0000000000000001.FlowStakingCollection",
"s.7465737400000000000000000000000000000000000000000000000000000000",
"A.0000000000000001.FlowServiceAccount",
"A.0000000000000004.FlowFees",
"A.0000000000000001.FlowEpoch",
"A.0000000000000001.RandomBeaconHistory",
"A.0000000000000001.FlowIDTableStaking",
"A.0000000000000001.LockedTokens",
"A.0000000000000003.FlowToken",
"A.0000000000000001.MetadataViews",
"A.0000000000000001.FlowStakingCollection",
"A.0000000000000001.ViewResolver",
"A.0000000000000001.ExampleNFT",
"A.0000000000000001.FlowIDTableStaking",
"A.0000000000000001.RandomBeaconHistory",
"I.Crypto",
"I.Test"
"A.0000000000000001.FlowClusterQC",
"A.0000000000000001.NodeVersionBeacon",
"A.0000000000000001.FlowStorageFees",
"A.0000000000000001.LockedTokens"
]
}
23 changes: 23 additions & 0 deletions lib/go/templates/internal/assets/assets.go

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

36 changes: 34 additions & 2 deletions lib/go/templates/transaction_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package templates
import (
_ "github.com/kevinburke/go-bindata"

"strings"

"github.com/onflow/flow-ft/lib/go/templates/internal/assets"
)

Expand All @@ -13,6 +15,7 @@ const (
genericTransferFilename = "generic_transfer.cdc"
transferManyAccountsFilename = "transfer_many_accounts.cdc"
setupAccountFilename = "setup_account.cdc"
setupGenericVaultFilename = "metadata/setup_account_from_address.cdc"
mintTokensFilename = "mint_tokens.cdc"
createForwarderFilename = "create_forwarder.cdc"
burnTokensFilename = "burn_tokens.cdc"
Expand All @@ -29,6 +32,29 @@ func GenerateCreateTokenScript(env Environment) []byte {
return []byte(ReplaceAddresses(code, env))
}

// GenerateSetupGenericVaultScript creates a script that instantiates
// a new Vault instance and stores it in storage. It can be used
// to create any token as long as you know the address of the contract
// and the name of the contract
func GenerateSetupAccountFromAddressScript(fungibleTokenAddr, fungibleTokenMVAddr string) []byte {

code := assets.MustAssetString(setupGenericVaultFilename)

code = strings.ReplaceAll(
code,
placeholderFungibleToken,
withHexPrefix(fungibleTokenAddr),
)

code = strings.ReplaceAll(
code,
placeholderFTMetadataViews,
withHexPrefix(fungibleTokenMVAddr),
)

return []byte(code)
}

// GenerateTransferVaultScript creates a script that withdraws an tokens from an account
// and deposits it to another account's vault
func GenerateTransferVaultScript(env Environment) []byte {
Expand All @@ -40,11 +66,17 @@ func GenerateTransferVaultScript(env Environment) []byte {

// GenerateTransferGenericVaultScript creates a script that withdraws an tokens from an account
// and deposits it to another account's vault for any vault type
func GenerateTransferGenericVaultScript(env Environment) []byte {
func GenerateTransferGenericVaultScript(fungibleTokenAddr string) []byte {

code := assets.MustAssetString(genericTransferFilename)

return []byte(ReplaceAddresses(code, env))
code = strings.ReplaceAll(
code,
placeholderFungibleToken,
withHexPrefix(fungibleTokenAddr),
)

return []byte(code)
}

// GenerateTransferManyAccountsScript creates a script that transfers the same number of tokens
Expand Down
2 changes: 1 addition & 1 deletion lib/go/test/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func TestTokenExternalTransfers(t *testing.T) {

t.Run("Should be able to transfer tokens with the generic transfer transaction", func(t *testing.T) {

script := templates.GenerateTransferGenericVaultScript(env)
script := templates.GenerateTransferGenericVaultScript(env.FungibleTokenAddress)

tx := createTxWithTemplateAndAuthorizer(b, script, joshAddress)

Expand Down
13 changes: 12 additions & 1 deletion tests/metadata_views_tests.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import "FungibleTokenMetadataViews"
import "ExampleToken"
import "FungibleToken"

access(all) let admin = Test.getAccount(0x0000000000000007)

/* Test Setup */

access(all) fun setup() {
Expand All @@ -27,12 +29,21 @@ access(all) fun testSetupAccountUsingFTView() {
setupExampleToken(alice)
let aliceBalance = getExampleTokenBalance(alice)
txExecutor("metadata/setup_account_from_vault_reference.cdc", [bob], [alice.address, /public/exampleTokenVault], nil, nil)
let bobBalance = getExampleTokenBalance(alice)
let bobBalance = getExampleTokenBalance(bob)

Test.assertEqual(0.0, aliceBalance)
Test.assertEqual(0.0, bobBalance)
}

access(all) fun testSetupAccountUsingContractAddressAndName() {
let bob = Test.createAccount()

txExecutor("metadata/setup_account_from_address.cdc", [bob], [admin.address, "ExampleToken"], nil, nil)
let bobBalance = getExampleTokenBalance(bob)

Test.assertEqual(0.0, bobBalance)
}

access(all) fun testRetrieveVaultDisplayInfo() {
let alice = Test.createAccount()

Expand Down
37 changes: 37 additions & 0 deletions transactions/metadata/setup_account_from_address.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import FungibleToken from "FungibleToken"
import FungibleTokenMetadataViews from "FungibleTokenMetadataViews"

/// This transaction is what an account would run
/// to set itself up to manage fungible tokens. This function
/// uses views to know where to set up the vault
/// in storage and to create the empty vault.
transaction(contractAddress: Address, contractName: String) {

prepare(signer: auth(SaveValue, Capabilities) &Account) {
// Borrow a reference to the vault stored on the passed account at the passed publicPath
let resolverRef = getAccount(contractAddress)
.contracts.borrow<&FungibleToken>(name: contractName)
?? panic("Could not borrow a reference to the fungible token contract")

// Use that reference to retrieve the FTView
let ftVaultData = resolverRef.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTVaultData>()) as! FungibleTokenMetadataViews.FTVaultData?
?? panic("Could not resolve the FTVaultData view for the given Fungible token contract")

// Create a new empty vault using the createEmptyVault function inside the FTVaultData
let emptyVault <-ftVaultData.createEmptyVault()

// Save it to the account
signer.storage.save(<-emptyVault, to: ftVaultData.storagePath)

// Create a public capability for the vault which includes the .Resolver interface
let vaultCap = signer.capabilities.storage.issue<&{FungibleToken.Vault}>(ftVaultData.storagePath)
signer.capabilities.publish(vaultCap, at: ftVaultData.metadataPath)

// Create a public capability for the vault exposing the receiver interface
let receiverCap = signer.capabilities.storage.issue<&{FungibleToken.Receiver}>(ftVaultData.storagePath)
signer.capabilities.publish(receiverCap, at: ftVaultData.receiverPath)

}
}

0 comments on commit 2ab8303

Please sign in to comment.