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
123 pub fraction: u128,123 pub fraction: u128,
124}124}
125
126#[derive(Encode, Decode, Debug, Clone, PartialEq)]
127#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
128pub enum SponsorshipState<AccountId> {
129 /// The fees are applied to the transaction sender
130 Disabled,
131 Unconfirmed(AccountId),
132 /// Transactions are sponsored by specified account
133 Confirmed(AccountId),
134}
135
136impl<AccountId> SponsorshipState<AccountId> {
137 fn sponsor(&self) -> Option<&AccountId> {
138 match self {
139 Self::Confirmed(sponsor) => Some(sponsor),
140 _ => None,
141 }
142 }
143
144 fn pending_sponsor(&self) -> Option<&AccountId> {
145 match self {
146 Self::Unconfirmed(sponsor) | Self::Confirmed(sponsor) => Some(sponsor),
147 _ => None,
148 }
149 }
150
151 fn confirmed(&self) -> bool {
152 matches!(self, Self::Confirmed(_))
153 }
154}
155
156impl<T> Default for SponsorshipState<T> {
157 fn default() -> Self {
158 Self::Disabled
159 }
160}
125161
126#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]162#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]
127#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]163#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
136 pub mint_mode: bool,172 pub mint_mode: bool,
137 pub offchain_schema: Vec<u8>,173 pub offchain_schema: Vec<u8>,
138 pub schema_version: SchemaVersion,174 pub schema_version: SchemaVersion,
139 pub sponsor: AccountId, // Who pays fees. If set to default address, the fees are applied to the transaction sender175 pub sponsorship: SponsorshipState<AccountId>,
140 pub sponsor_confirmed: bool, // False if sponsor address has not yet confirmed sponsorship. True otherwise.
141 pub limits: CollectionLimits, // Collection private restrictions 176 pub limits: CollectionLimits, // Collection private restrictions
142 pub variable_on_chain_schema: Vec<u8>, //177 pub variable_on_chain_schema: Vec<u8>, //
143 pub const_on_chain_schema: Vec<u8>, //178 pub const_on_chain_schema: Vec<u8>, //
665 token_prefix: token_prefix,700 token_prefix: token_prefix,
666 offchain_schema: Vec::new(),701 offchain_schema: Vec::new(),
667 schema_version: SchemaVersion::ImageURL,702 schema_version: SchemaVersion::ImageURL,
668 sponsor: T::AccountId::default(),703 sponsorship: SponsorshipState::Disabled,
669 sponsor_confirmed: false,
670 variable_on_chain_schema: Vec::new(),704 variable_on_chain_schema: Vec::new(),
671 const_on_chain_schema: Vec::new(),705 const_on_chain_schema: Vec::new(),
672 limits,706 limits,
922 let mut target_collection = <Collection<T>>::get(collection_id);956 let mut target_collection = <Collection<T>>::get(collection_id);
923 ensure!(sender == target_collection.owner, Error::<T>::NoPermission);957 ensure!(sender == target_collection.owner, Error::<T>::NoPermission);
924958
925 target_collection.sponsor = new_sponsor;959 target_collection.sponsorship = SponsorshipState::Unconfirmed(new_sponsor);
926 target_collection.sponsor_confirmed = false;
927 <Collection<T>>::insert(collection_id, target_collection);960 <Collection<T>>::insert(collection_id, target_collection);
928961
929 Ok(())962 Ok(())
943 ensure!(<Collection<T>>::contains_key(collection_id), Error::<T>::CollectionNotFound);976 ensure!(<Collection<T>>::contains_key(collection_id), Error::<T>::CollectionNotFound);
944977
945 let mut target_collection = <Collection<T>>::get(collection_id);978 let mut target_collection = <Collection<T>>::get(collection_id);
946 ensure!(sender == target_collection.sponsor, Error::<T>::ConfirmUnsetSponsorFail);979 ensure!(
980 target_collection.sponsorship.pending_sponsor() == Some(&sender),
981 Error::<T>::ConfirmUnsetSponsorFail
982 );
947983
948 target_collection.sponsor_confirmed = true;984 target_collection.sponsorship = SponsorshipState::Confirmed(sender);
949 <Collection<T>>::insert(collection_id, target_collection);985 <Collection<T>>::insert(collection_id, target_collection);
950986
951 Ok(())987 Ok(())
969 let mut target_collection = <Collection<T>>::get(collection_id);1005 let mut target_collection = <Collection<T>>::get(collection_id);
970 ensure!(sender == target_collection.owner, Error::<T>::NoPermission);1006 ensure!(sender == target_collection.owner, Error::<T>::NoPermission);
9711007
972 target_collection.sponsor = T::AccountId::default();1008 target_collection.sponsorship = SponsorshipState::Disabled;
973 target_collection.sponsor_confirmed = false;
974 <Collection<T>>::insert(collection_id, target_collection);1009 <Collection<T>>::insert(collection_id, target_collection);
9751010
976 Ok(())1011 Ok(())
2485 // sponsor timeout2520 // sponsor timeout
2486 let block_number = <system::Module<T>>::block_number() as T::BlockNumber;2521 let block_number = <system::Module<T>>::block_number() as T::BlockNumber;
24872522
2488 let limit = <Collection<T>>::get(collection_id).limits.sponsor_transfer_timeout;2523 let collection = <Collection<T>>::get(collection_id);
2524
2525 let limit = collection.limits.sponsor_transfer_timeout;
2489 let mut sponsored = true;2526 let mut sponsored = true;
2490 if <CreateItemBasket<T>>::contains_key((collection_id, &who)) {2527 if <CreateItemBasket<T>>::contains_key((collection_id, &who)) {
2491 let last_tx_block = <CreateItemBasket<T>>::get((collection_id, &who));2528 let last_tx_block = <CreateItemBasket<T>>::get((collection_id, &who));
2499 }2536 }
25002537
2501 // check free create limit2538 // check free create limit
2502 if (<Collection<T>>::get(collection_id).limits.sponsored_data_size >= (_properties.len() as u32)) &&2539 if (collection.limits.sponsored_data_size >= (_properties.len() as u32)) &&
2503 (<Collection<T>>::get(collection_id).sponsor_confirmed) &&
2504 (sponsored)2540 (sponsored)
2505 {2541 {
2542 collection.sponsorship.sponsor()
2506 <Collection<T>>::get(collection_id).sponsor2543 .cloned()
2544 .unwrap_or_default()
2507 } else {2545 } else {
2508 T::AccountId::default()2546 T::AccountId::default()
2509 }2547 }
2510 }2548 }
2511 Some(Call::transfer(_new_owner, collection_id, item_id, _value)) => {2549 Some(Call::transfer(_new_owner, collection_id, item_id, _value)) => {
2512 2550
2513 let mut sponsor_transfer = false;2551 let mut sponsor_transfer = false;
2514 if <Collection<T>>::get(collection_id).sponsor_confirmed {2552 if <Collection<T>>::get(collection_id).sponsorship.confirmed() {
25152553
2516 let collection_limits = <Collection<T>>::get(collection_id).limits;2554 let collection_limits = <Collection<T>>::get(collection_id).limits;
2517 let collection_mode = <Collection<T>>::get(collection_id).mode;2555 let collection_mode = <Collection<T>>::get(collection_id).mode;
2598 if !sponsor_transfer {2636 if !sponsor_transfer {
2599 T::AccountId::default()2637 T::AccountId::default()
2600 } else {2638 } else {
2601 <Collection<T>>::get(collection_id).sponsor2639 <Collection<T>>::get(collection_id).sponsorship.sponsor()
2640 .cloned()
2641 .unwrap_or_default()
2602 }2642 }
2603 }2643 }
26042644
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
--- a/tests/package.json
+++ b/tests/package.json
@@ -26,6 +26,7 @@
     "testSetVariableMetaData": "mocha --timeout 9999999 -r ts-node/register ./**/setVariableMetaData.test.ts",
     "testSetCollectionLimits": "mocha --timeout 9999999 -r ts-node/register ./**/setCollectionLimits.test.ts",
     "testRemoveCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/removeCollectionAdmin.test.ts",
+    "testRemoveCollectionSponsor": "mocha --timeout 9999999 -r ts-node/register ./**/removeCollectionSponsor.test.ts",
     "testRemoveFromWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/removeFromWhiteList.test.ts",
     "testConnection": "mocha --timeout 9999999 -r ts-node/register ./**/connection.test.ts",
     "testCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",
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 });
   });
 }