git.delta.rocks / unique-network / refs/commits / 6c48590c156a

difftreelog

source

tests/src/contracts.test.ts9.7 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 {23  deployFlipper,24  getFlipValue,25  deployTransferContract,26} from './util/contracthelpers';2728import {29  addToAllowListExpectSuccess,30  approveExpectSuccess,31  createCollectionExpectSuccess,32  createItemExpectSuccess,33  enablePublicMintingExpectSuccess,34  enableAllowListExpectSuccess,35  getGenericResult,36  normalizeAccountId,37  isAllowlisted,38  transferFromExpectSuccess,39  getTokenOwner,40} from './util/helpers';414243chai.use(chaiAsPromised);44const expect = chai.expect;4546const value = 0;47const gasLimit = 9000n * 1000000n;48const marketContractAddress = '5CYN9j3YvRkqxewoxeSvRbhAym4465C57uMmX5j4yz99L5H6';4950describe.skip('Contracts', () => {51  it('Can deploy smart contract Flipper, instantiate it and call it\'s get and flip messages.', async () => {52    await usingApi(async (api, privateKeyWrapper) => {53      const [contract, deployer] = await deployFlipper(api, privateKeyWrapper);54      const initialGetResponse = await getFlipValue(contract, deployer);5556      const bob = privateKeyWrapper('//Bob');57      const flip = contract.tx.flip({value, gasLimit});58      await submitTransactionAsync(bob, flip);5960      const afterFlipGetResponse = await getFlipValue(contract, deployer);61      expect(afterFlipGetResponse).not.to.be.eq(initialGetResponse, 'Flipping should change value.');62    });63  });6465  it('Can initialize contract instance', async () => {66    await usingApi(async (api) => {67      const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));68      const abi = new Abi(metadata);69      const newContractInstance = new Contract(api, abi, marketContractAddress);70      expect(newContractInstance).to.have.property('address');71      expect(newContractInstance.address.toString()).to.equal(marketContractAddress);72    });73  });74});7576describe.skip('Chain extensions', () => {77  it('Transfer CE', async () => {78    await usingApi(async (api, privateKeyWrapper) => {79      const alice = privateKeyWrapper('//Alice');80      const bob = privateKeyWrapper('//Bob');8182      // Prep work83      const collectionId = await createCollectionExpectSuccess();84      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');85      const [contract] = await deployTransferContract(api, privateKeyWrapper);86      const changeAdminTx = api.tx.unique.addCollectionAdmin(collectionId, contract.address);87      await submitTransactionAsync(alice, changeAdminTx);8889      expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(normalizeAccountId(alice.address));9091      // Transfer92      const transferTx = contract.tx.transfer({value, gasLimit}, bob.address, collectionId, tokenId, 1);93      const events = await submitTransactionAsync(alice, transferTx);94      const result = getGenericResult(events);95      expect(result.success).to.be.true;9697      expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(normalizeAccountId(bob.address));98    });99  });100101  it('Mint CE', async () => {102    await usingApi(async (api, privateKeyWrapper) => {103      const alice = privateKeyWrapper('//Alice');104      const bob = privateKeyWrapper('//Bob');105106      const collectionId = await createCollectionExpectSuccess();107      const [contract] = await deployTransferContract(api, privateKeyWrapper);108      await enablePublicMintingExpectSuccess(alice, collectionId);109      await enableAllowListExpectSuccess(alice, collectionId);110      await addToAllowListExpectSuccess(alice, collectionId, contract.address);111      await addToAllowListExpectSuccess(alice, collectionId, bob.address);112113      const transferTx = contract.tx.createItem({value, gasLimit}, bob.address, collectionId, {Nft: {const_data: '0x010203'}});114      const events = await submitTransactionAsync(alice, transferTx);115      const result = getGenericResult(events);116      expect(result.success).to.be.true;117118      const tokensAfter = (await api.query.unique.nftItemList.entries(collectionId)).map((kv: any) => kv[1].toJSON());119      expect(tokensAfter).to.be.deep.equal([120        {121          owner: bob.address,122          constData: '0x010203',123        },124      ]);125    });126  });127128  it('Bulk mint CE', async () => {129    await usingApi(async (api, privateKeyWrapper) => {130      const alice = privateKeyWrapper('//Alice');131      const bob = privateKeyWrapper('//Bob');132133      const collectionId = await createCollectionExpectSuccess();134      const [contract] = await deployTransferContract(api, privateKeyWrapper);135      await enablePublicMintingExpectSuccess(alice, collectionId);136      await enableAllowListExpectSuccess(alice, collectionId);137      await addToAllowListExpectSuccess(alice, collectionId, contract.address);138      await addToAllowListExpectSuccess(alice, collectionId, bob.address);139140      const transferTx = contract.tx.createMultipleItems({value, gasLimit}, bob.address, collectionId, [141        {NFT: {/*const_data: '0x010203'*/}},142        {NFT: {/*const_data: '0x010204'*/}},143        {NFT: {/*const_data: '0x010205'*/}},144      ]);145      const events = await submitTransactionAsync(alice, transferTx);146      const result = getGenericResult(events);147      expect(result.success).to.be.true;148149      const tokensAfter: any = (await api.query.unique.nftItemList.entries(collectionId) as any)150        .map((kv: any) => kv[1].toJSON())151        .sort((a: any, b: any) => a.constData.localeCompare(b.constData));152      expect(tokensAfter).to.be.deep.equal([153        {154          Owner: bob.address,155          //ConstData: '0x010203',156        },157        {158          Owner: bob.address,159          //ConstData: '0x010204',160        },161        {162          Owner: bob.address,163          //ConstData: '0x010205',164        },165      ]);166    });167  });168169  it('Approve CE', async () => {170    await usingApi(async (api, privateKeyWrapper) => {171      const alice = privateKeyWrapper('//Alice');172      const bob = privateKeyWrapper('//Bob');173      const charlie = privateKeyWrapper('//Charlie');174175      const collectionId = await createCollectionExpectSuccess();176      const [contract] = await deployTransferContract(api, privateKeyWrapper);177      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', contract.address.toString());178179      const transferTx = contract.tx.approve({value, gasLimit}, bob.address, collectionId, tokenId, 1);180      const events = await submitTransactionAsync(alice, transferTx);181      const result = getGenericResult(events);182      expect(result.success).to.be.true;183184      await transferFromExpectSuccess(collectionId, tokenId, bob, normalizeAccountId(contract.address.toString()), charlie, 1, 'NFT');185    });186  });187188  it('TransferFrom CE', async () => {189    await usingApi(async (api, privateKeyWrapper) => {190      const alice = privateKeyWrapper('//Alice');191      const bob = privateKeyWrapper('//Bob');192      const charlie = privateKeyWrapper('//Charlie');193194      const collectionId = await createCollectionExpectSuccess();195      const [contract] = await deployTransferContract(api, privateKeyWrapper);196      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', bob.address);197      await approveExpectSuccess(collectionId, tokenId, bob, contract.address.toString(), 1);198199      const transferTx = contract.tx.transferFrom({value, gasLimit}, bob.address, charlie.address, collectionId, tokenId, 1);200      const events = await submitTransactionAsync(alice, transferTx);201      const result = getGenericResult(events);202      expect(result.success).to.be.true;203204      const token: any = (await api.query.unique.nftItemList(collectionId, tokenId) as any).unwrap();205      expect(token.owner.toString()).to.be.equal(charlie.address);206    });207  });208209  it('ToggleAllowList CE', async () => {210    await usingApi(async (api, privateKeyWrapper) => {211      const alice = privateKeyWrapper('//Alice');212      const bob = privateKeyWrapper('//Bob');213214      const collectionId = await createCollectionExpectSuccess();215      const [contract] = await deployTransferContract(api, privateKeyWrapper);216      const changeAdminTx = api.tx.unique.addCollectionAdmin(collectionId, contract.address);217      await submitTransactionAsync(alice, changeAdminTx);218219      expect(await isAllowlisted(api, collectionId, bob.address)).to.be.false;220221      {222        const transferTx = contract.tx.toggleAllowList({value, gasLimit}, collectionId, bob.address, true);223        const events = await submitTransactionAsync(alice, transferTx);224        const result = getGenericResult(events);225        expect(result.success).to.be.true;226227        expect(await isAllowlisted(api, collectionId, bob.address)).to.be.true;228      }229      {230        const transferTx = contract.tx.toggleAllowList({value, gasLimit}, collectionId, bob.address, false);231        const events = await submitTransactionAsync(alice, transferTx);232        const result = getGenericResult(events);233        expect(result.success).to.be.true;234235        expect(await isAllowlisted(api, collectionId, bob.address)).to.be.false;236      }237    });238  });239});