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

difftreelog

test basic tests for CE

Yaroslav Bolyukin2021-06-04parent: #a0285b6.patch.diff
in: master

1 file changed

modifiedtests/src/contracts.test.tsdiffbeforeafterboth
before · tests/src/contracts.test.ts
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, submitTransactionExpectFailAsync } 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  createCollectionExpectSuccess,20  createItemExpectSuccess,21  getGenericResult22} from "./util/helpers";232425chai.use(chaiAsPromised);26const expect = chai.expect;2728const value = 0;29const gasLimit = 3000n * 1000000n;30const marketContractAddress = '5CYN9j3YvRkqxewoxeSvRbhAym4465C57uMmX5j4yz99L5H6';3132describe('Contracts', () => {33  it(`Can deploy smart contract Flipper, instantiate it and call it's get and flip messages.`, async () => {34    await usingApi(async api => {35      const [contract, deployer] = await deployFlipper(api);36      const initialGetResponse = await getFlipValue(contract, deployer);3738      const bob = privateKey("//Bob");39      const flip = contract.tx.flip(value, gasLimit);40      await submitTransactionAsync(bob, flip);4142      const afterFlipGetResponse = await getFlipValue(contract, deployer);43      expect(afterFlipGetResponse).not.to.be.eq(initialGetResponse, 'Flipping should change value.');44    });45  });4647  it('Can initialize contract instance', async () => {48    await usingApi(async (api) => {49      const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));50      const abi = new Abi(metadata);51      const newContractInstance = new Contract(api, abi, marketContractAddress);52      expect(newContractInstance).to.have.property('address');53      expect(newContractInstance.address.toString()).to.equal(marketContractAddress);54    });55  });5657  it('Can transfer NFT using smart contract.', async () => {58    await usingApi(async api => {59      const alice = privateKey("//Alice");60      const bob = privateKey("//Bob");6162      // Prep work63      const collectionId = await createCollectionExpectSuccess();64      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');65      const [contract, deployer] = await deployTransferContract(api);66      const tokenBefore: any = (await api.query.nft.nftItemList(collectionId, tokenId) as any).unwrap();67      68      // Transfer69      const transferTx = contract.tx.transfer(value, gasLimit, bob.address, collectionId, tokenId, 1);70      const events = await submitTransactionAsync(alice, transferTx);71      const result = getGenericResult(events);72      const tokenAfter: any = (await api.query.nft.nftItemList(collectionId, tokenId) as any).unwrap();7374      // tslint:disable-next-line:no-unused-expression75      expect(result.success).to.be.true;76      expect(tokenBefore.Owner.toString()).to.be.equal(alice.address);77      expect(tokenAfter.Owner.toString()).to.be.equal(bob.address);78    });79  });80});
after · tests/src/contracts.test.ts
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, submitTransactionExpectFailAsync } 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  addToWhiteListExpectSuccess,20  approveExpectSuccess,21  createCollectionExpectSuccess,22  createItemExpectSuccess,23  enablePublicMintingExpectSuccess,24  enableWhiteListExpectSuccess,25  getGenericResult,26  isWhitelisted,27  transferFromExpectSuccess28} from "./util/helpers";293031chai.use(chaiAsPromised);32const expect = chai.expect;3334const value = 0;35const gasLimit = 9000n * 1000000n;36const marketContractAddress = '5CYN9j3YvRkqxewoxeSvRbhAym4465C57uMmX5j4yz99L5H6';3738describe('Contracts', () => {39  it(`Can deploy smart contract Flipper, instantiate it and call it's get and flip messages.`, async () => {40    await usingApi(async api => {41      const [contract, deployer] = await deployFlipper(api);42      const initialGetResponse = await getFlipValue(contract, deployer);4344      const bob = privateKey("//Bob");45      const flip = contract.tx.flip(value, gasLimit);46      await submitTransactionAsync(bob, flip);4748      const afterFlipGetResponse = await getFlipValue(contract, deployer);49      expect(afterFlipGetResponse).not.to.be.eq(initialGetResponse, 'Flipping should change value.');50    });51  });5253  it('Can initialize contract instance', async () => {54    await usingApi(async (api) => {55      const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));56      const abi = new Abi(metadata);57      const newContractInstance = new Contract(api, abi, marketContractAddress);58      expect(newContractInstance).to.have.property('address');59      expect(newContractInstance.address.toString()).to.equal(marketContractAddress);60    });61  });62});6364describe.only('Chain extensions', () => {65  it('Transfer CE', async () => {66    await usingApi(async api => {67      const alice = privateKey("//Alice");68      const bob = privateKey("//Bob");6970      // Prep work71      const collectionId = await createCollectionExpectSuccess();72      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');73      const [contract, deployer] = await deployTransferContract(api);74      const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, contract.address);75      await submitTransactionAsync(alice, changeAdminTx);7677      const tokenBefore: any = (await api.query.nft.nftItemList(collectionId, tokenId) as any).unwrap();78      79      // Transfer80      const transferTx = contract.tx.transfer(value, gasLimit, bob.address, collectionId, tokenId, 1);81      const events = await submitTransactionAsync(alice, transferTx);82      const result = getGenericResult(events);83      const tokenAfter: any = (await api.query.nft.nftItemList(collectionId, tokenId) as any).unwrap();8485      // tslint:disable-next-line:no-unused-expression86      expect(result.success).to.be.true;87      expect(tokenBefore.Owner.toString()).to.be.equal(alice.address);88      expect(tokenAfter.Owner.toString()).to.be.equal(bob.address);89    });90  });9192  it('Mint CE', async () => {93    await usingApi(async api => {94      const alice = privateKey('//Alice');95      const bob = privateKey('//Bob');9697      const collectionId = await createCollectionExpectSuccess();98      const [contract, deployer] = await deployTransferContract(api);99      await enablePublicMintingExpectSuccess(alice, collectionId);100      await enableWhiteListExpectSuccess(alice, collectionId);101      await addToWhiteListExpectSuccess(alice, collectionId, contract.address);102      await addToWhiteListExpectSuccess(alice, collectionId, bob.address);103104      const transferTx = contract.tx.createItem(value, gasLimit, bob.address, collectionId, { Nft: {const_data: '0x010203', variable_data: '0x020304' }});105      const events = await submitTransactionAsync(alice, transferTx);106      const result = getGenericResult(events);107      expect(result.success).to.be.true;108109      const tokensAfter: any = (await api.query.nft.nftItemList.entries(collectionId) as any).map((kv: any) => kv[1].toJSON());110      expect(tokensAfter).to.be.deep.equal([111        {112          Owner: bob.address,113          ConstData: '0x010203',114          VariableData: '0x020304',115        },116      ]);117    });118  });119120  it('Bulk mint CE', async () => {121    await usingApi(async api => {122      const alice = privateKey('//Alice');123      const bob = privateKey('//Bob');124125      const collectionId = await createCollectionExpectSuccess();126      const [contract, deployer] = await deployTransferContract(api);127      await enablePublicMintingExpectSuccess(alice, collectionId);128      await enableWhiteListExpectSuccess(alice, collectionId);129      await addToWhiteListExpectSuccess(alice, collectionId, contract.address);130      await addToWhiteListExpectSuccess(alice, collectionId, bob.address);131132      const transferTx = contract.tx.createMultipleItems(value, gasLimit, bob.address, collectionId, [133        { Nft: { const_data: '0x010203', variable_data: '0x020304' } },134        { Nft: { const_data: '0x010204', variable_data: '0x020305' } },135        { Nft: { const_data: '0x010205', variable_data: '0x020306' } }136      ]);137      const events = await submitTransactionAsync(alice, transferTx);138      const result = getGenericResult(events);139      expect(result.success).to.be.true;140141      const tokensAfter: any = (await api.query.nft.nftItemList.entries(collectionId) as any)142        .map((kv: any) => kv[1].toJSON())143        .sort((a: any, b: any) => a.ConstData.localeCompare(b.ConstData));144      expect(tokensAfter).to.be.deep.equal([145        {146          Owner: bob.address,147          ConstData: '0x010203',148          VariableData: '0x020304',149        },150        {151          Owner: bob.address,152          ConstData: '0x010204',153          VariableData: '0x020305',154        },155        {156          Owner: bob.address,157          ConstData: '0x010205',158          VariableData: '0x020306',159        },160      ]);161    });162  });163164  it('Approve CE', async () => {165    await usingApi(async api => {166      const alice = privateKey('//Alice');167      const bob = privateKey('//Bob');168      const charlie = privateKey('//Charlie');169170      const collectionId = await createCollectionExpectSuccess();171      const [contract, deployer] = await deployTransferContract(api);172      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', contract.address);173174      const transferTx = contract.tx.approve(value, gasLimit, bob.address, collectionId, tokenId, 1);175      const events = await submitTransactionAsync(alice, transferTx);176      const result = getGenericResult(events);177      expect(result.success).to.be.true;178179      await transferFromExpectSuccess(collectionId, tokenId, bob, contract.address.toString(), charlie, 1, 'NFT');180    });181  });182183  it('TransferFrom CE', async () => {184    await usingApi(async api => {185      const alice = privateKey('//Alice');186      const bob = privateKey('//Bob');187      const charlie = privateKey('//Charlie');188189      const collectionId = await createCollectionExpectSuccess();190      const [contract, deployer] = await deployTransferContract(api);191      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', bob.address);192      await approveExpectSuccess(collectionId, tokenId, bob, contract.address.toString(), 1);193194      const transferTx = contract.tx.transferFrom(value, gasLimit, bob.address, charlie.address, collectionId, tokenId, 1);195      const events = await submitTransactionAsync(alice, transferTx);196      const result = getGenericResult(events);197      expect(result.success).to.be.true;198199      const token: any = (await api.query.nft.nftItemList(collectionId, tokenId) as any).unwrap()200      expect(token.Owner.toString()).to.be.equal(charlie.address);201    });202  });203204  it('SetVariableMetaData CE', async () => {205    await usingApi(async api => {206      const alice = privateKey('//Alice');207208      const collectionId = await createCollectionExpectSuccess();209      const [contract, deployer] = await deployTransferContract(api);210      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', contract.address);211212      const transferTx = contract.tx.setVariableMetaData(value, gasLimit, collectionId, tokenId, '0x121314');213      const events = await submitTransactionAsync(alice, transferTx);214      const result = getGenericResult(events);215      expect(result.success).to.be.true;216217      const token: any = (await api.query.nft.nftItemList(collectionId, tokenId) as any).unwrap()218      expect(token.VariableData.toString()).to.be.equal('0x121314');219    });220  });221222  it('ToggleWhiteList CE', async () => {223    await usingApi(async api => {224      const alice = privateKey('//Alice');225      const bob = privateKey('//Bob');226227      const collectionId = await createCollectionExpectSuccess();228      const [contract, deployer] = await deployTransferContract(api);229      const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, contract.address);230      await submitTransactionAsync(alice, changeAdminTx);      231232      expect(await isWhitelisted(collectionId, bob.address)).to.be.false;233234      {235        const transferTx = contract.tx.toggleWhiteList(value, gasLimit, collectionId, bob.address, true);236        const events = await submitTransactionAsync(alice, transferTx);237        const result = getGenericResult(events);238        expect(result.success).to.be.true;239240        expect(await isWhitelisted(collectionId, bob.address)).to.be.true;241      }242      {243        const transferTx = contract.tx.toggleWhiteList(value, gasLimit, collectionId, bob.address, false);244        const events = await submitTransactionAsync(alice, transferTx);245        const result = getGenericResult(events);246        expect(result.success).to.be.true;247248        expect(await isWhitelisted(collectionId, bob.address)).to.be.false;249      }250    });251  });252});