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 getGenericResult,22 normalizeAccountId23} from "./util/helpers";242526chai.use(chaiAsPromised);27const expect = chai.expect;2829const value = 0;30const gasLimit = 3000n * 1000000n;31const marketContractAddress = '5CYN9j3YvRkqxewoxeSvRbhAym4465C57uMmX5j4yz99L5H6';3233describe('Contracts', () => {34 it(`Can deploy smart contract Flipper, instantiate it and call it's get and flip messages.`, async () => {35 await usingApi(async api => {36 const [contract, deployer] = await deployFlipper(api);37 const initialGetResponse = await getFlipValue(contract, deployer);3839 const bob = privateKey("//Bob");40 const flip = contract.tx.flip(value, gasLimit);41 await submitTransactionAsync(bob, flip);4243 const afterFlipGetResponse = await getFlipValue(contract, deployer);44 expect(afterFlipGetResponse).not.to.be.eq(initialGetResponse, 'Flipping should change value.');45 });46 });4748 it('Can initialize contract instance', async () => {49 await usingApi(async (api) => {50 const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));51 const abi = new Abi(metadata);52 const newContractInstance = new Contract(api, abi, marketContractAddress);53 expect(newContractInstance).to.have.property('address');54 expect(newContractInstance.address.toString()).to.equal(marketContractAddress);55 });56 });5758 it('Can transfer NFT using smart contract.', async () => {59 await usingApi(async api => {60 const alice = privateKey("//Alice");61 const bob = privateKey("//Bob");6263 64 const collectionId = await createCollectionExpectSuccess();65 const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');66 const [contract, deployer] = await deployTransferContract(api);67 const tokenBefore: any = (await api.query.nft.nftItemList(collectionId, tokenId) as any).toJSON();68 69 70 const transferTx = contract.tx.transfer(value, gasLimit, bob.address, collectionId, tokenId, 1);71 const events = await submitTransactionAsync(alice, transferTx);72 const result = getGenericResult(events);73 const tokenAfter: any = (await api.query.nft.nftItemList(collectionId, tokenId) as any).toJSON();7475 76 expect(result.success).to.be.true;77 expect(tokenBefore.Owner).to.be.deep.equal(normalizeAccountId(alice.address));78 expect(tokenAfter.Owner).to.be.deep.equal(normalizeAccountId(bob.address));79 });80 });81});