Skip to content

Commit

Permalink
update NFTForwarding transactions for new implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sisyphusSmiling committed Oct 20, 2023
1 parent 1a261c0 commit d9ef40e
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 171 deletions.
35 changes: 0 additions & 35 deletions transactions/NFTForwarding/change_forwarder_recipient.cdc

This file was deleted.

63 changes: 0 additions & 63 deletions transactions/NFTForwarding/create_forwarder.cdc

This file was deleted.

44 changes: 0 additions & 44 deletions transactions/NFTForwarding/transfer_nft_to_receiver.cdc

This file was deleted.

29 changes: 0 additions & 29 deletions transactions/NFTForwarding/unlink_forwarder_link_collection.cdc

This file was deleted.

30 changes: 30 additions & 0 deletions transactions/nft-forwarding/change_forwarder_recipient.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import NonFungibleToken from "NonFungibleToken"
import NFTForwarding from "NFTForwarding"

/// This transaction updates the NFTForwarder recipient to the one given at the specified PublicPath
///
transaction(newRecipientAddress: Address, collectionPublicPath: PublicPath) {

// reference to the NFTFowarder Resource
let forwarderRef: auth(NFTForwarding.Mutable) &NFTForwarding.NFTForwarder
// Collection we will designate as forwarding recipient
let newRecipientCollection: Capability<&{NonFungibleToken.Collection}>

prepare(signer: auth(BorrowValue) &Account) {
// borrow reference to NFTForwarder resource
self.forwarderRef = signer.storage.borrow<auth(NFTForwarding.Mutable) &NFTForwarding.NFTForwarder>(
from: NFTForwarding.StoragePath
) ?? panic("Could not borrow reference to NFTForwarder")

// get Collection Capability from the recipientAddress account
self.newRecipientCollection = getAccount(newRecipientAddress).capabilities.get<&{NonFungibleToken.Collection}>(
collectionPublicPath
) ?? panic("Could not get Collection capability from recipient")

}

execute {
// set new recipient
self.forwarderRef.changeRecipient(self.newRecipientCollection)
}
}
29 changes: 29 additions & 0 deletions transactions/nft-forwarding/create_forwarder.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import NonFungibleToken from "NonFungibleToken"
import MetadataViews from "MetadataViews"
import NFTForwarding from "NFTForwarding"

/// This transaction is what an account would run to set itself up to forward NFTs to a designated recipient's
/// NFT.Collection assuming the recipient is configured for the given NFT Collection
///
transaction(recipientAddress: Address, collectionPublicPath: PublicPath) {

prepare(signer: auth(BorrowValue, IssueStorageCapabilityController, PublishCapability, SaveValue) &Account) {

// get Collection Capability from the recipientAddress account
let recipientCollectionCap = getAccount(recipientAddress).capabilities.get<&{NonFungibleToken.Collection}>(
collectionPublicPath
) ?? panic("Recipient is not configured with NFT Collection at the given path")

// create a new NFTForwarder resource & save in storage, forwarding to the recipient's Collection
let forwarder <- NFTForwarding.createNewNFTForwarder(recipient: recipientCollectionCap)
signer.storage.save(<-forwarder, to: NFTForwarding.StoragePath)

// unpublish existing Collection capabilities from PublicPath
signer.capabilities.unpublish(collectionPublicPath)

// create & publish a capability for the forwarder where the collection would normally be
let forwarderReceiverCap = signer.capabilities.storage.issue<&{NonFungibleToken.Receiver}>(NFTForwarding.StoragePath)
signer.capabilities.publish(forwarderReceiverCap, at: collectionPublicPath)

}
}
50 changes: 50 additions & 0 deletions transactions/nft-forwarding/transfer_nft_to_receiver.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import NonFungibleToken from "NonFungibleToken"
import ViewResolver from "ViewResolver"
import MetadataViews from "MetadataViews"

/// This transaction is for transferring an NFT from one account to the recipient's Receiver
///
transaction(
contractAddress: Address,
contractName: String,
recipient: Address,
withdrawID: UInt64
) {

// reference to the withdrawer's collection
let withdrawRef: auth(Withdrawable) &{NonFungibleToken.Collection}
// reference of the Receiver to deposit the NFT to
let depositRef: &{NonFungibleToken.Receiver}

prepare(signer: auth(BorrowValue) &Account) {

// get the collection data from the NFT contract
let nftContract = getAccount(contractAddress).contracts.borrow<&ViewResolver>(name: contractName)
?? panic("Could not borrow ViewResolver reference to the contract")
let collectionData = nftContract.resolveView(Type<MetadataViews.NFTCollectionData>()) as MetadataViews.NFTCollectionData?
?? panic("Could not resolve NFTCollectionData view")

// borrow a reference to the signer's NFT collection
self.withdrawRef = signer.storage.borrow<auth(Withdrawable) &{NonFungibleToken.Collection}>(
from: collectionData.storagePath
) ?? panic("Account does not store an object at the specified path")

// borrow a public reference to the recipient's Receiver
self.depositRef = getAccount(recipient).capabilities.borrow<&{NonFungibleToken.Receiver}>(
collectionData.publicPath
) ?? panic("Could not borrow a reference to the recipient's Receiver")
}

execute {

// withdraw the NFT from the owner's collection
let nft <- self.withdrawRef.withdraw(withdrawID: withdrawID)

// Deposit the NFT in the recipient
self.depositRef.deposit(token: <-nft)
}

post {
!self.withdrawRef.getIDs().contains(withdrawID): "Original owner should not have the NFT anymore"
}
}
26 changes: 26 additions & 0 deletions transactions/nft-forwarding/unlink_forwarder_link_collection.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

import NonFungibleToken from "NonFungibleToken"
import MetadataViews from "MetadataViews"
import NFTForwarding from "NFTForwarding"

// This transaction replaces NFTForwarder Receiver Capabilities with a collection to its public storage after having configured
// its NFTForwarder
///
transaction(collectionStoragePath: StoragePath, receiverPublicPath: PublicPath) {

prepare(signer: AuthAccount) {

// a collection is already published, do nothing - remember .NFTForwarder only conforms to NFT.Receiver
if signer.capabilities.get<&{NonFungibleToken.Collection}>(receiverPublicPath) != nil {
return
}

// otherwise, unpublish the published Capability
signer.capabilities.unpublish(receiverPublicPath)

// create & publish a capability for the collection
let collectionCap = signer.capabilities.storage.issue<&{NonFungibleToken.Collection}>(collectionStoragePath)
signer.capabilities.publish(collectionCap, receiverPublicPath)

}
}

0 comments on commit d9ef40e

Please sign in to comment.