difftreelog
Merge pull request #115 from usetech-llc/Antz0x0z-patch-1
in: master
Antz0x0z patch 1
3 files changed
.github/workflows/notify.ymldiffbeforeafterboth6 build: 6 build: 7 runs-on: ubuntu-latest 7 runs-on: ubuntu-latest 8 steps: 8 steps: 9 - uses: avkviring/telegram-github-action@v0.0.89 - uses: avkviring/telegram-github-action@v0.0.1310 env:10 env:11 telegram_to: ${{ secrets.TELEGRAM_TO }} 11 telegram_to: ${{ secrets.TELEGRAM_TO }} 12 telegram_token: ${{ secrets.TELEGRAM_TOKEN }}12 telegram_token: ${{ secrets.TELEGRAM_TOKEN }}pallets/nft/src/lib.rsdiffbeforeafterboth109810981099 // Transfer permissions check1099 // Transfer permissions check1100 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 None1102 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 };110411151105 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);111611301117 Ok(())1131 Ok(())1883 Ok(())1897 Ok(())1884 }1898 }18991900 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);19061907 match target_collection.mode {1908 CollectionMode::NFT => {1909 if <NftItemList<T>>::get(collection_id, item_id).owner == subject {1910 return Some(1)1911 }1912 None1913 },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 None1920 },1921 CollectionMode::ReFungible => <ReFungibleItemList<T>>::get(collection_id, item_id)1922 .owner1923 .iter()1924 .find(|i| i.owner == subject)1925 .map(|i| i.fraction),1926 CollectionMode::Invalid => None,1927 }1928 }188519291886 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);tests/src/approve.test.tsdiffbeforeafterboth2// This file is subject to the terms and conditions defined in2// This file is subject to the terms and conditions defined in3// 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';5import { IKeyringPair } from '@polkadot/types/types';6import BN from 'bn.js';6import { ApiPromise } from '@polkadot/api';7import chai from 'chai';7import chai from 'chai';8import chaiAsPromised from 'chai-as-promised';8import chaiAsPromised from 'chai-as-promised';9import privateKey from './substrate/privateKey';9import privateKey from './substrate/privateKey';12 approveExpectFail,12 approveExpectFail,13 approveExpectSuccess,13 approveExpectSuccess,14 createCollectionExpectSuccess,14 createCollectionExpectSuccess,15 createFungibleItemExpectSuccess,16 createItemExpectSuccess,15 createItemExpectSuccess,17 destroyCollectionExpectSuccess,16 destroyCollectionExpectSuccess,18 transferFromExpectSuccess,17 transferExpectSuccess,19 U128_MAX,20} from './util/helpers';18} from './util/helpers';211922chai.use(chaiAsPromised);20chai.use(chaiAsPromised);2123const expect = chai.expect;22let Alice: IKeyringPair;23let Bob: IKeyringPair;242425describe('Integration Test approve(spender, collection_id, item_id, amount):', () => {25describe.only('Integration Test approve(spender, collection_id, item_id, amount):', () => {26 before(async () => {27 await usingApi(async () => {28 Alice = privateKey('//Alice');29 Bob = privateKey('//Bob');30 });31 });3226 it('Execute the extrinsic and check approvedList', async () => {33 it('Execute the extrinsic and check approvedList', async () => {27 await usingApi(async (api: ApiPromise) => {28 const Alice = privateKey('//Alice');29 const Bob = privateKey('//Bob');30 const nftCollectionId = await createCollectionExpectSuccess();34 const nftCollectionId = await createCollectionExpectSuccess();31 // nft35 // nft32 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');36 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');40 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});44 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});41 const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');45 const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');42 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob);46 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob);43 });44 });47 });454846 it('Remove approval by using 0 amount', async () => {49 it('Remove approval by using 0 amount', async () => {47 await usingApi(async (api: ApiPromise) => {48 const Alice = privateKey('//Alice');49 const Bob = privateKey('//Bob');50 const nftCollectionId = await createCollectionExpectSuccess();50 const nftCollectionId = await createCollectionExpectSuccess();51 // nft51 // nft52 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');52 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');63 const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');63 const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');64 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 1);64 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 1);65 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 0);65 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 0);66 });67 });66 });68});67});696870describe('Negative Integration Test approve(spender, collection_id, item_id, amount):', () => {69describe.only('Negative Integration Test approve(spender, collection_id, item_id, amount):', () => {70 before(async () => {71 await usingApi(async (api) => {72 Alice = privateKey('//Alice');73 Bob = privateKey('//Bob');74 });75 });7671 it('Approve for a collection that does not exist', async () => {77 it('Approve for a collection that does not exist', async () => {72 await usingApi(async (api: ApiPromise) => {78 await usingApi(async (api: ApiPromise) => {73 const Alice = privateKey('//Alice');74 const Bob = privateKey('//Bob');75 // nft79 // nft76 const nftCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;80 const nftCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;77 await approveExpectFail(nftCollectionCount + 1, 1, Alice, Bob);81 await approveExpectFail(nftCollectionCount + 1, 1, Alice, Bob);85 });89 });869087 it('Approve for a collection that was destroyed', async () => {91 it('Approve for a collection that was destroyed', async () => {88 await usingApi(async (api: ApiPromise) => {89 const Alice = privateKey('//Alice');90 const Bob = privateKey('//Bob');91 // nft92 // nft92 const nftCollectionId = await createCollectionExpectSuccess();93 const nftCollectionId = await createCollectionExpectSuccess();93 await destroyCollectionExpectSuccess(nftCollectionId);94 await destroyCollectionExpectSuccess(nftCollectionId);101 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});102 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});102 await destroyCollectionExpectSuccess(reFungibleCollectionId);103 await destroyCollectionExpectSuccess(reFungibleCollectionId);103 await approveExpectFail(reFungibleCollectionId, 1, Alice, Bob);104 await approveExpectFail(reFungibleCollectionId, 1, Alice, Bob);104 });105 });105 });106106107 it('Approve transfer of a token that does not exist', async () => {107 it('Approve transfer of a token that does not exist', async () => {108 await usingApi(async (api: ApiPromise) => {109 const Alice = privateKey('//Alice');110 const Bob = privateKey('//Bob');111 // nft108 // nft112 const nftCollectionId = await createCollectionExpectSuccess();109 const nftCollectionId = await createCollectionExpectSuccess();113 await approveExpectFail(nftCollectionId, 2, Alice, Bob);110 await approveExpectFail(nftCollectionId, 2, Alice, Bob);118 const reFungibleCollectionId =115 const reFungibleCollectionId =119 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});116 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});120 await approveExpectFail(reFungibleCollectionId, 2, Alice, Bob);117 await approveExpectFail(reFungibleCollectionId, 2, Alice, Bob);121 });122 });118 });123119124 it('Approve using the address that does not own the approved token', async () => {120 it('Approve using the address that does not own the approved token', async () => {125 await usingApi(async (api: ApiPromise) => {126 const Alice = privateKey('//Alice');127 const Bob = privateKey('//Bob');128 const nftCollectionId = await createCollectionExpectSuccess();121 const nftCollectionId = await createCollectionExpectSuccess();129 // nft122 // nft130 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');123 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');138 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});131 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});139 const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');132 const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');140 await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, Bob, Alice);133 await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, Bob, Alice);141 });142 });134 });135136 it('should fail if approved more NFTs than owned', async () => {137 const nftCollectionId = await createCollectionExpectSuccess({ mode: { type: 'NFT' } });138 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');139 await transferExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 1, 'NFT');140 await approveExpectSuccess(nftCollectionId, newNftTokenId, Bob, Alice);141 await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice);142 });143144 it('should fail if approved more ReFungibles than owned', async () => {145 const nftCollectionId = await createCollectionExpectSuccess({ mode: { type: 'ReFungible' } });146 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'ReFungible');147 await transferExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 100, 'ReFungible');148 await approveExpectSuccess(nftCollectionId, newNftTokenId, Bob, Alice, 100);149 await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice, 1);150 });151152 it('should fail if approved more Fungibles than owned', async () => {153 const nftCollectionId = await createCollectionExpectSuccess({ mode: { type: 'Fungible', decimalPoints: 0 } });154 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'Fungible');155 await transferExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 10, 'Fungible');156 await approveExpectSuccess(nftCollectionId, newNftTokenId, Bob, Alice, 10);157 await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice, 1);158 });143});159});144160