git.delta.rocks / unique-network / refs/commits / b5214e36e279

difftreelog

source

tests/src/util/contracthelpers.ts3.4 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 = '200000000000';21const endowment = '100000000000000000';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 initValue = true;40    const constructorIndex = 0;4142    // const unsub = await blueprint43    // .createContract(constructorIndex, { gasLimit: gasLimit, salt: null, value: endowment }, initValue)4445    // const unsub = await blueprint.tx46    // .new({ gasLimit: gasLimit, salt: null, value: endowment }, initValue)4748    const unsub = await blueprint.tx49    .new(endowment, gasLimit, initValue)50    .signAndSend(alice, (result) => {51      if (result.status.isInBlock || result.status.isFinalized) {5253        console.log("Contract deployed: ", result);5455        unsub();56        resolve(result);57      }58    });    59  });60}6162async function prepareDeployer(api: ApiPromise) {63  // Find unused address64  const deployer = await findUnusedAddress(api);6566  // Transfer balance to it67  const keyring = new Keyring({ type: 'sr25519' });68  const alice = keyring.addFromUri(`//Alice`);69  let amount = new BigNumber(endowment);70  amount = amount.plus(1e15);71  const tx = api.tx.balances.transfer(deployer.address, amount.toFixed());72  await submitTransactionAsync(alice, tx);7374  return deployer;75}7677export async function deployFlipper(api: ApiPromise): Promise<[Contract, IKeyringPair]> {78  const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));79  const abi = new Abi(metadata);8081  const deployer = await prepareDeployer(api);8283  const wasm = fs.readFileSync('./src/flipper/flipper.wasm');8485  const code = new CodePromise(api, abi, wasm);8687  const blueprint = await deployBlueprint(deployer, code);88  const contract = (await deployContract(deployer, blueprint))['contract'] as Contract;8990  const initialGetResponse = await getFlipValue(contract, deployer);91  expect(initialGetResponse).to.be.true;9293  return [contract, deployer];94}9596export async function getFlipValue(contract: Contract, deployer: IKeyringPair) {97  const result = await contract.query.get(deployer.address, value, gasLimit);9899  if(!result.result.isOk) {100    throw `Failed to get flipper value`;101  }102  return (result.result.asOk.data[0] == 0x00) ? false : true;103}