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

difftreelog

source

tests/src/util/contracthelpers.ts4.4 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import chai from 'chai';18import chaiAsPromised from 'chai-as-promised';19import {submitTransactionAsync, submitTransactionExpectFailAsync} from '../substrate/substrate-api';20import fs from 'fs';21import {Abi, CodePromise, ContractPromise as Contract} from '@polkadot/api-contract';22import {IKeyringPair} from '@polkadot/types/types';23import {ApiPromise} from '@polkadot/api';2425chai.use(chaiAsPromised);26const expect = chai.expect;27import {findUnusedAddress, getGenericResult} from '../util/helpers';2829const value = 0;30const gasLimit = '200000000000';31const endowment = '100000000000000000';3233/* eslint no-async-promise-executor: "off" */34function deployContract(alice: IKeyringPair, code: CodePromise, constructor = 'default', ...args: any[]): Promise<Contract> {35  return new Promise<Contract>(async (resolve) => {36    const unsub = await (code as any)37      .tx[constructor]({value: endowment, gasLimit}, ...args)38      .signAndSend(alice, (result: any) => {39        if (result.status.isInBlock || result.status.isFinalized) {40          // here we have an additional field in the result, containing the blueprint41          resolve((result as any).contract);42          unsub();43        }44      });45  });46}4748async function prepareDeployer(api: ApiPromise, privateKeyWrapper: (account: string) => IKeyringPair) {49  // Find unused address50  const deployer = await findUnusedAddress(api, privateKeyWrapper);5152  // Transfer balance to it53  const alice = privateKeyWrapper('//Alice');54  const amount = BigInt(endowment) + 10n**15n;55  const tx = api.tx.balances.transfer(deployer.address, amount);56  await submitTransactionAsync(alice, tx);5758  return deployer;59}6061export async function deployFlipper(api: ApiPromise, privateKeyWrapper: (account: string) => IKeyringPair): Promise<[Contract, IKeyringPair]> {62  const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));63  const abi = new Abi(metadata);6465  const deployer = await prepareDeployer(api, privateKeyWrapper);6667  const wasm = fs.readFileSync('./src/flipper/flipper.wasm');6869  const code = new CodePromise(api, abi, wasm);7071  const contract = (await deployContract(deployer, code, 'new', true)) as Contract;7273  const initialGetResponse = await getFlipValue(contract, deployer);74  expect(initialGetResponse).to.be.true;7576  return [contract, deployer];77}7879export async function getFlipValue(contract: Contract, deployer: IKeyringPair) {80  const result = await contract.query.get(deployer.address, value, gasLimit);8182  if(!result.result.isOk) {83    throw 'Failed to get flipper value';84  }85  return (result.result.asOk.data[0] == 0x00) ? false : true;86}8788export async function toggleFlipValueExpectSuccess(sender: IKeyringPair, contract: Contract) {89  const tx = contract.tx.flip(value, gasLimit);90  const events = await submitTransactionAsync(sender, tx);91  const result = getGenericResult(events);9293  expect(result.success).to.be.true;94}9596export async function toggleFlipValueExpectFailure(sender: IKeyringPair, contract: Contract) {97  const tx = contract.tx.flip(value, gasLimit);98  await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;99}100101export async function deployTransferContract(api: ApiPromise, privateKeyWrapper: (account: string) => IKeyringPair): Promise<[Contract, IKeyringPair]> {102  const metadata = JSON.parse(fs.readFileSync('./src/transfer_contract/metadata.json').toString('utf-8'));103  const abi = new Abi(metadata);104105  const deployer = await prepareDeployer(api, privateKeyWrapper);106107  const wasm = fs.readFileSync('./src/transfer_contract/nft_transfer.wasm');108109  const code = new CodePromise(api, abi, wasm);110111  const contract = await deployContract(deployer, code);112113  return [contract, deployer];114}