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

difftreelog

source

tests/src/util/contracthelpers.ts4.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, submitTransactionExpectFailAsync } 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, getGenericResult } from '../util/helpers';1819const value = 0;20const gasLimit = '200000000000';21const endowment = '100000000000000000';2223function deployContract(alice: IKeyringPair, code: CodePromise, constructor: string = 'default', ...args: any[]): Promise<Contract> {24  return new Promise<Contract>(async (resolve, reject) => {25    const unsub = await (code as any)26      .tx[constructor]({value: endowment, gasLimit}, ...args)27      .signAndSend(alice, (result: any) => {28        if (result.status.isInBlock || result.status.isFinalized) {29          // here we have an additional field in the result, containing the blueprint30          resolve((result as any).contract);31          unsub();32        }33      })34  });35}3637async function prepareDeployer(api: ApiPromise) {38  // Find unused address39  const deployer = await findUnusedAddress(api);4041  // Transfer balance to it42  const keyring = new Keyring({ type: 'sr25519' });43  const alice = keyring.addFromUri(`//Alice`);44  let amount = new BigNumber(endowment);45  amount = amount.plus(100e15);46  const tx = api.tx.balances.transfer(deployer.address, amount.toFixed());47  await submitTransactionAsync(alice, tx);4849  return deployer;50}5152export async function deployFlipper(api: ApiPromise): Promise<[Contract, IKeyringPair]> {53  const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));54  const abi = new Abi(metadata);5556  const deployer = await prepareDeployer(api);5758  const wasm = fs.readFileSync('./src/flipper/flipper.wasm');5960  const code = new CodePromise(api, abi, wasm);6162  const contract = (await deployContract(deployer, code, 'new', true)) as Contract;6364  const initialGetResponse = await getFlipValue(contract, deployer);65  expect(initialGetResponse).to.be.true;6667  return [contract, deployer];68}6970export async function getFlipValue(contract: Contract, deployer: IKeyringPair) {71  const result = await contract.query.get(deployer.address, value, gasLimit);7273  if(!result.result.isOk) {74    throw `Failed to get flipper value`;75  }76  return (result.result.asOk.data[0] == 0x00) ? false : true;77}7879export async function toggleFlipValueExpectSuccess(sender: IKeyringPair, contract: Contract) {80  const tx = contract.tx.flip(value, gasLimit);81  const events = await submitTransactionAsync(sender, tx);82  const result = getGenericResult(events);8384  expect(result.success).to.be.true;85}8687export async function toggleFlipValueExpectFailure(sender: IKeyringPair, contract: Contract) {88  const tx = contract.tx.flip(value, gasLimit);89  await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;90}9192function instantiateTransferContract(alice: IKeyringPair, blueprint: Blueprint) : Promise<any> {93  return new Promise<any>(async (resolve, reject) => {94    const unsub = await blueprint.tx95    .default(endowment, gasLimit)96    .signAndSend(alice, (result) => {97      if (result.status.isInBlock || result.status.isFinalized) {98        unsub();99        resolve(result);100      }101    });    102  });103}104105export async function deployTransferContract(api: ApiPromise): Promise<[Contract, IKeyringPair]> {106  const metadata = JSON.parse(fs.readFileSync('./src/transfer_contract/metadata.json').toString('utf-8'));107  const abi = new Abi(metadata);108109  const deployer = await prepareDeployer(api);110111  const wasm = fs.readFileSync('./src/transfer_contract/nft_transfer.wasm');112113  const code = new CodePromise(api, abi, wasm);114115  const contract = await deployContract(deployer, code);116117  return [contract, deployer];118}