git.delta.rocks / unique-network / refs/commits / 96967bb10fde

difftreelog

source

tests/src/contracts.test.ts4.8 KiBsourcehistory
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 { ApiPromise } from "@polkadot/api";7import { expect } from "chai";8import { default as usingApi, submitTransactionAsync } from "./substrate/substrate-api";9import fs from "fs";10import { Abi, BlueprintPromise, CodePromise } from "@polkadot/api-contract";11import { IKeyringPair } from "@polkadot/types/types";12import { Keyring } from "@polkadot/api";13import { ApiTypes, SubmittableExtrinsic } from "@polkadot/api/types";14import { BigNumber } from 'bignumber.js';15import { findUnusedAddress } from './util/helpers'1617const value = 0;18const gasLimit = 3000n * 1000000n;19const endowment = `1000000000000000`;2021function deployBlueprint(alice: IKeyringPair, code: CodePromise): Promise<BlueprintPromise> {22  return new Promise<BlueprintPromise>(async (resolve, reject) => {23    const unsub = await code24      .createBlueprint()25      .signAndSend(alice, (result) => {26        if (result.status.isInBlock || result.status.isFinalized) {27          // here we have an additional field in the result, containing the blueprint28          resolve(result.blueprint);29          unsub();30        }31      })32  });33}3435function deployContract(alice: IKeyringPair, blueprint: BlueprintPromise) : Promise<any> {36  return new Promise<any>(async (resolve, reject) => {37    const initValue = true;3839    const unsub = await blueprint.tx40    .new(endowment, gasLimit, initValue)41    .signAndSend(alice, (result) => {42      if (result.status.isInBlock || result.status.isFinalized) {43        unsub();44        resolve(result);45      }46    });    47  });48}4950async function prepareDeployer(api: ApiPromise) {51  // Find unused address52  const deployer = await findUnusedAddress(api);5354  // Transfer balance to it55  const keyring = new Keyring({ type: 'sr25519' });56  const alice = keyring.addFromUri(`//Alice`);57  let amount = new BigNumber(endowment);58  amount = amount.plus(1e15);59  const tx = api.tx.balances.transfer(deployer.address, amount.toFixed());60  await submitTransactionAsync(alice, tx);6162  return deployer;63}6465describe('Contracts smoke test', () => {66  it(`Can deploy smart contract Flipper, instantiate it and call it's get and flip messages.`, async () => {67    await usingApi(async api => {68      const deployer = await prepareDeployer(api);69      70      const wasm = fs.readFileSync('./src/flipper/flipper.wasm');71      72      const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));73      const abi = new Abi(metadata);7475      const code = new CodePromise(api, abi, wasm);7677      const blueprint = await deployBlueprint(deployer, code);78      const contract = (await deployContract(deployer, blueprint))['contract'];7980      const getFlipValue = async () => {81        const result = await contract.query.get(deployer.address, value, gasLimit);8283        if(!result.result.isSuccess) {84          throw `Failed to get flipper value`;85        }86        return (result.result.asSuccess.data[0] == 0x00) ? false : true;87      }8889      const initialGetResponse = await getFlipValue();90      expect(initialGetResponse).to.be.true;9192      const flip = contract.exec('flip', value, gasLimit);93      await submitTransactionAsync(deployer, flip);9495      const afterFlipGetResponse = await getFlipValue();9697      expect(afterFlipGetResponse).to.be.false;98    });99  });100101  it.skip('Can transfer balance using smart contract.', async () => {102    await usingApi(async api => {103      // const [alicesBalanceBefore, bobsBalanceBefore] = await getBalance(api, [alicesPublicKey, bobsPublicKey]);104      // const wasm = fs.readFileSync('./src/balance-transfer-contract/calls.wasm');105      // const contract = compactAddLength(u8aToU8a(wasm));106107      // const metadata = JSON.parse(fs.readFileSync('./src/balance-transfer-contract/metadata.json').toString('utf-8'));108      // const abi = new Abi(api.registry as any, metadata);109110      // const alicesPrivateKey = privateKey('//Alice');111112      // const contractHash = await deployContract(api, contract, alicesPrivateKey);113114      // // const args = abi.constructors[0]();115      // const instanceAccountId = await instantiateContract(api, contractHash, /*args,*/ alicesPrivateKey);116      // const contractInstance = new ContractPromise(api, abi, instanceAccountId);117      // const bob = new GenericAccountId(api.registry, bobsPublicKey);118119      // const transfer = contractInstance.exec('balance_transfer', 0, 1000000000000n, [bob, new u128(api.registry, 1000000)]);120      // await submitTransactionAsync(alicesPrivateKey, transfer);121122      // const [alicesBalanceAfter, bobsBalanceAfter] = await getBalance(api, [alicesPublicKey, bobsPublicKey]);123124      // expect(alicesBalanceAfter < alicesBalanceBefore).to.be.true;125      // expect(bobsBalanceAfter > bobsBalanceBefore).to.be.true;126    });127  })128});