git.delta.rocks / unique-network / refs/commits / d82d09e3c952

difftreelog

fix(refungible-pallet) fixed wrong amount in ItemCreated event

Grigoriy Simonov2022-08-04parent: #3203150.patch.diff
in: master

4 files changed

modifiedpallets/fungible/src/lib.rsdiffbeforeafterboth
393 })393 })
394 .ok_or(ArithmeticError::Overflow)?;394 .ok_or(ArithmeticError::Overflow)?;
395
396 let mut balances = data;
397 for (k, v) in balances.iter_mut() {
398 *v = <Balance<T>>::get((collection.id, &k))
399 .checked_add(*v)
400 .ok_or(ArithmeticError::Overflow)?;
401 }
402395
403 for (to, _) in balances.iter() {396 for (to, _) in data.iter() {
404 <PalletStructure<T>>::check_nesting(397 <PalletStructure<T>>::check_nesting(
405 sender.clone(),398 sender.clone(),
406 to,399 to,
413 // =========406 // =========
414407
415 <TotalSupply<T>>::insert(collection.id, total_supply);408 <TotalSupply<T>>::insert(collection.id, total_supply);
416 for (user, amount) in balances {409 for (user, amount) in data {
410 let updated_balance = <Balance<T>>::get((collection.id, &user))
411 .checked_add(amount)
412 .ok_or(ArithmeticError::Overflow)?;
417 <Balance<T>>::insert((collection.id, &user), amount);413 <Balance<T>>::insert((collection.id, &user), updated_balance);
418 <PalletStructure<T>>::nest_if_sent_to_token_unchecked(414 <PalletStructure<T>>::nest_if_sent_to_token_unchecked(
419 &user,415 &user,
420 collection.id,416 collection.id,
modifiedpallets/refungible/src/erc.rsdiffbeforeafterboth
562 &caller,562 &caller,
563 CreateItemData::<T> {563 CreateItemData::<T> { users, properties },
564 users,
565 properties,
566 },
567 &budget,564 &budget,
568 )565 )
modifiedtests/src/createItem.test.tsdiffbeforeafterboth
14// You should have received a copy of the GNU General Public License14// You should have received a copy of the GNU General Public License
15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
1616
17import {default as usingApi} from './substrate/substrate-api';17import {default as usingApi, executeTransaction} from './substrate/substrate-api';
18import chai from 'chai';18import chai from 'chai';
19import {IKeyringPair} from '@polkadot/types/types';19import {IKeyringPair} from '@polkadot/types/types';
20import {20import {
26 createItemWithPropsExpectFailure,26 createItemWithPropsExpectFailure,
27 createCollection,27 createCollection,
28 transferExpectSuccess,28 transferExpectSuccess,
29 itApi,
30 normalizeAccountId,
31 getCreateItemResult,
29} from './util/helpers';32} from './util/helpers';
3033
31const expect = chai.expect;34const expect = chai.expect;
50 const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});53 const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});
51 await createItemExpectSuccess(alice, newCollectionID, createMode);54 await createItemExpectSuccess(alice, newCollectionID, createMode);
52 });55 });
56 itApi('Check events on create new item in Fungible collection', async ({api}) => {
57 const createMode = 'Fungible';
58
59 const newCollectionID = (await createCollection(api, alice, {mode: {type: createMode, decimalPoints: 0}})).collectionId;
60
61 const to = normalizeAccountId(alice);
62 {
63 const createData = {fungible: {value: 100}};
64 const tx = api.tx.unique.createItem(newCollectionID, to, createData as any);
65 const events = await executeTransaction(api, alice, tx);
66 const result = getCreateItemResult(events);
67 expect(result.amount).to.be.equal(100);
68 expect(result.collectionId).to.be.equal(newCollectionID);
69 expect(result.recipient).to.be.deep.equal(to);
70 }
71 {
72 const createData = {fungible: {value: 50}};
73 const tx = api.tx.unique.createItem(newCollectionID, to, createData as any);
74 const events = await executeTransaction(api, alice, tx);
75 const result = getCreateItemResult(events);
76 expect(result.amount).to.be.equal(50);
77 expect(result.collectionId).to.be.equal(newCollectionID);
78 expect(result.recipient).to.be.deep.equal(to);
79 }
80
81 });
53 it('Create new item in ReFungible collection', async () => {82 it('Create new item in ReFungible collection', async () => {
54 const createMode = 'ReFungible';83 const createMode = 'ReFungible';
55 const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode}});84 const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode}});
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
248}248}
249249
250export function getCreateItemResult(events: EventRecord[]): CreateItemResult {250export function getCreateItemResult(events: EventRecord[]): CreateItemResult {
251 const genericResult = getGenericResult<[number, number, CrossAccountId?]>(events, 'common', 'ItemCreated', (data) => [251 const genericResult = getGenericResult(events, 'common', 'ItemCreated', (data) => data.map(function(value) { return value.toJSON(); }));
252 parseInt(data[0].toString(), 10),252
253 parseInt(data[1].toString(), 10),
254 normalizeAccountId(data[2].toJSON() as any),
255 ]);
256
257 if (genericResult.data == null) genericResult.data = [0, 0];253 if (genericResult.data == null)
258254 return {
255 success: genericResult.success,
256 collectionId: 0,
257 itemId: 0,
258 amount: 0,
259 };
260 else
259 const result: CreateItemResult = {261 return {
260 success: genericResult.success,262 success: genericResult.success,
261 collectionId: genericResult.data[0],263 collectionId: genericResult.data[0] as number,
262 itemId: genericResult.data[1],264 itemId: genericResult.data[1] as number,
263 recipient: genericResult.data![2],265 recipient: normalizeAccountId(genericResult.data![2] as any),
266 amount: genericResult.data[3] as number,
264 };267 };
265
266 return result;
267}268}
268269
269export function getDestroyItemsResult(events: EventRecord[]): DestroyItemResult[] {270export function getDestroyItemsResult(events: EventRecord[]): DestroyItemResult[] {
1700 return result.success;1701 return result.success;
1701}1702}
1703
1704export async function itApi(name: string, cb: (apis: { api: ApiPromise, privateKeyWrapper: (account: string) => IKeyringPair }) => any, opts: { only?: boolean, skip?: boolean } = {}) {
1705 let i: any = it;
1706 if (opts.only) i = i.only;
1707 else if (opts.skip) i = i.skip;
1708 i(name, async () => {
1709 await usingApi(async (api, privateKeyWrapper) => {
1710 await cb({api, privateKeyWrapper});
1711 });
1712 });
1713}
17021714