git.delta.rocks / unique-network / refs/commits / 5b368f35ddd7

difftreelog

source

tests/src/contracts.test.ts9.8 KiBsourcehistory
1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//56import chai from 'chai';7import chaiAsPromised from 'chai-as-promised';8import usingApi, {submitTransactionAsync} from './substrate/substrate-api';9import fs from 'fs';10import {Abi, ContractPromise as Contract} from '@polkadot/api-contract';11import privateKey from './substrate/privateKey';12import {13  deployFlipper,14  getFlipValue,15  deployTransferContract,16} from './util/contracthelpers';1718import {19  addToAllowListExpectSuccess,20  approveExpectSuccess,21  createCollectionExpectSuccess,22  createItemExpectSuccess,23  enablePublicMintingExpectSuccess,24  enableAllowListExpectSuccess,25  getGenericResult,26  normalizeAccountId,27  isAllowlisted,28  transferFromExpectSuccess,29  getTokenOwner,30} from './util/helpers';313233chai.use(chaiAsPromised);34const expect = chai.expect;3536const value = 0;37const gasLimit = 9000n * 1000000n;38const marketContractAddress = '5CYN9j3YvRkqxewoxeSvRbhAym4465C57uMmX5j4yz99L5H6';3940describe.skip('Contracts', () => {41  it('Can deploy smart contract Flipper, instantiate it and call it\'s get and flip messages.', async () => {42    await usingApi(async api => {43      const [contract, deployer] = await deployFlipper(api);44      const initialGetResponse = await getFlipValue(contract, deployer);4546      const bob = privateKey('//Bob');47      const flip = contract.tx.flip(value, gasLimit);48      await submitTransactionAsync(bob, flip);4950      const afterFlipGetResponse = await getFlipValue(contract, deployer);51      expect(afterFlipGetResponse).not.to.be.eq(initialGetResponse, 'Flipping should change value.');52    });53  });5455  it('Can initialize contract instance', async () => {56    await usingApi(async (api) => {57      const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));58      const abi = new Abi(metadata);59      const newContractInstance = new Contract(api, abi, marketContractAddress);60      expect(newContractInstance).to.have.property('address');61      expect(newContractInstance.address.toString()).to.equal(marketContractAddress);62    });63  });64});6566describe.skip('Chain extensions', () => {67  it('Transfer CE', async () => {68    await usingApi(async api => {69      const alice = privateKey('//Alice');70      const bob = privateKey('//Bob');7172      // Prep work73      const collectionId = await createCollectionExpectSuccess();74      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');75      const [contract] = await deployTransferContract(api);76      const changeAdminTx = api.tx.unique.addCollectionAdmin(collectionId, contract.address);77      await submitTransactionAsync(alice, changeAdminTx);7879      expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(normalizeAccountId(alice.address));8081      // Transfer82      const transferTx = contract.tx.transfer(value, gasLimit, bob.address, collectionId, tokenId, 1);83      const events = await submitTransactionAsync(alice, transferTx);84      const result = getGenericResult(events);85      expect(result.success).to.be.true;8687      expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(normalizeAccountId(bob.address));88    });89  });9091  it('Mint CE', async () => {92    await usingApi(async api => {93      const alice = privateKey('//Alice');94      const bob = privateKey('//Bob');9596      const collectionId = await createCollectionExpectSuccess();97      const [contract] = await deployTransferContract(api);98      await enablePublicMintingExpectSuccess(alice, collectionId);99      await enableAllowListExpectSuccess(alice, collectionId);100      await addToAllowListExpectSuccess(alice, collectionId, contract.address);101      await addToAllowListExpectSuccess(alice, collectionId, bob.address);102103      const transferTx = contract.tx.createItem(value, gasLimit, bob.address, collectionId, {Nft: {const_data: '0x010203', variable_data: '0x020304'}});104      const events = await submitTransactionAsync(alice, transferTx);105      const result = getGenericResult(events);106      expect(result.success).to.be.true;107108      const tokensAfter = (await api.query.unique.nftItemList.entries(collectionId)).map((kv: any) => kv[1].toJSON());109      expect(tokensAfter).to.be.deep.equal([110        {111          owner: bob.address,112          constData: '0x010203',113          variableData: '0x020304',114        },115      ]);116    });117  });118119  it('Bulk mint CE', async () => {120    await usingApi(async api => {121      const alice = privateKey('//Alice');122      const bob = privateKey('//Bob');123124      const collectionId = await createCollectionExpectSuccess();125      const [contract] = await deployTransferContract(api);126      await enablePublicMintingExpectSuccess(alice, collectionId);127      await enableAllowListExpectSuccess(alice, collectionId);128      await addToAllowListExpectSuccess(alice, collectionId, contract.address);129      await addToAllowListExpectSuccess(alice, collectionId, bob.address);130131      const transferTx = contract.tx.createMultipleItems(value, gasLimit, bob.address, collectionId, [132        {Nft: {const_data: '0x010203', variable_data: '0x020304'}},133        {Nft: {const_data: '0x010204', variable_data: '0x020305'}},134        {Nft: {const_data: '0x010205', variable_data: '0x020306'}},135      ]);136      const events = await submitTransactionAsync(alice, transferTx);137      const result = getGenericResult(events);138      expect(result.success).to.be.true;139140      const tokensAfter: any = (await api.query.unique.nftItemList.entries(collectionId) as any)141        .map((kv: any) => kv[1].toJSON())142        .sort((a: any, b: any) => a.constData.localeCompare(b.constData));143      expect(tokensAfter).to.be.deep.equal([144        {145          Owner: bob.address,146          ConstData: '0x010203',147          VariableData: '0x020304',148        },149        {150          Owner: bob.address,151          ConstData: '0x010204',152          VariableData: '0x020305',153        },154        {155          Owner: bob.address,156          ConstData: '0x010205',157          VariableData: '0x020306',158        },159      ]);160    });161  });162163  it('Approve CE', async () => {164    await usingApi(async api => {165      const alice = privateKey('//Alice');166      const bob = privateKey('//Bob');167      const charlie = privateKey('//Charlie');168169      const collectionId = await createCollectionExpectSuccess();170      const [contract] = await deployTransferContract(api);171      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', contract.address.toString());172173      const transferTx = contract.tx.approve(value, gasLimit, bob.address, collectionId, tokenId, 1);174      const events = await submitTransactionAsync(alice, transferTx);175      const result = getGenericResult(events);176      expect(result.success).to.be.true;177178      await transferFromExpectSuccess(collectionId, tokenId, bob, normalizeAccountId(contract.address.toString()), charlie, 1, 'NFT');179    });180  });181182  it('TransferFrom CE', async () => {183    await usingApi(async api => {184      const alice = privateKey('//Alice');185      const bob = privateKey('//Bob');186      const charlie = privateKey('//Charlie');187188      const collectionId = await createCollectionExpectSuccess();189      const [contract] = await deployTransferContract(api);190      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', bob.address);191      await approveExpectSuccess(collectionId, tokenId, bob, contract.address.toString(), 1);192193      const transferTx = contract.tx.transferFrom(value, gasLimit, bob.address, charlie.address, collectionId, tokenId, 1);194      const events = await submitTransactionAsync(alice, transferTx);195      const result = getGenericResult(events);196      expect(result.success).to.be.true;197198      const token: any = (await api.query.unique.nftItemList(collectionId, tokenId) as any).unwrap();199      expect(token.owner.toString()).to.be.equal(charlie.address);200    });201  });202203  it('SetVariableMetaData CE', async () => {204    await usingApi(async api => {205      const alice = privateKey('//Alice');206207      const collectionId = await createCollectionExpectSuccess();208      const [contract] = await deployTransferContract(api);209      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', contract.address.toString());210211      const transferTx = contract.tx.setVariableMetaData(value, gasLimit, collectionId, tokenId, '0x121314');212      const events = await submitTransactionAsync(alice, transferTx);213      const result = getGenericResult(events);214      expect(result.success).to.be.true;215216      const token: any = (await api.query.unique.nftItemList(collectionId, tokenId) as any).unwrap();217      expect(token.variableData.toString()).to.be.equal('0x121314');218    });219  });220221  it('ToggleAllowList CE', async () => {222    await usingApi(async api => {223      const alice = privateKey('//Alice');224      const bob = privateKey('//Bob');225226      const collectionId = await createCollectionExpectSuccess();227      const [contract] = await deployTransferContract(api);228      const changeAdminTx = api.tx.unique.addCollectionAdmin(collectionId, contract.address);229      await submitTransactionAsync(alice, changeAdminTx);230231      expect(await isAllowlisted(api, collectionId, bob.address)).to.be.false;232233      {234        const transferTx = contract.tx.toggleAllowList(value, gasLimit, collectionId, bob.address, true);235        const events = await submitTransactionAsync(alice, transferTx);236        const result = getGenericResult(events);237        expect(result.success).to.be.true;238239        expect(await isAllowlisted(api, collectionId, bob.address)).to.be.true;240      }241      {242        const transferTx = contract.tx.toggleAllowList(value, gasLimit, collectionId, bob.address, false);243        const events = await submitTransactionAsync(alice, transferTx);244        const result = getGenericResult(events);245        expect(result.success).to.be.true;246247        expect(await isAllowlisted(api, collectionId, bob.address)).to.be.false;248      }249    });250  });251});