difftreelog
test refactoring. burn pieces for multiple users test.
in: master
2 files changed
tests/src/refungible.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} from './substrate/substrate-api';18import {IKeyringPair} from '@polkadot/types/types';19import {20 createCollectionExpectSuccess,21 createItemExpectSuccess,22 transferExpectSuccess,23 transferExpectFailure,24 getBalance,25 burnItemExpectSuccess,26 createMultipleItemsExpectSuccess,27 approveExpectSuccess,28 transferFromExpectSuccess,29 isTokenExists,30 getLastTokenId,31 getAllowance,32} from './util/helpers';3334import chai from 'chai';35import chaiAsPromised from 'chai-as-promised';36chai.use(chaiAsPromised);37const expect = chai.expect;3839let alice: IKeyringPair;40let bob: IKeyringPair;4142describe('integration test: Refungible functionality:', () => {43 before(async () => {44 await usingApi(async (api, privateKeyWrapper) => {45 alice = privateKeyWrapper('//Alice');46 bob = privateKeyWrapper('//Bob');47 });48 });4950 it('Create refungible collection and token. Token pieces transfer.', async () => {51 const createMode = 'ReFungible';52 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});53 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);5455 let aliceBalance = BigInt(0);56 await usingApi(async api => {57 aliceBalance = await getBalance(api, collectionId, alice.address, tokenId);58 });59 const transferAmount = BigInt(60);60 await transferExpectSuccess(collectionId, tokenId, alice, bob, transferAmount, 'ReFungible');61 aliceBalance = aliceBalance - transferAmount;62 await transferExpectFailure(collectionId, tokenId, alice, bob, aliceBalance + BigInt(1));63 });6465 it('Create multiple tokens', async () => {66 const createMode = 'ReFungible';67 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});68 const args = [69 {ReFungible: {pieces: 1}},70 {ReFungible: {pieces: 2}},71 {ReFungible: {pieces: 100}},72 ];73 await createMultipleItemsExpectSuccess(alice, collectionId, args);7475 let tokenId = 0;76 await usingApi(async api => { 77 tokenId = await getLastTokenId(api, collectionId);78 expect(tokenId).to.be.equal(3);79 });8081 const transferAmount = BigInt(60);82 await transferExpectSuccess(collectionId, tokenId, alice, bob, transferAmount, 'ReFungible');83 });8485 it('Burn some pieces', async () => {86 const createMode = 'ReFungible';87 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});88 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);8990 let aliceBalance = BigInt(0);91 await usingApi(async api => {92 aliceBalance = await getBalance(api, collectionId, alice.address, tokenId);93 expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;94 });95 await burnItemExpectSuccess(alice, collectionId, tokenId, aliceBalance - BigInt(1));96 await usingApi(async api => {97 expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;98 });99 });100101 it('Burn all pieces', async () => {102 const createMode = 'ReFungible';103 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});104 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);105106 let aliceBalance = BigInt(0);107 await usingApi(async api => {108 aliceBalance = await getBalance(api, collectionId, alice.address, tokenId);109 expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;110 });111 await burnItemExpectSuccess(alice, collectionId, tokenId, aliceBalance);112 await usingApi(async api => {113 expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;114 });115 });116117 it('Set allowance for token', async () => {118 const createMode = 'ReFungible';119 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});120 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);121122 let aliceBalance = BigInt(0);123 await usingApi(async api => {124 aliceBalance = await getBalance(api, collectionId, alice.address, tokenId);125 });126 const allowedAmount = BigInt(60);127 await approveExpectSuccess(collectionId, tokenId, alice, bob.address, allowedAmount);128 const transferAmount = BigInt(20);129 await transferFromExpectSuccess(collectionId, tokenId, bob, alice, bob, transferAmount, 'ReFungible');130131 await usingApi(async api => {132 expect(await getAllowance(api, collectionId, alice.address, bob.address, tokenId)).to.equal(allowedAmount - transferAmount);133 });134 135 });136});tests/src/util/helpers.tsdiffbeforeafterboth--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -318,6 +318,33 @@
tokenPrefix: 'prefix',
};
+export async function
+createCollection(
+ api: ApiPromise,
+ sender: IKeyringPair,
+ params: Partial<CreateCollectionParams> = {},
+): Promise<CreateCollectionResult> {
+ const {name, description, mode, tokenPrefix} = {...defaultCreateCollectionParams, ...params};
+
+ let modeprm = {};
+ if (mode.type === 'NFT') {
+ modeprm = {nft: null};
+ } else if (mode.type === 'Fungible') {
+ modeprm = {fungible: mode.decimalPoints};
+ } else if (mode.type === 'ReFungible') {
+ modeprm = {refungible: null};
+ }
+
+ const tx = api.tx.unique.createCollectionEx({
+ name: strToUTF16(name),
+ description: strToUTF16(description),
+ tokenPrefix: strToUTF16(tokenPrefix),
+ mode: modeprm as any,
+ });
+ const events = await submitTransactionAsync(sender, tx);
+ return getCreateCollectionResult(events);
+}
+
export async function createCollectionExpectSuccess(params: Partial<CreateCollectionParams> = {}): Promise<number> {
const {name, description, mode, tokenPrefix} = {...defaultCreateCollectionParams, ...params};
@@ -329,23 +356,7 @@
// Run the CreateCollection transaction
const alicePrivateKey = privateKeyWrapper('//Alice');
- let modeprm = {};
- if (mode.type === 'NFT') {
- modeprm = {nft: null};
- } else if (mode.type === 'Fungible') {
- modeprm = {fungible: mode.decimalPoints};
- } else if (mode.type === 'ReFungible') {
- modeprm = {refungible: null};
- }
-
- const tx = api.tx.unique.createCollectionEx({
- name: strToUTF16(name),
- description: strToUTF16(description),
- tokenPrefix: strToUTF16(tokenPrefix),
- mode: modeprm as any,
- });
- const events = await submitTransactionAsync(alicePrivateKey, tx);
- const result = getCreateCollectionResult(events);
+ const result = await createCollection(api, alicePrivateKey, params);
// Get number of collections after the transaction
const collectionCountAfter = await getCreatedCollectionCount(api);
@@ -490,7 +501,7 @@
return unused;
}
-export async function getAllowance(api: ApiPromise, collectionId: number, owner: CrossAccountId | string, approved: CrossAccountId | string, tokenId: number) {
+export async function getAllowance(api: ApiPromise, collectionId: number, owner: CrossAccountId | string | IKeyringPair, approved: CrossAccountId | string | IKeyringPair, tokenId: number) {
return (await api.rpc.unique.allowance(collectionId, normalizeAccountId(owner), normalizeAccountId(approved), tokenId)).toBigInt();
}
@@ -800,16 +811,19 @@
ReFungible: CreateReFungibleData;
};
+export async function burnItem(api: ApiPromise, sender: IKeyringPair, collectionId: number, tokenId: number, value: number | bigint) : Promise<boolean> {
+ const tx = api.tx.unique.burnItem(collectionId, tokenId, value);
+ const events = await submitTransactionAsync(sender, tx);
+ return getGenericResult(events).success;
+}
+
export async function burnItemExpectSuccess(sender: IKeyringPair, collectionId: number, tokenId: number, value: number | bigint = 1) {
await usingApi(async (api) => {
const balanceBefore = await getBalance(api, collectionId, normalizeAccountId(sender), tokenId);
// if burning token by admin - use adminButnItemExpectSuccess
expect(balanceBefore >= BigInt(value)).to.be.true;
- const tx = api.tx.unique.burnItem(collectionId, tokenId, value);
- const events = await submitTransactionAsync(sender, tx);
- const result = getGenericResult(events);
- expect(result.success).to.be.true;
+ expect(await burnItem(api, sender, collectionId, tokenId, value)).to.be.true;
const balanceAfter = await getBalance(api, collectionId, normalizeAccountId(sender), tokenId);
expect(balanceAfter + BigInt(value)).to.be.equal(balanceBefore);
@@ -817,15 +831,24 @@
}
export async function
+approve(
+ api: ApiPromise,
+ collectionId: number,
+ tokenId: number, owner: IKeyringPair, approved: CrossAccountId | string | IKeyringPair, amount: number | bigint = 1,
+) {
+ const approveUniqueTx = api.tx.unique.approve(normalizeAccountId(approved), collectionId, tokenId, amount);
+ const events = await submitTransactionAsync(owner, approveUniqueTx);
+ return getGenericResult(events).success;
+}
+
+export async function
approveExpectSuccess(
collectionId: number,
tokenId: number, owner: IKeyringPair, approved: CrossAccountId | string, amount: number | bigint = 1,
) {
await usingApi(async (api: ApiPromise) => {
- const approveUniqueTx = api.tx.unique.approve(normalizeAccountId(approved), collectionId, tokenId, amount);
- const events = await submitTransactionAsync(owner, approveUniqueTx);
- const result = getGenericResult(events);
- expect(result.success).to.be.true;
+ const result = approve(api, collectionId, tokenId, owner, approved);
+ expect(result).to.be.true;
expect(await getAllowance(api, collectionId, owner.address, approved, tokenId)).to.be.equal(BigInt(amount));
});
@@ -846,6 +869,23 @@
}
export async function
+transferFrom(
+ api: ApiPromise,
+ collectionId: number,
+ tokenId: number,
+ accountApproved: IKeyringPair,
+ accountFrom: IKeyringPair | CrossAccountId,
+ accountTo: IKeyringPair | CrossAccountId,
+ value: number | bigint,
+) {
+ const from = normalizeAccountId(accountFrom);
+ const to = normalizeAccountId(accountTo);
+ const transferFromTx = api.tx.unique.transferFrom(from, to, collectionId, tokenId, value);
+ const events = await submitTransactionAsync(accountApproved, transferFromTx);
+ return getGenericResult(events).success;
+}
+
+export async function
transferFromExpectSuccess(
collectionId: number,
tokenId: number,
@@ -862,11 +902,7 @@
if (type === 'Fungible' || type === 'ReFungible') {
balanceBefore = await getBalance(api, collectionId, to, tokenId);
}
- const transferFromTx = api.tx.unique.transferFrom(normalizeAccountId(accountFrom), to, collectionId, tokenId, value);
- const events = await submitTransactionAsync(accountApproved, transferFromTx);
- const result = getGenericResult(events);
- // tslint:disable-next-line:no-unused-expression
- expect(result.success).to.be.true;
+ expect(transferFrom(api, collectionId, tokenId, accountApproved, accountFrom, accountTo, value)).to.be.true;
if (type === 'NFT') {
expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(to);
}
@@ -1071,6 +1107,20 @@
}
export async function
+transfer(
+ api: ApiPromise,
+ collectionId: number,
+ tokenId: number,
+ sender: IKeyringPair,
+ recipient: IKeyringPair | CrossAccountId,
+ value: number | bigint,
+) : Promise<boolean> {
+ const transferTx = api.tx.unique.transfer(normalizeAccountId(recipient), collectionId, tokenId, value);
+ const events = await executeTransaction(api, sender, transferTx);
+ return getGenericResult(events).success;
+}
+
+export async function
transferExpectSuccess(
collectionId: number,
tokenId: number,
@@ -1087,10 +1137,11 @@
if (type === 'Fungible' || type === 'ReFungible') {
balanceBefore = await getBalance(api, collectionId, to, tokenId);
}
- const transferTx = api.tx.unique.transfer(to, collectionId, tokenId, value);
+
+ const transferTx = api.tx.unique.transfer(normalizeAccountId(recipient), collectionId, tokenId, value);
const events = await executeTransaction(api, sender, transferTx);
+ const result = getTransferResult(api, events);
- const result = getTransferResult(api, events);
expect(result.collectionId).to.be.equal(collectionId);
expect(result.itemId).to.be.equal(tokenId);
expect(result.sender).to.be.deep.equal(normalizeAccountId(sender.address));
@@ -1148,7 +1199,7 @@
export async function getBalance(
api: ApiPromise,
collectionId: number,
- owner: string | CrossAccountId,
+ owner: string | CrossAccountId | IKeyringPair,
token: number,
): Promise<bigint> {
return (await api.rpc.unique.balance(collectionId, normalizeAccountId(owner), token)).toBigInt();
@@ -1229,7 +1280,7 @@
const tx = api.tx.unique.createMultipleItems(collectionId, to, itemsData);
const events = await submitTransactionAsync(sender, tx);
- const result = getCreateItemsResult(events);
+ expect(getGenericResult(events).success).to.be.true;
});
}
@@ -1366,6 +1417,14 @@
return newItemId;
}
+export async function createRefungibleToken(api: ApiPromise, sender: IKeyringPair, collectionId: number, amount: bigint, owner: CrossAccountId | IKeyringPair | string = sender.address) : Promise<CreateItemResult> {
+ const createData = {refungible: {pieces: amount}};
+ const tx = api.tx.unique.createItem(collectionId, normalizeAccountId(owner), createData as any);
+
+ const events = await submitTransactionAsync(sender, tx);
+ return getCreateItemResult(events);
+}
+
export async function createItemExpectFailure(sender: IKeyringPair, collectionId: number, createMode: string, owner: CrossAccountId | string = sender.address) {
await usingApi(async (api) => {
const tx = api.tx.unique.createItem(collectionId, normalizeAccountId(owner), createMode);