difftreelog
Integration tests for setCollectionSponsor
in: master
6 files changed
node/src/chain_spec.rsdiffbeforeafterboth193 offchain_schema: vec![],193 offchain_schema: vec![],194 schema_version: SchemaVersion::default(),194 schema_version: SchemaVersion::default(),195 sponsor: get_account_id_from_seed::<sr25519::Public>("Alice"),195 sponsor: get_account_id_from_seed::<sr25519::Public>("Alice"),196 unconfirmed_sponsor: get_account_id_from_seed::<sr25519::Public>("Alice"),196 sponsor_confirmed: true,197 const_on_chain_schema: vec![],197 const_on_chain_schema: vec![],198 variable_on_chain_schema: vec![],198 variable_on_chain_schema: vec![],199 limits: CollectionLimits::default()199 limits: CollectionLimits::default()pallets/nft/src/lib.rsdiffbeforeafterboth136 pub offchain_schema: Vec<u8>,136 pub offchain_schema: Vec<u8>,137 pub schema_version: SchemaVersion,137 pub schema_version: SchemaVersion,138 pub sponsor: AccountId, // Who pays fees. If set to default address, the fees are applied to the transaction sender138 pub sponsor: AccountId, // Who pays fees. If set to default address, the fees are applied to the transaction sender139 pub unconfirmed_sponsor: AccountId, // Sponsor address that has not yet confirmed sponsorship139 pub sponsor_confirmed: bool, // False if sponsor address has not yet confirmed sponsorship. True otherwise.140 pub limits: CollectionLimits, // Collection private restrictions 140 pub limits: CollectionLimits, // Collection private restrictions 141 pub variable_on_chain_schema: Vec<u8>, //141 pub variable_on_chain_schema: Vec<u8>, //142 pub const_on_chain_schema: Vec<u8>, //142 pub const_on_chain_schema: Vec<u8>, //591 offchain_schema: Vec::new(),591 offchain_schema: Vec::new(),592 schema_version: SchemaVersion::ImageURL,592 schema_version: SchemaVersion::ImageURL,593 sponsor: T::AccountId::default(),593 sponsor: T::AccountId::default(),594 unconfirmed_sponsor: T::AccountId::default(),594 sponsor_confirmed: false,595 variable_on_chain_schema: Vec::new(),595 variable_on_chain_schema: Vec::new(),596 const_on_chain_schema: Vec::new(),596 const_on_chain_schema: Vec::new(),597 limits: CollectionLimits::default(),597 limits: CollectionLimits::default(),869 let mut target_collection = <Collection<T>>::get(collection_id);869 let mut target_collection = <Collection<T>>::get(collection_id);870 ensure!(sender == target_collection.owner, Error::<T>::NoPermission);870 ensure!(sender == target_collection.owner, Error::<T>::NoPermission);871871872 target_collection.unconfirmed_sponsor = new_sponsor;872 target_collection.sponsor = new_sponsor;873 target_collection.sponsor_confirmed = false;873 <Collection<T>>::insert(collection_id, target_collection);874 <Collection<T>>::insert(collection_id, target_collection);874875875 Ok(())876 Ok(())889 ensure!(<Collection<T>>::contains_key(collection_id), Error::<T>::CollectionNotFound);890 ensure!(<Collection<T>>::contains_key(collection_id), Error::<T>::CollectionNotFound);890891891 let mut target_collection = <Collection<T>>::get(collection_id);892 let mut target_collection = <Collection<T>>::get(collection_id);892 ensure!(sender == target_collection.unconfirmed_sponsor, Error::<T>::ConfirmUnsetSponsorFail);893 ensure!(sender == target_collection.sponsor, Error::<T>::ConfirmUnsetSponsorFail);893894894 target_collection.sponsor = target_collection.unconfirmed_sponsor;895 target_collection.sponsor_confirmed = true;895 target_collection.unconfirmed_sponsor = T::AccountId::default();896 <Collection<T>>::insert(collection_id, target_collection);896 <Collection<T>>::insert(collection_id, target_collection);897897898 Ok(())898 Ok(())917 ensure!(sender == target_collection.owner, Error::<T>::NoPermission);917 ensure!(sender == target_collection.owner, Error::<T>::NoPermission);918918919 target_collection.sponsor = T::AccountId::default();919 target_collection.sponsor = T::AccountId::default();920 target_collection.sponsor_confirmed = false;920 <Collection<T>>::insert(collection_id, target_collection);921 <Collection<T>>::insert(collection_id, target_collection);921922922 Ok(())923 Ok(())2338 Some(Call::create_item(collection_id, _owner, _properties)) => {2339 Some(Call::create_item(collection_id, _owner, _properties)) => {233923402340 // check free create limit2341 // check free create limit2341 if <Collection<T>>::get(collection_id).limits.sponsored_data_size >= (_properties.len() as u32)2342 if (<Collection<T>>::get(collection_id).limits.sponsored_data_size >= (_properties.len() as u32)) &&2343 (<Collection<T>>::get(collection_id).sponsor_confirmed)2342 {2344 {2343 <Collection<T>>::get(collection_id).sponsor2345 <Collection<T>>::get(collection_id).sponsor2344 } else {2346 } else {2347 }2349 }2348 Some(Call::transfer(_new_owner, collection_id, _item_id, _value)) => {2350 Some(Call::transfer(_new_owner, collection_id, _item_id, _value)) => {2349 2351 2352 let mut sponsor_transfer = false;2353 if <Collection<T>>::get(collection_id).sponsor_confirmed {2350 let _collection_limits = <Collection<T>>::get(collection_id).limits;2354 let _collection_limits = <Collection<T>>::get(collection_id).limits;2351 let _collection_mode = <Collection<T>>::get(collection_id).mode;2355 let _collection_mode = <Collection<T>>::get(collection_id).mode;23522356 2353 // sponsor timeout2357 // sponsor timeout2354 let sponsor_transfer = match _collection_mode {2358 sponsor_transfer = match _collection_mode {2355 CollectionMode::NFT => {2359 CollectionMode::NFT => {23562360 2357 // get correct limit2361 // get correct limit2425 false2429 false2426 },2430 },2427 };2431 };2432 }242824332429 if !sponsor_transfer {2434 if !sponsor_transfer {2430 T::AccountId::default()2435 T::AccountId::default()runtime_types.jsondiffbeforeafterboth70 "OffchainSchema": "Vec<u8>",70 "OffchainSchema": "Vec<u8>",71 "SchemaVersion": "SchemaVersion",71 "SchemaVersion": "SchemaVersion",72 "Sponsor": "AccountId",72 "Sponsor": "AccountId",73 "UnconfirmedSponsor": "AccountId",73 "SponsorConfirmed": "bool",74 "Limits": "CollectionLimits",74 "Limits": "CollectionLimits",75 "VariableOnChainSchema": "Vec<u8>",75 "VariableOnChainSchema": "Vec<u8>",76 "ConstOnChainSchema": "Vec<u8>"76 "ConstOnChainSchema": "Vec<u8>"tests/src/destroyCollection.test.tsdiffbeforeafterboth1import chai from 'chai';1import chai from 'chai';2import chaiAsPromised from 'chai-as-promised';2import chaiAsPromised from 'chai-as-promised';3import { default as usingApi, submitTransactionAsync } from "./substrate/substrate-api";3import { default as usingApi, submitTransactionAsync } from "./substrate/substrate-api";4import { createCollectionExpectSuccess, createCollectionExpectFailure } from "./util/helpers";4import { createCollectionExpectSuccess, createCollectionExpectFailure, destroyCollectionExpectSuccess, destroyCollectionExpectFailure } from "./util/helpers";5import type { AccountId, EventRecord } from '@polkadot/types/interfaces';5import type { AccountId, EventRecord } from '@polkadot/types/interfaces';6import privateKey from './substrate/privateKey';6import privateKey from './substrate/privateKey';7import { nullPublicKey } from './accounts'; 879chai.use(chaiAsPromised);8chai.use(chaiAsPromised);10const expect = chai.expect;9const expect = chai.expect;1112function getDestroyResult(events: EventRecord[]): boolean {13 let success: boolean = false;14 events.forEach(({ phase, event: { data, method, section } }) => {15 // console.log(` ${phase}: ${section}.${method}:: ${data}`);16 if (method == 'ExtrinsicSuccess') {17 success = true;18 }19 });20 return success;21}2223async function destroyCollectionExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {24 await usingApi(async (api) => {25 // Run the DestroyCollection transaction26 const alicePrivateKey = privateKey(senderSeed);27 const tx = api.tx.nft.destroyCollection(collectionId);28 const events = await submitTransactionAsync(alicePrivateKey, tx);29 const result = getDestroyResult(events);3031 // Get the collection 32 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();3334 // What to expect35 expect(result).to.be.true;36 expect(collection).to.be.not.null;37 expect(collection.Owner).to.be.equal(nullPublicKey);38 });39}4041async function destroyCollectionExpectFailure(collectionId: number, senderSeed: string = '//Alice') {42 await usingApi(async (api) => {43 // Run the DestroyCollection transaction44 const alicePrivateKey = privateKey(senderSeed);45 const tx = api.tx.nft.destroyCollection(collectionId);46 const events = await submitTransactionAsync(alicePrivateKey, tx);47 const result = getDestroyResult(events);4849 // What to expect50 expect(result).to.be.false;51 });52}531054describe('integration test: ext. destroyCollection():', () => {11describe('integration test: ext. destroyCollection():', () => {55 it('NFT collection can be destroyed', async () => {12 it('NFT collection can be destroyed', async () => {tests/src/setCollectionSponsor.test.tsdiffbeforeafterbothno changes
tests/src/util/helpers.tsdiffbeforeafterboth9import { ApiPromise, Keyring } from "@polkadot/api";9import { ApiPromise, Keyring } from "@polkadot/api";10import { default as usingApi, submitTransactionAsync } from "../substrate/substrate-api";10import { default as usingApi, submitTransactionAsync } from "../substrate/substrate-api";11import privateKey from '../substrate/privateKey';11import privateKey from '../substrate/privateKey';12import { alicesPublicKey } from "../accounts";12import { alicesPublicKey, nullPublicKey } from "../accounts";13import { strToUTF16, utf16ToStr, hexToStr } from '../util/util';13import { strToUTF16, utf16ToStr, hexToStr } from '../util/util';14import { IKeyringPair } from "@polkadot/types/types";14import { IKeyringPair } from "@polkadot/types/types";15import { BigNumber } from 'bignumber.js';15import { BigNumber } from 'bignumber.js';123 return unused; 123 return unused; 124}124}125126function getDestroyResult(events: EventRecord[]): boolean {127 let success: boolean = false;128 events.forEach(({ phase, event: { data, method, section } }) => {129 // console.log(` ${phase}: ${section}.${method}:: ${data}`);130 if (method == 'ExtrinsicSuccess') {131 success = true;132 }133 });134 return success;135}136137export async function setCollectionSponsorExpectSuccess(collectionId: number, sponsor: string) {138 await usingApi(async (api) => {139140 // Run the transaction141 const alicePrivateKey = privateKey('//Alice');142 const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);143 const events = await submitTransactionAsync(alicePrivateKey, tx);144 const result = getGenericResult(events);145146 // Get the collection 147 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();148149 // What to expect150 expect(result.success).to.be.true;151 expect(collection.Sponsor.toString()).to.be.equal(sponsor.toString());152 expect(collection.SponsorConfirmed).to.be.false;153 });154}155156export async function destroyCollectionExpectFailure(collectionId: number, senderSeed: string = '//Alice') {157 await usingApi(async (api) => {158 // Run the DestroyCollection transaction159 const alicePrivateKey = privateKey(senderSeed);160 const tx = api.tx.nft.destroyCollection(collectionId);161 const events = await submitTransactionAsync(alicePrivateKey, tx);162 const result = getDestroyResult(events);163164 // What to expect165 expect(result).to.be.false;166 });167}168169export async function destroyCollectionExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {170 await usingApi(async (api) => {171 // Run the DestroyCollection transaction172 const alicePrivateKey = privateKey(senderSeed);173 const tx = api.tx.nft.destroyCollection(collectionId);174 const events = await submitTransactionAsync(alicePrivateKey, tx);175 const result = getDestroyResult(events);176177 // Get the collection 178 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();179180 // What to expect181 expect(result).to.be.true;182 expect(collection).to.be.not.null;183 expect(collection.Owner).to.be.equal(nullPublicKey);184 });185}186187export async function setCollectionSponsorExpectFailure(collectionId: number, sponsor: string) {188 await usingApi(async (api) => {189190 // Run the transaction191 const alicePrivateKey = privateKey('//Alice');192 const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);193 const events = await submitTransactionAsync(alicePrivateKey, tx);194 const result = getGenericResult(events);195196 // What to expect197 expect(result.success).to.be.false;198 });199}200