Skip to content

Commit

Permalink
enable any custom attributes in addition to the subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
thounyy committed Oct 4, 2023
1 parent 18bcfe2 commit c85b768
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 129 deletions.
85 changes: 42 additions & 43 deletions contracts/examples/nft-subscription/scenarios/mint_nft.scen.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "mint nfts",
"name": "mint and update nft",
"steps": [
{
"step": "externalSteps",
Expand All @@ -15,32 +15,52 @@
"arguments": [],
"gasLimit": "20,000,000",
"gasPrice": "0"
},
"expect": {
"out": [],
"status": "0",
"message": "",
"gas": "*",
"refund": "*"
}
},
{
"step": "checkState",
"accounts": {
"address:owner": {
"nonce": "2",
"balance": "0",
"esdt": {
"str:NFT-123456": {
"instances": [
{
"nonce": "1",
"balance": "1",
"creator": "sc:nft-subscription",
"attributes": {
"0-expiration": "u64:0",
"1-attributes": "nested:str:common"
}
}
]
}
}
},
"+": ""
}
},
{
"step": "scCall",
"id": "create-NFT-2",
"id": "update-NFT-1",
"tx": {
"from": "address:owner",
"to": "sc:nft-subscription",
"function": "mint",
"arguments": [],
"esdtValue": [
{
"tokenIdentifier": "str:NFT-123456",
"nonce": "1",
"value": "1"
}
],
"function": "update_attributes",
"arguments": [
"str:rare"
],
"gasLimit": "20,000,000",
"gasPrice": "0"
},
"expect": {
"out": [],
"status": "0",
"message": "",
"gas": "*",
"refund": "*"
}
},
{
Expand All @@ -56,36 +76,15 @@
"nonce": "1",
"balance": "1",
"creator": "sc:nft-subscription",
"attributes": "0"
},
{
"nonce": "2",
"balance": "1",
"creator": "sc:nft-subscription",
"attributes": "0"
"attributes": {
"0-expiration": "u64:0",
"1-attributes": "nested:str:rare"
}
}
]
}
}
},
"sc:nft-subscription": {
"nonce": "0",
"balance": "0",
"esdt": {
"str:NFT-123456": {
"lastNonce": "2",
"roles": [
"ESDTRoleNFTCreate",
"ESDTRoleNFTUpdateAttributes"
]
}
},
"storage": {
"str:tokenId": "str:NFT-123456"
},
"code": "file:../output/nft-subscription.wasm",
"owner": "address:owner"
},
"+": ""
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@
],
"gasLimit": "20,000,000",
"gasPrice": "0"
},
"expect": {
"out": [],
"status": "0",
"message": "",
"gas": "*",
"refund": "*"
}
},
{
Expand All @@ -52,13 +45,10 @@
"nonce": "1",
"balance": "1",
"creator": "sc:nft-subscription",
"attributes": "3"
},
{
"nonce": "2",
"balance": "1",
"creator": "sc:nft-subscription",
"attributes": "0"
"attributes": {
"0-expiration": "u64:3",
"1-attributes": "nested:str:rare"
}
}
]
}
Expand Down Expand Up @@ -92,13 +82,6 @@
],
"gasLimit": "20,000,000",
"gasPrice": "0"
},
"expect": {
"out": [],
"status": "0",
"message": "",
"gas": "*",
"refund": "*"
}
},
{
Expand All @@ -114,13 +97,10 @@
"nonce": "1",
"balance": "1",
"creator": "sc:nft-subscription",
"attributes": "6"
},
{
"nonce": "2",
"balance": "1",
"creator": "sc:nft-subscription",
"attributes": "0"
"attributes": {
"0-expiration": "u64:6",
"1-attributes": "nested:str:rare"
}
}
]
}
Expand Down Expand Up @@ -154,13 +134,6 @@
],
"gasLimit": "20,000,000",
"gasPrice": "0"
},
"expect": {
"out": [],
"status": "0",
"message": "",
"gas": "*",
"refund": "*"
}
},
{
Expand All @@ -176,13 +149,10 @@
"nonce": "1",
"balance": "1",
"creator": "sc:nft-subscription",
"attributes": "15"
},
{
"nonce": "2",
"balance": "1",
"creator": "sc:nft-subscription",
"attributes": "0"
"attributes": {
"0-expiration": "u64:15",
"1-attributes": "nested:str:rare"
}
}
]
}
Expand Down Expand Up @@ -214,13 +184,6 @@
"arguments": [],
"gasLimit": "20,000,000",
"gasPrice": "0"
},
"expect": {
"out": [],
"status": "0",
"message": "",
"gas": "*",
"refund": "*"
}
},
{
Expand All @@ -236,13 +199,10 @@
"nonce": "1",
"balance": "1",
"creator": "sc:nft-subscription",
"attributes": "0"
},
{
"nonce": "2",
"balance": "1",
"creator": "sc:nft-subscription",
"attributes": "0"
"attributes": {
"0-expiration": "u64:0",
"1-attributes": "nested:str:rare"
}
}
]
}
Expand Down
39 changes: 29 additions & 10 deletions contracts/examples/nft-subscription/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,29 @@ pub trait NftSubscription:

