git.delta.rocks / unique-network / refs/commits / 12fbdbdcf4df

difftreelog

source

tests/src/contracts.test.ts9.8 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';4950// todo:playgrounds skipped ~ postponed51describe.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, privateKeyWrapper) => {54      const [contract, deployer] = await deployFlipper(api, privateKeyWrapper);55      const initialGetResponse = await getFlipValue(contract, deployer);5657      const bob = privateKeyWrapper('//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, privateKeyWrapper) => {80      const alice = privateKeyWrapper('//Alice');81      const bob = privateKeyWrapper('//Bob');8283      // Prep work84      const collectionId = await createCollectionExpectSuccess();85      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');86      const [contract] = await deployTransferContract(api, privateKeyWrapper);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, privateKeyWrapper) => {104      const alice = privateKeyWrapper('//Alice');105      const bob = privateKeyWrapper('//Bob');106107      const collectionId = await createCollectionExpectSuccess();108      const [contract] = await deployTransferContract(api, privateKeyWrapper);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'}});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        },125      ]);126    });127  });128129  it('Bulk mint CE', async () => {130    await usingApi(async (api, privateKeyWrapper) => {131      const alice = privateKeyWrapper('//Alice');132      const bob = privateKeyWrapper('//Bob');133134      const collectionId = await createCollectionExpectSuccess();135      const [contract] = await deployTransferContract(api, privateKeyWrapper);136      await enablePublicMintingExpectSuccess(alice, collectionId);137      await enableAllowListExpectSuccess(alice, collectionId);138      await addToAllowListExpectSuccess(alice, collectionId, contract.address);139      await addToAllowListExpectSuccess(alice, collectionId, bob.address);140141      const transferTx = contract.tx.createMultipleItems({value, gasLimit}, bob.address, collectionId, [142        {NFT: {/*const_data: '0x010203'*/}},143        {NFT: {/*const_data: '0x010204'*/}},144        {NFT: {/*const_data: '0x010205'*/}},145      ]);146      const events = await submitTransactionAsync(alice, transferTx);147      const result = getGenericResult(events);148      expect(result.success).to.be.true;149150      const tokensAfter: any = (await api.query.unique.nftItemList.entries(collectionId) as any)151        .map((kv: any) => kv[1].toJSON())152        .sort((a: any, b: any) => a.constData.localeCompare(b.constData));153      expect(tokensAfter).to.be.deep.equal([154        {155          Owner: bob.address,156          //ConstData: '0x010203',157        },158        {159          Owner: bob.address,160          //ConstData: '0x010204',161        },162        {163          Owner: bob.address,164          //ConstData: '0x010205',165        },166      ]);167    });168  });169170  it('Approve CE', async () => {171    await usingApi(async (api, privateKeyWrapper) => {172      const alice = privateKeyWrapper('//Alice');173      const bob = privateKeyWrapper('//Bob');174      const charlie = privateKeyWrapper('//Charlie');175176      const collectionId = await createCollectionExpectSuccess();177      const [contract] = await deployTransferContract(api, privateKeyWrapper);178      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', contract.address.toString());179180      const transferTx = contract.tx.approve({value, gasLimit}, bob.address, collectionId, tokenId, 1);181      const events = await submitTransactionAsync(alice, transferTx);182      const result = getGenericResult(events);183      expect(result.success).to.be.true;184185      await transferFromExpectSuccess(collectionId, tokenId, bob, normalizeAccountId(contract.address.toString()), charlie, 1, 'NFT');186    });187  });188189  it('TransferFrom CE', async () => {190    await usingApi(async (api, privateKeyWrapper) => {191      const alice = privateKeyWrapper('//Alice');192      const bob = privateKeyWrapper('//Bob');193      const charlie = privateKeyWrapper('//Charlie');194195      const collectionId = await createCollectionExpectSuccess();196      const [contract] = await deployTransferContract(api, privateKeyWrapper);197      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', bob.address);198      await approveExpectSuccess(collectionId, tokenId, bob, contract.address.toString(), 1);199200      const transferTx = contract.tx.transferFrom({value, gasLimit}, bob.address, charlie.address, collectionId, tokenId, 1);201      const events = await submitTransactionAsync(alice, transferTx);202      const result = getGenericResult(events);203      expect(result.success).to.be.true;204205      const token: any = (await api.query.unique.nftItemList(collectionId, tokenId) as any).unwrap();206      expect(token.owner.toString()).to.be.equal(charlie.address);207    });208  });209210  it('ToggleAllowList CE', async () => {211    await usingApi(async (api, privateKeyWrapper) => {212      const alice = privateKeyWrapper('//Alice');213      const bob = privateKeyWrapper('//Bob');214215      const collectionId = await createCollectionExpectSuccess();216      const [contract] = await deployTransferContract(api, privateKeyWrapper);217      const changeAdminTx = api.tx.unique.addCollectionAdmin(collectionId, contract.address);218      await submitTransactionAsync(alice, changeAdminTx);219220      expect(await isAllowlisted(api, collectionId, bob.address)).to.be.false;221222      {223        const transferTx = contract.tx.toggleAllowList({value, gasLimit}, collectionId, bob.address, true);224        const events = await submitTransactionAsync(alice, transferTx);225        const result = getGenericResult(events);226        expect(result.success).to.be.true;227228        expect(await isAllowlisted(api, collectionId, bob.address)).to.be.true;229      }230      {231        const transferTx = contract.tx.toggleAllowList({value, gasLimit}, collectionId, bob.address, false);232        const events = await submitTransactionAsync(alice, transferTx);233        const result = getGenericResult(events);234        expect(result.success).to.be.true;235236        expect(await isAllowlisted(api, collectionId, bob.address)).to.be.false;237      }238    });239  });240});