difftreelog
Style fix
in: master
5 files changed
pallets/unique/src/sponsorship.rsdiffbeforeafterboth--- a/pallets/unique/src/sponsorship.rs
+++ b/pallets/unique/src/sponsorship.rs
@@ -29,6 +29,7 @@
CollectionId, FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT, MetaUpdatePermission,
NFT_SPONSOR_TRANSFER_TIMEOUT, REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT, TokenId,
};
+use sp_runtime::traits::Saturating;
use pallet_common::{CollectionHandle};
use pallet_evm::account::CrossAccountId;
@@ -315,12 +316,7 @@
u64: From<<T as frame_system::Config>::BlockNumber>,
{
let collection = <CollectionHandle<T>>::try_get(collection_id).ok()?;
-
- // preliminary sponsoring correctness check
- match collection.sponsorship {
- SponsorshipState::Disabled | SponsorshipState::Unconfirmed(_) => return None,
- _ => (),
- }
+ let _ = collection.sponsorship.sponsor()?;
// sponsor timeout
let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;
@@ -343,11 +339,12 @@
};
if let Some(last_tx_block) = last_tx_block {
- let timeout = last_tx_block + limit.into();
- if block_number < timeout {
- return Some((timeout - block_number).into());
- }
- return Some(0);
+ return Some(
+ last_tx_block
+ .saturating_add(limit.into())
+ .saturating_sub(block_number)
+ .into(),
+ );
}
let token_exists = match collection.mode {
tests/src/interfaces/augment-api-rpc.tsdiffbeforeafterboth--- a/tests/src/interfaces/augment-api-rpc.ts
+++ b/tests/src/interfaces/augment-api-rpc.ts
@@ -626,7 +626,7 @@
/**
* nextSponsored transaction
**/
- nextSponsored: AugmentedRpc<(collection: u32 | AnyNumber | Uint8Array, account: PalletCommonAccountBasicCrossAccountIdRepr | { Substrate: any } | { Ethereum: any } | string | Uint8Array, tokenId: u32 | AnyNumber | Uint8Array, at?: Hash | string | Uint8Array) => Observable<Option<u64>>>;
+ nextSponsored: AugmentedRpc<(collection: u32 | AnyNumber | Uint8Array, account: AccountId | string | Uint8Array | { Substrate: any } | { Ethereum: any } | string | Uint8Array, tokenId: u32 | AnyNumber | Uint8Array, at?: Hash | string | Uint8Array) => Observable<Option<u64>>>;
};
web3: {
/**
tests/src/interfaces/unique/definitions.tsdiffbeforeafterboth--- a/tests/src/interfaces/unique/definitions.ts
+++ b/tests/src/interfaces/unique/definitions.ts
@@ -55,7 +55,7 @@
collectionById: fun('Get collection by specified id', [collectionParam], 'Option<UpDataStructsCollection>'),
collectionStats: fun('Get collection stats', [], 'UpDataStructsCollectionStats'),
allowed: fun('Check if user is allowed to use collection', [collectionParam, crossAccountParam()], 'bool'),
- nextSponsored: fun('Check if user is allowed to use collection', [collectionParam, crossAccountParam(), tokenParam], 'Option<u64>'),
+ nextSponsored: fun('Get number of blocks when sponsored transaction is available', [collectionParam, crossAccountParam(), tokenParam], 'Option<u64>'),
effectiveCollectionLimits: fun('Get effective collection limits', [collectionParam], 'Option<UpDataStructsCollectionLimits>'),
},
};
tests/src/nextSponsoring.test.tsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {ApiPromise} from '@polkadot/api';18import {IKeyringPair} from '@polkadot/types/types';19import chai from 'chai';20import chaiAsPromised from 'chai-as-promised';21import privateKey from './substrate/privateKey';22import {default as usingApi} from './substrate/substrate-api';23import {24 createCollectionExpectSuccess,25 setCollectionSponsorExpectSuccess,26 confirmSponsorshipExpectSuccess,27 createItemExpectSuccess,28 transferExpectSuccess,29 normalizeAccountId,30 getNextSponsored,31} from './util/helpers';3233chai.use(chaiAsPromised);34const expect = chai.expect;35363738describe('Integration Test getNextSponsored(collection_id, owner, item_id):', () => {39 let alice: IKeyringPair;40 let bob: IKeyringPair;4142 before(async () => {43 await usingApi(async () => {44 alice = privateKey('//Alice');45 bob = privateKey('//Bob');46 });47 });4849 it('NFT', async () => {50 await usingApi(async (api: ApiPromise) => {5152 // Not existing collection 53 expect(await getNextSponsored(api, 0, normalizeAccountId(alice), 0)).to.be.equal(-1);5455 const collectionId = await createCollectionExpectSuccess();56 const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', alice.address);5758 // Check with Disabled sponsoring state59 expect(await getNextSponsored(api, collectionId, normalizeAccountId(alice), itemId)).to.be.equal(-1);60 await setCollectionSponsorExpectSuccess(collectionId, bob.address);6162 // Check with Unconfirmed sponsoring state63 expect(await getNextSponsored(api, collectionId, normalizeAccountId(alice), itemId)).to.be.equal(-1);64 await confirmSponsorshipExpectSuccess(collectionId, '//Bob');6566 // Check with Confirmed sponsoring state67 expect(await getNextSponsored(api, collectionId, normalizeAccountId(alice), itemId)).to.be.equal(0);6869 // After transfer70 await transferExpectSuccess(collectionId, itemId, alice, bob, 1);71 expect(await getNextSponsored(api, collectionId, normalizeAccountId(alice), itemId)).to.be.equal(5);7273 // Not existing token 74 expect(await getNextSponsored(api, collectionId, normalizeAccountId(alice), itemId+1)).to.be.equal(-1);75 });76 });7778 it('Fungible', async () => {79 await usingApi(async (api: ApiPromise) => {8081 const createMode = 'Fungible';82 const funCollectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});83 await createItemExpectSuccess(alice, funCollectionId, createMode);84 await setCollectionSponsorExpectSuccess(funCollectionId, bob.address);85 await confirmSponsorshipExpectSuccess(funCollectionId, '//Bob');86 expect(await getNextSponsored(api, funCollectionId, normalizeAccountId(alice), 0)).to.be.equal(0);8788 await transferExpectSuccess(funCollectionId, 0, alice, bob, 10, 'Fungible');89 expect(await getNextSponsored(api, funCollectionId, normalizeAccountId(alice), 0)).to.be.equal(5);90 });91 });9293 it('ReFungible', async () => {94 await usingApi(async (api: ApiPromise) => {9596 const createMode = 'ReFungible';97 const refunCollectionId = await createCollectionExpectSuccess({mode: {type: createMode}});98 const refunItemId = await createItemExpectSuccess(alice, refunCollectionId, createMode);99 await setCollectionSponsorExpectSuccess(refunCollectionId, bob.address);100 await confirmSponsorshipExpectSuccess(refunCollectionId, '//Bob');101 expect(await getNextSponsored(api, refunCollectionId, normalizeAccountId(alice), refunItemId)).to.be.equal(0);102103 await transferExpectSuccess(refunCollectionId, refunItemId, alice, bob, 10, 'ReFungible');104 expect(await getNextSponsored(api, refunCollectionId, normalizeAccountId(alice), refunItemId)).to.be.equal(5);105106 // Not existing token 107 expect(await getNextSponsored(api, refunCollectionId, normalizeAccountId(alice), refunItemId+1)).to.be.equal(-1);108 });109 });110});tests/src/util/helpers.tsdiffbeforeafterboth--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -608,6 +608,15 @@
});
}
+export async function getNextSponsored(
+ api: ApiPromise,
+ collectionId: number,
+ account: string | CrossAccountId,
+ tokenId: number,
+): Promise<number> {
+ return Number((await api.rpc.unique.nextSponsored(collectionId, account, tokenId)).unwrapOr(-1));
+}
+
export async function toggleContractAllowlistExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, value = true) {
await usingApi(async (api) => {
const tx = api.tx.unique.toggleContractAllowList(contractAddress, value);