1234567891011121314151617import 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, Keyring} 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';323334function 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 41 resolve((result as any).contract);42 unsub();43 }44 });45 });46}4748async function prepareDeployer(api: ApiPromise) {49 50 const deployer = await findUnusedAddress(api);5152 53 const keyring = new Keyring({type: 'sr25519'});54 const alice = keyring.addFromUri('//Alice');55 const amount = BigInt(endowment) + 10n**15n;56 const tx = api.tx.balances.transfer(deployer.address, amount);57 await submitTransactionAsync(alice, tx);5859 return deployer;60}6162export async function deployFlipper(api: ApiPromise): Promise<[Contract, IKeyringPair]> {63 const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));64 const abi = new Abi(metadata);6566 const deployer = await prepareDeployer(api);6768 const wasm = fs.readFileSync('./src/flipper/flipper.wasm');6970 const code = new CodePromise(api, abi, wasm);7172 const contract = (await deployContract(deployer, code, 'new', true)) as Contract;7374 const initialGetResponse = await getFlipValue(contract, deployer);75 expect(initialGetResponse).to.be.true;7677 return [contract, deployer];78}7980export async function getFlipValue(contract: Contract, deployer: IKeyringPair) {81 const result = await contract.query.get(deployer.address, value, gasLimit);8283 if(!result.result.isOk) {84 throw 'Failed to get flipper value';85 }86 return (result.result.asOk.data[0] == 0x00) ? false : true;87}8889export async function toggleFlipValueExpectSuccess(sender: IKeyringPair, contract: Contract) {90 const tx = contract.tx.flip(value, gasLimit);91 const events = await submitTransactionAsync(sender, tx);92 const result = getGenericResult(events);9394 expect(result.success).to.be.true;95}9697export async function toggleFlipValueExpectFailure(sender: IKeyringPair, contract: Contract) {98 const tx = contract.tx.flip(value, gasLimit);99 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;100}101102export async function deployTransferContract(api: ApiPromise): Promise<[Contract, IKeyringPair]> {103 const metadata = JSON.parse(fs.readFileSync('./src/transfer_contract/metadata.json').toString('utf-8'));104 const abi = new Abi(metadata);105106 const deployer = await prepareDeployer(api);107108 const wasm = fs.readFileSync('./src/transfer_contract/nft_transfer.wasm');109110 const code = new CodePromise(api, abi, wasm);111112 const contract = await deployContract(deployer, code);113114 return [contract, deployer];115}