git.delta.rocks / unique-network / refs/commits / ffe7ca258082

difftreelog

source

tests/src/contracts.test.ts2.9 KiBsourcehistory
1import chai from "chai";2import chaiAsPromised from 'chai-as-promised';3import usingApi, { submitTransactionAsync, submitTransactionExpectFailAsync } from "./substrate/substrate-api";4import fs from "fs";5import { Abi, ContractPromise as Contract } from "@polkadot/api-contract";6import privateKey from "./substrate/privateKey";7import {8  deployFlipper,9  getFlipValue,10  deployTransferContract,11} from "./util/contracthelpers";1213import {14  createCollectionExpectSuccess,15  createItemExpectSuccess,16  getGenericResult17} from "./util/helpers";181920chai.use(chaiAsPromised);21const expect = chai.expect;2223const value = 0;24const gasLimit = 3000n * 1000000n;25const marketContractAddress = '5CYN9j3YvRkqxewoxeSvRbhAym4465C57uMmX5j4yz99L5H6';2627describe('Contracts', () => {28  it(`Can deploy smart contract Flipper, instantiate it and call it's get and flip messages.`, async () => {29    await usingApi(async api => {30      const [contract, deployer] = await deployFlipper(api);31      const initialGetResponse = await getFlipValue(contract, deployer);3233      const bob = privateKey("//Bob");34      const flip = contract.tx.flip(value, gasLimit);35      await submitTransactionAsync(bob, flip);3637      const afterFlipGetResponse = await getFlipValue(contract, deployer);38      expect(afterFlipGetResponse).not.to.be.eq(initialGetResponse, 'Flipping should change value.');39    });40  });4142  it('Can initialize contract instance', async () => {43    await usingApi(async (api) => {44      const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));45      const abi = new Abi(metadata);46      const newContractInstance = new Contract(api, abi, marketContractAddress);47      expect(newContractInstance).to.have.property('address');48      expect(newContractInstance.address.toString()).to.equal(marketContractAddress);49    });50  });5152  it('Can transfer NFT using smart contract.', async () => {53    await usingApi(async api => {54      const alice = privateKey("//Alice");55      const bob = privateKey("//Bob");5657      // Prep work58      const collectionId = await createCollectionExpectSuccess();59      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');60      const [contract, deployer] = await deployTransferContract(api);61      const tokenBefore: any = await api.query.nft.nftItemList(collectionId, tokenId);62      63      // Transfer64      const transferTx = contract.tx.transfer(value, gasLimit, bob.address, collectionId, tokenId, 1);65      const events = await submitTransactionAsync(alice, transferTx);66      const result = getGenericResult(events);67      const tokenAfter: any = await api.query.nft.nftItemList(collectionId, tokenId);6869      // tslint:disable-next-line:no-unused-expression70      expect(result.success).to.be.true;71      expect(tokenBefore.Owner.toString()).to.be.equal(alice.address);72      expect(tokenAfter.Owner.toString()).to.be.equal(bob.address);73    });74  });75});