git.delta.rocks / unique-network / refs/commits / 0217f0bf8cd8

difftreelog

source

tests/src/contracts.test.ts10.4 KiBsourcehistory
1// 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 chai from 'chai';18import chaiAsPromised from 'chai-as-promised';19import usingApi, {submitTransactionAsync} from './substrate/substrate-api';20import fs from 'fs';21import {Abi, ContractPromise as Contract} from '@polkadot/api-contract';22import privateKey from './substrate/privateKey';23import {24  deployFlipper,25  getFlipValue,26  deployTransferContract,27} from './util/contracthelpers';2829import {30  addToAllowListExpectSuccess,31  approveExpectSuccess,32  createCollectionExpectSuccess,33  createItemExpectSuccess,34  enablePublicMintingExpectSuccess,35  enableAllowListExpectSuccess,36  getGenericResult,37  normalizeAccountId,38  isAllowlisted,39  transferFromExpectSuccess,40  getTokenOwner,41} from './util/helpers';424344chai.use(chaiAsPromised);45const expect = chai.expect;4647const value = 0;48const gasLimit = 9000n * 1000000n;49const marketContractAddress = '5CYN9j3YvRkqxewoxeSvRbhAym4465C57uMmX5j4yz99L5H6';5051describe.skip('Contracts', () => {52  it('Can deploy smart contract Flipper, instantiate it and call it\'s get and flip messages.', async () => {53    await usingApi(async api => {54      const [contract, deployer] = await deployFlipper(api);55      const initialGetResponse = await getFlipValue(contract, deployer);5657      const bob = privateKey('//Bob');58      const flip = contract.tx.flip(value, gasLimit);59      await submitTransactionAsync(bob, flip);6061      const afterFlipGetResponse = await getFlipValue(contract, deployer);62      expect(afterFlipGetResponse).not.to.be.eq(initialGetResponse, 'Flipping should change value.');63    });64  });6566  it('Can initialize contract instance', async () => {67    await usingApi(async (api) => {68      const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));69      const abi = new Abi(metadata);70      const newContractInstance = new Contract(api, abi, marketContractAddress);71      expect(newContractInstance).to.have.property('address');72      expect(newContractInstance.address.toString()).to.equal(marketContractAddress);73    });74  });75});7677describe.skip('Chain extensions', () => {78  it('Transfer CE', async () => {79    await usingApi(async api => {80      const alice = privateKey('//Alice');81      const bob = privateKey('//Bob');8283      // Prep work84      const collectionId = await createCollectionExpectSuccess();85      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');86      const [contract] = await deployTransferContract(api);87      const changeAdminTx = api.tx.unique.addCollectionAdmin(collectionId, contract.address);88      await submitTransactionAsync(alice, changeAdminTx);8990      expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(normalizeAccountId(alice.address));9192      // Transfer93      const transferTx = contract.tx.transfer(value, gasLimit, bob.address, collectionId, tokenId, 1);94      const events = await submitTransactionAsync(alice, transferTx);95      const result = getGenericResult(events);96      expect(result.success).to.be.true;9798      expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(normalizeAccountId(bob.address));99    });100  });101102  it('Mint CE', async () => {103    await usingApi(async api => {104      const alice = privateKey('//Alice');105      const bob = privateKey('//Bob');106107      const collectionId = await createCollectionExpectSuccess();108      const [contract] = await deployTransferContract(api);109      await enablePublicMintingExpectSuccess(alice, collectionId);110      await enableAllowListExpectSuccess(alice, collectionId);111      await addToAllowListExpectSuccess(alice, collectionId, contract.address);112      await addToAllowListExpectSuccess(alice, collectionId, bob.address);113114      const transferTx = contract.tx.createItem(value, gasLimit, bob.address, collectionId, {Nft: {const_data: '0x010203', variable_data: '0x020304'}});115      const events = await submitTransactionAsync(alice, transferTx);116      const result = getGenericResult(events);117      expect(result.success).to.be.true;118119      const tokensAfter = (await api.query.unique.nftItemList.entries(collectionId)).map((kv: any) => kv[1].toJSON());120      expect(tokensAfter).to.be.deep.equal([121        {122          owner: bob.address,123          constData: '0x010203',124          variableData: '0x020304',125        },126      ]);127    });128  });129130  it('Bulk mint CE', async () => {131    await usingApi(async api => {132      const alice = privateKey('//Alice');133      const bob = privateKey('//Bob');134135      const collectionId = await createCollectionExpectSuccess();136      const [contract] = await deployTransferContract(api);137      await enablePublicMintingExpectSuccess(alice, collectionId);138      await enableAllowListExpectSuccess(alice, collectionId);139      await addToAllowListExpectSuccess(alice, collectionId, contract.address);140      await addToAllowListExpectSuccess(alice, collectionId, bob.address);141142      const transferTx = contract.tx.createMultipleItems(value, gasLimit, bob.address, collectionId, [143        {Nft: {const_data: '0x010203', variable_data: '0x020304'}},144        {Nft: {const_data: '0x010204', variable_data: '0x020305'}},145        {Nft: {const_data: '0x010205', variable_data: '0x020306'}},146      ]);147      const events = await submitTransactionAsync(alice, transferTx);148      const result = getGenericResult(events);149      expect(result.success).to.be.true;150151      const tokensAfter: any = (await api.query.unique.nftItemList.entries(collectionId) as any)152        .map((kv: any) => kv[1].toJSON())153        .sort((a: any, b: any) => a.constData.localeCompare(b.constData));154      expect(tokensAfter).to.be.deep.equal([155        {156          Owner: bob.address,157          ConstData: '0x010203',158          VariableData: '0x020304',159        },160        {161          Owner: bob.address,162          ConstData: '0x010204',163          VariableData: '0x020305',164        },165        {166          Owner: bob.address,167          ConstData: '0x010205',168          VariableData: '0x020306',169        },170      ]);171    });172  });173174  it('Approve CE', async () => {175    await usingApi(async api => {176      const alice = privateKey('//Alice');177      const bob = privateKey('//Bob');178      const charlie = privateKey('//Charlie');179180      const collectionId = await createCollectionExpectSuccess();181      const [contract] = await deployTransferContract(api);182      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', contract.address.toString());183184      const transferTx = contract.tx.approve(value, gasLimit, bob.address, collectionId, tokenId, 1);185      const events = await submitTransactionAsync(alice, transferTx);186      const result = getGenericResult(events);187      expect(result.success).to.be.true;188189      await transferFromExpectSuccess(collectionId, tokenId, bob, normalizeAccountId(contract.address.toString()), charlie, 1, 'NFT');190    });191  });192193  it('TransferFrom CE', async () => {194    await usingApi(async api => {195      const alice = privateKey('//Alice');196      const bob = privateKey('//Bob');197      const charlie = privateKey('//Charlie');198199      const collectionId = await createCollectionExpectSuccess();200      const [contract] = await deployTransferContract(api);201      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', bob.address);202      await approveExpectSuccess(collectionId, tokenId, bob, contract.address.toString(), 1);203204      const transferTx = contract.tx.transferFrom(value, gasLimit, bob.address, charlie.address, collectionId, tokenId, 1);205      const events = await submitTransactionAsync(alice, transferTx);206      const result = getGenericResult(events);207      expect(result.success).to.be.true;208209      const token: any = (await api.query.unique.nftItemList(collectionId, tokenId) as any).unwrap();210      expect(token.owner.toString()).to.be.equal(charlie.address);211    });212  });213214  it('SetVariableMetaData CE', async () => {215    await usingApi(async api => {216      const alice = privateKey('//Alice');217218      const collectionId = await createCollectionExpectSuccess();219      const [contract] = await deployTransferContract(api);220      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', contract.address.toString());221222      const transferTx = contract.tx.setVariableMetaData(value, gasLimit, collectionId, tokenId, '0x121314');223      const events = await submitTransactionAsync(alice, transferTx);224      const result = getGenericResult(events);225      expect(result.success).to.be.true;226227      const token: any = (await api.query.unique.nftItemList(collectionId, tokenId) as any).unwrap();228      expect(token.variableData.toString()).to.be.equal('0x121314');229    });230  });231232  it('ToggleAllowList CE', async () => {233    await usingApi(async api => {234      const alice = privateKey('//Alice');235      const bob = privateKey('//Bob');236237      const collectionId = await createCollectionExpectSuccess();238      const [contract] = await deployTransferContract(api);239      const changeAdminTx = api.tx.unique.addCollectionAdmin(collectionId, contract.address);240      await submitTransactionAsync(alice, changeAdminTx);241242      expect(await isAllowlisted(api, collectionId, bob.address)).to.be.false;243244      {245        const transferTx = contract.tx.toggleAllowList(value, gasLimit, collectionId, bob.address, true);246        const events = await submitTransactionAsync(alice, transferTx);247        const result = getGenericResult(events);248        expect(result.success).to.be.true;249250        expect(await isAllowlisted(api, collectionId, bob.address)).to.be.true;251      }252      {253        const transferTx = contract.tx.toggleAllowList(value, gasLimit, collectionId, bob.address, false);254        const events = await submitTransactionAsync(alice, transferTx);255        const result = getGenericResult(events);256        expect(result.success).to.be.true;257258        expect(await isAllowlisted(api, collectionId, bob.address)).to.be.false;259      }260    });261  });262});