Skip to content

Commit c6b1792

Browse files
committed
fixes: #58 fixes
1 parent 0e04489 commit c6b1792

10 files changed

Lines changed: 55 additions & 70 deletions

File tree

cadence-contracts/IPackNFT.cdc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ pub contract interface IPackNFT{
4444
// TODO Pack resource
4545

4646
pub resource interface IOperator {
47-
pub fun mint(distId: UInt64, commitHash: String, issuer: Address)
47+
pub fun mint(distId: UInt64, commitHash: String, issuer: Address): @NFT
4848
pub fun reveal(id: UInt64, nfts: [{Collectible}], salt: String)
4949
pub fun open(id: UInt64)
5050
}
5151
pub resource PackNFTOperator: IOperator {
52-
pub fun mint(distId: UInt64, commitHash: String, issuer: Address)
52+
pub fun mint(distId: UInt64, commitHash: String, issuer: Address): @NFT
5353
pub fun reveal(id: UInt64, nfts: [{Collectible}], salt: String)
5454
pub fun open(id: UInt64)
5555
}

cadence-contracts/PDS.cdc

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub contract PDS{
1111
pub let distCreatorPrivPath: PrivatePath
1212
pub let distManagerStoragePath: StoragePath
1313

14-
pub var DistId: UInt64
14+
pub var nextDistId: UInt64
1515
access(contract) let Distributions: @{UInt64: SharedCapabilities}
1616

1717
pub struct Collectible: IPackNFT.Collectible {
@@ -57,14 +57,14 @@ pub contract PDS{
5757
return <- c.withdraw(withdrawID: withdrawID)
5858
}
5959

60-
// TODO: maybe we do not need to specify the issuer here, should be the creator of the SharedCapabilities
61-
// this is also used in storing inside the NFT though
62-
pub fun mintPackNFT(distId: UInt64, commitHashes: [String], issuer: Address){
60+
pub fun mintPackNFT(distId: UInt64, commitHashes: [String], issuer: Address, recvCap: &{NonFungibleToken.CollectionPublic} ){
6361
var i = 0
6462
let c = self.operatorCap.borrow() ?? panic("no such cap")
6563
while i < commitHashes.length{
66-
c.mint(distId: distId, commitHash: commitHashes[i], issuer: issuer)
64+
let nft <- c.mint(distId: distId, commitHash: commitHashes[i], issuer: issuer)
6765
i = i + 1
66+
let n <- nft as! @NonFungibleToken.NFT
67+
recvCap.deposit(token: <- n)
6868
}
6969
}
7070

@@ -73,11 +73,11 @@ pub contract PDS{
7373
c.reveal(id: packId, nfts: nfts, salt: salt)
7474
}
7575

76-
pub fun openPackNFT(packId: UInt64, nftIds: [UInt64], owner: Address, collectionProviderPath: PrivatePath, recvCollectionPublicPath: PublicPath) {
76+
pub fun openPackNFT(packId: UInt64, nftIds: [UInt64], recvCap: &{NonFungibleToken.CollectionPublic}, collectionProviderPath: PrivatePath) {
7777
let c = self.operatorCap.borrow() ?? panic("no such cap")
7878
// This checks and sets the status of the pack before releasing escrow
7979
c.open(id: packId)
80-
PDS.releaseEscrow(nftIds: nftIds, owner: owner, collectionProviderPath: collectionProviderPath, recvCollectionPublicPath: recvCollectionPublicPath)
80+
PDS.releaseEscrow(nftIds: nftIds, recvCap: recvCap , collectionProviderPath: collectionProviderPath)
8181
}
8282

8383

@@ -125,9 +125,9 @@ pub contract PDS{
125125

126126
pub resource DistributionCreator: IDistCreator {
127127
pub fun createNewDist(sharedCap: @SharedCapabilities) {
128-
let currentId = PDS.DistId
128+
let currentId = PDS.nextDistId
129129
PDS.Distributions[currentId] <-! sharedCap
130-
PDS.DistId = currentId + 1
130+
PDS.nextDistId = currentId + 1
131131
emit DistributionCreated(DistId: currentId)
132132
}
133133
}
@@ -146,10 +146,10 @@ pub contract PDS{
146146
PDS.Distributions[distId] <-! d
147147
}
148148

149-
pub fun mintPackNFT(distId: UInt64, commitHashes: [String], issuer: Address){
149+
pub fun mintPackNFT(distId: UInt64, commitHashes: [String], issuer: Address, recvCap: &{NonFungibleToken.CollectionPublic}){
150150
assert(PDS.Distributions.containsKey(distId), message: "No such distribution")
151151
let d <- PDS.Distributions.remove(key: distId)!
152-
d.mintPackNFT(distId: distId, commitHashes: commitHashes, issuer: issuer)
152+
d.mintPackNFT(distId: distId, commitHashes: commitHashes, issuer: issuer, recvCap: recvCap)
153153
PDS.Distributions[distId] <-! d
154154
}
155155

@@ -172,35 +172,27 @@ pub contract PDS{
172172
PDS.Distributions[distId] <-! d
173173
}
174174

175-
pub fun openPackNFT(distId: UInt64, packId: UInt64, nftIds: [UInt64], owner: Address, collectionProviderPath: PrivatePath, recvCollectionPublicPath: PublicPath){
175+
pub fun openPackNFT(distId: UInt64, packId: UInt64, nftIds: [UInt64], recvCap: &{NonFungibleToken.CollectionPublic}, collectionProviderPath: PrivatePath){
176176
assert(PDS.Distributions.containsKey(distId), message: "No such distribution")
177177
let d <- PDS.Distributions.remove(key: distId)!
178-
d.openPackNFT(packId: packId, nftIds: nftIds, owner: owner, collectionProviderPath: collectionProviderPath, recvCollectionPublicPath: recvCollectionPublicPath)
178+
d.openPackNFT(packId: packId, nftIds: nftIds, recvCap: recvCap, collectionProviderPath: collectionProviderPath)
179179
PDS.Distributions[distId] <-! d
180180
}
181181

182182
}
183183

184184
access(contract) fun getManagerCollectionCap(escrowCollectionPublic: PublicPath): Capability<&{NonFungibleToken.CollectionPublic}> {
185185
let pdsCollection = self.account.getCapability<&{NonFungibleToken.CollectionPublic}>(escrowCollectionPublic)
186-
if !pdsCollection.check(){
187-
panic("Please ensure PDS has created and linked a Collection for recieving escrows")
188-
}
186+
assert(pdsCollection.check(), message: "Please ensure PDS has created and linked a Collection for recieving escrows")
189187
return pdsCollection
190188
}
191189

192-
access(contract) fun releaseEscrow(nftIds: [UInt64], owner: Address, collectionProviderPath: PrivatePath, recvCollectionPublicPath: PublicPath) {
190+
access(contract) fun releaseEscrow(nftIds: [UInt64], recvCap: &{NonFungibleToken.CollectionPublic}, collectionProviderPath: PrivatePath ) {
193191
let pdsCollection = self.account.getCapability(collectionProviderPath).borrow<&{NonFungibleToken.Provider}>()
194192
?? panic("Unable to borrow PDS collection provider capability from private path")
195-
let recvAcct = getAccount(owner)
196-
let recv = recvAcct.getCapability(recvCollectionPublicPath).borrow<&{NonFungibleToken.CollectionPublic}>()
197-
?? panic("Unable to borrow Collection Public reference for recipient")
198-
log("releasing escrow")
199-
log(nftIds)
200193
var i = 0
201194
while i < nftIds.length {
202-
log(nftIds[i])
203-
recv.deposit(token: <- pdsCollection.withdraw(withdrawID: nftIds[i]))
195+
recvCap.deposit(token: <- pdsCollection.withdraw(withdrawID: nftIds[i]))
204196
i = i + 1
205197
}
206198
}
@@ -221,15 +213,14 @@ pub contract PDS{
221213

222214

223215
init(
224-
adminAccount: AuthAccount,
225216
packIssuerStoragePath: StoragePath,
226217
packIssuerCapRecv: PublicPath,
227218
distCreatorStoragePath: StoragePath,
228219
distCreatorPrivPath: PrivatePath,
229220
distManagerStoragePath: StoragePath,
230221
version: String
231222
) {
232-
self.DistId = 0
223+
self.nextDistId = 0
233224
self.Distributions <- {}
234225
self.packIssuerStoragePath = packIssuerStoragePath
235226
self.packIssuerCapRecv = packIssuerCapRecv
@@ -240,11 +231,11 @@ pub contract PDS{
240231

241232
// Create a distributionCreator to share create capability with PackIssuer
242233
let d <- create DistributionCreator()
243-
adminAccount.save(<-d, to: self.distCreatorStoragePath)
244-
adminAccount.link<&DistributionCreator{PDS.IDistCreator}>(self.distCreatorPrivPath, target: self.distCreatorStoragePath)
234+
self.account.save(<-d, to: self.distCreatorStoragePath)
235+
self.account.link<&DistributionCreator{PDS.IDistCreator}>(self.distCreatorPrivPath, target: self.distCreatorStoragePath)
245236

246237
// Create a distributionManager to manager distributions (withdraw for escrow, mint PackNFT todo: reveal / transfer)
247238
let m <- create DistributionManager()
248-
adminAccount.save(<-m, to: self.distManagerStoragePath)
239+
self.account.save(<-m, to: self.distManagerStoragePath)
249240
}
250241
}

cadence-contracts/PackNFT.cdc

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,14 @@ pub contract PackNFT: NonFungibleToken, IPackNFT {
2626

2727
pub resource PackNFTOperator: IPackNFT.IOperator {
2828

29-
pub fun mint(distId: UInt64, commitHash: String, issuer: Address) {
29+
pub fun mint(distId: UInt64, commitHash: String, issuer: Address): @NFT{
3030
let id = PackNFT.totalSupply
3131
let nft <- create NFT(initID: id, commitHash: commitHash, issuer: issuer)
3232
PackNFT.totalSupply = id + 1
33-
let recvAcct = getAccount(issuer)
34-
let recv = recvAcct.getCapability(PackNFT.collectionPublicPath).borrow<&{NonFungibleToken.CollectionPublic}>()
35-
?? panic("Unable to borrow Collection Public reference for recipient")
36-
37-
recv.deposit(token: <- nft)
3833
let p <-create Pack(commitHash: commitHash, issuer: issuer)
3934
PackNFT.packs[id] <-! p
4035
emit Mint(id: id, commitHash: commitHash, distId: distId)
36+
return <- nft
4137
}
4238

4339
pub fun reveal(id: UInt64, nfts: [{IPackNFT.Collectible}], salt: String) {
@@ -66,14 +62,11 @@ pub contract PackNFT: NonFungibleToken, IPackNFT {
6662
var hashString = self.salt!
6763
hashString = hashString.concat(",").concat(nftString)
6864
let hash = HashAlgorithm.SHA2_256.hash(hashString.utf8)
69-
if self.commitHash != String.encodeHex(hash) {
70-
return false
71-
} else {
72-
return true
73-
}
65+
assert(self.commitHash == String.encodeHex(hash), message: "CommitHash was not verified")
66+
return true
7467
}
7568

76-
access(self) fun _verify(nfts: [{IPackNFT.Collectible}], salt: String, commitHash: String): String? {
69+
access(self) fun _verify(nfts: [{IPackNFT.Collectible}], salt: String, commitHash: String): String {
7770
var hashString = salt
7871
var nftString = nfts[0].hashString()
7972
var i = 1
@@ -85,16 +78,13 @@ pub contract PackNFT: NonFungibleToken, IPackNFT {
8578
hashString = hashString.concat(",").concat(nftString)
8679
log(hashString)
8780
let hash = HashAlgorithm.SHA2_256.hash(hashString.utf8)
88-
if commitHash != String.encodeHex(hash) {
89-
return nil
90-
} else {
91-
return nftString
92-
}
81+
assert(self.commitHash == String.encodeHex(hash), message: "CommitHash was not verified")
82+
return nftString
9383
}
9484

9585
access(contract) fun reveal(id: UInt64, nfts: [{IPackNFT.Collectible}], salt: String) {
9686
assert(self.status == "Sealed", message: "Pack status is not Sealed")
97-
let v = self._verify(nfts: nfts, salt: salt, commitHash: self.commitHash) ?? panic("commitHash was not verified")
87+
let v = self._verify(nfts: nfts, salt: salt, commitHash: self.commitHash)
9888
self.salt = salt
9989
self.status = "Revealed"
10090
emit Revealed(id: id, salt: salt, nfts: v)
@@ -153,9 +143,7 @@ pub contract PackNFT: NonFungibleToken, IPackNFT {
153143
pub fun withdraw(withdrawID: UInt64): @NonFungibleToken.NFT {
154144
let token <- self.ownedNFTs.remove(key: withdrawID) ?? panic("missing NFT")
155145
emit Withdraw(id: token.id, from: self.owner?.address)
156-
let nonfungibleToken <- token
157-
158-
return <- nonfungibleToken
146+
return <- token
159147
}
160148

161149
// deposit takes a NFT and adds it to the collections dictionary
@@ -214,12 +202,10 @@ pub contract PackNFT: NonFungibleToken, IPackNFT {
214202
}
215203

216204
pub fun createEmptyCollection(): @NonFungibleToken.Collection {
217-
let c <- create Collection()
218-
return <- c
205+
return <- create Collection()
219206
}
220207

221208
init(
222-
adminAccount: AuthAccount,
223209
collectionStoragePath: StoragePath,
224210
collectionPublicPath: PublicPath,
225211
collectionIPackNFTPublicPath: PublicPath,
@@ -238,14 +224,14 @@ pub contract PackNFT: NonFungibleToken, IPackNFT {
238224

239225
// Create a collection to receive Pack NFTs
240226
let collection <- create Collection()
241-
adminAccount.save(<-collection, to: self.collectionStoragePath)
242-
adminAccount.link<&Collection{NonFungibleToken.CollectionPublic}>(self.collectionPublicPath, target: self.collectionStoragePath)
243-
adminAccount.link<&Collection{IPackNFT.IPackNFTCollectionPublic}>(self.collectionIPackNFTPublicPath, target: self.collectionStoragePath)
227+
self.account.save(<-collection, to: self.collectionStoragePath)
228+
self.account.link<&Collection{NonFungibleToken.CollectionPublic}>(self.collectionPublicPath, target: self.collectionStoragePath)
229+
self.account.link<&Collection{IPackNFT.IPackNFTCollectionPublic}>(self.collectionIPackNFTPublicPath, target: self.collectionStoragePath)
244230

245231
// Create a operator to share mint capability with proxy
246232
let operator <- create PackNFTOperator()
247-
adminAccount.save(<-operator, to: self.operatorStoragePath)
248-
adminAccount.link<&PackNFTOperator{IPackNFT.IOperator}>(self.operatorPrivPath, target: self.operatorStoragePath)
233+
self.account.save(<-operator, to: self.operatorStoragePath)
234+
self.account.link<&PackNFTOperator{IPackNFT.IOperator}>(self.operatorPrivPath, target: self.operatorStoragePath)
249235
}
250236

251237
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import PDS from 0x{{.PDS}}
22

33
pub fun main(): UInt64 {
4-
return PDS.DistId
5-
}
4+
return PDS.nextDistId
5+
}

cadence-transactions/deploy/deploy-packNFT-with-auth.cdc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ transaction(
1818
owner.contracts.add(
1919
name: contractName,
2020
code: code.decodeHex(),
21-
owner,
2221
collectionStoragePath: collectionStoragePath,
2322
collectionPublicPath: collectionPublicPath,
2423
collectionIPackNFTPublicPath: collectionIPackNFTPublicPath,

cadence-transactions/deploy/deploy-pds-with-auth.cdc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ transaction(
1818
owner.contracts.add(
1919
name: contractName,
2020
code: code.decodeHex(),
21-
owner,
2221
packIssuerStoragePath: packIssuerStoragePath,
2322
packIssuerCapRecv: packIssuerCapRecv,
2423
distCreatorStoragePath: distCreatorStoragePath,
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import PDS from 0x{{.PDS}}
2+
import PackNFT from 0x{{.PackNFT}}
3+
import NonFungibleToken from 0x{{.NonFungibleToken}}
24

35
transaction (distId: UInt64, commitHashes: [String], issuer: Address ) {
46
prepare(pds: AuthAccount) {
7+
let recvAcct = getAccount(issuer)
8+
let recv = recvAcct.getCapability(PackNFT.collectionPublicPath).borrow<&{NonFungibleToken.CollectionPublic}>()
9+
?? panic("Unable to borrow Collection Public reference for recipient")
510
let cap = pds.borrow<&PDS.DistributionManager>(from: PDS.distManagerStoragePath) ?? panic("pds does not have Dist manager")
6-
cap.mintPackNFT(distId: distId, commitHashes: commitHashes, issuer: issuer)
11+
cap.mintPackNFT(distId: distId, commitHashes: commitHashes, issuer: issuer, recvCap: recv)
712
}
813
}
914

cadence-transactions/pds/open_packNFT.cdc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import PDS from 0x{{.PDS}}
22
import ExampleNFT from 0x{{.ExampleNFT}}
3+
import NonFungibleToken from 0x{{.NonFungibleToken}}
34

45
transaction (distId: UInt64, packId: UInt64, nftIds: [UInt64], owner: Address) {
56
prepare(pds: AuthAccount) {
67
let cap = pds.borrow<&PDS.DistributionManager>(from: PDS.distManagerStoragePath) ?? panic("pds does not have Dist manager")
8+
let recvAcct = getAccount(owner)
9+
let recv = recvAcct.getCapability(ExampleNFT.CollectionPublicPath).borrow<&{NonFungibleToken.CollectionPublic}>()
10+
?? panic("Unable to borrow Collection Public reference for recipient")
711
cap.openPackNFT(
812
distId: distId,
913
packId: packId,
1014
nftIds: nftIds,
11-
owner: owner,
15+
recvCap: recv,
1216
collectionProviderPath: ExampleNFT.CollectionProviderPrivPath,
13-
recvCollectionPublicPath: ExampleNFT.CollectionPublicPath
1417
)
1518
}
1619
}

go-contracts/contracts_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func TestPDSMintPackNFTs(t *testing.T){
153153
AddField("id", strconv.Itoa(int(expectedId))).
154154
AddField("commitHash", hash).
155155
AddField("distId", strconv.Itoa(int(nextDistId - 1))).
156-
AssertEqual(t, events[1])
156+
AssertEqual(t, events[0])
157157

158158
nextPackNFTId, err := packnft.GetTotalPacks(g)
159159
assert.NoError(t, err)
@@ -419,6 +419,5 @@ func TestPublicVerify(t *testing.T) {
419419

420420
notNfts:= "A." + addr + ".ExampleNFT.2,A."+ addr +".ExampleNFT.4"
421421
v, err = packnft.Verify(g, currentPack, notNfts)
422-
assert.NoError(t, err)
423-
assert.Equal(t, false, v)
422+
assert.Error(t, err)
424423
}

go-contracts/packnft/packnft.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ func Verify(
4545
txScript:= "../cadence-scripts/packNFT/verify.cdc"
4646
code:= util.ParseCadenceTemplate(txScript)
4747
d, err := g.ScriptFromFile(txScript, code).UInt64Argument(id).StringArgument(nftString).RunReturns()
48+
if err != nil {
49+
return
50+
}
4851
verified = d.ToGoValue().(bool)
4952
return
5053
}

0 commit comments

Comments
 (0)