difftreelog
fix(refungible-pallet) fixed wrong amount in ItemCreated event
in: master
4 files changed
pallets/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,
pallets/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>)?;
tests/src/createItem.test.tsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {default as usingApi, executeTransaction} from './substrate/substrate-api';18import chai from 'chai';19import {IKeyringPair} from '@polkadot/types/types';20import {21 createCollectionExpectSuccess,22 createItemExpectSuccess,23 addCollectionAdminExpectSuccess,24 createCollectionWithPropsExpectSuccess,25 createItemWithPropsExpectSuccess,26 createItemWithPropsExpectFailure,27 createCollection,28 transferExpectSuccess,29 itApi,30 normalizeAccountId,31 getCreateItemResult,32} from './util/helpers';3334const expect = chai.expect;35let alice: IKeyringPair;36let bob: IKeyringPair;3738describe('integration test: ext. ():', () => {39 before(async () => {40 await usingApi(async (api, privateKeyWrapper) => {41 alice = privateKeyWrapper('//Alice');42 bob = privateKeyWrapper('//Bob');43 });44 });4546 it('Create new item in NFT collection', async () => {47 const createMode = 'NFT';48 const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode}});49 await createItemExpectSuccess(alice, newCollectionID, createMode);50 });51 it('Create new item in Fungible collection', async () => {52 const createMode = 'Fungible';53 const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});54 await createItemExpectSuccess(alice, newCollectionID, createMode);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 }8081 });82 it('Create new item in ReFungible collection', async () => {83 const createMode = 'ReFungible';84 const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode}});85 await createItemExpectSuccess(alice, newCollectionID, createMode);86 });87 it('Create new item in NFT collection with collection admin permissions', async () => {88 const createMode = 'NFT';89 const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode}});90 await addCollectionAdminExpectSuccess(alice, newCollectionID, bob.address);91 await createItemExpectSuccess(bob, newCollectionID, createMode);92 });93 it('Create new item in Fungible collection with collection admin permissions', async () => {94 const createMode = 'Fungible';95 const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});96 await addCollectionAdminExpectSuccess(alice, newCollectionID, bob.address);97 await createItemExpectSuccess(bob, newCollectionID, createMode);98 });99 it('Create new item in ReFungible collection with collection admin permissions', async () => {100 const createMode = 'ReFungible';101 const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode}});102 await addCollectionAdminExpectSuccess(alice, newCollectionID, bob.address);103 await createItemExpectSuccess(bob, newCollectionID, createMode);104 });105106 it('Set property Admin', async () => {107 const createMode = 'NFT';108 const newCollectionID = await createCollectionWithPropsExpectSuccess({mode: {type: createMode}, 109 propPerm: [{key: 'k', permission: {mutable: true, collectionAdmin: true, tokenOwner: false}}]});110 111 await createItemWithPropsExpectSuccess(alice, newCollectionID, createMode, [{key: 'k', value: 't2'}]);112 });113114 it('Set property AdminConst', async () => {115 const createMode = 'NFT';116 const newCollectionID = await createCollectionWithPropsExpectSuccess({mode: {type: createMode}, 117 propPerm: [{key: 'key1', permission: {mutable: false, collectionAdmin: true, tokenOwner: false}}]});118 119 await createItemWithPropsExpectSuccess(alice, newCollectionID, createMode, [{key: 'key1', value: 'val1'}]);120 });121122 it('Set property itemOwnerOrAdmin', async () => {123 const createMode = 'NFT';124 const newCollectionID = await createCollectionWithPropsExpectSuccess({mode: {type: createMode},125 propPerm: [{key: 'key1', permission: {mutable: true, collectionAdmin: true, tokenOwner: true}}]});126 127 await createItemWithPropsExpectSuccess(alice, newCollectionID, createMode, [{key: 'key1', value: 'val1'}]);128 });129130 it('Check total pieces of Fungible token', async () => {131 await usingApi(async api => {132 const createMode = 'Fungible';133 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});134 const amountPieces = 10n;135 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode, bob.address);136 {137 const totalPieces = await api.rpc.unique.totalPieces(collectionId, tokenId);138 expect(totalPieces.isSome).to.be.true;139 expect(totalPieces.unwrap().toBigInt()).to.be.eq(amountPieces);140 }141142 await transferExpectSuccess(collectionId, tokenId, bob, alice, 1, createMode);143 {144 const totalPieces = await api.rpc.unique.totalPieces(collectionId, tokenId);145 expect(totalPieces.isSome).to.be.true;146 expect(totalPieces.unwrap().toBigInt()).to.be.eq(amountPieces);147 }148149 const totalPieces = (await api.rpc.unique.tokenData(collectionId, tokenId, [])).pieces;150 expect(totalPieces.toBigInt()).to.be.eq(amountPieces);151 });152 });153154 it('Check total pieces of NFT token', async () => {155 await usingApi(async api => {156 const createMode = 'NFT';157 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});158 const amountPieces = 1n;159 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode, bob.address);160 {161 const totalPieces = await api.rpc.unique.totalPieces(collectionId, tokenId);162 expect(totalPieces.isSome).to.be.true;163 expect(totalPieces.unwrap().toBigInt()).to.be.eq(amountPieces);164 }165166 await transferExpectSuccess(collectionId, tokenId, bob, alice, 1, createMode);167 {168 const totalPieces = await api.rpc.unique.totalPieces(collectionId, tokenId);169 expect(totalPieces.isSome).to.be.true;170 expect(totalPieces.unwrap().toBigInt()).to.be.eq(amountPieces);171 }172173 const totalPieces = (await api.rpc.unique.tokenData(collectionId, tokenId, [])).pieces;174 expect(totalPieces.toBigInt()).to.be.eq(amountPieces);175 });176 });177178 it('Check total pieces of ReFungible token', async () => {179 await usingApi(async api => {180 const createMode = 'ReFungible';181 const createCollectionResult = await createCollection(api, alice, {mode: {type: createMode}});182 const collectionId = createCollectionResult.collectionId;183 const amountPieces = 100n;184 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode, bob.address);185 {186 const totalPieces = await api.rpc.unique.totalPieces(collectionId, tokenId);187 expect(totalPieces.isSome).to.be.true;188 expect(totalPieces.unwrap().toBigInt()).to.be.eq(amountPieces);189 }190191 await transferExpectSuccess(collectionId, tokenId, bob, alice, 60n, createMode);192 {193 const totalPieces = await api.rpc.unique.totalPieces(collectionId, tokenId);194 expect(totalPieces.isSome).to.be.true;195 expect(totalPieces.unwrap().toBigInt()).to.be.eq(amountPieces);196 }197198 const totalPieces = (await api.rpc.unique.tokenData(collectionId, tokenId, [])).pieces;199 expect(totalPieces.toBigInt()).to.be.eq(amountPieces);200 });201 });202});203204describe('Negative integration test: ext. createItem():', () => {205 before(async () => {206 await usingApi(async (api, privateKeyWrapper) => {207 alice = privateKeyWrapper('//Alice');208 bob = privateKeyWrapper('//Bob');209 });210 });211212 it('Regular user cannot create new item in NFT collection', async () => {213 const createMode = 'NFT';214 const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode}});215 await expect(createItemExpectSuccess(bob, newCollectionID, createMode)).to.be.rejected;216 });217 it('Regular user cannot create new item in Fungible collection', async () => {218 const createMode = 'Fungible';219 const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});220 await expect(createItemExpectSuccess(bob, newCollectionID, createMode)).to.be.rejected;221 });222 it('Regular user cannot create new item in ReFungible collection', async () => {223 const createMode = 'ReFungible';224 const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode}});225 await expect(createItemExpectSuccess(bob, newCollectionID, createMode)).to.be.rejected;226 });227228 it('No editing rights', async () => {229 await usingApi(async () => {230 const createMode = 'NFT';231 const newCollectionID = await createCollectionWithPropsExpectSuccess({mode: {type: createMode}, 232 propPerm: [{key: 'key1', permission: {mutable: false, collectionAdmin: false, tokenOwner: false}}]});233 await addCollectionAdminExpectSuccess(alice, newCollectionID, bob.address);234235 await createItemWithPropsExpectFailure(bob, newCollectionID, 'NFT', [{key: 'key1', value: 'v'}]);236 });237 });238239 it('User doesnt have editing rights', async () => {240 await usingApi(async () => {241 const newCollectionID = await createCollectionWithPropsExpectSuccess({propPerm: [{key: 'key1', permission: {mutable: true, collectionAdmin: false, tokenOwner: false}}]});242 await createItemWithPropsExpectFailure(bob, newCollectionID, 'NFT', [{key: 'key1', value: 'v'}]);243 });244 });245246 it('Adding property without access rights', async () => {247 await usingApi(async () => {248 const newCollectionID = await createCollectionWithPropsExpectSuccess();249 await addCollectionAdminExpectSuccess(alice, newCollectionID, bob.address);250251 await createItemWithPropsExpectFailure(bob, newCollectionID, 'NFT', [{key: 'k', value: 'v'}]);252 });253 });254255 it('Adding more than 64 prps', async () => {256 await usingApi(async () => {257 const prps = [];258259 for (let i = 0; i < 65; i++) {260 prps.push({key: `key${i}`, value: `value${i}`});261 }262263 const newCollectionID = await createCollectionWithPropsExpectSuccess();264 265 await createItemWithPropsExpectFailure(alice, newCollectionID, 'NFT', prps);266 });267 });268269 it('Trying to add bigger property than allowed', async () => {270 await usingApi(async () => {271 const newCollectionID = await createCollectionWithPropsExpectSuccess();272 273 await createItemWithPropsExpectFailure(alice, newCollectionID, 'NFT', [{key: 'k', value: 'vvvvvv'.repeat(5000)}, {key: 'k2', value: 'vvv'.repeat(5000)}]);274 });275 });276277 it('Check total pieces for invalid Fungible token', async () => {278 await usingApi(async api => {279 const createCollectionResult = await createCollection(api, alice, {mode: {type: 'Fungible', decimalPoints: 0}});280 const collectionId = createCollectionResult.collectionId;281 const invalidTokenId = 1000_000;282 283 expect((await api.rpc.unique.totalPieces(collectionId, invalidTokenId)).isNone).to.be.true;284 expect((await api.rpc.unique.tokenData(collectionId, invalidTokenId, [])).pieces.toBigInt()).to.be.eq(0n);285 });286 });287288 it('Check total pieces for invalid NFT token', async () => {289 await usingApi(async api => {290 const createCollectionResult = await createCollection(api, alice, {mode: {type: 'NFT'}});291 const collectionId = createCollectionResult.collectionId;292 const invalidTokenId = 1000_000;293 294 expect((await api.rpc.unique.totalPieces(collectionId, invalidTokenId)).isNone).to.be.true;295 expect((await api.rpc.unique.tokenData(collectionId, invalidTokenId, [])).pieces.toBigInt()).to.be.eq(0n);296 });297 });298299 it('Check total pieces for invalid Refungible token', async () => {300 await usingApi(async api => {301 const createCollectionResult = await createCollection(api, alice, {mode: {type: 'ReFungible'}});302 const collectionId = createCollectionResult.collectionId;303 const invalidTokenId = 1000_000;304 305 expect((await api.rpc.unique.totalPieces(collectionId, invalidTokenId)).isNone).to.be.true;306 expect((await api.rpc.unique.tokenData(collectionId, invalidTokenId, [])).pieces.toBigInt()).to.be.eq(0n);307 });308 });309});tests/src/util/helpers.tsdiffbeforeafterboth--- 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});
+ });
+ });
+}