git.delta.rocks / unique-network / refs/commits / 93112f6aa452

difftreelog

test(refungible-pallet) add tests for repartition events

Grigoriy Simonov2022-07-22parent: #d228734.patch.diff
in: master

3 files changed

modifiedtests/src/eth/reFungibleToken.test.tsdiffbeforeafterboth
239 expect(+await contract.methods.balanceOf(owner).call()).to.be.equal(0);239 expect(+await contract.methods.balanceOf(owner).call()).to.be.equal(0);
240 expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(200);240 expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(200);
241241
242 await contract.methods.repartition(150).send({from: receiver});242 const result = await contract.methods.repartition(150).send({from: receiver});
243 console.log(result.events);
243 await expect(contract.methods.transfer(owner, 160).send({from: receiver})).to.eventually.be.rejected;244 await expect(contract.methods.transfer(owner, 160).send({from: receiver})).to.eventually.be.rejected;
244 expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(150);245 expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(150);
245 });246 });
247
248 itWeb3('Can repartition with increased amount', async ({web3, api, privateKeyWrapper}) => {
249 const alice = privateKeyWrapper('//Alice');
250
251 const collectionId = (await createCollection(api, alice, {name: 'token name', mode: {type: 'ReFungible'}})).collectionId;
252
253 const owner = createEthAccount(web3);
254 await transferBalanceToEth(api, alice, owner);
255
256 const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n, {Ethereum: owner})).itemId;
257
258 const address = tokenIdToAddress(collectionId, tokenId);
259 const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: owner, ...GAS_ARGS});
260
261 const result = await contract.methods.repartition(200).send();
262 const events = normalizeEvents(result.events);
263
264 expect(events).to.include.deep.members([
265 {
266 address,
267 event: 'Transfer',
268 args: {
269 from: '0x0000000000000000000000000000000000000000',
270 to: owner,
271 value: '100',
272 },
273 },
274 ]);
275 });
276
277 itWeb3('Can repartition with decreased amount', async ({web3, api, privateKeyWrapper}) => {
278 const alice = privateKeyWrapper('//Alice');
279
280 const collectionId = (await createCollection(api, alice, {name: 'token name', mode: {type: 'ReFungible'}})).collectionId;
281
282 const owner = createEthAccount(web3);
283 await transferBalanceToEth(api, alice, owner);
284
285 const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n, {Ethereum: owner})).itemId;
286
287 const address = tokenIdToAddress(collectionId, tokenId);
288 const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: owner, ...GAS_ARGS});
289
290 const result = await contract.methods.repartition(50).send();
291 const events = normalizeEvents(result.events);
292
293 expect(events).to.include.deep.members([
294 {
295 address,
296 event: 'Transfer',
297 args: {
298 from: owner,
299 to: '0x0000000000000000000000000000000000000000',
300 value: '50',
301 },
302 },
303 ]);
304 });
246});305});
247306
248describe('Refungible: Fees', () => {307describe('Refungible: Fees', () => {
modifiedtests/src/refungible.test.tsdiffbeforeafterboth
--- a/tests/src/refungible.test.ts
+++ b/tests/src/refungible.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, executeTransaction} from './substrate/substrate-api';
+import {default as usingApi, submitTransactionAsync} from './substrate/substrate-api';
 import {IKeyringPair} from '@polkadot/types/types';
 import {
   createCollectionExpectSuccess,
@@ -32,6 +32,8 @@
   repartitionRFT,
   createCollectionWithPropsExpectSuccess,
   getDetailedCollectionInfo,
+  getCreateItemsResult,
+  getDestroyItemsResult,
 } from './util/helpers';
 
 import chai from 'chai';
@@ -188,6 +190,46 @@
       await expect(transfer(api, collectionId, tokenId, bob, alice, 160n)).to.eventually.be.rejected;
     });
   });
+
+  it('Repartition with increased amount', async () => {
+    await usingApi(async api => {
+      const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;
+      const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;
+
+      const tx = api.tx.unique.repartition(collectionId, tokenId, 200n);
+      const events = await submitTransactionAsync(alice, tx);
+      const substrateEvents = getCreateItemsResult(events);
+      expect(substrateEvents).to.include.deep.members([
+        {
+          success: true,
+          collectionId,
+          itemId: tokenId,
+          recipient: {Substrate: alice.address},
+          amount: 100,
+        },
+      ]);
+    });
+  });
+
+  it('Repartition with decreased amount', async () => {
+    await usingApi(async api => {
+      const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;
+      const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;
+
+      const tx = api.tx.unique.repartition(collectionId, tokenId, 50n);
+      const events = await submitTransactionAsync(alice, tx);
+      const substrateEvents = getDestroyItemsResult(events);
+      expect(substrateEvents).to.include.deep.members([
+        {
+          success: true,
+          collectionId,
+          itemId: tokenId,
+          owner: {Substrate: alice.address},
+          amount: 50,
+        },
+      ]);
+    });
+  });
 });
 
 describe('Test Refungible properties:', () => {
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -103,6 +103,15 @@
   collectionId: number;
   itemId: number;
   recipient?: CrossAccountId;
+  amount?: number;
+}
+
+interface DestroyItemResult {
+  success: boolean;
+  collectionId: number;
+  itemId: number;
+  owner: CrossAccountId;
+  amount: number;
 }
 
 interface TransferResult {
@@ -220,12 +229,14 @@
     const collectionId = parseInt(data[0].toString(), 10);
     const itemId = parseInt(data[1].toString(), 10);
     const recipient = normalizeAccountId(data[2].toJSON() as any);
+    const amount = parseInt(data[3].toString(), 10);
 
     const itemRes: CreateItemResult = {
       success: true,
       collectionId,
       itemId,
       recipient,
+      amount,
     };
 
     results.push(itemRes);
@@ -255,6 +266,31 @@
   return result;
 }
 
+export function getDestroyItemsResult(events: EventRecord[]): DestroyItemResult[] {
+  const results: DestroyItemResult[] = [];
+  
+  const genericResult = getGenericResult<DestroyItemResult[]>(events, 'common', 'ItemDestroyed', (data) => {
+    const collectionId = parseInt(data[0].toString(), 10);
+    const itemId = parseInt(data[1].toString(), 10);
+    const owner = normalizeAccountId(data[2].toJSON() as any);
+    const amount = parseInt(data[3].toString(), 10);
+
+    const itemRes: DestroyItemResult = {
+      success: true,
+      collectionId,
+      itemId,
+      owner,
+      amount,
+    };
+
+    results.push(itemRes);
+    return results;
+  });
+
+  if (!genericResult.success) return [];
+  return results;
+}
+
 export function getTransferResult(api: ApiPromise, events: EventRecord[]): TransferResult {
   for (const {event} of events) {
     if (api.events.common.Transfer.is(event)) {