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

difftreelog

Merge pull request #115 from usetech-llc/Antz0x0z-patch-1

Antz0x0z2021-02-28parents: #30e1ae5 #6a04420.patch.diff
in: master
Antz0x0z patch 1

3 files changed

modified.github/workflows/notify.ymldiffbeforeafterboth
--- a/.github/workflows/notify.yml
+++ b/.github/workflows/notify.yml
@@ -6,8 +6,8 @@
   build:    
     runs-on: ubuntu-latest    
     steps:        
-    - uses: avkviring/telegram-github-action@v0.0.8
+    - uses: avkviring/telegram-github-action@v0.0.13
       env:
         telegram_to: ${{ secrets.TELEGRAM_TO }}  
         telegram_token: ${{ secrets.TELEGRAM_TOKEN }}
-        event: ${{ toJson(github.event) }}
\ No newline at end of file
+        event: ${{ toJson(github.event) }}
modifiedpallets/nft/src/lib.rsdiffbeforeafterboth
10981098
1099 // Transfer permissions check1099 // Transfer permissions check
1100 let target_collection = <Collection<T>>::get(collection_id);1100 let target_collection = <Collection<T>>::get(collection_id);
1101 ensure!(Self::is_item_owner(sender.clone(), collection_id, item_id) ||1101 let allowance_limit = if Self::is_owner_or_admin_permissions(
1102 collection_id,
1103 sender.clone(),
1104 ) {
1105 None
1102 Self::is_owner_or_admin_permissions(collection_id, sender.clone()),1106 } else if let Some(amount) = Self::owned_amount(
1107 sender.clone(),
1108 collection_id,
1109 item_id,
1110 ) {
1111 Some(amount)
1112 } else {
1103 Error::<T>::NoPermission);1113 fail!(Error::<T>::NoPermission);
1114 };
11041115
1105 if target_collection.access == AccessMode::WhiteList {1116 if target_collection.access == AccessMode::WhiteList {
1106 Self::check_white_list(collection_id, &sender)?;1117 Self::check_white_list(collection_id, &sender)?;
1112 if allowance_exists {1123 if allowance_exists {
1113 allowance += <Allowances<T>>::get(collection_id, (item_id, &sender, &spender));1124 allowance += <Allowances<T>>::get(collection_id, (item_id, &sender, &spender));
1114 }1125 }
1126 if let Some(limit) = allowance_limit {
1127 ensure!(limit >= allowance, Error::<T>::TokenValueTooLow);
1128 }
1115 <Allowances<T>>::insert(collection_id, (item_id, sender.clone(), spender.clone()), allowance);1129 <Allowances<T>>::insert(collection_id, (item_id, sender.clone(), spender.clone()), allowance);
11161130
1117 Ok(())1131 Ok(())
1883 Ok(())1897 Ok(())
1884 }1898 }
1899
1900 fn owned_amount(
1901 subject: T::AccountId,
1902 collection_id: CollectionId,
1903 item_id: TokenId,
1904 ) -> Option<u128> {
1905 let target_collection = <Collection<T>>::get(collection_id);
1906
1907 match target_collection.mode {
1908 CollectionMode::NFT => {
1909 if <NftItemList<T>>::get(collection_id, item_id).owner == subject {
1910 return Some(1)
1911 }
1912 None
1913 },
1914 CollectionMode::Fungible(_) => {
1915 if <FungibleItemList<T>>::contains_key(collection_id, &subject) {
1916 return Some(<FungibleItemList<T>>::get(collection_id, &subject)
1917 .value);
1918 }
1919 None
1920 },
1921 CollectionMode::ReFungible => <ReFungibleItemList<T>>::get(collection_id, item_id)
1922 .owner
1923 .iter()
1924 .find(|i| i.owner == subject)
1925 .map(|i| i.fraction),
1926 CollectionMode::Invalid => None,
1927 }
1928 }
18851929
1886 fn is_item_owner(subject: T::AccountId, collection_id: CollectionId, item_id: TokenId) -> bool {1930 fn is_item_owner(subject: T::AccountId, collection_id: CollectionId, item_id: TokenId) -> bool {
1887 let target_collection = <Collection<T>>::get(collection_id);1931 let target_collection = <Collection<T>>::get(collection_id);
modifiedtests/src/approve.test.tsdiffbeforeafterboth
--- a/tests/src/approve.test.ts
+++ b/tests/src/approve.test.ts
@@ -2,8 +2,8 @@
 // This file is subject to the terms and conditions defined in
 // file 'LICENSE', which is part of this source code package.
 //
+import { IKeyringPair } from '@polkadot/types/types';
 import { ApiPromise } from '@polkadot/api';
-import BN from 'bn.js';
 import chai from 'chai';
 import chaiAsPromised from 'chai-as-promised';
 import privateKey from './substrate/privateKey';
@@ -12,66 +12,70 @@
   approveExpectFail,
   approveExpectSuccess,
   createCollectionExpectSuccess,
-  createFungibleItemExpectSuccess,
   createItemExpectSuccess,
   destroyCollectionExpectSuccess,
-  transferFromExpectSuccess,
-  U128_MAX,
+  transferExpectSuccess,
 } from './util/helpers';
 
 chai.use(chaiAsPromised);
-const expect = chai.expect;
 
-describe('Integration Test approve(spender, collection_id, item_id, amount):', () => {
-  it('Execute the extrinsic and check approvedList', async () => {
-    await usingApi(async (api: ApiPromise) => {
-      const Alice = privateKey('//Alice');
-      const Bob = privateKey('//Bob');
-      const nftCollectionId = await createCollectionExpectSuccess();
-      // nft
-      const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
-      await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob);
-      // fungible
-      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
-      const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');
-      await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob);
-      // reFungible
-      const reFungibleCollectionId =
-        await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});
-      const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
-      await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob);
+let Alice: IKeyringPair;
+let Bob: IKeyringPair;
+
+describe.only('Integration Test approve(spender, collection_id, item_id, amount):', () => {
+  before(async () => {
+    await usingApi(async () => {
+      Alice = privateKey('//Alice');
+      Bob = privateKey('//Bob');
     });
   });
 
+  it('Execute the extrinsic and check approvedList', async () => {
+    const nftCollectionId = await createCollectionExpectSuccess();
+    // nft
+    const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
+    await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob);
+    // fungible
+    const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+    const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');
+    await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob);
+    // reFungible
+    const reFungibleCollectionId =
+      await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});
+    const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
+    await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob);
+  });
+
   it('Remove approval by using 0 amount', async () => {
-    await usingApi(async (api: ApiPromise) => {
-      const Alice = privateKey('//Alice');
-      const Bob = privateKey('//Bob');
-      const nftCollectionId = await createCollectionExpectSuccess();
-      // nft
-      const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
-      await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 1);
-      await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 0);
-      // fungible
-      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
-      const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');
-      await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 1);
-      await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 0);
-      // reFungible
-      const reFungibleCollectionId =
-        await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});
-      const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
-      await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 1);
-      await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 0);
-    });
+    const nftCollectionId = await createCollectionExpectSuccess();
+    // nft
+    const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
+    await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 1);
+    await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 0);
+    // fungible
+    const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+    const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');
+    await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 1);
+    await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 0);
+    // reFungible
+    const reFungibleCollectionId =
+      await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});
+    const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
+    await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 1);
+    await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 0);
   });
 });
 
