Skip to content

Commit

Permalink
FungibleToken Factory Manager Setup Transaction (onflow#152)
Browse files Browse the repository at this point in the history
Closes: onflow#151
  • Loading branch information
sisyphusSmiling authored Sep 1, 2023
1 parent dd77f1b commit 604e872
Show file tree
Hide file tree
Showing 16 changed files with 229 additions and 23 deletions.
10 changes: 10 additions & 0 deletions contracts/factories/FTReceiverBalanceFactory.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import "CapabilityFactory"
import "FungibleToken"

pub contract FTReceiverBalanceFactory {
pub struct Factory: CapabilityFactory.Factory {
pub fun getCapability(acct: &AuthAccount, path: CapabilityPath): Capability {
return acct.getCapability<&{FungibleToken.Receiver, FungibleToken.Balance}>(path)
}
}
}
11 changes: 11 additions & 0 deletions flow.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@
"mainnet": "d8a7e05a7ac670c0"
}
},
"FTReceiverBalanceFactory": {
"source": "./contracts/factories/FTReceiverBalanceFactory.cdc",
"aliases": {
"emulator": "f8d6e0586b0a20c7",
"testnet": "294e44e1ec6993c6",
"mainnet": "d8a7e05a7ac670c0"
}
},
"FTReceiverFactory": {
"source": "./contracts/factories/FTReceiverFactory.cdc",
"aliases": {
Expand Down Expand Up @@ -233,6 +241,7 @@
"FTProviderFactory",
"FTAllFactory",
"FTBalanceFactory",
"FTReceiverBalanceFactory",
"FTReceiverFactory",
"NFTProviderFactory",
"NFTProviderAndCollectionFactory",
Expand All @@ -257,6 +266,7 @@
"FTProviderFactory",
"FTAllFactory",
"FTBalanceFactory",
"FTReceiverBalanceFactory",
"FTReceiverFactory"
]
},
Expand All @@ -272,6 +282,7 @@
"FTProviderFactory",
"FTAllFactory",
"FTBalanceFactory",
"FTReceiverBalanceFactory",
"FTReceiverFactory"
]
}
Expand Down
13 changes: 13 additions & 0 deletions scripts/test/get_ft_all_factory.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import "FungibleToken"

import "CapabilityFactory"

pub fun main(address: Address): Bool {

let managerRef = getAuthAccount(address).borrow<&CapabilityFactory.Manager>(from: CapabilityFactory.StoragePath)
?? panic("CapabilityFactory Manager not found")

let ftAllFactory = managerRef.getFactory(Type<&{FungibleToken.Provider, FungibleToken.Receiver, FungibleToken.Balance}>())

return ftAllFactory != nil
}
13 changes: 13 additions & 0 deletions scripts/test/get_ft_balance_factory.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import "FungibleToken"

import "CapabilityFactory"

pub fun main(address: Address): Bool {

let managerRef = getAuthAccount(address).borrow<&CapabilityFactory.Manager>(from: CapabilityFactory.StoragePath)
?? panic("CapabilityFactory Manager not found")

let balanceFactory = managerRef.getFactory(Type<&{FungibleToken.Balance}>())

return balanceFactory != nil
}
13 changes: 13 additions & 0 deletions scripts/test/get_ft_provider_factory.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import "FungibleToken"

import "CapabilityFactory"

pub fun main(address: Address): Bool {

let managerRef = getAuthAccount(address).borrow<&CapabilityFactory.Manager>(from: CapabilityFactory.StoragePath)
?? panic("CapabilityFactory Manager not found")

let providerFactory = managerRef.getFactory(Type<&{FungibleToken.Provider}>())

return providerFactory != nil
}
13 changes: 13 additions & 0 deletions scripts/test/get_ft_receiver_balance_factory.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import "FungibleToken"

import "CapabilityFactory"

pub fun main(address: Address): Bool {

let managerRef = getAuthAccount(address).borrow<&CapabilityFactory.Manager>(from: CapabilityFactory.StoragePath)
?? panic("CapabilityFactory Manager not found")

let receiverBalanceFactory = managerRef.getFactory(Type<&{FungibleToken.Receiver, FungibleToken.Balance}>())

return receiverBalanceFactory != nil
}
13 changes: 13 additions & 0 deletions scripts/test/get_ft_receiver_factory.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import "FungibleToken"

import "CapabilityFactory"

pub fun main(address: Address): Bool {

let managerRef = getAuthAccount(address).borrow<&CapabilityFactory.Manager>(from: CapabilityFactory.StoragePath)
?? panic("CapabilityFactory Manager not found")

let receiverFactory = managerRef.getFactory(Type<&{FungibleToken.Receiver}>())

return receiverFactory != nil
}
13 changes: 13 additions & 0 deletions scripts/test/get_nft_collection_public_factory.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import "NonFungibleToken"

