git.delta.rocks / unique-network / refs/commits / 96967bb10fde

difftreelog

Integration tests for setCollectionSponsor

Greg Zaitsev2020-12-23parent: #8686594.patch.diff
in: master

6 files changed

modifiednode/src/chain_spec.rsdiffbeforeafterboth
193 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()
modifiedpallets/nft/src/lib.rsdiffbeforeafterboth
136 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 sender
139 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);
871871
872 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);
874875
875 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);
890891
891 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);
893894
894 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);
897897
898 Ok(())898 Ok(())
917 ensure!(sender == target_collection.owner, Error::<T>::NoPermission);917 ensure!(sender == target_collection.owner, Error::<T>::NoPermission);
918918
919 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);
921922
922 Ok(())923 Ok(())
2338 Some(Call::create_item(collection_id, _owner, _properties)) => {2339 Some(Call::create_item(collection_id, _owner, _properties)) => {
23392340
2340 // check free create limit2341 // check free create limit
2341 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).sponsor
2344 } 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 timeout
2354 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 limit
2425 false2429 false
2426 },2430 },
2427 };2431 };
2432 }
24282433
2429 if !sponsor_transfer {2434 if !sponsor_transfer {
2430 T::AccountId::default()2435 T::AccountId::default()
modifiedruntime_types.jsondiffbeforeafterboth
70 "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>"
modifiedtests/src/destroyCollection.test.tsdiffbeforeafterboth
1import 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';
87
9chai.use(chaiAsPromised);8chai.use(chaiAsPromised);
10const expect = chai.expect;9const expect = chai.expect;
11
12function 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}
22
23async function destroyCollectionExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {
24 await usingApi(async (api) => {
25 // Run the DestroyCollection transaction
26 const alicePrivateKey = privateKey(senderSeed);
27 const tx = api.tx.nft.destroyCollection(collectionId);
28 const events = await submitTransactionAsync(alicePrivateKey, tx);
29 const result = getDestroyResult(events);
30
31 // Get the collection
32 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();
33
34 // What to expect
35 expect(result).to.be.true;
36 expect(collection).to.be.not.null;
37 expect(collection.Owner).to.be.equal(nullPublicKey);
38 });
39}
40
41async function destroyCollectionExpectFailure(collectionId: number, senderSeed: string = '//Alice') {
42 await usingApi(async (api) => {
43 // Run the DestroyCollection transaction
44 const alicePrivateKey = privateKey(senderSeed);
45 const tx = api.tx.nft.destroyCollection(collectionId);
46 const events = await submitTransactionAsync(alicePrivateKey, tx);
47 const result = getDestroyResult(events);
48
49 // What to expect
50 expect(result).to.be.false;
51 });
52}
5310
54describe('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 () => {
addedtests/src/setCollectionSponsor.test.tsdiffbeforeafterboth

no changes

modifiedtests/src/util/helpers.tsdiffbeforeafterboth
9import { 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}
125
126function 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}
136
137export async function setCollectionSponsorExpectSuccess(collectionId: number, sponsor: string) {
138 await usingApi(async (api) => {
139
140 // Run the transaction
141 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);
145
146 // Get the collection
147 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();
148
149 // What to expect
150 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}
155
156export async function destroyCollectionExpectFailure(collectionId: number, senderSeed: string = '//Alice') {
157 await usingApi(async (api) => {
158 // Run the DestroyCollection transaction
159 const alicePrivateKey = privateKey(senderSeed);
160 const tx = api.tx.nft.destroyCollection(collectionId);
161 const events = await submitTransactionAsync(alicePrivateKey, tx);
162 const result = getDestroyResult(events);
163
164 // What to expect
165 expect(result).to.be.false;
166 });
167}
168
169export async function destroyCollectionExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {
170 await usingApi(async (api) => {
171 // Run the DestroyCollection transaction
172 const alicePrivateKey = privateKey(senderSeed);
173 const tx = api.tx.nft.destroyCollection(collectionId);
174 const events = await submitTransactionAsync(alicePrivateKey, tx);
175 const result = getDestroyResult(events);
176
177 // Get the collection
178 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();
179
180 // What to expect
181 expect(result).to.be.true;
182 expect(collection).to.be.not.null;
183 expect(collection.Owner).to.be.equal(nullPublicKey);
184 });
185}
186
187export async function setCollectionSponsorExpectFailure(collectionId: number, sponsor: string) {
188 await usingApi(async (api) => {
189
190 // Run the transaction
191 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);
195
196 // What to expect
197 expect(result.success).to.be.false;
198 });
199}
200