-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
contract factory first attempt, OpenEdition factory template
- Loading branch information
1 parent
ced139d
commit c9677a3
Showing
14 changed files
with
339 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[{"type": "String","value": "MyCollection"},{"type": "Dictionary","value": [{"key": {"type": "String","value": "collectionDisplay"},"value": {"type": "Struct","value": {"id": "A.f8d6e0586b0a20c7.MetadataViews.NFTCollectionDisplay","fields": [{"name": "name","value": {"type": "String","value": "My Collection"}},{"name": "description","value": {"type": "String","value": "This is a test collection made by Flowty's contract factory!"}},{"name": "externalURL","value": {"type": "Struct","value": {"id": "A.f8d6e0586b0a20c7.MetadataViews.ExternalURL","fields": [{"name": "url","value": {"type": "String","value": "https://flowty.io"}}]}}},{"name": "squareImage","value": {"type": "Struct","value": {"id": "A.f8d6e0586b0a20c7.MetadataViews.Media","fields": [{"name": "file","value": {"type": "Struct","value": {"id": "A.f8d6e0586b0a20c7.MetadataViews.IPFSFile","fields": [{"name": "cid","value": {"type": "String","value": "QmWWLhnkPR3ejavNtzeJcdG9fwcBHKwBVEP4pZ9rGbdHEM"}},{"name": "path","value": {"type": "Optional","value": null}}]}}},{"name": "mediaType","value": {"type": "String","value": "image/png"}}]}}},{"name": "bannerImage","value": {"type": "Struct","value": {"id": "A.f8d6e0586b0a20c7.MetadataViews.Media","fields": [{"name": "file","value": {"type": "Struct","value": {"id": "A.f8d6e0586b0a20c7.MetadataViews.IPFSFile","fields": [{"name": "cid","value": {"type": "String","value": "QmYD8e5s59qYFFQXref1YzyqW1WKYUMPxfqVDEis2s23BF"}},{"name": "path","value": {"type": "Optional","value": null}}]}}},{"name": "mediaType","value": {"type": "String","value": "image/png"}}]}}},{"name": "socials","value": {"type": "Dictionary","value": [{"key": {"type": "String","value": "twitter"},"value": {"type": "Struct","value": {"id": "A.f8d6e0586b0a20c7.MetadataViews.ExternalURL","fields": [{"name": "url","value": {"type": "String","value": "https://x.com/flowty_io"}}]}}}]}}]}}},{"key": {"type": "String","value": "data"},"value": {"type": "Struct","value": {"id": "A.f8d6e0586b0a20c7.NFTMetadata.Metadata","fields": [{"name": "name","value": {"type": "String","value": "Fluid"}},{"name": "description","value": {"type": "String","value": "Fluid is an open edition collection generated by the Flowty contract factory!"}},{"name": "thumbnail","value": {"type": "Struct","value": {"id": "A.f8d6e0586b0a20c7.MetadataViews.IPFSFile","fields": [{"name": "cid","value": {"type": "String","value": "QmWWLhnkPR3ejavNtzeJcdG9fwcBHKwBVEP4pZ9rGbdHEM"}},{"name": "path","value": {"type": "Optional","value": null}}]}}},{"name": "traits","value": {"type": "Optional","value": null}},{"name": "editions","value": {"type": "Optional","value": null}},{"name": "externalURL","value": {"type": "Optional","value": null}},{"name": "data","value": {"type": "Dictionary","value": []}}]}}}]}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import "FungibleToken" | ||
import "NonFungibleToken" | ||
import "FlowtyDrops" | ||
import "NFTMetadata" | ||
import "AddressUtils" | ||
|
||
access(all) contract interface FlowtyMinters { | ||
access(all) resource interface Minter: FlowtyDrops.Minter { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import "NonFungibleToken" | ||
import "ViewResolver" | ||
import "MetadataViews" | ||
import "NFTMetadata" | ||
import "FlowtyDrops" | ||
import "AddressUtils" | ||
import "StringUtils" | ||
import "BaseNFTVars" | ||
|
||
access(all) contract interface BaseCollection: ViewResolver { | ||
// The base collection is an interface that attmepts to take more boilerplate | ||
// off of NFT-standard compliant definitions. | ||
access(all) resource interface Collection: NonFungibleToken.Collection { | ||
access(all) var ownedNFTs: @{UInt64: {NonFungibleToken.NFT}} | ||
access(all) var nftType: Type | ||
|
||
access(all) fun deposit(token: @{NonFungibleToken.NFT}) { | ||
pre { | ||
token.getType() == self.nftType: "unexpected nft type being deposited" | ||
} | ||
|
||
destroy self.ownedNFTs.insert(key: token.uuid, <-token) | ||
} | ||
|
||
access(all) view fun borrowNFT(_ id: UInt64): &{NonFungibleToken.NFT}? { | ||
return &self.ownedNFTs[id] | ||
} | ||
|
||
access(all) view fun getSupportedNFTTypes(): {Type: Bool} { | ||
return { | ||
self.nftType: true | ||
} | ||
} | ||
|
||
access(all) view fun isSupportedNFTType(type: Type): Bool { | ||
return type == self.nftType | ||
} | ||
|
||
access(NonFungibleToken.Withdraw) fun withdraw(withdrawID: UInt64): @{NonFungibleToken.NFT} { | ||
return <- self.ownedNFTs.remove(key: withdrawID)! | ||
} | ||
} | ||
|
||
access(all) view fun getContractViews(resourceType: Type?): [Type] { | ||
return [ | ||
Type<MetadataViews.NFTCollectionData>(), | ||
Type<MetadataViews.NFTCollectionDisplay>() | ||
] | ||
} | ||
|
||
access(all) fun resolveContractView(resourceType: Type?, viewType: Type): AnyStruct? { | ||
if resourceType == nil { | ||
return nil | ||
} | ||
|
||
let rt = resourceType! | ||
let segments = rt.identifier.split(separator: ".") | ||
let pathIdentifier = StringUtils.join([segments[2], segments[1]], "_") | ||
|
||
let addr = AddressUtils.parseAddress(rt)! | ||
let acct = getAccount(addr) | ||
|
||
switch viewType { | ||
case Type<MetadataViews.NFTCollectionData>(): | ||
let segments = rt.identifier.split(separator: ".") | ||
let pathIdentifier = StringUtils.join([segments[2], segments[1]], "_") | ||
|
||
return MetadataViews.NFTCollectionData( | ||
storagePath: StoragePath(identifier: pathIdentifier)!, | ||
publicPath: PublicPath(identifier: pathIdentifier)!, | ||
publicCollection: Type<&{NonFungibleToken.Collection}>(), | ||
publicLinkedType: Type<&{NonFungibleToken.Collection}>(), | ||
createEmptyCollectionFunction: fun(): @{NonFungibleToken.Collection} { | ||
let addr = AddressUtils.parseAddress(rt)! | ||
let c = getAccount(addr).contracts.borrow<&{BaseNFTVars}>(name: segments[2])! | ||
return <- c.createEmptyCollection(nftType: rt) | ||
} | ||
) | ||
case Type<MetadataViews.NFTCollectionDisplay>(): | ||
let c = getAccount(addr).contracts.borrow<&{BaseNFTVars}>(name: segments[2])! | ||
let md = c.MetadataCap.borrow() | ||
if md == nil { | ||
return nil | ||
} | ||
|
||
return md!.collectionInfo.collectionDisplay | ||
case Type<FlowtyDrops.DropResolver>(): | ||
return FlowtyDrops.DropResolver(cap: acct.capabilities.get<&{FlowtyDrops.ContainerPublic}>(FlowtyDrops.ContainerPublicPath)) | ||
} | ||
|
||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import "ContractFactoryTemplate" | ||
import "AddressUtils" | ||
|
||
access(all) contract ContractFactory { | ||
access(all) fun createContract(templateType: Type, acct: auth(AddContract) &Account, name: String, params: {String: AnyStruct}) { | ||
let templateAddr = AddressUtils.parseAddress(templateType)! | ||
let contractName = templateType.identifier.split(separator: ".")[2] | ||
let templateContract = getAccount(templateAddr).contracts.borrow<&{ContractFactoryTemplate}>(name: contractName) | ||
?? panic("provided type is not a ContractTemplateFactory") | ||
|
||
templateContract.createContract(acct: acct, name: name, params: params) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import "NonFungibleToken" | ||
import "MetadataViews" | ||
import "ViewResolver" | ||
|
||
import "FlowtyDrops" | ||
import "BaseNFT" | ||
import "BaseNFTVars" | ||
import "NFTMetadata" | ||
import "UniversalCollection" | ||
import "BaseCollection" | ||
|
||
import "AddressUtils" | ||
|
||
access(all) contract interface ContractFactoryTemplate { | ||
access(all) fun createContract(acct: auth(AddContract) &Account, name: String, params: {String: AnyStruct}) | ||
|
||
access(all) fun getContractAddresses(): {String: Address} { | ||
let d: {String: Address} = { | ||
"NonFungibleToken": AddressUtils.parseAddress(Type<&{NonFungibleToken}>())!, | ||
"MetadataViews": AddressUtils.parseAddress(Type<&MetadataViews>())!, | ||
"ViewResolver": AddressUtils.parseAddress(Type<&{ViewResolver}>())!, | ||
"FlowtyDrops": AddressUtils.parseAddress(Type<&FlowtyDrops>())!, | ||
"BaseNFT": AddressUtils.parseAddress(Type<&{BaseNFT}>())!, | ||
"BaseNFTVars": AddressUtils.parseAddress(Type<&{BaseNFTVars}>())!, | ||
"NFTMetadata": AddressUtils.parseAddress(Type<&NFTMetadata>())!, | ||
"UniversalCollection": AddressUtils.parseAddress(Type<&UniversalCollection>())!, | ||
"BaseCollection": AddressUtils.parseAddress(Type<&{BaseCollection}>())!, | ||
"AddressUtils": AddressUtils.parseAddress(Type<&AddressUtils>())! | ||
} | ||
|
||
return d | ||
} | ||
|
||
access(all) fun importLine(name: String, addr: Address): String { | ||
return "import ".concat(name).concat(" from ").concat(addr.toString()).concat("\n") | ||
} | ||
|
||
access(all) fun generateImports(names: [String]): String { | ||
let addresses = self.getContractAddresses() | ||
var imports = "" | ||
for n in names { | ||
imports = imports.concat(self.importLine(name: n, addr: addresses[n] ?? panic("missing contract import address: ".concat(n)))) | ||
} | ||
|
||
return imports | ||
} | ||
} |
Oops, something went wrong.