Skip to content

Commit

Permalink
feat: Upgrade Cadence1.0 (#18)
Browse files Browse the repository at this point in the history
* Fix data:image/ handling in metadata views image source helper (#16)

* feat: update cadence contracts

* feat: upgrade bug_hunter.js

* feat: upgrade c1.0

* feat: dynamic load flow.json

---------

Co-authored-by: Bastian Müller <[email protected]>
  • Loading branch information
btspoony and turbolent authored Aug 16, 2024
1 parent 9a870c9 commit a18ab8a
Show file tree
Hide file tree
Showing 74 changed files with 992 additions and 665 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ next-env.d.ts

*.private.json

*.bak
*.bak

imports
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"cadence.flowCommand": "flow-c1"
}
62 changes: 30 additions & 32 deletions cadence/contracts/FlowviewAccountBookmark.cdc
Original file line number Diff line number Diff line change
@@ -1,76 +1,74 @@
pub contract FlowviewAccountBookmark {
access(all) contract FlowviewAccountBookmark {

pub let AccountBookmarkCollectionStoragePath: StoragePath
pub let AccountBookmarkCollectionPublicPath: PublicPath
pub let AccountBookmarkCollectionPrivatePath: PrivatePath
access(all) entitlement Manage

pub event AccountBookmarkAdded(owner: Address, address: Address, note: String)
pub event AccountBookmarkRemoved(owner: Address, address: Address)
access(all) let AccountBookmarkCollectionStoragePath: StoragePath
access(all) let AccountBookmarkCollectionPublicPath: PublicPath
access(all) let AccountBookmarkCollectionPrivatePath: PrivatePath

pub event ContractInitialized()
access(all) event AccountBookmarkAdded(owner: Address, address: Address, note: String)
access(all) event AccountBookmarkRemoved(owner: Address, address: Address)

pub resource interface AccountBookmarkPublic {
pub let address: Address
pub var note: String
access(all) event ContractInitialized()

access(all) resource interface AccountBookmarkPublic {
access(all) let address: Address
access(all) var note: String
}

pub resource AccountBookmark: AccountBookmarkPublic {
pub let address: Address
pub var note: String
access(all) resource AccountBookmark: AccountBookmarkPublic {
access(all) let address: Address
access(all) var note: String

init(address: Address, note: String) {
self.address = address
self.note = note
}

pub fun setNote(note: String) {
access(Manage) fun setNote(note: String) {
self.note = note
}
}

pub resource interface CollectionPublic {
pub fun borrowPublicAccountBookmark(address: Address): &AccountBookmark{AccountBookmarkPublic}?
pub fun getAccountBookmarks(): &{Address: AccountBookmark{AccountBookmarkPublic}}
access(all) resource interface CollectionPublic {
access(all) view fun borrowPublicAccountBookmark(address: Address): &AccountBookmark?
access(all) view fun getAccountBookmarks(): &{Address: AccountBookmark}
}

pub resource AccountBookmarkCollection: CollectionPublic {
pub let bookmarks: @{Address: AccountBookmark}
access(all) resource AccountBookmarkCollection: CollectionPublic {
access(all) let bookmarks: @{Address: AccountBookmark}

init() {
self.bookmarks <- {}
}

pub fun addAccountBookmark(address: Address, note: String) {
access(Manage) fun addAccountBookmark(address: Address, note: String) {
pre {
self.bookmarks[address] == nil: "Account bookmark already exists"
}
self.bookmarks[address] <-! create AccountBookmark(address: address, note: note)
emit AccountBookmarkAdded(owner: self.owner!.address, address: address, note: note)
}

pub fun removeAccountBookmark(address: Address) {
access(Manage) fun removeAccountBookmark(address: Address) {
destroy self.bookmarks.remove(key: address)
emit AccountBookmarkRemoved(owner: self.owner!.address, address: address)
}

pub fun borrowPublicAccountBookmark(address: Address): &AccountBookmark{AccountBookmarkPublic}? {
return &self.bookmarks[address] as &AccountBookmark{AccountBookmarkPublic}?
}

pub fun borrowAccountBookmark(address: Address): &AccountBookmark? {
return &self.bookmarks[address] as &AccountBookmark?
access(all) view fun borrowPublicAccountBookmark(address: Address): &AccountBookmark? {
return &self.bookmarks[address]
}

pub fun getAccountBookmarks(): &{Address: AccountBookmark{AccountBookmarkPublic}} {
return &self.bookmarks as &{Address: AccountBookmark{AccountBookmarkPublic}}
access(Manage) fun borrowAccountBookmark(address: Address): auth(Manage) &AccountBookmark? {
return &self.bookmarks[address]
}

destroy() {
destroy self.bookmarks
access(all) view fun getAccountBookmarks(): &{Address: AccountBookmark} {
return &self.bookmarks
}
}

pub fun createEmptyCollection(): @AccountBookmarkCollection {
access(all) fun createEmptyCollection(): @AccountBookmarkCollection {
return <-create AccountBookmarkCollection()
}

Expand Down
7 changes: 4 additions & 3 deletions cadence/scripts/get_bookmark.cdc
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import FlowviewAccountBookmark from "../contracts/FlowviewAccountBookmark.cdc"

pub fun main(owner: Address, address: Address): &FlowviewAccountBookmark.AccountBookmark{FlowviewAccountBookmark.AccountBookmarkPublic}? {
let acct = getAuthAccount(owner)
let collection = acct.borrow<&FlowviewAccountBookmark.AccountBookmarkCollection>(from: FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath)
access(all) fun main(owner: Address, address: Address): &FlowviewAccountBookmark.AccountBookmark? {
let acct = getAuthAccount<auth(Storage) &Account>(owner)
let collection = acct.storage
.borrow<&FlowviewAccountBookmark.AccountBookmarkCollection>(from: FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath)
?? panic("Could not borrow AccountBookmarkCollection")

return collection.borrowPublicAccountBookmark(address: address)
Expand Down
7 changes: 4 additions & 3 deletions cadence/scripts/get_bookmarks.cdc
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import FlowviewAccountBookmark from "../contracts/FlowviewAccountBookmark.cdc"

pub fun main(owner: Address): &{Address: FlowviewAccountBookmark.AccountBookmark{FlowviewAccountBookmark.AccountBookmarkPublic}} {
let acct = getAuthAccount(owner)
let collection = acct.borrow<&FlowviewAccountBookmark.AccountBookmarkCollection>(from: FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath)
access(all) fun main(owner: Address): &{Address: FlowviewAccountBookmark.AccountBookmark} {
let acct = getAuthAccount<auth(Storage) &Account>(owner)
let collection = acct.storage
.borrow<&FlowviewAccountBookmark.AccountBookmarkCollection>(from: FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath)
?? panic("Could not borrow AccountBookmarkCollection")

return collection.getAccountBookmarks()
Expand Down
17 changes: 8 additions & 9 deletions cadence/transactions/add_bookmark.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ transaction(
address: Address,
note: String
) {
let bookmarkCollection: &FlowviewAccountBookmark.AccountBookmarkCollection
let bookmarkCollection: auth(FlowviewAccountBookmark.Manage) &FlowviewAccountBookmark.AccountBookmarkCollection

prepare(signer: AuthAccount) {
if signer.borrow<&FlowviewAccountBookmark.AccountBookmarkCollection>(from: FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath) == nil {
prepare(signer: auth(Storage, Capabilities) &Account) {
if signer.storage.borrow<&FlowviewAccountBookmark.AccountBookmarkCollection>(from: FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath) == nil {
let collection <- FlowviewAccountBookmark.createEmptyCollection()
signer.save(<-collection, to: FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath)
signer.storage.save(<-collection, to: FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath)

let cap = signer.link<&FlowviewAccountBookmark.AccountBookmarkCollection{FlowviewAccountBookmark.CollectionPublic}>(
FlowviewAccountBookmark.AccountBookmarkCollectionPublicPath,
target: FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath
) ?? panic("Could not create public capability")
let cap = signer.capabilities.storage.issue<&FlowviewAccountBookmark.AccountBookmarkCollection>(FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath)
signer.capabilities.publish(cap, at: FlowviewAccountBookmark.AccountBookmarkCollectionPublicPath)
}

self.bookmarkCollection = signer.borrow<&FlowviewAccountBookmark.AccountBookmarkCollection>(from: FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath)
self.bookmarkCollection = signer.storage
.borrow<auth(FlowviewAccountBookmark.Manage) &FlowviewAccountBookmark.AccountBookmarkCollection>(from: FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath)
?? panic("Could not borrow collection")
}

Expand Down
17 changes: 8 additions & 9 deletions cadence/transactions/edit_bookmark.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ transaction(
address: Address,
note: String
) {
let bookmarkCollection: &FlowviewAccountBookmark.AccountBookmarkCollection
let bookmarkCollection: auth(FlowviewAccountBookmark.Manage) &FlowviewAccountBookmark.AccountBookmarkCollection

prepare(signer: AuthAccount) {
if signer.borrow<&FlowviewAccountBookmark.AccountBookmarkCollection>(from: FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath) == nil {
prepare(signer: auth(Storage, Capabilities) &Account) {
if signer.storage.borrow<&FlowviewAccountBookmark.AccountBookmarkCollection>(from: FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath) == nil {
let collection <- FlowviewAccountBookmark.createEmptyCollection()
signer.save(<-collection, to: FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath)
signer.storage.save(<-collection, to: FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath)

let cap = signer.link<&FlowviewAccountBookmark.AccountBookmarkCollection{FlowviewAccountBookmark.CollectionPublic}>(
FlowviewAccountBookmark.AccountBookmarkCollectionPublicPath,
target: FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath
) ?? panic("Could not create public capability")
let cap = signer.capabilities.storage.issue<&FlowviewAccountBookmark.AccountBookmarkCollection>(FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath)
signer.capabilities.publish(cap, at: FlowviewAccountBookmark.AccountBookmarkCollectionPublicPath)
}

self.bookmarkCollection = signer.borrow<&FlowviewAccountBookmark.AccountBookmarkCollection>(from: FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath)
self.bookmarkCollection = signer.storage
.borrow<auth(FlowviewAccountBookmark.Manage) &FlowviewAccountBookmark.AccountBookmarkCollection>(from: FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath)
?? panic("Could not borrow collection")
}

Expand Down
17 changes: 8 additions & 9 deletions cadence/transactions/remove_bookmark.cdc
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import FlowviewAccountBookmark from "../contracts/FlowviewAccountBookmark.cdc"

transaction(address: Address) {
let bookmarkCollection: &FlowviewAccountBookmark.AccountBookmarkCollection
let bookmarkCollection: auth(FlowviewAccountBookmark.Manage) &FlowviewAccountBookmark.AccountBookmarkCollection

prepare(signer: AuthAccount) {
if signer.borrow<&FlowviewAccountBookmark.AccountBookmarkCollection>(from: FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath) == nil {
prepare(signer: auth(Storage, Capabilities) &Account) {
if signer.storage.borrow<&FlowviewAccountBookmark.AccountBookmarkCollection>(from: FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath) == nil {
let collection <- FlowviewAccountBookmark.createEmptyCollection()
signer.save(<-collection, to: FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath)
signer.storage.save(<-collection, to: FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath)

let cap = signer.link<&FlowviewAccountBookmark.AccountBookmarkCollection{FlowviewAccountBookmark.CollectionPublic}>(
FlowviewAccountBookmark.AccountBookmarkCollectionPublicPath,
target: FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath
) ?? panic("Could not create public capability")
let cap = signer.capabilities.storage.issue<&FlowviewAccountBookmark.AccountBookmarkCollection>(FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath)
signer.capabilities.publish(cap, at: FlowviewAccountBookmark.AccountBookmarkCollectionPublicPath)
}

self.bookmarkCollection = signer.borrow<&FlowviewAccountBookmark.AccountBookmarkCollection>(from: FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath)
self.bookmarkCollection = signer.storage
.borrow<auth(FlowviewAccountBookmark.Manage) &FlowviewAccountBookmark.AccountBookmarkCollection>(from: FlowviewAccountBookmark.AccountBookmarkCollectionStoragePath)
?? panic("Could not borrow collection")
}

Expand Down
1 change: 1 addition & 0 deletions flow.json
70 changes: 30 additions & 40 deletions flow/bug_hunter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,52 @@ import { getPublicPaths } from "./scripts"

export const getStoragePaths = async (address) => {
const code = `
pub fun main(address: Address): [String] {
let account = getAuthAccount(address)
access(all) fun main(address: Address): [String] {
let account = getAuthAccount<auth(Storage, Capabilities) &Account>(address)
let paths: [String] = []
account.forEachStored(fun (path: StoragePath, type: Type): Bool {
paths.append(path.toString())
return true
})
return paths
}
`
`;

const paths = await fcl.query({
cadence: code,
args: (arg, t) => [
arg(address, t.Address),
]
})
args: (arg, t) => [arg(address, t.Address)],
});

return paths
}
return paths;
};

export const getPrivatePaths = async (address) => {
const code = `
pub fun main(address: Address): [String] {
let account = getAuthAccount(address)
access(all) fun main(address: Address): [String] {
let account = getAuthAccount<auth(Storage, Capabilities) &Account>(address)
let paths: [String] = []
account.forEachPrivate(fun (path: PrivatePath, type: Type): Bool {
paths.append(path.toString())
return true
})
return paths
}
`
`;

const paths = await fcl.query({
cadence: code,
args: (arg, t) => [
arg(address, t.Address),
]
})
args: (arg, t) => [arg(address, t.Address)],
});

return paths
}
return paths;
};

export const getTypeOfPrivatePath = async (address, path) => {
const pathIdentifier = path.replace("/private/", "")
const pathIdentifier = path.replace("/private/", "");

const code = `
pub fun main(address: Address, pathIdentifier: String): String? {
let account = getAuthAccount(address)
access(all) fun main(address: Address, pathIdentifier: String): String? {
let account = getAuthAccount<auth(Storage, Capabilities) &Account>(address)
let path: PrivatePath = PrivatePath(identifier: pathIdentifier)!
let target = account.getLinkTarget(path)
var targetPath: String? = nil
Expand All @@ -62,40 +58,34 @@ export const getTypeOfPrivatePath = async (address, path) => {
}
return targetPath
}
`
`;

const type = await fcl.query({
cadence: code,
args: (arg, t) => [
arg(address, t.Address),
arg(pathIdentifier, t.String)
]
})
args: (arg, t) => [arg(address, t.Address), arg(pathIdentifier, t.String)],
});

return type
}
return type;
};

export const getTypeOfStoragePath = async (address, path) => {
const pathIdentifier = path.replace("/storage/", "")
const pathIdentifier = path.replace("/storage/", "");

const code = `
pub fun main(address: Address, pathIdentifier: String): Type? {
let account = getAuthAccount(address)
access(all) fun main(address: Address, pathIdentifier: String): Type? {
let account = getAuthAccount<auth(Storage, Capabilities) &Account>(address)
let path: StoragePath = StoragePath(identifier: pathIdentifier)!
return account.type(at: path)
}
`
`;

const type = await fcl.query({
cadence: code,
args: (arg, t) => [
arg(address, t.Address),
arg(pathIdentifier, t.String)
]
})
args: (arg, t) => [arg(address, t.Address), arg(pathIdentifier, t.String)],
});

return type
}
return type;
};

// export const hunt2 = async (address) => {
// const paths = await getStoragePaths(address)
Expand Down
Loading

0 comments on commit a18ab8a

Please sign in to comment.