git.delta.rocks / unique-network / refs/commits / 2df3f6b6e145

difftreelog

source

tests/src/.outdated/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 {deployFlipper, getFlipValue, deployTransferContract} from '../deprecated-helpers/contracthelpers';2324import {25  addToAllowListExpectSuccess,26  approveExpectSuccess,27  createCollectionExpectSuccess,28  createItemExpectSuccess,29  enablePublicMintingExpectSuccess,30  enableAllowListExpectSuccess,31  getGenericResult,32  normalizeAccountId,33  isAllowlisted,34  transferFromExpectSuccess,35  getTokenOwner,36} from '../deprecated-helpers/helpers';373839chai.use(chaiAsPromised);40const expect = chai.expect;4142const value = 0;43const gasLimit = 9000n * 1000000n;44const marketContractAddress = '5CYN9j3YvRkqxewoxeSvRbhAym4465C57uMmX5j4yz99L5H6';4546// todo:playgrounds skipped ~ postponed47describe.skip('Contracts', () => {48  it('Can deploy smart contract Flipper, instantiate it and call it\'s get and flip messages.', async () => {49    await usingApi(async (api, privateKeyWrapper) => {50      const [contract, deployer] = await deployFlipper(api, privateKeyWrapper);51      const initialGetResponse = await getFlipValue(contract, deployer);5253      const bob = privateKeyWrapper('//Bob');54      const flip = contract.tx.flip({value, gasLimit});55      await submitTransactionAsync(bob, flip);5657      const afterFlipGetResponse = await getFlipValue(contract, deployer);58      expect(afterFlipGetResponse).not.to.be.eq(initialGetResponse, 'Flipping should change value.');59    });60  });6162  it('Can initialize contract instance', async () => {63    await usingApi(async (api) => {64      const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));65      const abi = new Abi(metadata);66      const newContractInstance = new Contract(api, abi, marketContractAddress);67      expect(newContractInstance).to.have.property('address');68      expect(newContractInstance.address.toString()).to.equal(marketContractAddress);69    });70  });71});7273describe.skip('Chain extensions', () => {74  it('Transfer CE', async () => {75    await usingApi(async (api, privateKeyWrapper) => {76      const alice = privateKeyWrapper('//Alice');77      const bob = privateKeyWrapper('//Bob');7879      // Prep work80      const collectionId = await createCollectionExpectSuccess();81      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');82      const [contract] = await deployTransferContract(api, privateKeyWrapper);83      const changeAdminTx = api.tx.unique.addCollectionAdmin(collectionId, contract.address);84      await submitTransactionAsync(alice, changeAdminTx);8586      expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(normalizeAccountId(alice.address));8788      // Transfer89      const transferTx = contract.tx.transfer({value, gasLimit}, bob.address, collectionId, tokenId, 1);90      const events = await submitTransactionAsync(alice, transferTx);91      const result = getGenericResult(events);92      expect(result.success).to.be.true;9394      expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(normalizeAccountId(bob.address));95    });96  });9798  it('Mint CE', async () => {99    await usingApi(async (api, privateKeyWrapper) => {100      const alice = privateKeyWrapper('//Alice');101      const bob = privateKeyWrapper('//Bob');102103      const collectionId = await createCollectionExpectSuccess();104      const [contract] = await deployTransferContract(api, privateKeyWrapper);105      await enablePublicMintingExpectSuccess(alice, collectionId);106      await enableAllowListExpectSuccess(alice, collectionId);107      await addToAllowListExpectSuccess(alice, collectionId, contract.address);108      await addToAllowListExpectSuccess(alice, collectionId, bob.address);109110      const transferTx = contract.tx.createItem({value, gasLimit}, bob.address, collectionId, {Nft: {const_data: '0x010203'}});111      const events = await submitTransactionAsync(alice, transferTx);112      const result = getGenericResult(events);113      expect(result.success).to.be.true;114115      const tokensAfter = (await api.query.unique.nftItemList.entries(collectionId)).map((kv: any) => kv[1].toJSON());116      expect(tokensAfter).to.be.deep.equal([117        {118          owner: bob.address,119          constData: '0x010203',120        },121      ]);122    });123  });124125  it('Bulk mint CE', async () => {126    await usingApi(async (api, privateKeyWrapper) => {127      const alice = privateKeyWrapper('//Alice');128      const bob = privateKeyWrapper('//Bob');129130      const collectionId = await createCollectionExpectSuccess();131      const [contract] = await deployTransferContract(api, privateKeyWrapper);132      await enablePublicMintingExpectSuccess(alice, collectionId);133      await enableAllowListExpectSuccess(alice, collectionId);134      await addToAllowListExpectSuccess(alice, collectionId, contract.address);135      await addToAllowListExpectSuccess(alice, collectionId, bob.address);136137      const transferTx = contract.tx.createMultipleItems({value, gasLimit}, bob.address, collectionId, [138        {NFT: {/*const_data: '0x010203'*/}},139        {NFT: {/*const_data: '0x010204'*/}},140        {NFT: {/*const_data: '0x010205'*/}},141      ]);142      const events = await submitTransactionAsync(alice, transferTx);143      const result = getGenericResult(events);144      expect(result.success).to.be.true;145146      const tokensAfter: any = (await api.query.unique.nftItemList.entries(collectionId) as any)147        .map((kv: any) => kv[1].toJSON())148        .sort((a: any, b: any) => a.constData.localeCompare(b.constData));149      expect(tokensAfter).to.be.deep.equal([150        {151          Owner: bob.address,152          //ConstData: '0x010203',153        },154        {155          Owner: bob.address,156          //ConstData: '0x010204',157        },158        {159          Owner: bob.address,160          //ConstData: '0x010205',161        },162      ]);163    });164  });165166  it('Approve CE', async () => {167    await usingApi(async (api, privateKeyWrapper) => {168      const alice = privateKeyWrapper('//Alice');169      const bob = privateKeyWrapper('//Bob');170      const charlie = privateKeyWrapper('//Charlie');171172      const collectionId = await createCollectionExpectSuccess();173      const [contract] = await deployTransferContract(api, privateKeyWrapper);174      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', contract.address.toString());175176      const transferTx = contract.tx.approve({value, gasLimit}, bob.address, collectionId, tokenId, 1);177      const events = await submitTransactionAsync(alice, transferTx);178      const result = getGenericResult(events);179      expect(result.success).to.be.true;180181      await transferFromExpectSuccess(collectionId, tokenId, bob, normalizeAccountId(contract.address.toString()), charlie, 1, 'NFT');182    });183  });184185  it('TransferFrom CE', async () => {186    await usingApi(async (api, privateKeyWrapper) => {187      const alice = privateKeyWrapper('//Alice');188      const bob = privateKeyWrapper('//Bob');189      const charlie = privateKeyWrapper('//Charlie');190191      const collectionId = await createCollectionExpectSuccess();192      const [contract] = await deployTransferContract(api, privateKeyWrapper);193      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', bob.address);194      await approveExpectSuccess(collectionId, tokenId, bob, contract.address.toString(), 1);195196      const transferTx = contract.tx.transferFrom({value, gasLimit}, bob.address, charlie.address, collectionId, tokenId, 1);197      const events = await submitTransactionAsync(alice, transferTx);198      const result = getGenericResult(events);199      expect(result.success).to.be.true;200201      const token: any = (await api.query.unique.nftItemList(collectionId, tokenId) as any).unwrap();202      expect(token.owner.toString()).to.be.equal(charlie.address);203    });204  });205206  it('ToggleAllowList CE', async () => {207    await usingApi(async (api, privateKeyWrapper) => {208      const alice = privateKeyWrapper('//Alice');209      const bob = privateKeyWrapper('//Bob');210211      const collectionId = await createCollectionExpectSuccess();212      const [contract] = await deployTransferContract(api, privateKeyWrapper);213      const changeAdminTx = api.tx.unique.addCollectionAdmin(collectionId, contract.address);214      await submitTransactionAsync(alice, changeAdminTx);215216      expect(await isAllowlisted(api, collectionId, bob.address)).to.be.false;217218      {219        const transferTx = contract.tx.toggleAllowList({value, gasLimit}, collectionId, bob.address, true);220        const events = await submitTransactionAsync(alice, transferTx);221        const result = getGenericResult(events);222        expect(result.success).to.be.true;223224        expect(await isAllowlisted(api, collectionId, bob.address)).to.be.true;225      }226      {227        const transferTx = contract.tx.toggleAllowList({value, gasLimit}, collectionId, bob.address, false);228        const events = await submitTransactionAsync(alice, transferTx);229        const result = getGenericResult(events);230        expect(result.success).to.be.true;231232        expect(await isAllowlisted(api, collectionId, bob.address)).to.be.false;233      }234    });235  });236});