git.delta.rocks / unique-network / refs/commits / 55ff5a901dfa

difftreelog

source

tests/src/util/contracthelpers.ts3.1 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 chai from "chai";7import chaiAsPromised from 'chai-as-promised';8import { submitTransactionAsync } from "../substrate/substrate-api";9import fs from "fs";10import { Abi, BlueprintPromise as Blueprint, CodePromise, ContractPromise as Contract } from "@polkadot/api-contract";11import { IKeyringPair } from "@polkadot/types/types";12import { ApiPromise, Keyring } from "@polkadot/api";1314chai.use(chaiAsPromised);15const expect = chai.expect;16import { BigNumber } from 'bignumber.js';17import { findUnusedAddress } from '../util/helpers';1819const value = 0;20const gasLimit = 3000n * 1000000n;21const endowment = `1000000000000000`;2223function deployBlueprint(alice: IKeyringPair, code: CodePromise): Promise<Blueprint> {24  return new Promise<Blueprint>(async (resolve, reject) => {25    const unsub = await code26      .createBlueprint()27      .signAndSend(alice, (result) => {28        if (result.status.isInBlock || result.status.isFinalized) {29          // here we have an additional field in the result, containing the blueprint30          resolve(result.blueprint);31          unsub();32        }33      })34  });35}3637function deployContract(alice: IKeyringPair, blueprint: Blueprint) : Promise<any> {38  return new Promise<any>(async (resolve, reject) => {39    const endowment = 1000000000000000n;40    const initValue = true;4142    const unsub = await blueprint.tx43    .new(endowment, gasLimit, initValue)44    .signAndSend(alice, (result) => {45      if (result.status.isInBlock || result.status.isFinalized) {46        unsub();47        resolve(result);48      }49    });    50  });51}5253async function prepareDeployer(api: ApiPromise) {54  // Find unused address55  const deployer = await findUnusedAddress(api);5657  // Transfer balance to it58  const keyring = new Keyring({ type: 'sr25519' });59  const alice = keyring.addFromUri(`//Alice`);60  let amount = new BigNumber(endowment);61  amount = amount.plus(1e15);62  const tx = api.tx.balances.transfer(deployer.address, amount.toFixed());63  await submitTransactionAsync(alice, tx);6465  return deployer;66}6768export async function deployFlipper(api: ApiPromise): Promise<[Contract, IKeyringPair]> {69  const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));70  const abi = new Abi(metadata);7172  const deployer = await prepareDeployer(api);7374  const wasm = fs.readFileSync('./src/flipper/flipper.wasm');7576  const code = new CodePromise(api, abi, wasm);7778  const blueprint = await deployBlueprint(deployer, code);79  const contract = (await deployContract(deployer, blueprint))['contract'] as Contract;8081  const initialGetResponse = await getFlipValue(contract, deployer);82  expect(initialGetResponse).to.be.true;8384  return [contract, deployer];85}8687export async function getFlipValue(contract: Contract, deployer: IKeyringPair) {88  const result = await contract.query.get(deployer.address, value, gasLimit);8990  if(!result.result.isSuccess) {91    throw `Failed to get flipper value`;92  }93  return (result.result.asSuccess.data[0] == 0x00) ? false : true;94}