1import { expect } from "chai";2import usingApi from "./substrate/substrate-api";3import fs from "fs";4import { Abi, BlueprintPromise, CodePromise, ContractPromise } from "@polkadot/api-contract";5import { IKeyringPair } from "@polkadot/types/types";6import { Keyring } from "@polkadot/api";7import { ApiTypes, SubmittableExtrinsic } from "@polkadot/api/types";89const value = 0;10const gasLimit = 3000n * 1000000n;11const marketContractAddress = '5CYN9j3YvRkqxewoxeSvRbhAym4465C57uMmX5j4yz99L5H6';1213function deployBlueprint(alice: IKeyringPair, code: CodePromise): Promise<BlueprintPromise> {14 return new Promise<BlueprintPromise>(async (resolve, reject) => {15 const unsub = await code16 .createBlueprint()17 .signAndSend(alice, (result) => {18 if (result.status.isInBlock || result.status.isFinalized) {19 20 resolve(result.blueprint);21 unsub();22 }23 })24 });25}2627function deployContract(alice: IKeyringPair, blueprint: BlueprintPromise) : Promise<any> {28 return new Promise<any>(async (resolve, reject) => {29 const endowment = 1000000000000000n;30 const initValue = true;3132 const unsub = await blueprint.tx33 .new(endowment, gasLimit, initValue)34 .signAndSend(alice, (result) => {35 if (result.status.isInBlock || result.status.isFinalized) {36 unsub();37 resolve(result);38 }39 }); 40 });41}4243function runTransaction(privateKey: IKeyringPair, extrinsic: SubmittableExtrinsic<ApiTypes>) {44 return new Promise<void>(async (resolve, reject) => {45 extrinsic.signAndSend(privateKey, async result => {46 if(!result.isInBlock) {47 return;48 }4950 if(result.findRecord('system', 'ExtrinsicSuccess')) {51 resolve();52 }53 else {54 reject('Failed to flip value.');55 }56 })57 });58}5960describe('Contracts', () => {61 it(`Can deploy smart contract Flipper, instantiate it and call it's get and flip messages.`, async () => {62 await usingApi(async api => {63 const keyring = new Keyring({ type: 'sr25519' });64 const alice = keyring.addFromUri("//Alice");65 66 const wasm = fs.readFileSync('./src/flipper/flipper.wasm');67 68 const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));69 const abi = new Abi(metadata);7071 const code = new CodePromise(api, abi, wasm);7273 const blueprint = await deployBlueprint(alice, code);74 const contract = (await deployContract(alice, blueprint))['contract'];7576 const getFlipValue = async () => {77 const result = await contract.query.get(alice.address, value, gasLimit);7879 if(!result.result.isSuccess) {80 throw `Failed to get flipper value`;81 }82 return (result.result.asSuccess.data[0] == 0x00) ? false : true;83 }8485 const initialGetResponse = await getFlipValue();86 expect(initialGetResponse).to.be.true;8788 const flip = contract.exec('flip', value, gasLimit);89 await runTransaction(alice, flip);9091 const afterFlipGetResponse = await getFlipValue();9293 expect(afterFlipGetResponse).to.be.false;94 });95 });9697 it('Can initialize contract instance', async () => {98 await usingApi(async (api) => {99 const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));100 const abi = new Abi(metadata);101 const newContractInstance = new ContractPromise(api, abi, marketContractAddress);102 expect(newContractInstance).to.have.property('address');103 expect(newContractInstance.address.toString()).to.equal(marketContractAddress);104 });105 });106107 it.skip('Can transfer balance using smart contract.', async () => {108 await usingApi(async api => {109 110 111 112113 114 115116 117118 119120 121 122 123 124125 126 127128 129130 131 132 });133 })134});