git.delta.rocks / unique-network / refs/commits / 2b5c24e2aa92

difftreelog

source

tests/src/util/contracthelpers.ts3.6 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, 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 {findUnusedAddress, getGenericResult} from '../util/helpers';1718const value = 0;19const gasLimit = '200000000000';20const endowment = '100000000000000000';2122/* eslint no-async-promise-executor: "off" */23function deployContract(alice: IKeyringPair, code: CodePromise, constructor = 'default', ...args: any[]): Promise<Contract> {24  return new Promise<Contract>(async (resolve) => {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  const amount = BigInt(endowment) + 10n**15n;45  const tx = api.tx.balances.transfer(deployer.address, amount);46  await submitTransactionAsync(alice, tx);4748  return deployer;49}5051export async function deployFlipper(api: ApiPromise): Promise<[Contract, IKeyringPair]> {52  const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));53  const abi = new Abi(metadata);5455  const deployer = await prepareDeployer(api);5657  const wasm = fs.readFileSync('./src/flipper/flipper.wasm');5859  const code = new CodePromise(api, abi, wasm);6061  const contract = (await deployContract(deployer, code, 'new', true)) as Contract;6263  const initialGetResponse = await getFlipValue(contract, deployer);64  expect(initialGetResponse).to.be.true;6566  return [contract, deployer];67}6869export async function getFlipValue(contract: Contract, deployer: IKeyringPair) {70  const result = await contract.query.get(deployer.address, value, gasLimit);7172  if(!result.result.isOk) {73    throw 'Failed to get flipper value';74  }75  return (result.result.asOk.data[0] == 0x00) ? false : true;76}7778export async function toggleFlipValueExpectSuccess(sender: IKeyringPair, contract: Contract) {79  const tx = contract.tx.flip(value, gasLimit);80  const events = await submitTransactionAsync(sender, tx);81  const result = getGenericResult(events);8283  expect(result.success).to.be.true;84}8586export async function toggleFlipValueExpectFailure(sender: IKeyringPair, contract: Contract) {87  const tx = contract.tx.flip(value, gasLimit);88  await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;89}9091export async function deployTransferContract(api: ApiPromise): Promise<[Contract, IKeyringPair]> {92  const metadata = JSON.parse(fs.readFileSync('./src/transfer_contract/metadata.json').toString('utf-8'));93  const abi = new Abi(metadata);9495  const deployer = await prepareDeployer(api);9697  const wasm = fs.readFileSync('./src/transfer_contract/nft_transfer.wasm');9899  const code = new CodePromise(api, abi, wasm);100101  const contract = await deployContract(deployer, code);102103  return [contract, deployer];104}