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} 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, privateKeyWrapper: (account: string) => IKeyringPair) {49 50 const deployer = await findUnusedAddress(api, privateKeyWrapper);5152 53 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}