import "CapabilityFactory"

pub fun main(address: Address): Bool {

let managerRef = getAuthAccount(address).borrow<&CapabilityFactory.Manager>(from: CapabilityFactory.StoragePath)
?? panic("CapabilityFactory Manager not found")

let collectionPublicFactory = managerRef.getFactory(Type<&{NonFungibleToken.CollectionPublic}>())

return collectionPublicFactory != nil
}
13 changes: 13 additions & 0 deletions scripts/test/get_nft_provider_collection_factory.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import "NonFungibleToken"

import "CapabilityFactory"

pub fun main(address: Address): Bool {

let managerRef = getAuthAccount(address).borrow<&CapabilityFactory.Manager>(from: CapabilityFactory.StoragePath)
?? panic("CapabilityFactory Manager not found")

let providerCollectionFactory = managerRef.getFactory(Type<&{NonFungibleToken.Provider, NonFungibleToken.CollectionPublic}>())

return providerCollectionFactory != nil
}
13 changes: 13 additions & 0 deletions scripts/test/get_nft_provider_factory.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import "NonFungibleToken"

import "CapabilityFactory"

pub fun main(address: Address): Bool {

let managerRef = getAuthAccount(address).borrow<&CapabilityFactory.Manager>(from: CapabilityFactory.StoragePath)
?? panic("CapabilityFactory Manager not found")

let providerFactory = managerRef.getFactory(Type<&{NonFungibleToken.Provider}>())

return providerFactory != nil
}
89 changes: 83 additions & 6 deletions test/CapabilityFactory_tests.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@ pub let flowtyThumbnail = "https://storage.googleapis.com/flowty-images/flowty-l