-describe('Negative Integration Test approve(spender, collection_id, item_id, amount):', () => {
+describe.only('Negative Integration Test approve(spender, collection_id, item_id, amount):', () => {
+  before(async () => {
+    await usingApi(async (api) => {
+      Alice = privateKey('//Alice');
+      Bob = privateKey('//Bob');
+    });
+  });
+
   it('Approve for a collection that does not exist', async () => {
     await usingApi(async (api: ApiPromise) => {
-      const Alice = privateKey('//Alice');
-      const Bob = privateKey('//Bob');
       // nft
       const nftCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;
       await approveExpectFail(nftCollectionCount + 1, 1, Alice, Bob);
@@ -85,59 +89,71 @@
   });
 
   it('Approve for a collection that was destroyed', async () => {
-    await usingApi(async (api: ApiPromise) => {
-      const Alice = privateKey('//Alice');
-      const Bob = privateKey('//Bob');
-      // nft
-      const nftCollectionId = await createCollectionExpectSuccess();
-      await destroyCollectionExpectSuccess(nftCollectionId);
-      await approveExpectFail(nftCollectionId, 1, Alice, Bob);
-      // fungible
-      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
-      await destroyCollectionExpectSuccess(fungibleCollectionId);
-      await approveExpectFail(fungibleCollectionId, 1, Alice, Bob);
-      // reFungible
-      const reFungibleCollectionId =
-        await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});
-      await destroyCollectionExpectSuccess(reFungibleCollectionId);
-      await approveExpectFail(reFungibleCollectionId, 1, Alice, Bob);
-    });
+    // nft
+    const nftCollectionId = await createCollectionExpectSuccess();
+    await destroyCollectionExpectSuccess(nftCollectionId);
+    await approveExpectFail(nftCollectionId, 1, Alice, Bob);
+    // fungible
+    const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+    await destroyCollectionExpectSuccess(fungibleCollectionId);
+    await approveExpectFail(fungibleCollectionId, 1, Alice, Bob);
+    // reFungible
+    const reFungibleCollectionId =
+      await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});
+    await destroyCollectionExpectSuccess(reFungibleCollectionId);
+    await approveExpectFail(reFungibleCollectionId, 1, Alice, Bob);
   });
 
   it('Approve transfer of a token that does not exist', async () => {
-    await usingApi(async (api: ApiPromise) => {
-      const Alice = privateKey('//Alice');
-      const Bob = privateKey('//Bob');
-      // nft
-      const nftCollectionId = await createCollectionExpectSuccess();
-      await approveExpectFail(nftCollectionId, 2, Alice, Bob);
-      // fungible
-      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
-      await approveExpectFail(fungibleCollectionId, 2, Alice, Bob);
-      // reFungible
-      const reFungibleCollectionId =
-        await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});
-      await approveExpectFail(reFungibleCollectionId, 2, Alice, Bob);
-    });
+    // nft
+    const nftCollectionId = await createCollectionExpectSuccess();
+    await approveExpectFail(nftCollectionId, 2, Alice, Bob);
+    // fungible
+    const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+    await approveExpectFail(fungibleCollectionId, 2, Alice, Bob);
+    // reFungible
+    const reFungibleCollectionId =
+      await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});
+    await approveExpectFail(reFungibleCollectionId, 2, Alice, Bob);
   });
 
   it('Approve using the address that does not own the approved token', async () => {
-    await usingApi(async (api: ApiPromise) => {
-      const Alice = privateKey('//Alice');
-      const Bob = privateKey('//Bob');
-      const nftCollectionId = await createCollectionExpectSuccess();
-      // nft
-      const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
-      await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice);
-      // fungible
-      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
-      const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');
-      await approveExpectFail(fungibleCollectionId, newFungibleTokenId, Bob, Alice);
-      // reFungible
-      const reFungibleCollectionId =
-        await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});
-      const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
-      await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, Bob, Alice);
-    });
+    const nftCollectionId = await createCollectionExpectSuccess();
+    // nft
+    const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
+    await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice);
+    // fungible
+    const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+    const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');
+    await approveExpectFail(fungibleCollectionId, newFungibleTokenId, Bob, Alice);
+    // reFungible
+    const reFungibleCollectionId =
+      await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});
+    const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
+    await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, Bob, Alice);
+  });
+
+  it('should fail if approved more NFTs than owned', async () => {
+    const nftCollectionId = await createCollectionExpectSuccess({ mode: { type: 'NFT' } });
+    const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
+    await transferExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 1, 'NFT');
+    await approveExpectSuccess(nftCollectionId, newNftTokenId, Bob, Alice);
+    await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice);
+  });
+
+  it('should fail if approved more ReFungibles than owned', async () => {
+    const nftCollectionId = await createCollectionExpectSuccess({ mode: { type: 'ReFungible' } });
+    const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'ReFungible');
+    await transferExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 100, 'ReFungible');
+    await approveExpectSuccess(nftCollectionId, newNftTokenId, Bob, Alice, 100);
+    await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice, 1);
+  });
+
+  it('should fail if approved more Fungibles than owned', async () => {
+    const nftCollectionId = await createCollectionExpectSuccess({ mode: { type: 'Fungible', decimalPoints: 0 } });
+    const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'Fungible');
+    await transferExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 10, 'Fungible');
+    await approveExpectSuccess(nftCollectionId, newNftTokenId, Bob, Alice, 10);
+    await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice, 1);
   });
 });