Skip to content

add flix registry smart contract, to store interaction templates on chain #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions registry/cadence/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
### 👋 Welcome Flix Developer!

To run the tests execute the following command:
```bash
flow test --cover --covercode="contracts" cadence/test/flix_registry_test.cdc
```
126 changes: 126 additions & 0 deletions registry/cadence/contracts/flix_registry.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import "FLIXRegistryInterface"

access(all)
contract FLIXRegistry: FLIXRegistryInterface {

access(all)
event ContractInitialized()

access(all)
event RegistryCreated(name: String)

access(all)
event Published(registryOwner: Address, registryName: String, registryUuid: UInt64, alias: String, id: String, cadenceBodyHash: String)

access(all)
event Removed(registryOwner: Address, registryName: String, registryUuid: UInt64, id: String)

access(all)
event Deprecated(registryOwner: Address, registryName: String, registryUuid: UInt64, id: String)

access(all)
event AliasLinked(registryOwner: Address, registryName: String, registryUuid: UInt64, alias: String, id: String)

access(all)
event AliasUnlinked(registryOwner: Address, registryName: String, registryUuid: UInt64, alias: String)

access(all)
resource Registry: FLIXRegistryInterface.Queryable, FLIXRegistryInterface.Removable, FLIXRegistryInterface.Admin {

access(account) var flixes: {String: {FLIXRegistryInterface.InteractionTemplate}}
access(account) var aliases: {String: String}
access(account) var cadenceBodyHashes: {String: {FLIXRegistryInterface.InteractionTemplate}}
access(account) let name: String

access(all)
fun getName(): String {
return self.name
}

access(all)
fun publish(alias: String, flix: {FLIXRegistryInterface.InteractionTemplate}) {
self.flixes[flix.getId()] = flix
self.aliases[alias] = flix.getId()
self.cadenceBodyHashes[flix.getCadenceBodyHash()] = flix
emit Published(registryOwner: self.owner!.address, registryName: self.name, registryUuid: self.uuid, alias: alias, id: flix.getId(), cadenceBodyHash: flix.getCadenceBodyHash())
}

access(all)
fun link(alias: String, id: String) {
self.aliases[alias] = id
emit AliasLinked(registryOwner: self.owner!.address, registryName: self.name, registryUuid: self.uuid, alias: alias, id: id)
}

access(all)
fun unlink(alias: String) {
self.aliases.remove(key: alias)
emit AliasUnlinked(registryOwner: self.owner!.address, registryName: self.name, registryUuid: self.uuid, alias: alias)
}

access(all)
fun lookup(idOrAlias: String): {FLIXRegistryInterface.InteractionTemplate}? {
if self.aliases.containsKey(idOrAlias) {
return self.flixes[self.aliases[idOrAlias]!]
}
return self.flixes[idOrAlias]
}

access(all)
fun resolve(cadenceBodyHash: String): {FLIXRegistryInterface.InteractionTemplate}? {
return self.cadenceBodyHashes[cadenceBodyHash]
}

access(all)
fun deprecate(idOrAlias: String) {
var flix = self.lookup(idOrAlias: idOrAlias) ?? panic("FLIX does not exist with the given id or alias: ".concat(idOrAlias))
flix.status = "deprecated"
self.flixes[flix.getId()] = flix
emit Deprecated(registryOwner: self.owner!.address, registryName: self.name, registryUuid: self.uuid, id: flix.getId())
}

access(all)
fun remove(id: String): {FLIXRegistryInterface.InteractionTemplate}? {
let removed = self.flixes.remove(key: id)
if(removed != nil) { emit Removed(registryOwner: self.owner!.address, registryName: self.name, registryUuid: self.uuid, id: id) }
return removed
}

access(all)
fun getIds(): [String] {
return self.flixes.keys
}

access(all)
fun getAllAlias(): {String: String} {
return self.aliases
}

init(name: String) {
self.name = name
self.flixes = {}
self.aliases = {}
self.cadenceBodyHashes = {}
emit RegistryCreated(name: name)
}
}

access(all)
fun createRegistry(name: String): @Registry {
return <- create Registry(name: name)
}

access(all)
fun PublicPath(name: String): PublicPath {
return PublicPath(identifier: "flix_".concat(name))!
}

access(all)
fun StoragePath(name: String): StoragePath {
return StoragePath(identifier: "flix_".concat(name))!
}

init() {
emit ContractInitialized()
}

}
93 changes: 93 additions & 0 deletions registry/cadence/contracts/flix_registry_interface.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
access(all)
contract interface FLIXRegistryInterface {

access(all)
event ContractInitialized()

access(all)
event RegistryCreated(name: String)

access(all)
event Published(registryOwner: Address, registryName: String, registryUuid: UInt64, alias: String, id: String, cadenceBodyHash: String)

access(all)
event Removed(registryOwner: Address, registryName: String, registryUuid: UInt64, id: String)

access(all)
event Deprecated(registryOwner: Address, registryName: String, registryUuid: UInt64, id: String)

access(all)
event AliasLinked(registryOwner: Address, registryName: String, registryUuid: UInt64, alias: String, id: String)

access(all)
event AliasUnlinked(registryOwner: Address, registryName: String, registryUuid: UInt64, alias: String)

access(all)
struct interface InteractionTemplate {
access(all)
fun getId(): String

access(all)
fun getVersion(): String

access(all)
fun getCadenceBodyHash(): String

pub(set)
var status: String

access(all)
fun getData(): AnyStruct
}

access(all)
resource interface Queryable{
access(all)
fun getName(): String

access(all)
fun resolve(cadenceBodyHash: String): {FLIXRegistryInterface.InteractionTemplate}?

access(all)
fun lookup(idOrAlias: String): {FLIXRegistryInterface.InteractionTemplate}?

access(all)
fun getIds(): [String]

access(all)
fun getAllAlias(): {String: String}
}

access(all)
resource interface Admin {

// Add the flix to the registry and add or update the alias to point to this flix
access(all)
fun publish(alias: String, flix: {FLIXRegistryInterface.InteractionTemplate})

access(all)
fun link(alias: String, id: String)

access(all)
fun unlink(alias: String)

access(all)
fun deprecate(idOrAlias: String)
}

access(all)
resource interface Removable {
access(all)
fun remove(id: String): AnyStruct?
}

access(all)
resource Registry: Queryable, Removable, Admin {
access(all)
fun getName(): String
}

access(all)
fun createRegistry(name: String): @Registry

}
49 changes: 49 additions & 0 deletions registry/cadence/contracts/flix_schema_draft.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import "FLIXRegistryInterface"
import "FLIXRegistry"

access(all)
contract FLIXSchema_draft {

access(all)
struct FLIX: FLIXRegistryInterface.InteractionTemplate {
access(all)
let id: String

access(all)
let data: AnyStruct

access(all)
let cadenceBodyHash: String

access(all)
fun getId(): String {
return self.id
}

access(all)
fun getData(): AnyStruct {
return self.data
}

access(all)
fun getVersion(): String {
return "draft"
}

access(all)
fun getCadenceBodyHash(): String {
return self.cadenceBodyHash
}

pub(set)
var status: String

init(id: String, data: AnyStruct, cadenceBodyHash: String) {
self.id = id
self.data = data
self.cadenceBodyHash = cadenceBodyHash
self.status = "active"
}
}

}
Loading