#[endpoint]
fn mint(&self) {
self.token_id().nft_create_and_send(
let nonce = self.create_subscription_nft(
self.token_id().get_token_id_ref(),
&BigUint::from(1u8),
&ManagedBuffer::new(),
&BigUint::from(0u8),
&ManagedBuffer::new(),
0,
ManagedBuffer::from(b"common"),
&ManagedVec::new(),
);
self.send().direct_esdt(
&self.blockchain().get_caller(),
BigUint::from(1u8),
&0u64,
self.token_id().get_token_id_ref(),
nonce,
&BigUint::from(1u8),
);
}

#[payable("*")]
#[endpoint]
fn renew(&self, duration: u64) {
fn update_attributes(&self, attributes: ManagedBuffer) {
let (id, nonce, _) = self.call_value().single_esdt().into_tuple();
self.renew_subscription(&id, nonce, duration);
self.update_subscription_attributes::<ManagedBuffer>(&id, nonce, attributes);
self.send().direct_esdt(
&self.blockchain().get_caller(),
&id,
Expand All @@ -48,9 +59,9 @@ pub trait NftSubscription:

#[payable("*")]
#[endpoint]
fn cancel(&self) {
fn renew(&self, duration: u64) {
let (id, nonce, _) = self.call_value().single_esdt().into_tuple();
self.cancel_subscription(&id, nonce);
self.renew_subscription::<ManagedBuffer>(&id, nonce, duration);
self.send().direct_esdt(
&self.blockchain().get_caller(),
&id,
Expand All @@ -59,9 +70,17 @@ pub trait NftSubscription:
);
}

#[view]
fn expires(&self, id: &TokenIdentifier, nonce: u64) -> u64 {
self.expires_at(id, nonce)
#[payable("*")]
#[endpoint]
fn cancel(&self) {
let (id, nonce, _) = self.call_value().single_esdt().into_tuple();
self.cancel_subscription::<ManagedBuffer>(&id, nonce);
self.send().direct_esdt(
&self.blockchain().get_caller(),
&id,
nonce,
&BigUint::from(1u8),
);
}

#[storage_mapper("tokenId")]
Expand Down
9 changes: 3 additions & 6 deletions contracts/examples/nft-subscription/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
////////////////////////////////////////////////////

// Init: 1
// Endpoints: 8
// Endpoints: 5
// Async Callback: 1
// Total number of exported functions: 10
// Total number of exported functions: 7

#![no_std]

Expand All @@ -24,12 +24,9 @@ multiversx_sc_wasm_adapter::endpoints! {
init => init
issue => issue
mint => mint
update_attributes => update_attributes
renew => renew
cancel => cancel
expires => expires
renewSubscription => renew_subscription
cancelSubscription => cancel_subscription
getExpiresAt => expires_at
)
}

Expand Down
Loading

0 comments on commit c85b768

Please sign in to comment.