Skip to content

Commit

Permalink
Merge pull request #194 from onflow/standard-v2-updates
Browse files Browse the repository at this point in the history
Standard v2 Cadence 1.0 updates
  • Loading branch information
sisyphusSmiling authored Nov 7, 2023
2 parents ac62857 + 3f3d5a7 commit 683df24
Show file tree
Hide file tree
Showing 72 changed files with 1,804 additions and 28,599 deletions.
9 changes: 1 addition & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,12 @@ jobs:
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- uses: actions/setup-node@v3
with:
node-version: 16
cache: 'npm'
cache-dependency-path: lib/js/test/package-lock.json
- name: Install Flow CLI
run: sh -ci "$(curl -fsSL https://raw.githubusercontent.com/onflow/flow-cli/master/install.sh)" -- v1.3.1
run: sh -ci "$(curl -fsSL https://raw.githubusercontent.com/onflow/flow-cli/master/install.sh)" -- v1.5.0-stable-cadence.3
- name: Flow CLI Version
run: flow version
- name: Update PATH
run: echo "/root/.local/bin" >> $GITHUB_PATH
- name: Install dependencies
run: cd lib/js/test && npm ci
- name: Run tests
run: make ci

18 changes: 1 addition & 17 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,4 @@ jobs:
name: 🚀 release
runs-on: ubuntu-latest
steps:
- name: 📚 checkout
uses: actions/[email protected]
- name: 🟢 node
uses: actions/[email protected]
with:
node-version: 15
registry-url: https://registry.npmjs.org
- name: Install Dependencies
run: npm i
- name: Create Release Pull Request or Publish to npm
id: changesets
uses: changesets/action@v1
with:
publish: npm run release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: 📚 checkout
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
.vscode
node_modules
node_modules
coverage.json
6 changes: 2 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
test:
$(MAKE) generate -C lib/go
$(MAKE) test -C lib/go
$(MAKE) test -C lib/js/test
flow test --cover tests/test_example_nft.cdc
flow test --cover tests/*_tests.cdc

.PHONY: ci
ci:
$(MAKE) ci -C lib/go
$(MAKE) ci -C lib/js/test
flow test --cover tests/test_example_nft.cdc
flow test --cover tests/*_tests.cdc
29 changes: 17 additions & 12 deletions contracts/ExampleNFT-v2.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ access(all) contract ExampleNFT: MultipleNFT, ViewResolver {
return self.ownedNFTs.keys
}

/// Gets the amount of NFTs stored in the collection
access(all) view fun getLength(): Int {
return self.ownedNFTs.keys.length
}

access(all) view fun getIDsWithTypes(): {Type: [UInt64]} {
let typeIDs: {Type: [UInt64]} = {}
typeIDs[Type<@ExampleNFT.NFT>()] = self.getIDs()
Expand All @@ -251,9 +256,10 @@ access(all) contract ExampleNFT: MultipleNFT, ViewResolver {

/// Borrow the view resolver for the specified NFT ID
access(all) view fun borrowViewResolver(id: UInt64): &{ViewResolver.Resolver}? {
let nft = (&self.ownedNFTs[id] as &ExampleNFT.NFT?)!
let exampleNFT = nft as! &ExampleNFT.NFT
return exampleNFT as &{ViewResolver.Resolver}
if let nft = &self.ownedNFTs[id] as &ExampleNFT.NFT? {
return nft as &{ViewResolver.Resolver}
}
return nil
}

/// public function that anyone can call to create a new empty collection
Expand Down Expand Up @@ -336,12 +342,13 @@ access(all) contract ExampleNFT: MultipleNFT, ViewResolver {
access(all) view fun getCollectionData(nftType: Type): MetadataViews.NFTCollectionData? {
switch nftType {
case Type<@ExampleNFT.NFT>():
let collectionRef = self.account.borrow<&ExampleNFT.Collection>(from: /storage/cadenceExampleNFTCollection)
?? panic("Could not borrow a reference to the stored collection")
let collectionRef = self.account.storage.borrow<&ExampleNFT.Collection>(
from: /storage/cadenceExampleNFTCollection
) ?? panic("Could not borrow a reference to the stored collection")
let collectionData = MetadataViews.NFTCollectionData(
storagePath: collectionRef.getDefaultStoragePath()!,
publicPath: collectionRef.getDefaultPublicPath()!,
providerPath: /private/exampleNFTCollection,
providerPath: /private/cadenceExampleNFTCollection,
publicCollection: Type<&ExampleNFT.Collection>(),
publicLinkedType: Type<&ExampleNFT.Collection>(),
providerLinkedType: Type<auth(NonFungibleToken.Withdrawable) &ExampleNFT.Collection>(),
Expand Down Expand Up @@ -424,17 +431,15 @@ access(all) contract ExampleNFT: MultipleNFT, ViewResolver {
let collection <- create Collection()
let defaultStoragePath = collection.getDefaultStoragePath()!
let defaultPublicPath = collection.getDefaultPublicPath()!
self.account.save(<-collection, to: defaultStoragePath)
self.account.storage.save(<-collection, to: defaultStoragePath)

// create a public capability for the collection
self.account.link<&ExampleNFT.Collection>(
defaultPublicPath,
target: defaultStoragePath
)
let collectionCap = self.account.capabilities.storage.issue<&ExampleNFT.Collection>(defaultStoragePath)
self.account.capabilities.publish(collectionCap, at: defaultPublicPath)

// Create a Minter resource and save it to storage
let minter <- create NFTMinter()
self.account.save(<-minter, to: self.MinterStoragePath)
self.account.storage.save(<-minter, to: self.MinterStoragePath)
}
}

43 changes: 21 additions & 22 deletions contracts/NonFungibleToken-v2.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ access(all) contract NonFungibleToken {
///
access(all) event Withdraw(id: UInt64, uuid: UInt64, from: Address?, type: String)

access(self) fun emitNFTWithdraw(id: UInt64, uuid: UInt64, from: Address?, type: String): Bool
access(self) view fun emitNFTWithdraw(id: UInt64, uuid: UInt64, from: Address?, type: String): Bool
{
emit Withdraw(id: id, uuid: uuid, from: from, type: type)
return true
Expand All @@ -70,7 +70,7 @@ access(all) contract NonFungibleToken {
///
access(all) event Deposit(id: UInt64, uuid: UInt64, to: Address?, type: String)

access(self) fun emitNFTDeposit(id: UInt64, uuid: UInt64, to: Address?, type: String): Bool
access(self) view fun emitNFTDeposit(id: UInt64, uuid: UInt64, to: Address?, type: String): Bool
{
emit Deposit(id: id, uuid: uuid, to: to, type: type)
return true
Expand All @@ -82,7 +82,7 @@ access(all) contract NonFungibleToken {
///
access(all) event Transfer(id: UInt64, uuid: UInt64, from: Address?, to: Address?, type: String)

access(self) fun emitNFTTransfer(id: UInt64, uuid: UInt64?, from: Address?, to: Address?, type: String?): Bool
access(self) view fun emitNFTTransfer(id: UInt64, uuid: UInt64?, from: Address?, to: Address?, type: String?): Bool
{
// The transfer method can return false even if it didn't do a transfer
// in which case we don't want the event to be emitted
Expand All @@ -99,7 +99,7 @@ access(all) contract NonFungibleToken {
/// The event that should be emitted when an NFT is destroyed
access(all) event Destroy(id: UInt64, uuid: UInt64, type: String)

access(self) fun emitNFTDestroy(id: UInt64, uuid: UInt64, type: String): Bool
access(self) view fun emitNFTDestroy(id: UInt64, uuid: UInt64, type: String): Bool
{
emit Destroy(id: id, uuid: uuid, type: type)
return true
Expand All @@ -122,7 +122,7 @@ access(all) contract NonFungibleToken {
destroy() {
pre {
//NonFungibleToken.emitNFTDestroy(id: self.getID(), uuid: self.uuid, type: self.getType().identifier)
NonFungibleToken.emitNFTDestroy(id: self.getID(), uuid: self.uuid, type: self.getType().identifier)
}
}
}
Expand All @@ -143,7 +143,7 @@ access(all) contract NonFungibleToken {
access(Withdrawable) fun withdraw(withdrawID: UInt64): @{NFT} {
post {
result.getID() == withdrawID: "The ID of the withdrawn token must be the same as the requested ID"
//NonFungibleToken.emitNFTWithdraw(id: result.getID(), uuid: result.uuid, from: self.owner?.address, type: result.getType().identifier)
NonFungibleToken.emitNFTWithdraw(id: result.getID(), uuid: result.uuid, from: self.owner?.address, type: result.getType().identifier)
}
}

Expand All @@ -161,7 +161,7 @@ access(all) contract NonFungibleToken {
access(Withdrawable) fun withdrawWithUUID(_ uuid: UInt64): @{NFT} {
post {
result == nil || result!.uuid == uuid: "The ID of the withdrawn token must be the same as the requested ID"
//NonFungibleToken.emitNFTWithdraw(id: result.getID(), uuid: result.uuid, from: self.owner?.address, type: result.getType().identifier)
NonFungibleToken.emitNFTWithdraw(id: result.getID(), uuid: result.uuid, from: self.owner?.address, type: result.getType().identifier)
}
}

Expand All @@ -170,7 +170,7 @@ access(all) contract NonFungibleToken {
access(Withdrawable) fun withdrawWithType(type: Type, withdrawID: UInt64): @{NFT} {
post {
result == nil || result.getID() == withdrawID: "The ID of the withdrawn token must be the same as the requested ID"
//NonFungibleToken.emitNFTWithdraw(id: result.getID(), uuid: result.uuid, from: self.owner?.address, type: result.getType().identifier)
NonFungibleToken.emitNFTWithdraw(id: result.getID(), uuid: result.uuid, from: self.owner?.address, type: result.getType().identifier)
}
}

Expand All @@ -179,7 +179,7 @@ access(all) contract NonFungibleToken {
access(Withdrawable) fun withdrawWithTypeAndUUID(type: Type, uuid: UInt64): @{NFT} {
post {
result == nil || result!.uuid == uuid: "The ID of the withdrawn token must be the same as the requested ID"
//NonFungibleToken.emitNFTWithdraw(id: result.getID(), uuid: result.uuid, from: self.owner?.address, type: result.getType().identifier)
NonFungibleToken.emitNFTWithdraw(id: result.getID(), uuid: result.uuid, from: self.owner?.address, type: result.getType().identifier)
}
}
}
Expand Down Expand Up @@ -247,7 +247,7 @@ access(all) contract NonFungibleToken {
/// and returns it to the caller so that they can own NFTs
access(all) fun createEmptyCollection(): @{Collection} {
post {
result.getIDs().length == 0: "The created collection must be empty!"
result.getLength() == 0: "The created collection must be empty!"
}
}

Expand All @@ -260,24 +260,23 @@ access(all) contract NonFungibleToken {

/// deposit takes a NFT and adds it to the collections dictionary
/// and adds the ID to the id array
access(all) fun deposit(token: @{NonFungibleToken.NFT})
// {
// pre {
// // We emit the deposit event in the `Collection` interface
// // because the `Collection` interface is almost always the final destination
// // of tokens and deposit emissions from custom receivers could be confusing
// // and hard to reconcile to event listeners
// //NonFungibleToken.emitNFTDeposit(id: token.getID(), uuid: token.uuid, to: self.owner?.address, type: token.getType().identifier)
// }
// }
access(all) fun deposit(token: @{NonFungibleToken.NFT}) {
pre {
// We emit the deposit event in the `Collection` interface
// because the `Collection` interface is almost always the final destination
// of tokens and deposit emissions from custom receivers could be confusing
// and hard to reconcile to event listeners
NonFungibleToken.emitNFTDeposit(id: token.getID(), uuid: token.uuid, to: self.owner?.address, type: token.getType().identifier)
}
}

/// Function for a direct transfer instead of having to do a deposit and withdrawal
/// This can and should return false if the transfer doesn't succeed and true if it does succeed
///
access(Withdrawable) fun transfer(id: UInt64, receiver: Capability<&{NonFungibleToken.Receiver}>): Bool {
pre {
receiver.check(): "Could not borrow a reference to the NFT receiver"
//NonFungibleToken.emitNFTTransfer(id: id, uuid: self.borrowNFTSafe(id: id)?.uuid, from: self.owner?.address, to: receiver.borrow()?.owner?.address, type: self.borrowNFT(id).getType().identifier)
self.getIDs().contains(id): "The collection does not contain the specified ID"
NonFungibleToken.emitNFTTransfer(id: id, uuid: self.borrowNFTSafe(id: id)?.uuid, from: self.owner?.address, to: receiver.borrow()?.owner?.address, type: self.borrowNFT(id).getType().identifier)
}
}

Expand Down
Loading

0 comments on commit 683df24

Please sign in to comment.