git.delta.rocks / unique-network / refs/commits / 6f2e0cc4002e

difftreelog

refactor combine sponsorship fields

Yaroslav Bolyukin2021-03-12parent: #bc76286.patch.diff
in: master

5 files changed

modifiednode/src/chain_spec.rsdiffbeforeafterboth
--- a/node/src/chain_spec.rs
+++ b/node/src/chain_spec.rs
@@ -193,8 +193,7 @@
                     mint_mode: false,
 					offchain_schema: vec![],
 					schema_version: SchemaVersion::default(),
-                    sponsor: get_account_id_from_seed::<sr25519::Public>("Alice"),
-                    sponsor_confirmed: true,
+                    sponsorship: SponsorshipState::Confirmed(get_account_id_from_seed::<sr25519::Public>("Alice")),
                     const_on_chain_schema: vec![],
 					variable_on_chain_schema: vec![],
 					limits: CollectionLimits::default()
modifiedpallets/nft/src/lib.rsdiffbeforeafterboth
--- a/pallets/nft/src/lib.rs
+++ b/pallets/nft/src/lib.rs
@@ -123,6 +123,42 @@
     pub fraction: u128,
 }
 
+#[derive(Encode, Decode, Debug, Clone, PartialEq)]
+#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
+pub enum SponsorshipState<AccountId> {
+    /// The fees are applied to the transaction sender
+    Disabled,
+    Unconfirmed(AccountId),
+    /// Transactions are sponsored by specified account
+    Confirmed(AccountId),
+}
+
+impl<AccountId> SponsorshipState<AccountId> {
+    fn sponsor(&self) -> Option<&AccountId> {
+        match self {
+            Self::Confirmed(sponsor) => Some(sponsor),
+            _ => None,
+        }
+    }
+
+    fn pending_sponsor(&self) -> Option<&AccountId> {
+        match self {
+            Self::Unconfirmed(sponsor) | Self::Confirmed(sponsor) => Some(sponsor),
+            _ => None,
+        }
+    }
+
+    fn confirmed(&self) -> bool {
+        matches!(self, Self::Confirmed(_))
+    }
+}
+
+impl<T> Default for SponsorshipState<T> {
+    fn default() -> Self {
+        Self::Disabled
+    }
+}
+
 #[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]
 #[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
 pub struct CollectionType<AccountId> {
@@ -136,8 +172,7 @@
     pub mint_mode: bool,
     pub offchain_schema: Vec<u8>,
     pub schema_version: SchemaVersion,
-    pub sponsor: AccountId, // Who pays fees. If set to default address, the fees are applied to the transaction sender
-    pub sponsor_confirmed: bool, // False if sponsor address has not yet confirmed sponsorship. True otherwise.
+    pub sponsorship: SponsorshipState<AccountId>,
     pub limits: CollectionLimits, // Collection private restrictions 
     pub variable_on_chain_schema: Vec<u8>, //
     pub const_on_chain_schema: Vec<u8>, //
@@ -665,8 +700,7 @@
                 token_prefix: token_prefix,
                 offchain_schema: Vec::new(),
                 schema_version: SchemaVersion::ImageURL,
-                sponsor: T::AccountId::default(),
-                sponsor_confirmed: false,
+                sponsorship: SponsorshipState::Disabled,
                 variable_on_chain_schema: Vec::new(),
                 const_on_chain_schema: Vec::new(),
                 limits,
@@ -922,8 +956,7 @@
             let mut target_collection = <Collection<T>>::get(collection_id);
             ensure!(sender == target_collection.owner, Error::<T>::NoPermission);
 
-            target_collection.sponsor = new_sponsor;
-            target_collection.sponsor_confirmed = false;
+            target_collection.sponsorship = SponsorshipState::Unconfirmed(new_sponsor);
             <Collection<T>>::insert(collection_id, target_collection);
 
             Ok(())
@@ -943,9 +976,12 @@
             ensure!(<Collection<T>>::contains_key(collection_id), Error::<T>::CollectionNotFound);
 
             let mut target_collection = <Collection<T>>::get(collection_id);
-            ensure!(sender == target_collection.sponsor, Error::<T>::ConfirmUnsetSponsorFail);
+            ensure!(
+                target_collection.sponsorship.pending_sponsor() == Some(&sender),
+                Error::<T>::ConfirmUnsetSponsorFail
+            );
 
-            target_collection.sponsor_confirmed = true;
+            target_collection.sponsorship = SponsorshipState::Confirmed(sender);
             <Collection<T>>::insert(collection_id, target_collection);
 
             Ok(())
@@ -969,8 +1005,7 @@
             let mut target_collection = <Collection<T>>::get(collection_id);
             ensure!(sender == target_collection.owner, Error::<T>::NoPermission);
 
-            target_collection.sponsor = T::AccountId::default();
-            target_collection.sponsor_confirmed = false;
+            target_collection.sponsorship = SponsorshipState::Disabled;
             <Collection<T>>::insert(collection_id, target_collection);
 
             Ok(())
@@ -2485,7 +2520,9 @@
                 // sponsor timeout
                 let block_number = <system::Module<T>>::block_number() as T::BlockNumber;
 
-                let limit = <Collection<T>>::get(collection_id).limits.sponsor_transfer_timeout;
+                let collection = <Collection<T>>::get(collection_id);
+
+                let limit = collection.limits.sponsor_transfer_timeout;
                 let mut sponsored = true;
                 if <CreateItemBasket<T>>::contains_key((collection_id, &who)) {
                     let last_tx_block = <CreateItemBasket<T>>::get((collection_id, &who));
@@ -2499,11 +2536,12 @@
                 }
 
                 // check free create limit
-                if (<Collection<T>>::get(collection_id).limits.sponsored_data_size >= (_properties.len() as u32)) &&
-                   (<Collection<T>>::get(collection_id).sponsor_confirmed) &&
+                if (collection.limits.sponsored_data_size >= (_properties.len() as u32)) &&
                    (sponsored)
                 {
-                    <Collection<T>>::get(collection_id).sponsor
+                    collection.sponsorship.sponsor()
+                        .cloned()
+                        .unwrap_or_default()
                 } else {
                     T::AccountId::default()
                 }
@@ -2511,7 +2549,7 @@
             Some(Call::transfer(_new_owner, collection_id, item_id, _value)) => {
                 
                 let mut sponsor_transfer = false;
-                if <Collection<T>>::get(collection_id).sponsor_confirmed {
+                if <Collection<T>>::get(collection_id).sponsorship.confirmed() {
 
                     let collection_limits = <Collection<T>>::get(collection_id).limits;
                     let collection_mode = <Collection<T>>::get(collection_id).mode;
@@ -2598,7 +2636,9 @@
                 if !sponsor_transfer {
                     T::AccountId::default()
                 } else {
-                    <Collection<T>>::get(collection_id).sponsor
+                    <Collection<T>>::get(collection_id).sponsorship.sponsor()
+                        .cloned()
+                        .unwrap_or_default()
                 }
             }
 
modifiedruntime_types.jsondiffbeforeafterboth
--- a/runtime_types.json
+++ b/runtime_types.json
@@ -31,6 +31,13 @@
       "ConstData": "Vec<u8>",
       "VariableData": "Vec<u8>"
     },
+    "SponsorshipState": {
+      "_enum": {
+        "Disabled": null,
+        "Unconfirmed": "AccountId",
+        "Confirmed": "AccountId"
+      }
+    },
     "CollectionType": {
       "Owner": "AccountId",
       "Mode": "CollectionMode",
@@ -42,8 +49,7 @@
       "MintMode": "bool",
       "OffchainSchema": "Vec<u8>",
       "SchemaVersion": "SchemaVersion",
-      "Sponsor": "AccountId",
-      "SponsorConfirmed": "bool",
+      "Sponsorship": "SponsorshipState",
       "Limits": "CollectionLimits",
       "VariableOnChainSchema": "Vec<u8>",
       "ConstOnChainSchema": "Vec<u8>"
modifiedtests/package.jsondiffbeforeafterboth
before · tests/package.json
1{2  "name": "NftTests",3  "version": "1.0.0",4  "description": "Substrate Nft tests",5  "main": "",6  "devDependencies": {7    "@polkadot/dev": "^0.61.24",8    "@polkadot/ts": "^0.3.59",9    "@polkadot/typegen": "^3.6.4",10    "@polkadot/util-crypto": "^5.4.4",11    "@types/chai": "^4.2.12",12    "@types/chai-as-promised": "^7.1.3",13    "@types/mocha": "^8.0.3",14    "chai": "^4.2.0",15    "mocha": "^8.1.1",16    "ts-node": "^9.0.0",17    "tslint": "^5.20.1",18    "typescript": "^3.9.7"19  },20  "scripts": {21    "test": "mocha --timeout 9999999 -r ts-node/register ./**/*.test.ts",22    "load": "mocha --timeout 9999999 -r ts-node/register ./**/*.load.ts",23    "loadTransfer": "ts-node src/transfer.nload.ts",24    "testAddCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/addCollectionAdmin.test.ts",25    "testSetSchemaVersion": "mocha --timeout 9999999 -r ts-node/register ./**/setSchemaVersion.test.ts",26    "testSetVariableMetaData": "mocha --timeout 9999999 -r ts-node/register ./**/setVariableMetaData.test.ts",27    "testSetCollectionLimits": "mocha --timeout 9999999 -r ts-node/register ./**/setCollectionLimits.test.ts",28    "testRemoveCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/removeCollectionAdmin.test.ts",29    "testRemoveFromWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/removeFromWhiteList.test.ts",30    "testConnection": "mocha --timeout 9999999 -r ts-node/register ./**/connection.test.ts",31    "testCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",32    "testCreateItem": "mocha --timeout 9999999 -r ts-node/register ./**/createItem.test.ts",33    "testCreateMultipleItems": "mocha --timeout 9999999 -r ts-node/register ./**/createMultipleItems.test.ts",34    "testApprove": "mocha --timeout 9999999 -r ts-node/register ./**/approve.test.ts",35    "testTransferFrom": "mocha --timeout 9999999 -r ts-node/register ./**/transferFrom.test.ts",36    "testCreateCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",37    "testToggleContractWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/toggleContractWhiteList.test.ts",38    "testAddToContractWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/addToContractWhiteList.test.ts",39    "testTransfer": "mocha --timeout 9999999 -r ts-node/register ./**/transfer.test.ts",40    "testBurnItem": "mocha --timeout 9999999 -r ts-node/register ./**/burnItem.test.ts",41    "testSetMintPermission": "mocha --timeout 9999999 -r ts-node/register ./**/setMintPermission.test.ts",42    "testCreditFeesToTreasury": "mocha --timeout 9999999 -r ts-node/register ./**/creditFeesToTreasury.test.ts",43    "testEnableContractSponsoring": "mocha --timeout 9999999 -r ts-node/register ./**/enableContractSponsoring.test.ts",44    "testSetContractSponsoringRateLimit": "mocha --timeout 9999999 -r ts-node/register ./**/setContractSponsoringRateLimit.test.ts",45    "testSetOffchainSchema": "mocha --timeout 9999999 -r ts-node/register ./**/setOffchainSchema.test.ts",46    "testOverflow": "mocha --timeout 9999999 -r ts-node/register ./**/overflow.test.ts"47  },48  "author": "",49  "license": "SEE LICENSE IN ../LICENSE",50  "homepage": "",51  "dependencies": {52    "@polkadot/api": "3.8.1",53    "@polkadot/api-contract": "3.8.1",54    "@polkadot/util": "5.6.2",55    "bignumber.js": "^9.0.0",56    "chai-as-promised": "^7.1.1"57  },58  "standard": {59    "globals": [60      "it",61      "assert",62      "beforeEach",63      "afterEach",64      "describe",65      "contract",66      "artifacts"67    ]68  }69}
after · tests/package.json
1{2  "name": "NftTests",3  "version": "1.0.0",4  "description": "Substrate Nft tests",5  "main": "",6  "devDependencies": {7    "@polkadot/dev": "^0.61.24",8    "@polkadot/ts": "^0.3.59",9    "@polkadot/typegen": "^3.6.4",10    "@polkadot/util-crypto": "^5.4.4",11    "@types/chai": "^4.2.12",12    "@types/chai-as-promised": "^7.1.3",13    "@types/mocha": "^8.0.3",14    "chai": "^4.2.0",15    "mocha": "^8.1.1",16    "ts-node": "^9.0.0",17    "tslint": "^5.20.1",18    "typescript": "^3.9.7"19  },20  "scripts": {21    "test": "mocha --timeout 9999999 -r ts-node/register ./**/*.test.ts",22    "load": "mocha --timeout 9999999 -r ts-node/register ./**/*.load.ts",23    "loadTransfer": "ts-node src/transfer.nload.ts",24    "testAddCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/addCollectionAdmin.test.ts",25    "testSetSchemaVersion": "mocha --timeout 9999999 -r ts-node/register ./**/setSchemaVersion.test.ts",26    "testSetVariableMetaData": "mocha --timeout 9999999 -r ts-node/register ./**/setVariableMetaData.test.ts",27    "testSetCollectionLimits": "mocha --timeout 9999999 -r ts-node/register ./**/setCollectionLimits.test.ts",28    "testRemoveCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/removeCollectionAdmin.test.ts",29    "testRemoveCollectionSponsor": "mocha --timeout 9999999 -r ts-node/register ./**/removeCollectionSponsor.test.ts",30    "testRemoveFromWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/removeFromWhiteList.test.ts",31    "testConnection": "mocha --timeout 9999999 -r ts-node/register ./**/connection.test.ts",32    "testCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",33    "testCreateItem": "mocha --timeout 9999999 -r ts-node/register ./**/createItem.test.ts",34    "testCreateMultipleItems": "mocha --timeout 9999999 -r ts-node/register ./**/createMultipleItems.test.ts",35    "testApprove": "mocha --timeout 9999999 -r ts-node/register ./**/approve.test.ts",36    "testTransferFrom": "mocha --timeout 9999999 -r ts-node/register ./**/transferFrom.test.ts",37    "testCreateCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",38    "testToggleContractWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/toggleContractWhiteList.test.ts",39    "testAddToContractWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/addToContractWhiteList.test.ts",40    "testTransfer": "mocha --timeout 9999999 -r ts-node/register ./**/transfer.test.ts",41    "testBurnItem": "mocha --timeout 9999999 -r ts-node/register ./**/burnItem.test.ts",42    "testSetMintPermission": "mocha --timeout 9999999 -r ts-node/register ./**/setMintPermission.test.ts",43    "testCreditFeesToTreasury": "mocha --timeout 9999999 -r ts-node/register ./**/creditFeesToTreasury.test.ts",44    "testEnableContractSponsoring": "mocha --timeout 9999999 -r ts-node/register ./**/enableContractSponsoring.test.ts",45    "testSetContractSponsoringRateLimit": "mocha --timeout 9999999 -r ts-node/register ./**/setContractSponsoringRateLimit.test.ts",46    "testSetOffchainSchema": "mocha --timeout 9999999 -r ts-node/register ./**/setOffchainSchema.test.ts",47    "testOverflow": "mocha --timeout 9999999 -r ts-node/register ./**/overflow.test.ts"48  },49  "author": "",50  "license": "SEE LICENSE IN ../LICENSE",51  "homepage": "",52  "dependencies": {53    "@polkadot/api": "3.8.1",54    "@polkadot/api-contract": "3.8.1",55    "@polkadot/util": "5.6.2",56    "bignumber.js": "^9.0.0",57    "chai-as-promised": "^7.1.1"58  },59  "standard": {60    "globals": [61      "it",62      "assert",63      "beforeEach",64      "afterEach",65      "describe",66      "contract",67      "artifacts"68    ]69  }70}
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -396,8 +396,7 @@
 
     // What to expect
     expect(result.success).to.be.true;
-    expect(collection.Sponsor).to.be.equal(nullPublicKey);
-    expect(collection.SponsorConfirmed).to.be.false;
+    expect(collection.Sponsorship).to.be.deep.equal({ Disabled: null });
   });
 }