From 1ae3db3366fb6725272f010685bed808e6ba4a3b Mon Sep 17 00:00:00 2001 From: str-mv Date: Mon, 11 Apr 2022 09:33:16 +0000 Subject: [PATCH] Style fix --- --- 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<::BlockNumber>, { let collection = >::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 = >::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 { --- 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>>; + 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>>; }; web3: { /** --- 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'), 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'), + nextSponsored: fun('Get number of blocks when sponsored transaction is available', [collectionParam, crossAccountParam(), tokenParam], 'Option'), effectiveCollectionLimits: fun('Get effective collection limits', [collectionParam], 'Option'), }, }; --- /dev/null +++ b/tests/src/nextSponsoring.test.ts @@ -0,0 +1,110 @@ +// Copyright 2019-2022 Unique Network (Gibraltar) Ltd. +// This file is part of Unique Network. + +// Unique Network is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Unique Network is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Unique Network. If not, see . + +import {ApiPromise} from '@polkadot/api'; +import {IKeyringPair} from '@polkadot/types/types'; +import chai from 'chai'; +import chaiAsPromised from 'chai-as-promised'; +import privateKey from './substrate/privateKey'; +import {default as usingApi} from './substrate/substrate-api'; +import { + createCollectionExpectSuccess, + setCollectionSponsorExpectSuccess, + confirmSponsorshipExpectSuccess, + createItemExpectSuccess, + transferExpectSuccess, + normalizeAccountId, + getNextSponsored, +} from './util/helpers'; + +chai.use(chaiAsPromised); +const expect = chai.expect; + + + +describe('Integration Test getNextSponsored(collection_id, owner, item_id):', () => { + let alice: IKeyringPair; + let bob: IKeyringPair; + + before(async () => { + await usingApi(async () => { + alice = privateKey('//Alice'); + bob = privateKey('//Bob'); + }); + }); + + it('NFT', async () => { + await usingApi(async (api: ApiPromise) => { + + // Not existing collection + expect(await getNextSponsored(api, 0, normalizeAccountId(alice), 0)).to.be.equal(-1); + + const collectionId = await createCollectionExpectSuccess(); + const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', alice.address); + + // Check with Disabled sponsoring state + expect(await getNextSponsored(api, collectionId, normalizeAccountId(alice), itemId)).to.be.equal(-1); + await setCollectionSponsorExpectSuccess(collectionId, bob.address); + + // Check with Unconfirmed sponsoring state + expect(await getNextSponsored(api, collectionId, normalizeAccountId(alice), itemId)).to.be.equal(-1); + await confirmSponsorshipExpectSuccess(collectionId, '//Bob'); + + // Check with Confirmed sponsoring state + expect(await getNextSponsored(api, collectionId, normalizeAccountId(alice), itemId)).to.be.equal(0); + + // After transfer + await transferExpectSuccess(collectionId, itemId, alice, bob, 1); + expect(await getNextSponsored(api, collectionId, normalizeAccountId(alice), itemId)).to.be.equal(5); + + // Not existing token + expect(await getNextSponsored(api, collectionId, normalizeAccountId(alice), itemId+1)).to.be.equal(-1); + }); + }); + + it('Fungible', async () => { + await usingApi(async (api: ApiPromise) => { + + const createMode = 'Fungible'; + const funCollectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}}); + await createItemExpectSuccess(alice, funCollectionId, createMode); + await setCollectionSponsorExpectSuccess(funCollectionId, bob.address); + await confirmSponsorshipExpectSuccess(funCollectionId, '//Bob'); + expect(await getNextSponsored(api, funCollectionId, normalizeAccountId(alice), 0)).to.be.equal(0); + + await transferExpectSuccess(funCollectionId, 0, alice, bob, 10, 'Fungible'); + expect(await getNextSponsored(api, funCollectionId, normalizeAccountId(alice), 0)).to.be.equal(5); + }); + }); + + it('ReFungible', async () => { + await usingApi(async (api: ApiPromise) => { + + const createMode = 'ReFungible'; + const refunCollectionId = await createCollectionExpectSuccess({mode: {type: createMode}}); + const refunItemId = await createItemExpectSuccess(alice, refunCollectionId, createMode); + await setCollectionSponsorExpectSuccess(refunCollectionId, bob.address); + await confirmSponsorshipExpectSuccess(refunCollectionId, '//Bob'); + expect(await getNextSponsored(api, refunCollectionId, normalizeAccountId(alice), refunItemId)).to.be.equal(0); + + await transferExpectSuccess(refunCollectionId, refunItemId, alice, bob, 10, 'ReFungible'); + expect(await getNextSponsored(api, refunCollectionId, normalizeAccountId(alice), refunItemId)).to.be.equal(5); + + // Not existing token + expect(await getNextSponsored(api, refunCollectionId, normalizeAccountId(alice), refunItemId+1)).to.be.equal(-1); + }); + }); +}); --- 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 { + 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); -- gitstuff