Skip to content
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

Migrate to Cadence 1.0 #2

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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
dapper-wallet-restrictions-testnet.key
dapper-wallet-restrictions-mainnet.key
dapper-wallet-collections-testnet.key
dapper-wallet-collections-mainnet.key
coverage.json
30 changes: 30 additions & 0 deletions contracts/DapperWalletCollections.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
access(all) contract DapperWalletCollections {
access(all) let StoragePath: StoragePath

access(all) event TypeChanged(identifier: String, added: Bool)

access(self) let types: {Type: Bool}

access(all) resource Admin {
access(all) fun addType(_ t: Type) {
DapperWalletCollections.types.insert(key: t, true)
emit TypeChanged(identifier: t.identifier, added: true)
}

access(all) fun removeType( _ t: Type) {
DapperWalletCollections.types.remove(key: t)
emit TypeChanged(identifier: t.identifier, added: false)
}
}

access(all) view fun containsType(_ t: Type): Bool {
return self.types.containsKey(t)
}

init () {
self.types = {}

self.StoragePath = /storage/dapperWalletAllowlistCollections
self.account.storage.save(<- create Admin(), to: self.StoragePath)
}
}
37 changes: 18 additions & 19 deletions contracts/DapperWalletRestrictions.cdc
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
pub contract DapperWalletRestrictions {
//
pub let StoragePath: StoragePath
access(all) contract DapperWalletRestrictions {
access(all) let StoragePath: StoragePath

pub event TypeChanged(identifier: Type, newConfig: TypeConfig)
pub event TypeRemoved(identifier: Type)
access(all) event TypeChanged(identifier: Type, newConfig: TypeConfig)
access(all) event TypeRemoved(identifier: Type)

pub fun GetConfigFlags(): {String: String} {
access(all) view fun GetConfigFlags(): {String: String} {
return {
"CAN_INIT": "Can initialize collection in Dapper Custodial Wallet",
"CAN_WITHDRAW": "Can withdraw NFT out of Dapper Custodial space",
Expand All @@ -16,52 +15,52 @@ pub contract DapperWalletRestrictions {
}
}

pub struct TypeConfig{
pub let flags: {String: Bool}
access(all) struct TypeConfig {
access(all) let flags: {String: Bool}

pub fun setFlag(_ flag: String, _ value: Bool) {
access(all) fun setFlag(_ flag: String, _ value: Bool) {
if DapperWalletRestrictions.GetConfigFlags()[flag] == nil {
panic("Invalid flag")
}

self.flags[flag] = value
}

pub fun getFlag(_ flag: String): Bool {
access(all) view fun getFlag(_ flag: String): Bool {
return self.flags[flag] ?? false
}

init () {
self.flags= {}
self.flags = {}
}
}

access(self) let types: {Type: TypeConfig}

access(self) let ext: {String: AnyStruct}

pub resource Admin {
pub fun addType(_ t: Type, conf: TypeConfig) {
access(all) resource Admin {
access(all) fun addType(_ t: Type, conf: TypeConfig) {
DapperWalletRestrictions.types.insert(key: t, conf)
emit TypeChanged(identifier: t, newConfig: conf)
}

pub fun updateType(_ t: Type, conf: TypeConfig) {
access(all) fun updateType(_ t: Type, conf: TypeConfig) {
DapperWalletRestrictions.types[t] = conf
emit TypeChanged(identifier: t, newConfig: conf)
}

pub fun removeType( _ t: Type) {
access(all) fun removeType( _ t: Type) {
DapperWalletRestrictions.types.remove(key: t)
emit TypeRemoved(identifier: t)
}
}

pub fun getTypes(): {Type:TypeConfig} {
access(all) view fun getTypes(): {Type:TypeConfig} {
return self.types
}

pub fun getConfig(_ t: Type): TypeConfig? {
access(all) view fun getConfig(_ t: Type): TypeConfig? {
return self.types[t]
}

Expand All @@ -70,6 +69,6 @@ pub contract DapperWalletRestrictions {
self.ext = {}

self.StoragePath = /storage/dapperWalletCollections
self.account.save(<- create Admin(), to: self.StoragePath)
self.account.storage.save(<- create Admin(), to: self.StoragePath)
}
}
}
40 changes: 32 additions & 8 deletions flow.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"contracts": {
"DapperWalletCollections": "./contracts/DapperWalletCollections.cdc",
"DapperWalletRestrictions": "./contracts/DapperWalletRestrictions.cdc"
},
"networks": {
Expand All @@ -8,19 +9,35 @@
"testnet": "access.devnet.nodes.onflow.org:9000"
},
"accounts": {
"dapper-wallet-restrictions-mainnet": {
"address": "2d4cebdb9eca6f49",
"key": {
"type": "file",
"hashAlgorithm": "SHA2_256",
"location": "dapper-wallet-restrictions-mainnet.key"
}
},
"dapper-collection-allowlist-mainnet": {
"address": "42a54b4f70e7dc81",
"key": {
"type": "file",
"hashAlgorithm": "SHA3_256",
"index": 2,
"location": "dapper-wallet-collections-mainnet.key"
}
},
"dapper-wallet-restrictions-testnet": {
"address": "a7d10afc50b14991",
"key": {
"type": "file",
"location": "dapper-wallet-restrictions-testnet.key"
}
},
"dapper-wallet-restrictions-mainnet": {
"address": "2d4cebdb9eca6f49",
"dapper-wallet-restrictions-testnet-2": {
"address": "b67c471ade6fdbea",
"key": {
"type": "file",
"hashAlgorithm": "SHA2_256",
"location": "dapper-wallet-restrictions-mainnet.key"
"location": "dapper-wallet-restrictions-testnet.key"
}
},
"emulator-account": {
Expand All @@ -34,14 +51,21 @@
"DapperWalletRestrictions"
]
},
"testnet": {
"dapper-wallet-restrictions-testnet": [
"mainnet": {
"dapper-wallet-restrictions-mainnet": [
"DapperWalletRestrictions"
],
"dapper-collection-allowlist-mainnet": [
"DapperWalletCollections"
]
},
"mainnet": {
"dapper-wallet-restrictions-mainnet": [
"testnet": {
"dapper-wallet-restrictions-testnet": [
"DapperWalletRestrictions"
],
"dapper-wallet-restrictions-testnet-2": [
"DapperWalletRestrictions",
"DapperWalletCollections"
]
}
}
Expand Down
6 changes: 6 additions & 0 deletions scripts/collections_contains_type.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import "DapperWalletCollections"

access(all) fun main(typeIdentifier: String): Bool {
let t = CompositeType(typeIdentifier)!
return DapperWalletCollections.containsType(t)
}
2 changes: 1 addition & 1 deletion scripts/get_all_types.cdc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "DapperWalletRestrictions"

pub fun main(): {Type: DapperWalletRestrictions.TypeConfig} {
access(all) fun main(): {Type: DapperWalletRestrictions.TypeConfig} {
return DapperWalletRestrictions.getTypes()
}
11 changes: 11 additions & 0 deletions transactions/add_collection_restriction.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import DapperWalletCollections from "DapperWalletCollections"

transaction(typeIdentifier: String) {
prepare(acct: auth(BorrowValue) &Account) {
let ref = acct.storage.borrow<&DapperWalletCollections.Admin>(from: DapperWalletCollections.StoragePath)
?? panic("admin not found")

let t = CompositeType(typeIdentifier)!
ref.addType(t)
}
}
6 changes: 3 additions & 3 deletions transactions/add_restriction.cdc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import DapperWalletRestrictions from "DapperWalletRestrictions"

transaction(typeIdentifier: String, flags: {String:Bool}) {
prepare(acct: AuthAccount) {
let ref = acct.borrow<&DapperWalletRestrictions.Admin>(from: DapperWalletRestrictions.StoragePath)
prepare(acct: auth(BorrowValue) &Account) {
let ref = acct.storage.borrow<&DapperWalletRestrictions.Admin>(from: DapperWalletRestrictions.StoragePath)
?? panic("admin not found")

let conf = DapperWalletRestrictions.TypeConfig()
Expand All @@ -12,4 +12,4 @@ transaction(typeIdentifier: String, flags: {String:Bool}) {
let t = CompositeType(typeIdentifier)!
ref.addType(t, conf: conf)
}
}
}
15 changes: 15 additions & 0 deletions transactions/update_restriction.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import DapperWalletRestrictions from "DapperWalletRestrictions"

transaction(typeIdentifier: String, flags: {String:Bool}) {
prepare(acct: auth(BorrowValue) &Account) {
let ref = acct.storage.borrow<&DapperWalletRestrictions.Admin>(from: DapperWalletRestrictions.StoragePath)
?? panic("admin not found")

let conf = DapperWalletRestrictions.TypeConfig()
for k in flags.keys {
conf.setFlag(k, flags[k]!)
}
let t = CompositeType(typeIdentifier)!
ref.updateType(t, conf: conf)
}
}