123456import 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 63 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);67 68 69 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);7374 75 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});