// BEGIN SECTION - Test Cases
pub fun testGetProviderCapability() {
pub fun testGetNFTProviderCapability() {
setupNFTCollection(creator)

scriptExecutor("factory/get_nft_provider_from_factory.cdc", [creator.address])
}

pub fun testGetFTReceiverBalanceCapability() {
txExecutor("example-token/setup.cdc", [creator], [], nil, nil)
scriptExecutor("factory/get_ft_receiver_from_factory.cdc", [creator.address])
}

pub fun testGetSupportedTypesFromManager() {
setupCapabilityFactoryManager(creator)

let supportedTypes = scriptExecutor(
"factory/get_supported_types_from_manager.cdc",
[creator.address]
)! as! [Type]
Test.assertEqual(6, supportedTypes.length)
Test.assertEqual(8, supportedTypes.length)
}

pub fun testAddFactoryFails() {
Expand All @@ -36,7 +41,7 @@ pub fun testAddFactorySucceeds() {
"factory/get_supported_types_from_manager.cdc",
[creator.address]
)! as! [Type]
Test.assertEqual(7, supportedTypes.length)
Test.assertEqual(9, supportedTypes.length)

let scriptResult = scriptExecutor(
"test/get_nft_receiver_factory.cdc",
Expand Down Expand Up @@ -66,22 +71,92 @@ pub fun testRemoveNFTProviderFactory() {
)
}

pub fun testSetupNFTManager() {
let tmp = blockchain.createAccount()
txExecutor("factory/setup_nft_manager.cdc", [tmp], [], nil, nil)

let supportedTypes = scriptExecutor("factory/get_supported_types_from_manager.cdc", [tmp.address])! as! [Type]
Test.assertEqual(3, supportedTypes.length)

let collectionPublicFactorySuccess = scriptExecutor("test/get_nft_collection_public_factory.cdc", [tmp.address])! as! Bool
let providerFactorySuccess = scriptExecutor("test/get_nft_provider_factory.cdc", [tmp.address])! as! Bool
let providerCollectionFactorySuccess = scriptExecutor("test/get_nft_provider_collection_factory.cdc", [tmp.address])! as! Bool

Test.assertEqual(true, collectionPublicFactorySuccess)
Test.assertEqual(true, providerFactorySuccess)
Test.assertEqual(true, providerCollectionFactorySuccess)
}

pub fun testSetupFTManager() {
let tmp = blockchain.createAccount()
txExecutor("factory/setup_ft_manager.cdc", [tmp], [], nil, nil)

let supportedTypes = scriptExecutor("factory/get_supported_types_from_manager.cdc", [tmp.address])! as! [Type]
Test.assertEqual(5, supportedTypes.length)

let balanceFactorySuccess = scriptExecutor("test/get_ft_balance_factory.cdc", [tmp.address])! as! Bool
let receiverFactorySuccess = scriptExecutor("test/get_ft_receiver_factory.cdc", [tmp.address])! as! Bool
let receiverBalanceFactorySuccess = scriptExecutor("test/get_ft_receiver_balance_factory.cdc", [tmp.address])! as! Bool
let providerFactorySuccess = scriptExecutor("test/get_ft_provider_factory.cdc", [tmp.address])! as! Bool
let allFactorySuccess = scriptExecutor("test/get_ft_all_factory.cdc", [tmp.address])! as! Bool


Test.assertEqual(true, balanceFactorySuccess)
Test.assertEqual(true, receiverFactorySuccess)
Test.assertEqual(true, receiverBalanceFactorySuccess)
Test.assertEqual(true, providerFactorySuccess)
Test.assertEqual(true, allFactorySuccess)
}

pub fun testSetupNFTFTManager() {
let tmp = blockchain.createAccount()
txExecutor("factory/setup_nft_ft_manager.cdc", [tmp], [], nil, nil)

let supportedTypes = scriptExecutor("factory/get_supported_types_from_manager.cdc", [tmp.address])! as! [Type]
Test.assertEqual(8, supportedTypes.length)

let collectionPublicFactorySuccess = scriptExecutor("test/get_nft_collection_public_factory.cdc", [tmp.address])! as! Bool
let nftProviderFactorySuccess = scriptExecutor("test/get_nft_provider_factory.cdc", [tmp.address])! as! Bool
let providerCollectionFactorySuccess = scriptExecutor("test/get_nft_provider_collection_factory.cdc", [tmp.address])! as! Bool

Test.assertEqual(true, collectionPublicFactorySuccess)
Test.assertEqual(true, nftProviderFactorySuccess)
Test.assertEqual(true, providerCollectionFactorySuccess)

let balanceFactorySuccess = scriptExecutor("test/get_ft_balance_factory.cdc", [tmp.address])! as! Bool
let receiverFactorySuccess = scriptExecutor("test/get_ft_receiver_factory.cdc", [tmp.address])! as! Bool
let receiverBalanceFactorySuccess = scriptExecutor("test/get_ft_receiver_balance_factory.cdc", [tmp.address])! as! Bool
let ftProviderFactorySuccess = scriptExecutor("test/get_ft_provider_factory.cdc", [tmp.address])! as! Bool
let allFactorySuccess = scriptExecutor("test/get_ft_all_factory.cdc", [tmp.address])! as! Bool


Test.assertEqual(true, balanceFactorySuccess)
Test.assertEqual(true, receiverFactorySuccess)
Test.assertEqual(true, receiverBalanceFactorySuccess)
Test.assertEqual(true, ftProviderFactorySuccess)
Test.assertEqual(true, allFactorySuccess)
}

// END SECTION - Test Cases
pub fun setup() {
blockchain.useConfiguration(Test.Configuration({
"CapabilityFactory": adminAccount.address,
"ExampleNFT": adminAccount.address,
"ExampleToken": adminAccount.address,
"NFTProviderFactory": adminAccount.address,
"NFTCollectionPublicFactory": adminAccount.address,
"NFTProviderAndCollectionFactory": adminAccount.address,
"FTProviderFactory": adminAccount.address,
"FTBalanceFactory": adminAccount.address,
"FTReceiverFactory": adminAccount.address
"FTReceiverFactory": adminAccount.address,
"FTReceiverBalanceFactory": adminAccount.address,
"FTAllFactory": adminAccount.address
}))

// helper nft contract so we can actually talk to nfts with tests
// helper nft & ft contract so we can actually talk to nfts & fts with tests
deploy("ExampleNFT", adminAccount, "../modules/flow-nft/contracts/ExampleNFT.cdc")
deploy("ExampleToken", adminAccount, "../contracts/standard/ExampleToken.cdc")

// our main contract is last
deploy("CapabilityFactory", adminAccount, "../contracts/CapabilityFactory.cdc")
Expand All @@ -91,6 +166,8 @@ pub fun setup() {
deploy("FTProviderFactory", adminAccount, "../contracts/factories/FTProviderFactory.cdc")
deploy("FTBalanceFactory", adminAccount, "../contracts/factories/FTBalanceFactory.cdc")
deploy("FTReceiverFactory", adminAccount, "../contracts/factories/FTReceiverFactory.cdc")
deploy("FTReceiverBalanceFactory", adminAccount, "../contracts/factories/FTReceiverBalanceFactory.cdc")
deploy("FTAllFactory", adminAccount, "../contracts/factories/FTAllFactory.cdc")
}

// BEGIN SECTION - transactions used in tests
Expand All @@ -112,7 +189,7 @@ pub fun mintNFTDefault(_ minter: Test.Account, receiver: Test.Account) {
}

pub fun setupCapabilityFactoryManager(_ acct: Test.Account) {
txExecutor("factory/setup.cdc", [acct], [], nil, nil)
txExecutor("factory/setup_nft_ft_manager.cdc", [acct], [], nil, nil)
}

// END SECTION - transactions use in tests
Expand Down
8 changes: 7 additions & 1 deletion test/HybridCustody_tests.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ pub fun setupOwnedAccount(_ acct: Test.Account, _ filterKind: String) {
}

pub fun setupFactoryManager(_ acct: Test.Account) {
txExecutor("factory/setup.cdc", [acct], [], nil, nil)
txExecutor("factory/setup_nft_ft_manager.cdc", [acct], [], nil, nil)
}

pub fun setupNFTCollection(_ acct: Test.Account) {
Expand Down Expand Up @@ -1022,7 +1022,9 @@ pub fun setup() {
accounts["NFTProviderFactory"] = adminAccount
accounts["FTProviderFactory"] = adminAccount
accounts["FTBalanceFactory"] = adminAccount
accounts["FTReceiverBalanceFactory"] = adminAccount
accounts["FTReceiverFactory"] = adminAccount
accounts["FTAllFactory"] = adminAccount
accounts["ExampleNFT"] = adminAccount
accounts["ExampleNFT2"] = adminAccount
accounts["ExampleToken"] = adminAccount
Expand All @@ -1041,7 +1043,9 @@ pub fun setup() {
"NFTProviderFactory": adminAccount.address,
"FTProviderFactory": adminAccount.address,
"FTBalanceFactory": adminAccount.address,
"FTReceiverBalanceFactory": adminAccount.address,
"FTReceiverFactory": adminAccount.address,
"FTAllFactory": adminAccount.address,
"ExampleNFT": adminAccount.address,
"ExampleNFT2": adminAccount.address,
"ExampleToken": adminAccount.address
Expand All @@ -1061,6 +1065,8 @@ pub fun setup() {
deploy("NFTProviderFactory", adminAccount, "../contracts/factories/NFTProviderFactory.cdc")
deploy("FTProviderFactory", adminAccount, "../contracts/factories/FTProviderFactory.cdc")
deploy("FTBalanceFactory", adminAccount, "../contracts/factories/FTBalanceFactory.cdc")
deploy("FTReceiverBalanceFactory", adminAccount, "../contracts/factories/FTReceiverBalanceFactory.cdc")
deploy("FTReceiverFactory", adminAccount, "../contracts/factories/FTReceiverFactory.cdc")
deploy("FTAllFactory", adminAccount, "../contracts/factories/FTAllFactory.cdc")
deploy("HybridCustody", adminAccount, "../contracts/HybridCustody.cdc")
}
2 changes: 1 addition & 1 deletion transactions/example-token/setup.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ transaction {

// Create a public capability to the Vault that only exposes
// the deposit function through the Receiver interface
acct.link<&ExampleToken.Vault{FungibleToken.Receiver}>(
acct.link<&ExampleToken.Vault{FungibleToken.Receiver, FungibleToken.Balance}>(
ExampleToken.ReceiverPublicPath,
target: ExampleToken.VaultStoragePath
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import "FungibleToken"

import "CapabilityFactory"
import "NFTCollectionPublicFactory"
import "NFTProviderAndCollectionFactory"
import "NFTProviderFactory"
import "FTProviderFactory"
import "FTBalanceFactory"
import "FTReceiverBalanceFactory"
import "FTReceiverFactory"

import "NonFungibleToken"
import "FungibleToken"
import "FTAllFactory"

transaction {
prepare(acct: AuthAccount) {
Expand All @@ -29,11 +27,10 @@ transaction {
let manager = acct.borrow<&CapabilityFactory.Manager>(from: CapabilityFactory.StoragePath)
?? panic("manager not found")

manager.updateFactory(Type<&{NonFungibleToken.CollectionPublic}>(), NFTCollectionPublicFactory.Factory())
manager.updateFactory(Type<&{NonFungibleToken.Provider, NonFungibleToken.CollectionPublic}>(), NFTProviderAndCollectionFactory.Factory())
manager.updateFactory(Type<&{NonFungibleToken.Provider}>(), NFTProviderFactory.Factory())
manager.updateFactory(Type<&{FungibleToken.Provider}>(), FTProviderFactory.Factory())
manager.updateFactory(Type<&{FungibleToken.Balance}>(), FTBalanceFactory.Factory())
manager.updateFactory(Type<&{FungibleToken.Receiver}>(), FTReceiverFactory.Factory())
manager.updateFactory(Type<&{FungibleToken.Receiver, FungibleToken.Balance}>(), FTReceiverBalanceFactory.Factory())
manager.updateFactory(Type<&{FungibleToken.Provider, FungibleToken.Receiver, FungibleToken.Balance}>(), FTAllFactory.Factory())
}
}
Loading

0 comments on commit 604e872

Please sign in to comment.