From d82d09e3c9529878a411088db11d5c19c45c6421 Mon Sep 17 00:00:00 2001 From: Grigoriy Simonov Date: Thu, 04 Aug 2022 12:42:32 +0000 Subject: [PATCH] fix(refungible-pallet): fixed wrong amount in ItemCreated event --- --- a/pallets/fungible/src/lib.rs +++ b/pallets/fungible/src/lib.rs @@ -393,14 +393,7 @@ }) .ok_or(ArithmeticError::Overflow)?; - let mut balances = data; - for (k, v) in balances.iter_mut() { - *v = >::get((collection.id, &k)) - .checked_add(*v) - .ok_or(ArithmeticError::Overflow)?; - } - - for (to, _) in balances.iter() { + for (to, _) in data.iter() { >::check_nesting( sender.clone(), to, @@ -413,8 +406,11 @@ // ========= >::insert(collection.id, total_supply); - for (user, amount) in balances { - >::insert((collection.id, &user), amount); + for (user, amount) in data { + let updated_balance = >::get((collection.id, &user)) + .checked_add(amount) + .ok_or(ArithmeticError::Overflow)?; + >::insert((collection.id, &user), updated_balance); >::nest_if_sent_to_token_unchecked( &user, collection.id, --- a/pallets/refungible/src/erc.rs +++ b/pallets/refungible/src/erc.rs @@ -560,10 +560,7 @@ >::create_item( self, &caller, - CreateItemData:: { - users, - properties, - }, + CreateItemData:: { users, properties }, &budget, ) .map_err(dispatch_to_evm::)?; --- a/tests/src/createItem.test.ts +++ b/tests/src/createItem.test.ts @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Unique Network. If not, see . -import {default as usingApi} from './substrate/substrate-api'; +import {default as usingApi, executeTransaction} from './substrate/substrate-api'; import chai from 'chai'; import {IKeyringPair} from '@polkadot/types/types'; import { @@ -26,6 +26,9 @@ createItemWithPropsExpectFailure, createCollection, transferExpectSuccess, + itApi, + normalizeAccountId, + getCreateItemResult, } from './util/helpers'; const expect = chai.expect; @@ -50,6 +53,32 @@ const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}}); await createItemExpectSuccess(alice, newCollectionID, createMode); }); + itApi('Check events on create new item in Fungible collection', async ({api}) => { + const createMode = 'Fungible'; + + const newCollectionID = (await createCollection(api, alice, {mode: {type: createMode, decimalPoints: 0}})).collectionId; + + const to = normalizeAccountId(alice); + { + const createData = {fungible: {value: 100}}; + const tx = api.tx.unique.createItem(newCollectionID, to, createData as any); + const events = await executeTransaction(api, alice, tx); + const result = getCreateItemResult(events); + expect(result.amount).to.be.equal(100); + expect(result.collectionId).to.be.equal(newCollectionID); + expect(result.recipient).to.be.deep.equal(to); + } + { + const createData = {fungible: {value: 50}}; + const tx = api.tx.unique.createItem(newCollectionID, to, createData as any); + const events = await executeTransaction(api, alice, tx); + const result = getCreateItemResult(events); + expect(result.amount).to.be.equal(50); + expect(result.collectionId).to.be.equal(newCollectionID); + expect(result.recipient).to.be.deep.equal(to); + } + + }); it('Create new item in ReFungible collection', async () => { const createMode = 'ReFungible'; const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode}}); --- a/tests/src/util/helpers.ts +++ b/tests/src/util/helpers.ts @@ -248,22 +248,23 @@ } export function getCreateItemResult(events: EventRecord[]): CreateItemResult { - const genericResult = getGenericResult<[number, number, CrossAccountId?]>(events, 'common', 'ItemCreated', (data) => [ - parseInt(data[0].toString(), 10), - parseInt(data[1].toString(), 10), - normalizeAccountId(data[2].toJSON() as any), - ]); - - if (genericResult.data == null) genericResult.data = [0, 0]; - - const result: CreateItemResult = { - success: genericResult.success, - collectionId: genericResult.data[0], - itemId: genericResult.data[1], - recipient: genericResult.data![2], - }; + const genericResult = getGenericResult(events, 'common', 'ItemCreated', (data) => data.map(function(value) { return value.toJSON(); })); - return result; + if (genericResult.data == null) + return { + success: genericResult.success, + collectionId: 0, + itemId: 0, + amount: 0, + }; + else + return { + success: genericResult.success, + collectionId: genericResult.data[0] as number, + itemId: genericResult.data[1] as number, + recipient: normalizeAccountId(genericResult.data![2] as any), + amount: genericResult.data[3] as number, + }; } export function getDestroyItemsResult(events: EventRecord[]): DestroyItemResult[] { @@ -1699,3 +1700,14 @@ return result.success; } + +export async function itApi(name: string, cb: (apis: { api: ApiPromise, privateKeyWrapper: (account: string) => IKeyringPair }) => any, opts: { only?: boolean, skip?: boolean } = {}) { + let i: any = it; + if (opts.only) i = i.only; + else if (opts.skip) i = i.skip; + i(name, async () => { + await usingApi(async (api, privateKeyWrapper) => { + await cb({api, privateKeyWrapper}); + }); + }); +} -- gitstuff