difftreelog
Merge pull request #404 from UniqueNetwork/test/unique-refungible
in: master
2 files changed
tests/src/refungible.test.tsdiffbeforeafterboth--- /dev/null
+++ b/tests/src/refungible.test.ts
@@ -0,0 +1,165 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// Unique Network is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// 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 {IKeyringPair} from '@polkadot/types/types';
+import {
+ createCollectionExpectSuccess,
+ getBalance,
+ createMultipleItemsExpectSuccess,
+ isTokenExists,
+ getLastTokenId,
+ getAllowance,
+ approve,
+ transferFrom,
+ createCollection,
+ createRefungibleToken,
+ transfer,
+ burnItem,
+} from './util/helpers';
+
+import chai from 'chai';
+import chaiAsPromised from 'chai-as-promised';
+chai.use(chaiAsPromised);
+const expect = chai.expect;
+
+let alice: IKeyringPair;
+let bob: IKeyringPair;
+
+describe('integration test: Refungible functionality:', () => {
+ before(async () => {
+ await usingApi(async (api, privateKeyWrapper) => {
+ alice = privateKeyWrapper('//Alice');
+ bob = privateKeyWrapper('//Bob');
+ });
+ });
+
+ it('Create refungible collection and token', async () => {
+ await usingApi(async api => {
+ const createCollectionResult = await createCollection(api, alice, {mode: {type: 'ReFungible'}});
+ expect(createCollectionResult.success).to.be.true;
+ const collectionId = createCollectionResult.collectionId;
+
+ const itemCountBefore = await getLastTokenId(api, collectionId);
+ const result = await createRefungibleToken(api, alice, collectionId, 100n);
+
+ const itemCountAfter = await getLastTokenId(api, collectionId);
+
+ // What to expect
+ // tslint:disable-next-line:no-unused-expression
+ expect(result.success).to.be.true;
+ expect(itemCountAfter).to.be.equal(itemCountBefore + 1);
+ expect(collectionId).to.be.equal(result.collectionId);
+ expect(itemCountAfter.toString()).to.be.equal(result.itemId.toString());
+ });
+ });
+
+ it('Transfer token pieces', async () => {
+ await usingApi(async api => {
+ const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;
+ const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;
+
+ expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);
+ expect(await transfer(api, collectionId, tokenId, alice, bob, 60n)).to.be.true;
+
+ expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(40n);
+ expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(60n);
+ await expect(transfer(api, collectionId, tokenId, alice, bob, 41n)).to.eventually.be.rejected;
+ });
+ });
+
+ it('Create multiple tokens', async () => {
+ const collectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});
+ const args = [
+ {ReFungible: {pieces: 1}},
+ {ReFungible: {pieces: 2}},
+ {ReFungible: {pieces: 100}},
+ ];
+ await createMultipleItemsExpectSuccess(alice, collectionId, args);
+
+ await usingApi(async api => {
+ const tokenId = await getLastTokenId(api, collectionId);
+ expect(tokenId).to.be.equal(3);
+ expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);
+ });
+ });
+
+ it('Burn some pieces', async () => {
+ await usingApi(async api => {
+ const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;
+ const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;
+ expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;
+ expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);
+ expect(await burnItem(api, alice, collectionId, tokenId, 99n)).to.be.true;
+ expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;
+ expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(1n);
+ });
+ });
+
+ it('Burn all pieces', async () => {
+ await usingApi(async api => {
+ const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;
+ const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;
+ expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;
+ expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);
+ expect(await burnItem(api, alice, collectionId, tokenId, 100n)).to.be.true;
+ expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;
+ });
+ });
+
+ it('Burn some pieces for multiple users', async () => {
+ await usingApi(async api => {
+ const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;
+ const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;
+ expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;
+
+ expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);
+ expect(await transfer(api, collectionId, tokenId, alice, bob, 60n)).to.be.true;
+
+
+ expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(40n);
+ expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(60n);
+ expect(await burnItem(api, alice, collectionId, tokenId, 40n)).to.be.true;
+
+ expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(0n);
+ expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;
+ expect(await burnItem(api, bob, collectionId, tokenId, 59n)).to.be.true;
+
+ expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(1n);
+ expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;
+ expect(await burnItem(api, bob, collectionId, tokenId, 1n)).to.be.true;
+
+ expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;
+ });
+ });
+
+ it('Set allowance for token', async () => {
+ await usingApi(async api => {
+ const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;
+ const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;
+
+ expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);
+
+ expect(await approve(api, collectionId, tokenId, alice, bob, 60n)).to.be.true;
+ expect(await getAllowance(api, collectionId, alice, bob, tokenId)).to.be.equal(60n);
+
+ expect(await transferFrom(api, collectionId, tokenId, bob, alice, bob, 20n)).to.be.true;
+ expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(80n);
+ expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(20n);
+ expect(await getAllowance(api, collectionId, alice, bob, tokenId)).to.be.equal(40n);
+ });
+ });
+});
tests/src/util/helpers.tsdiffbeforeafterboth318 tokenPrefix: 'prefix',318 tokenPrefix: 'prefix',319};319};320321export async function322createCollection(323 api: ApiPromise,324 sender: IKeyringPair,325 params: Partial<CreateCollectionParams> = {},326): Promise<CreateCollectionResult> {327 const {name, description, mode, tokenPrefix} = {...defaultCreateCollectionParams, ...params};328329 let modeprm = {};330 if (mode.type === 'NFT') {331 modeprm = {nft: null};332 } else if (mode.type === 'Fungible') {333 modeprm = {fungible: mode.decimalPoints};334 } else if (mode.type === 'ReFungible') {335 modeprm = {refungible: null};336 }337338 const tx = api.tx.unique.createCollectionEx({339 name: strToUTF16(name),340 description: strToUTF16(description),341 tokenPrefix: strToUTF16(tokenPrefix),342 mode: modeprm as any,343 });344 const events = await submitTransactionAsync(sender, tx);345 return getCreateCollectionResult(events);346}320347321export async function createCollectionExpectSuccess(params: Partial<CreateCollectionParams> = {}): Promise<number> {348export async function createCollectionExpectSuccess(params: Partial<CreateCollectionParams> = {}): Promise<number> {322 const {name, description, mode, tokenPrefix} = {...defaultCreateCollectionParams, ...params};349 const {name, description, mode, tokenPrefix} = {...defaultCreateCollectionParams, ...params};329 // Run the CreateCollection transaction356 // Run the CreateCollection transaction330 const alicePrivateKey = privateKeyWrapper('//Alice');357 const alicePrivateKey = privateKeyWrapper('//Alice');331358332 let modeprm = {};333 if (mode.type === 'NFT') {334 modeprm = {nft: null};335 } else if (mode.type === 'Fungible') {336 modeprm = {fungible: mode.decimalPoints};337 } else if (mode.type === 'ReFungible') {338 modeprm = {refungible: null};339 }340341 const tx = api.tx.unique.createCollectionEx({342 name: strToUTF16(name),343 description: strToUTF16(description),344 tokenPrefix: strToUTF16(tokenPrefix),345 mode: modeprm as any,346 });347 const events = await submitTransactionAsync(alicePrivateKey, tx);359 const result = await createCollection(api, alicePrivateKey, params);348 const result = getCreateCollectionResult(events);349360350 // Get number of collections after the transaction361 // Get number of collections after the transaction351 const collectionCountAfter = await getCreatedCollectionCount(api);362 const collectionCountAfter = await getCreatedCollectionCount(api);490 return unused;501 return unused;491}502}492503493export async function getAllowance(api: ApiPromise, collectionId: number, owner: CrossAccountId | string, approved: CrossAccountId | string, tokenId: number) {504export async function getAllowance(api: ApiPromise, collectionId: number, owner: CrossAccountId | string | IKeyringPair, approved: CrossAccountId | string | IKeyringPair, tokenId: number) {494 return (await api.rpc.unique.allowance(collectionId, normalizeAccountId(owner), normalizeAccountId(approved), tokenId)).toBigInt();505 return (await api.rpc.unique.allowance(collectionId, normalizeAccountId(owner), normalizeAccountId(approved), tokenId)).toBigInt();495}506}496507800 ReFungible: CreateReFungibleData;811 ReFungible: CreateReFungibleData;801};812};813814export async function burnItem(api: ApiPromise, sender: IKeyringPair, collectionId: number, tokenId: number, value: number | bigint) : Promise<boolean> {815 const tx = api.tx.unique.burnItem(collectionId, tokenId, value);816 const events = await submitTransactionAsync(sender, tx);817 return getGenericResult(events).success;818}802819803export async function burnItemExpectSuccess(sender: IKeyringPair, collectionId: number, tokenId: number, value = 1) {820export async function burnItemExpectSuccess(sender: IKeyringPair, collectionId: number, tokenId: number, value: number | bigint = 1) {804 await usingApi(async (api) => {821 await usingApi(async (api) => {805 const balanceBefore = await getBalance(api, collectionId, normalizeAccountId(sender), tokenId);822 const balanceBefore = await getBalance(api, collectionId, normalizeAccountId(sender), tokenId);806 // if burning token by admin - use adminButnItemExpectSuccess823 // if burning token by admin - use adminButnItemExpectSuccess807 expect(balanceBefore >= BigInt(value)).to.be.true;824 expect(balanceBefore >= BigInt(value)).to.be.true;808825809 const tx = api.tx.unique.burnItem(collectionId, tokenId, value);810 const events = await submitTransactionAsync(sender, tx);811 const result = getGenericResult(events);812 expect(result.success).to.be.true;826 expect(await burnItem(api, sender, collectionId, tokenId, value)).to.be.true;813827814 const balanceAfter = await getBalance(api, collectionId, normalizeAccountId(sender), tokenId);828 const balanceAfter = await getBalance(api, collectionId, normalizeAccountId(sender), tokenId);815 expect(balanceAfter + BigInt(value)).to.be.equal(balanceBefore);829 expect(balanceAfter + BigInt(value)).to.be.equal(balanceBefore);816 });830 });817}831}832833export async function834approve(835 api: ApiPromise,836 collectionId: number,837 tokenId: number, owner: IKeyringPair, approved: CrossAccountId | string | IKeyringPair, amount: number | bigint,838) {839 const approveUniqueTx = api.tx.unique.approve(normalizeAccountId(approved), collectionId, tokenId, amount);840 const events = await submitTransactionAsync(owner, approveUniqueTx);841 return getGenericResult(events).success;842}818843819export async function844export async function820approveExpectSuccess(845approveExpectSuccess(821 collectionId: number,846 collectionId: number,822 tokenId: number, owner: IKeyringPair, approved: CrossAccountId | string, amount: number | bigint = 1,847 tokenId: number, owner: IKeyringPair, approved: CrossAccountId | string, amount: number | bigint = 1,823) {848) {824 await usingApi(async (api: ApiPromise) => {849 await usingApi(async (api: ApiPromise) => {825 const approveUniqueTx = api.tx.unique.approve(normalizeAccountId(approved), collectionId, tokenId, amount);850 const result = await approve(api, collectionId, tokenId, owner, approved, amount);826 const events = await submitTransactionAsync(owner, approveUniqueTx);827 const result = getGenericResult(events);828 expect(result.success).to.be.true;851 expect(result).to.be.true;829852830 expect(await getAllowance(api, collectionId, owner.address, approved, tokenId)).to.be.equal(BigInt(amount));853 expect(await getAllowance(api, collectionId, owner, approved, tokenId)).to.be.equal(BigInt(amount));831 });854 });832}855}833856845 });868 });846}869}870871export async function872transferFrom(873 api: ApiPromise,874 collectionId: number,875 tokenId: number,876 accountApproved: IKeyringPair,877 accountFrom: IKeyringPair | CrossAccountId,878 accountTo: IKeyringPair | CrossAccountId,879 value: number | bigint,880) {881 const from = normalizeAccountId(accountFrom);882 const to = normalizeAccountId(accountTo);883 const transferFromTx = api.tx.unique.transferFrom(from, to, collectionId, tokenId, value);884 const events = await submitTransactionAsync(accountApproved, transferFromTx);885 return getGenericResult(events).success;886}847887848export async function888export async function849transferFromExpectSuccess(889transferFromExpectSuccess(862 if (type === 'Fungible' || type === 'ReFungible') {902 if (type === 'Fungible' || type === 'ReFungible') {863 balanceBefore = await getBalance(api, collectionId, to, tokenId);903 balanceBefore = await getBalance(api, collectionId, to, tokenId);864 }904 }865 const transferFromTx = api.tx.unique.transferFrom(normalizeAccountId(accountFrom), to, collectionId, tokenId, value);866 const events = await submitTransactionAsync(accountApproved, transferFromTx);867 const result = getGenericResult(events);868 // tslint:disable-next-line:no-unused-expression869 expect(result.success).to.be.true;905 expect(await transferFrom(api, collectionId, tokenId, accountApproved, accountFrom, accountTo, value)).to.be.true;870 if (type === 'NFT') {906 if (type === 'NFT') {871 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(to);907 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(to);872 }908 }1070 });1106 });1071}1107}11081109export async function1110transfer(1111 api: ApiPromise,1112 collectionId: number,1113 tokenId: number,1114 sender: IKeyringPair,1115 recipient: IKeyringPair | CrossAccountId,1116 value: number | bigint,1117) : Promise<boolean> {1118 const transferTx = api.tx.unique.transfer(normalizeAccountId(recipient), collectionId, tokenId, value);1119 const events = await executeTransaction(api, sender, transferTx);1120 return getGenericResult(events).success;1121}107211221073export async function1123export async function1074transferExpectSuccess(1124transferExpectSuccess(1084 const to = normalizeAccountId(recipient);1134 const to = normalizeAccountId(recipient);108511351086 let balanceBefore = 0n;1136 let balanceBefore = 0n;1087 if (type === 'Fungible') {1137 if (type === 'Fungible' || type === 'ReFungible') {1088 balanceBefore = await getBalance(api, collectionId, to, tokenId);1138 balanceBefore = await getBalance(api, collectionId, to, tokenId);1089 }1139 }11401090 const transferTx = api.tx.unique.transfer(to, collectionId, tokenId, value);1141 const transferTx = api.tx.unique.transfer(normalizeAccountId(recipient), collectionId, tokenId, value);1091 const events = await executeTransaction(api, sender, transferTx);1142 const events = await executeTransaction(api, sender, transferTx);10921093 const result = getTransferResult(api, events);1143 const result = getTransferResult(api, events);1100 if (type === 'NFT') {1151 if (type === 'NFT') {1101 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(to);1152 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(to);1102 }1153 }1103 if (type === 'Fungible') {1154 if (type === 'Fungible' || type === 'ReFungible') {1104 const balanceAfter = await getBalance(api, collectionId, to, tokenId);1155 const balanceAfter = await getBalance(api, collectionId, to, tokenId);1105 if (JSON.stringify(to) !== JSON.stringify(from)) {1156 if (JSON.stringify(to) !== JSON.stringify(from)) {1106 expect(balanceAfter - balanceBefore).to.be.equal(BigInt(value));1157 expect(balanceAfter - balanceBefore).to.be.equal(BigInt(value));1107 } else {1158 } else {1108 expect(balanceAfter).to.be.equal(balanceBefore);1159 expect(balanceAfter).to.be.equal(balanceBefore);1109 }1160 }1110 }1161 }1111 if (type === 'ReFungible') {1112 expect(await getBalance(api, collectionId, to, tokenId) >= value).to.be.true;1113 }1114 });1162 });1115}1163}111611641151export async function getBalance(1199export async function getBalance(1152 api: ApiPromise,1200 api: ApiPromise,1153 collectionId: number,1201 collectionId: number,1154 owner: string | CrossAccountId,1202 owner: string | CrossAccountId | IKeyringPair,1155 token: number,1203 token: number,1156): Promise<bigint> {1204): Promise<bigint> {1157 return (await api.rpc.unique.balance(collectionId, normalizeAccountId(owner), token)).toBigInt();1205 return (await api.rpc.unique.balance(collectionId, normalizeAccountId(owner), token)).toBigInt();1226 });1274 });1227}1275}12761277export async function createMultipleItemsExpectSuccess(sender: IKeyringPair, collectionId: number, itemsData: any, owner: CrossAccountId | string = sender.address) {1278 await usingApi(async (api) => {1279 const to = normalizeAccountId(owner);1280 const tx = api.tx.unique.createMultipleItems(collectionId, to, itemsData);12811282 const events = await submitTransactionAsync(sender, tx);1283 expect(getGenericResult(events).success).to.be.true;1284 });1285}122812861229export async function createMultipleItemsWithPropsExpectSuccess(sender: IKeyringPair, collectionId: number, itemsData: any, owner: CrossAccountId | string = sender.address) {1287export async function createMultipleItemsWithPropsExpectSuccess(sender: IKeyringPair, collectionId: number, itemsData: any, owner: CrossAccountId | string = sender.address) {1230 await usingApi(async (api) => {1288 await usingApi(async (api) => {1359 return newItemId;1417 return newItemId;1360}1418}14191420export async function createRefungibleToken(api: ApiPromise, sender: IKeyringPair, collectionId: number, amount: bigint, owner: CrossAccountId | IKeyringPair | string = sender.address) : Promise<CreateItemResult> {1421 const createData = {refungible: {pieces: amount}};1422 const tx = api.tx.unique.createItem(collectionId, normalizeAccountId(owner), createData as any);14231424 const events = await submitTransactionAsync(sender, tx);1425 return getCreateItemResult(events);1426}136114271362export async function createItemExpectFailure(sender: IKeyringPair, collectionId: number, createMode: string, owner: CrossAccountId | string = sender.address) {1428export async function createItemExpectFailure(sender: IKeyringPair, collectionId: number, createMode: string, owner: CrossAccountId | string = sender.address) {1363 await usingApi(async (api) => {1429 await usingApi(async (api) => {