Skip to content

Commit

Permalink
cadence 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
avcdsld committed Jul 29, 2024
1 parent 9ebe03a commit 7736f92
Show file tree
Hide file tree
Showing 22 changed files with 1,266 additions and 421 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,5 @@ packages/contracts/deployments

# Aptos CLI
.aptos

*.key
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"source.fixAll.eslint": "explicit"
},
"solidity.packageDefaultDependenciesContractsDirectory": "",
"solidity.packageDefaultDependenciesDirectory": "node_modules"
"solidity.packageDefaultDependenciesDirectory": "node_modules",
"cadence.flowCommand": "flow-c1"
}
43 changes: 43 additions & 0 deletions packages/contracts/flow/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## Cadence 1.0 Migration 2024/07/29

```sh
sudo sh -ci "$(curl -fsSL https://raw.githubusercontent.com/onflow/flow-cli/master/install.sh)"
flow-c1 version

flow-c1 emulator
flow-c1 deploy


flow-c1 migrate stage Base64Util --network=testnet
flow-c1 migrate is-staged Base64Util --network=testnet
flow-c1 migrate is-validated Base64Util --network=testnet

flow-c1 migrate stage SakutaroPoemContent --network=testnet
flow-c1 migrate is-staged SakutaroPoemContent --network=testnet
flow-c1 migrate is-validated SakutaroPoemContent --network=testnet

flow-c1 migrate stage SakutaroPoem --network=testnet
flow-c1 migrate is-staged SakutaroPoem --network=testnet
flow-c1 migrate is-validated SakutaroPoem --network=testnet

flow-c1 migrate stage SakutaroPoemReplica --network=testnet
flow-c1 migrate is-staged SakutaroPoemReplica --network=testnet
flow-c1 migrate is-validated SakutaroPoemReplica --network=testnet


flow-c1 migrate stage Base64Util --network=mainnet
flow-c1 migrate is-staged Base64Util --network=mainnet
flow-c1 migrate is-validated Base64Util --network=mainnet

flow-c1 migrate stage SakutaroPoemContent --network=mainnet
flow-c1 migrate is-staged SakutaroPoemContent --network=mainnet
flow-c1 migrate is-validated SakutaroPoemContent --network=mainnet

flow-c1 migrate stage SakutaroPoem --network=mainnet
flow-c1 migrate is-staged SakutaroPoem --network=mainnet
flow-c1 migrate is-validated SakutaroPoem --network=mainnet

flow-c1 migrate stage SakutaroPoemReplica --network=mainnet
flow-c1 migrate is-staged SakutaroPoemReplica --network=mainnet
flow-c1 migrate is-validated SakutaroPoemReplica --network=mainnet
```
10 changes: 5 additions & 5 deletions packages/contracts/flow/contracts/Base64Util.cdc
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// SPDX-License-Identifier: Unlicense
pub contract Base64Util {
pub fun encodeFromDict(_ dict: {String: String}): String {
access(all) contract Base64Util {
access(all) fun encodeFromDict(_ dict: {String: String}): String {
let jsonStr = Base64Util.dictToJsonStr(dict)
return Base64Util.encode(jsonStr)
}

pub fun encode(_ str: String): String {
access(all) fun encode(_ str: String): String {
let base64Map: {UInt8: String} = {
0: "A", 1: "B", 2: "C", 3: "D",
4: "E", 5: "F", 6: "G", 7: "H",
Expand Down Expand Up @@ -46,7 +46,7 @@ pub contract Base64Util {
return res
}

priv fun dictToJsonStr(_ dict: {String: String}): String {
access(self) fun dictToJsonStr(_ dict: {String: String}): String {
var res = "{"
var flag = false
for key in dict.keys {
Expand All @@ -65,7 +65,7 @@ pub contract Base64Util {
return res
}

priv fun escape(_ str: String): String {
access(self) fun escape(_ str: String): String {
var res = ""
var i = 0
while i < str.length {
Expand Down
162 changes: 92 additions & 70 deletions packages/contracts/flow/contracts/SakutaroPoem.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,32 @@
//
//
import FungibleToken from 0xee82856bf20e2aa6
import NonFungibleToken from "./core/NonFungibleToken.cdc"
import MetadataViews from "./core/MetadataViews.cdc"
import SakutaroPoemContent from "./SakutaroPoemContent.cdc"

pub contract SakutaroPoem: NonFungibleToken {
pub let CollectionPublicPath: PublicPath
pub let CollectionStoragePath: StoragePath
pub var totalSupply: UInt64
priv var royalties: [MetadataViews.Royalty]

pub event ContractInitialized()
pub event Withdraw(id: UInt64, from: Address?)
pub event Deposit(id: UInt64, to: Address?)
pub event Mint(id: UInt64)
pub event Destroy(id: UInt64)

pub struct SakutaroPoemMetadataView {
pub let poemID: UInt32?
pub let name: String?
pub let description: String?
pub let thumbnail: AnyStruct{MetadataViews.File}
pub let svg: String?
pub let svgBase64: String?
pub let license: String
pub let creator: String
import "NonFungibleToken"
import "ViewResolver"
import "MetadataViews"
import "SakutaroPoemContent"

access(all) contract SakutaroPoem: NonFungibleToken {
access(all) let CollectionPublicPath: PublicPath
access(all) let CollectionStoragePath: StoragePath
access(all) var totalSupply: UInt64
access(self) var royalties: [MetadataViews.Royalty]

access(all) struct SakutaroPoemMetadataView {
access(all) let poemID: UInt32?
access(all) let name: String?
access(all) let description: String?
access(all) let thumbnail: {MetadataViews.File}
access(all) let svg: String?
access(all) let svgBase64: String?
access(all) let license: String
access(all) let creator: String

init(
poemID: UInt32?,
name: String?,
description: String?,
thumbnail: AnyStruct{MetadataViews.File},
thumbnail: {MetadataViews.File},
svg: String?,
svgBase64: String?,
license: String,
Expand All @@ -55,22 +50,22 @@ pub contract SakutaroPoem: NonFungibleToken {
}
}

pub resource NFT: NonFungibleToken.INFT, MetadataViews.Resolver {
pub let id: UInt64
access(all) resource NFT: NonFungibleToken.NFT {
access(all) let id: UInt64

init(id: UInt64) {
self.id = id
}

pub fun getViews(): [Type] {
access(all) view fun getViews(): [Type] {
return [
Type<MetadataViews.Display>(),
Type<MetadataViews.Royalties>(),
Type<SakutaroPoemMetadataView>()
]
}

pub fun resolveView(_ view: Type): AnyStruct? {
access(all) fun resolveView(_ view: Type): AnyStruct? {
switch view {
case Type<MetadataViews.Display>():
let poem = self.getPoem()
Expand Down Expand Up @@ -99,7 +94,7 @@ pub contract SakutaroPoem: NonFungibleToken {
return nil
}

pub fun getPoemID(): UInt32? {
access(all) fun getPoemID(): UInt32? {
if self.owner == nil {
return nil
}
Expand All @@ -111,93 +106,124 @@ pub contract SakutaroPoem: NonFungibleToken {
return num % 39
}

pub fun getPoem(): SakutaroPoemContent.Poem? {
access(all) fun getPoem(): SakutaroPoemContent.Poem? {
let poemID = self.getPoemID()
if poemID == nil {
return nil
}
return SakutaroPoemContent.getPoem(poemID!)
}

destroy() {
emit Destroy(id: self.id)
access(all) fun createEmptyCollection(): @{NonFungibleToken.Collection} {
return <- SakutaroPoem.createEmptyCollection(nftType: Type<@SakutaroPoem.NFT>())
}
}

pub resource interface SakutaroPoemCollectionPublic {
pub fun deposit(token: @NonFungibleToken.NFT)
pub fun getIDs(): [UInt64]
pub fun borrowNFT(id: UInt64): &NonFungibleToken.NFT
pub fun borrowPoem(id: UInt64): &SakutaroPoem.NFT? {
access(all) resource interface SakutaroPoemCollectionPublic {
access(all) fun deposit(token: @{NonFungibleToken.NFT})
access(all) view fun getIDs(): [UInt64]
access(all) view fun borrowNFT(_ id: UInt64): &{NonFungibleToken.NFT}?
access(all) view fun borrowPoem(_ id: UInt64): &SakutaroPoem.NFT? {
post {
(result == nil) || (result?.id == id):
"Cannot borrow Poem reference: the ID of the returned reference is incorrect"
}
}
}

pub resource Collection: SakutaroPoemCollectionPublic, NonFungibleToken.Provider, NonFungibleToken.Receiver, NonFungibleToken.CollectionPublic, MetadataViews.ResolverCollection {
pub var ownedNFTs: @{UInt64: NonFungibleToken.NFT}
access(all) resource Collection: SakutaroPoemCollectionPublic, NonFungibleToken.Collection {
access(all) var ownedNFTs: @{UInt64: {NonFungibleToken.NFT}}

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

pub fun withdraw(withdrawID: UInt64): @NonFungibleToken.NFT {
access(all) view fun getSupportedNFTTypes(): {Type: Bool} {
let supportedTypes: {Type: Bool} = {}
supportedTypes[Type<@SakutaroPoem.NFT>()] = true
return supportedTypes
}

access(all) view fun isSupportedNFTType(type: Type): Bool {
return type == Type<@SakutaroPoem.NFT>()
}

access(NonFungibleToken.Withdraw) fun withdraw(withdrawID: UInt64): @{NonFungibleToken.NFT} {
let token <- self.ownedNFTs.remove(key: withdrawID) ?? panic("Missing NFT")
emit Withdraw(id: token.id, from: self.owner?.address)
return <- token
}

pub fun deposit(token: @NonFungibleToken.NFT) {
access(all) fun deposit(token: @{NonFungibleToken.NFT}) {
let token <- token as! @SakutaroPoem.NFT
let id: UInt64 = token.id
let oldToken <- self.ownedNFTs[id] <- token
emit Deposit(id: id, to: self.owner?.address)
destroy oldToken
}

pub fun getIDs(): [UInt64] {
access(all) view fun getIDs(): [UInt64] {
return self.ownedNFTs.keys
}

pub fun borrowNFT(id: UInt64): &NonFungibleToken.NFT {
return (&self.ownedNFTs[id] as &NonFungibleToken.NFT?)!
access(all) view fun borrowNFT(_ id: UInt64): &{NonFungibleToken.NFT}? {
return &self.ownedNFTs[id] as &{NonFungibleToken.NFT}?
}

pub fun borrowPoem(id: UInt64): &SakutaroPoem.NFT? {
access(all) view fun borrowPoem(_ id: UInt64): &SakutaroPoem.NFT? {
if self.ownedNFTs[id] != nil {
let ref = &self.ownedNFTs[id] as auth &NonFungibleToken.NFT?
let ref = &self.ownedNFTs[id] as &{NonFungibleToken.NFT}?
return ref as! &SakutaroPoem.NFT?
}
return nil
}

pub fun borrowViewResolver(id: UInt64): &AnyResource{MetadataViews.Resolver} {
let nft = &self.ownedNFTs[id] as auth &NonFungibleToken.NFT?
return nft as! &SakutaroPoem.NFT
access(all) view fun borrowViewResolver(id: UInt64): &{ViewResolver.Resolver}? {
if let nft = &self.ownedNFTs[id] as &{NonFungibleToken.NFT}? {
return nft as &{ViewResolver.Resolver}
}
return nil
}

destroy() {
destroy self.ownedNFTs
access(all) fun createEmptyCollection(): @{NonFungibleToken.Collection} {
return <- SakutaroPoem.createEmptyCollection(nftType: Type<@SakutaroPoem.NFT>())
}
}

pub fun createEmptyCollection(): @NonFungibleToken.Collection {
access(all) fun createEmptyCollection(nftType: Type): @{NonFungibleToken.Collection} {
return <- create Collection()
}

pub fun mintNFT() : @NFT {
access(all) view fun getContractViews(resourceType: Type?): [Type] {
return [
Type<MetadataViews.NFTCollectionData>()
]
}

access(all) view fun resolveContractView(resourceType: Type?, viewType: Type): AnyStruct? {
switch viewType {
case Type<MetadataViews.NFTCollectionData>():
let collectionData = MetadataViews.NFTCollectionData(
storagePath: self.CollectionStoragePath,
publicPath: self.CollectionPublicPath,
publicCollection: Type<&SakutaroPoem.Collection>(),
publicLinkedType: Type<&SakutaroPoem.Collection>(),
createEmptyCollectionFunction: (fun(): @{NonFungibleToken.Collection} {
return <- SakutaroPoem.createEmptyCollection(nftType: Type<@SakutaroPoem.NFT>())
})
)
return collectionData
}
return nil
}

access(all) fun mintNFT(): @NFT {
pre {
SakutaroPoem.totalSupply < 39: "Can't mint any more"
}
SakutaroPoem.totalSupply = SakutaroPoem.totalSupply + 1
let token <- create NFT(id: SakutaroPoem.totalSupply)
emit Mint(id: token.id)
return <- token
return <- create NFT(id: SakutaroPoem.totalSupply)
}

pub fun getRoyalties(): MetadataViews.Royalties {
access(all) fun getRoyalties(): MetadataViews.Royalties {
return MetadataViews.Royalties(SakutaroPoem.royalties)
}

Expand All @@ -206,15 +232,11 @@ pub contract SakutaroPoem: NonFungibleToken {
self.CollectionStoragePath = /storage/SakutaroPoemCollection
self.totalSupply = 0

let recepient = self.account.getCapability<&{FungibleToken.Receiver}>(/public/flowTokenReceiver)!
self.royalties = [MetadataViews.Royalty(recepient: recepient, cut: 0.1, description: "39")]

self.account.save(<- create Collection(), to: self.CollectionStoragePath)
self.account.link<&SakutaroPoem.Collection{NonFungibleToken.CollectionPublic, SakutaroPoem.SakutaroPoemCollectionPublic}>(
self.CollectionPublicPath,
target: self.CollectionStoragePath
)
let receiver = self.account.capabilities.get<&{FungibleToken.Receiver}>(/public/flowTokenReceiver)
self.royalties = [MetadataViews.Royalty(receiver: receiver, cut: 0.1, description: "39")]

emit ContractInitialized()
self.account.storage.save(<- create Collection(), to: self.CollectionStoragePath)
let cap: Capability = self.account.capabilities.storage.issue<&SakutaroPoem.Collection>(self.CollectionStoragePath)
self.account.capabilities.publish(cap, at: self.CollectionPublicPath)
}
}
Loading

0 comments on commit 7736f92

Please sign in to comment.