git.delta.rocks / unique-network / refs/commits / ff4b3341c8c4

difftreelog

Merge pull request #119 from usetech-llc/fix/merge-master-to-develop

Yaroslav Bolyukin2021-03-02parents: #05b679a #6efcbc5.patch.diff
in: master
Merge master to develop

2 files changed

modifiedpallets/nft/src/lib.rsdiffbeforeafterboth
--- a/pallets/nft/src/lib.rs
+++ b/pallets/nft/src/lib.rs
@@ -194,7 +194,7 @@
         CollectionLimits { 
             account_token_ownership_limit: 10_000_000, 
             token_limit: u32::max_value(),
-            sponsored_data_size: u32::max_value(), 
+            sponsored_data_size: ChainLimit::get().custom_data_limit, 
             sponsor_transfer_timeout: 14400,
             owner_can_transfer: true,
             owner_can_destroy: true
@@ -1110,14 +1110,23 @@
 
             // Transfer permissions check
             let target_collection = <Collection<T>>::get(collection_id);
-            ensure!(
-                Self::is_item_owner(sender.clone(), collection_id, item_id) ||
-                (
-                    target_collection.limits.owner_can_transfer &&
-                    Self::is_owner_or_admin_permissions(collection_id, sender.clone())
-                ),
-                Error::<T>::NoPermission
-            );
+            let allowance_limit = if (
+                target_collection.limits.owner_can_transfer &&
+                Self::is_owner_or_admin_permissions(
+                    collection_id,
+                    sender.clone(),
+                )
+            ) {
+                None
+            } else if let Some(amount) = Self::owned_amount(
+                sender.clone(),
+                collection_id,
+                item_id,
+            ) {
+                Some(amount)
+            } else {
+                fail!(Error::<T>::NoPermission);
+            };
 
             if target_collection.access == AccessMode::WhiteList {
                 Self::check_white_list(collection_id, &sender)?;
@@ -1129,6 +1138,9 @@
             if allowance_exists {
                 allowance += <Allowances<T>>::get(collection_id, (item_id, &sender, &spender));
             }
+            if let Some(limit) = allowance_limit {
+                ensure!(limit >= allowance, Error::<T>::TokenValueTooLow);
+            }
             <Allowances<T>>::insert(collection_id, (item_id, sender.clone(), spender.clone()), allowance);
 
             Ok(())
@@ -1912,6 +1924,36 @@
         Ok(())
     }
 
+    fn owned_amount(
+        subject: T::AccountId,
+        collection_id: CollectionId,
+        item_id: TokenId,
+    ) -> Option<u128> {
+        let target_collection = <Collection<T>>::get(collection_id);
+
+        match target_collection.mode {
+            CollectionMode::NFT => {
+                if <NftItemList<T>>::get(collection_id, item_id).owner == subject {
+                    return Some(1)
+                }
+                None
+            },
+            CollectionMode::Fungible(_) => {
+                if <FungibleItemList<T>>::contains_key(collection_id, &subject) {
+                    return Some(<FungibleItemList<T>>::get(collection_id, &subject)
+                        .value);
+                }
+                None
+            },
+            CollectionMode::ReFungible => <ReFungibleItemList<T>>::get(collection_id, item_id)
+                .owner
+                .iter()
+                .find(|i| i.owner == subject)
+                .map(|i| i.fraction),
+            CollectionMode::Invalid => None,
+        }
+    }
+
     fn is_item_owner(subject: T::AccountId, collection_id: CollectionId, item_id: TokenId) -> bool {
         let target_collection = <Collection<T>>::get(collection_id);
 
modifiedtests/src/approve.test.tsdiffbeforeafterboth
2// This file is subject to the terms and conditions defined in2// This file is subject to the terms and conditions defined in
3// file 'LICENSE', which is part of this source code package.3// file 'LICENSE', which is part of this source code package.
4//4//
5import { ApiPromise } from '@polkadot/api';
6import { IKeyringPair } from '@polkadot/types/types';5import { IKeyringPair } from '@polkadot/types/types';
7import BN from 'bn.js';6import { ApiPromise } from '@polkadot/api';
8import chai from 'chai';7import chai from 'chai';
9import chaiAsPromised from 'chai-as-promised';8import chaiAsPromised from 'chai-as-promised';
10import privateKey from './substrate/privateKey';9import privateKey from './substrate/privateKey';
13 approveExpectFail,12 approveExpectFail,
14 approveExpectSuccess,13 approveExpectSuccess,
15 createCollectionExpectSuccess,14 createCollectionExpectSuccess,
16 createFungibleItemExpectSuccess,
17 createItemExpectSuccess,15 createItemExpectSuccess,
18 destroyCollectionExpectSuccess,16 destroyCollectionExpectSuccess,
19 setCollectionLimitsExpectSuccess,17 setCollectionLimitsExpectSuccess,
20 transferFromExpectSuccess,18 transferExpectSuccess,
21 U128_MAX,
22} from './util/helpers';19} from './util/helpers';
2320
24chai.use(chaiAsPromised);21chai.use(chaiAsPromised);
25const expect = chai.expect;
2622
27describe('Integration Test approve(spender, collection_id, item_id, amount):', () => {23describe('Integration Test approve(spender, collection_id, item_id, amount):', () => {
28 let Alice: IKeyringPair;24 let Alice: IKeyringPair;
29 let Bob: IKeyringPair;25 let Bob: IKeyringPair;
30 let Charlie: IKeyringPair;26 let Charlie: IKeyringPair;
3127
32 before(async () => {28 before(async () => {
33 await usingApi(async (api) => {29 await usingApi(async () => {
34 Alice = privateKey('//Alice');30 Alice = privateKey('//Alice');
35 Bob = privateKey('//Bob');31 Bob = privateKey('//Bob');
36 Charlie = privateKey('//Charlie');32 Charlie = privateKey('//Charlie');
37 });33 });
38 });34 });
3935
40 it('Execute the extrinsic and check approvedList', async () => {36 it('Execute the extrinsic and check approvedList', async () => {
41 await usingApi(async (api: ApiPromise) => {
42 const nftCollectionId = await createCollectionExpectSuccess();37 const nftCollectionId = await createCollectionExpectSuccess();
43 // nft38 // nft
44 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');39 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
52 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});47 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});
53 const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');48 const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
54 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob);49 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob);
55 });
56 });50 });
5751
58 it('Remove approval by using 0 amount', async () => {52 it('Remove approval by using 0 amount', async () => {
59 await usingApi(async (api: ApiPromise) => {
60 const nftCollectionId = await createCollectionExpectSuccess();53 const nftCollectionId = await createCollectionExpectSuccess();
61 // nft54 // nft
62 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');55 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
73 const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');66 const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
74 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 1);67 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 1);
75 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 0);68 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 0);
76 });
77 });69 });
7870
79 it('can be called by collection owner on non-owned item when OwnerCanTransfer == true', async () => {71 it('can be called by collection owner on non-owned item when OwnerCanTransfer == true', async () => {
112 });104 });
113105
114 it('Approve for a collection that was destroyed', async () => {106 it('Approve for a collection that was destroyed', async () => {
115 await usingApi(async (api: ApiPromise) => {
116 // nft107 // nft
117 const nftCollectionId = await createCollectionExpectSuccess();108 const nftCollectionId = await createCollectionExpectSuccess();
118 await destroyCollectionExpectSuccess(nftCollectionId);109 await destroyCollectionExpectSuccess(nftCollectionId);
126 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});117 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});
127 await destroyCollectionExpectSuccess(reFungibleCollectionId);118 await destroyCollectionExpectSuccess(reFungibleCollectionId);
128 await approveExpectFail(reFungibleCollectionId, 1, Alice, Bob);119 await approveExpectFail(reFungibleCollectionId, 1, Alice, Bob);
129 });
130 });120 });
131121
132 it('Approve transfer of a token that does not exist', async () => {122 it('Approve transfer of a token that does not exist', async () => {
133 await usingApi(async (api: ApiPromise) => {
134 // nft123 // nft
135 const nftCollectionId = await createCollectionExpectSuccess();124 const nftCollectionId = await createCollectionExpectSuccess();
136 await approveExpectFail(nftCollectionId, 2, Alice, Bob);125 await approveExpectFail(nftCollectionId, 2, Alice, Bob);
141 const reFungibleCollectionId =130 const reFungibleCollectionId =
142 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});131 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});
143 await approveExpectFail(reFungibleCollectionId, 2, Alice, Bob);132 await approveExpectFail(reFungibleCollectionId, 2, Alice, Bob);
144 });
145 });133 });
146134
147 it('Approve using the address that does not own the approved token', async () => {135 it('Approve using the address that does not own the approved token', async () => {
148 await usingApi(async (api: ApiPromise) => {
149 const nftCollectionId = await createCollectionExpectSuccess();136 const nftCollectionId = await createCollectionExpectSuccess();
150 // nft137 // nft
151 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');138 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
159 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});146 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});
160 const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');147 const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
161 await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, Bob, Alice);148 await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, Bob, Alice);
162 });
163 });149 });
150
151 it('should fail if approved more NFTs than owned', async () => {
152 const nftCollectionId = await createCollectionExpectSuccess({ mode: { type: 'NFT' } });
153 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
154 await transferExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 1, 'NFT');
155 await approveExpectSuccess(nftCollectionId, newNftTokenId, Bob, Alice);
156 await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice);
157 });
158
159 it('should fail if approved more ReFungibles than owned', async () => {
160 const nftCollectionId = await createCollectionExpectSuccess({ mode: { type: 'ReFungible' } });
161 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'ReFungible');
162 await transferExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 100, 'ReFungible');
163 await approveExpectSuccess(nftCollectionId, newNftTokenId, Bob, Alice, 100);
164 await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice, 1);
165 });
166
167 it('should fail if approved more Fungibles than owned', async () => {
168 const nftCollectionId = await createCollectionExpectSuccess({ mode: { type: 'Fungible', decimalPoints: 0 } });
169 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'Fungible');
170 await transferExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 10, 'Fungible');
171 await approveExpectSuccess(nftCollectionId, newNftTokenId, Bob, Alice, 10);
172 await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice, 1);
173 });
164174
165 it('fails when called by collection owner on non-owned item when OwnerCanTransfer == false', async () => {175 it('fails when called by collection owner on non-owned item when OwnerCanTransfer == false', async () => {
166 const collectionId = await createCollectionExpectSuccess();176 const collectionId = await createCollectionExpectSuccess();