Skip to content

Commit 5f9d32b

Browse files
committed
allow updating distribution
1 parent c25cf88 commit 5f9d32b

4 files changed

Lines changed: 44 additions & 10 deletions

File tree

pds/contracts/PDS.cdc

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ access(all) contract PDS{
3030
///
3131
access(all) event DistributionCreated(DistId: UInt64, title: String, metadata: {String: String}, state: UInt8)
3232

33+
/// Emitted when an issuer has updated a distribution.
34+
///
35+
access(all) event DistributionUpdated(DistId: UInt64, title: String, metadata: {String: String}, state: UInt8)
36+
3337
/// Emmitted when a distribution manager has updated a distribution state.
3438
///
3539
access(all) event DistributionStateUpdated(DistId: UInt64, state: UInt8)
@@ -45,14 +49,19 @@ access(all) contract PDS{
4549
/// Struct that defines the details of a Distribution.
4650
///
4751
access(all) struct DistInfo {
48-
access(all) let title: String
49-
access(all) let metadata: {String: String}
52+
access(all) var title: String
53+
access(all) var metadata: {String: String}
5054
access(all) var state: PDS.DistState
5155

5256
access(contract) fun setState(newState: PDS.DistState) {
5357
self.state = newState
5458
}
5559

60+
access(contract) fun update(title: String, metadata: {String: String}) {
61+
self.title = title
62+
self.metadata = metadata
63+
}
64+
5665
/// DistInfo struct initializer.
5766
///
5867
view init(title: String, metadata: {String: String}) {
@@ -209,11 +218,23 @@ access(all) contract PDS{
209218
}
210219

211220
access(Operate) fun createDist(sharedCap: @SharedCapabilities, title: String, metadata: {String: String}) {
212-
assert(title.length > 0, message: "Title must not be empty")
221+
pre {
222+
title.length > 0: "Title must not be empty"
223+
}
213224
let c = self.cap!.borrow()!
214225
c.createNewDist(sharedCap: <- sharedCap, title: title, metadata: metadata)
215226
}
216227

228+
access(Operate) fun updateDist(distId: UInt64, title: String, metadata: {String: String}) {
229+
pre {
230+
title.length > 0: "Title must not be empty"
231+
}
232+
let d = PDS.Distributions.remove(key: distId) ?? panic ("No such distribution")
233+
d.update(title: title, metadata: metadata)
234+
PDS.Distributions.insert(key: distId, d)
235+
emit DistributionUpdated(DistId: distId, title: title, metadata: metadata, state: d.state.rawValue)
236+
}
237+
217238
/// PackIssuer resource initializer.
218239
///
219240
view init() {

pds/embed.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,6 @@ var (
3535
PDSRevealPackNFT []byte
3636
//go:embed transactions/pds/create_distribution.cdc
3737
PDSCreateDistribution []byte
38+
//go:embed transactions/pds/update_distribution.cdc
39+
PDSUpdateDistribution []byte
3840
)

pds/transactions/pds/create_distribution.cdc

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
import PDS from "PDS"
22
import PackNFT from "PackNFT"
3-
import ExampleNFT from "ExampleNFT"
43
import IPackNFT from "IPackNFT"
54
import NonFungibleToken from "NonFungibleToken"
6-
import MetadataViews from "MetadataViews"
75

86
transaction(nftWithdrawCapPath: StoragePath, title: String, metadata: {String: String}) {
97
prepare (issuer: auth(Storage, Capabilities) &Account) {
10-
let collectionData = ExampleNFT.resolveContractView(resourceType: nil, viewType: Type<MetadataViews.NFTCollectionData>()) as! MetadataViews.NFTCollectionData?
11-
?? panic("ViewResolver does not resolve NFTCollectionData view")
12-
138
// get pack issuer reference
14-
let i = issuer.storage.borrow<auth(PDS.Operate) &PDS.PackIssuer>(from: PDS.PackIssuerStoragePath)
9+
let issuerRef = issuer.storage.borrow<auth(PDS.Operate) &PDS.PackIssuer>(from: PDS.PackIssuerStoragePath)
1510
?? panic ("issuer does not have PackIssuer resource")
1611

1712
// get withdraw capability from issuer
@@ -24,7 +19,7 @@ transaction(nftWithdrawCapPath: StoragePath, title: String, metadata: {String: S
2419
assert(operatorCap.check(), message: "cannot borrow operator capability")
2520

2621
// create SharedCapabilities resource with withdraw and operator capabilities, and create distribution with it
27-
i.createDist(
22+
issuerRef.createDist(
2823
sharedCap: <- PDS.createSharedCapabilities(withdrawCap: withdrawCap, operatorCap: operatorCap),
2924
title: title,
3025
metadata: metadata,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import PDS from "PDS"
2+
3+
transaction (distId: UInt64, title: String, metadata: {String: String}) {
4+
prepare(issuer: auth(BorrowValue) &Account) {
5+
// get pack issuer reference
6+
let issuerRef = issuer.storage.borrow<auth(PDS.Operate) &PDS.PackIssuer>(from: PDS.PackIssuerStoragePath)
7+
?? panic ("issuer does not have PackIssuer resource")
8+
9+
// update distribution
10+
issuerRef.updateDist(
11+
distId: distId,
12+
title: title,
13+
metadata: metadata,
14+
)
15+
}
16+
}

0 commit comments

Comments
 (0)