difftreelog
Merge pull request #115 from usetech-llc/Antz0x0z-patch-1
in: master
Antz0x0z patch 1
3 files changed
.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) }}
pallets/nft/src/lib.rsdiffbeforeafterboth--- a/pallets/nft/src/lib.rs
+++ b/pallets/nft/src/lib.rs
@@ -1098,9 +1098,20 @@
// Transfer permissions check
let target_collection = <Collection<T>>::get(collection_id);
- ensure!(Self::is_item_owner(sender.clone(), collection_id, item_id) ||
- Self::is_owner_or_admin_permissions(collection_id, sender.clone()),
- Error::<T>::NoPermission);
+ let allowance_limit = if 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)?;
@@ -1112,6 +1123,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(())
@@ -1883,6 +1897,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);
tests/src/approve.test.tsdiffbeforeafterboth1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//5import { ApiPromise } from '@polkadot/api';6import BN from 'bn.js';7import chai from 'chai';8import chaiAsPromised from 'chai-as-promised';9import privateKey from './substrate/privateKey';10import { default as usingApi } from './substrate/substrate-api';11import {12 approveExpectFail,13 approveExpectSuccess,14 createCollectionExpectSuccess,15 createFungibleItemExpectSuccess,16 createItemExpectSuccess,17 destroyCollectionExpectSuccess,18 transferFromExpectSuccess,19 U128_MAX,20} from './util/helpers';2122chai.use(chaiAsPromised);23const expect = chai.expect;2425describe('Integration Test approve(spender, collection_id, item_id, amount):', () => {26 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();31 // nft32 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');33 await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob);34 // fungible35 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});36 const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');37 await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob);38 // reFungible39 const reFungibleCollectionId =40 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});41 const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');42 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob);43 });44 });4546 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();51 // nft52 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');53 await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 1);54 await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 0);55 // fungible56 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});57 const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');58 await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 1);59 await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 0);60 // reFungible61 const reFungibleCollectionId =62 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});63 const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');64 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 1);65 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 0);66 });67 });68});6970describe('Negative Integration Test approve(spender, collection_id, item_id, amount):', () => {71 it('Approve for a collection that does not exist', async () => {72 await usingApi(async (api: ApiPromise) => {73 const Alice = privateKey('//Alice');74 const Bob = privateKey('//Bob');75 // nft76 const nftCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;77 await approveExpectFail(nftCollectionCount + 1, 1, Alice, Bob);78 // fungible79 const fungibleCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;80 await approveExpectFail(fungibleCollectionCount + 1, 1, Alice, Bob);81 // reFungible82 const reFungibleCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;83 await approveExpectFail(reFungibleCollectionCount + 1, 1, Alice, Bob);84 });85 });8687 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 const nftCollectionId = await createCollectionExpectSuccess();93 await destroyCollectionExpectSuccess(nftCollectionId);94 await approveExpectFail(nftCollectionId, 1, Alice, Bob);95 // fungible96 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});97 await destroyCollectionExpectSuccess(fungibleCollectionId);98 await approveExpectFail(fungibleCollectionId, 1, Alice, Bob);99 // reFungible100 const reFungibleCollectionId =101 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});102 await destroyCollectionExpectSuccess(reFungibleCollectionId);103 await approveExpectFail(reFungibleCollectionId, 1, Alice, Bob);104 });105 });106107 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 // nft112 const nftCollectionId = await createCollectionExpectSuccess();113 await approveExpectFail(nftCollectionId, 2, Alice, Bob);114 // fungible115 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});116 await approveExpectFail(fungibleCollectionId, 2, Alice, Bob);117 // reFungible118 const reFungibleCollectionId =119 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});120 await approveExpectFail(reFungibleCollectionId, 2, Alice, Bob);121 });122 });123124 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();129 // nft130 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');131 await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice);132 // fungible133 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});134 const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');135 await approveExpectFail(fungibleCollectionId, newFungibleTokenId, Bob, Alice);136 // reFungible137 const reFungibleCollectionId =138 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});139 const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');140 await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, Bob, Alice);141 });142 });143});