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
--- 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 = <Balance<T>>::get((collection.id, &k))
-				.checked_add(*v)
-				.ok_or(ArithmeticError::Overflow)?;
-		}
-
-		for (to, _) in balances.iter() {
+		for (to, _) in data.iter() {
 			<PalletStructure<T>>::check_nesting(
 				sender.clone(),
 				to,
@@ -413,8 +406,11 @@
 		// =========
 
 		<TotalSupply<T>>::insert(collection.id, total_supply);
-		for (user, amount) in balances {
-			<Balance<T>>::insert((collection.id, &user), amount);
+		for (user, amount) in data {
+			let updated_balance = <Balance<T>>::get((collection.id, &user))
+				.checked_add(amount)
+				.ok_or(ArithmeticError::Overflow)?;
+			<Balance<T>>::insert((collection.id, &user), updated_balance);
 			<PalletStructure<T>>::nest_if_sent_to_token_unchecked(
 				&user,
 				collection.id,
modifiedpallets/refungible/src/erc.rsdiffbeforeafterboth
--- a/pallets/refungible/src/erc.rs
+++ b/pallets/refungible/src/erc.rs
@@ -560,10 +560,7 @@
 		<Pallet<T>>::create_item(
 			self,
 			&caller,
-			CreateItemData::<T> {
-				users,
-				properties,
-			},
+			CreateItemData::<T> { users, properties },
 			&budget,
 		)
 		.map_err(dispatch_to_evm::<T>)?;
modifiedtests/src/createItem.test.tsdiffbeforeafterboth
--- 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 <http://www.gnu.org/licenses/>.
 
-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}});
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