123456import 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 { BigNumber } from 'bignumber.js';17import { findUnusedAddress, getGenericResult } from '../util/helpers';1819const value = 0;20const gasLimit = '200000000000';21const endowment = '100000000000000000';222324function deployContract(alice: IKeyringPair, code: CodePromise, constructor = 'default', ...args: any[]): Promise<Contract> {25 return new Promise<Contract>(async (resolve) => {26 const unsub = await (code as any)27 .tx[constructor]({value: endowment, gasLimit}, ...args)28 .signAndSend(alice, (result: any) => {29 if (result.status.isInBlock || result.status.isFinalized) {30 31 resolve((result as any).contract);32 unsub();33 }34 });35 });36}3738async function prepareDeployer(api: ApiPromise) {39 40 const deployer = await findUnusedAddress(api);4142 43 const keyring = new Keyring({ type: 'sr25519' });44 const alice = keyring.addFromUri('//Alice');45 let amount = new BigNumber(endowment);46 amount = amount.plus(100e15);47 const tx = api.tx.balances.transfer(deployer.address, amount.toFixed());48 await submitTransactionAsync(alice, tx);4950 return deployer;51}5253export async function deployFlipper(api: ApiPromise): Promise<[Contract, IKeyringPair]> {54 const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));55 const abi = new Abi(metadata);5657 const deployer = await prepareDeployer(api);5859 const wasm = fs.readFileSync('./src/flipper/flipper.wasm');6061 const code = new CodePromise(api, abi, wasm);6263 const contract = (await deployContract(deployer, code, 'new', true)) as Contract;6465 const initialGetResponse = await getFlipValue(contract, deployer);66 expect(initialGetResponse).to.be.true;6768 return [contract, deployer];69}7071export async function getFlipValue(contract: Contract, deployer: IKeyringPair) {72 const result = await contract.query.get(deployer.address, value, gasLimit);7374 if(!result.result.isOk) {75 throw 'Failed to get flipper value';76 }77 return (result.result.asOk.data[0] == 0x00) ? false : true;78}7980export async function toggleFlipValueExpectSuccess(sender: IKeyringPair, contract: Contract) {81 const tx = contract.tx.flip(value, gasLimit);82 const events = await submitTransactionAsync(sender, tx);83 const result = getGenericResult(events);8485 expect(result.success).to.be.true;86}8788export async function toggleFlipValueExpectFailure(sender: IKeyringPair, contract: Contract) {89 const tx = contract.tx.flip(value, gasLimit);90 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;91}9293export async function deployTransferContract(api: ApiPromise): Promise<[Contract, IKeyringPair]> {94 const metadata = JSON.parse(fs.readFileSync('./src/transfer_contract/metadata.json').toString('utf-8'));95 const abi = new Abi(metadata);9697 const deployer = await prepareDeployer(api);9899 const wasm = fs.readFileSync('./src/transfer_contract/nft_transfer.wasm');100101 const code = new CodePromise(api, abi, wasm);102103 const contract = await deployContract(deployer, code);104105 return [contract, deployer];106}