diff --git a/packages/test-support/scripts/createBounties.ts b/packages/test-support/scripts/createBounties.ts index 811fd33406..63655cf973 100644 --- a/packages/test-support/scripts/createBounties.ts +++ b/packages/test-support/scripts/createBounties.ts @@ -2,12 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import { createApi } from '@polkadot/test-support/api'; -import { aliceSigner, - bobSigner, - charlieSigner, - daveSigner, - eveSigner, - ferdieSigner } from '@polkadot/test-support/keyring'; +import { aliceSigner } from '@polkadot/test-support/keyring'; import { multiAcceptCurator, multiApproveBounty, @@ -21,28 +16,26 @@ import { multiAcceptCurator, (async () => { const api = await createApi(9944); - const SIGNERS = [aliceSigner(), bobSigner(), charlieSigner(), daveSigner(), eveSigner(), ferdieSigner()]; - - const indexes = await multiProposeBounty(api, 6); + const indexes = await multiProposeBounty(api, 6, aliceSigner()); indexes.pop(); - await multiApproveBounty(api, indexes); + await multiApproveBounty(api, indexes, aliceSigner()); await multiWaitForBountyFunded(api, indexes); indexes.pop(); - await multiProposeCurator(api, indexes, SIGNERS); + await multiProposeCurator(api, indexes, aliceSigner()); indexes.pop(); - await multiAcceptCurator(api, indexes, SIGNERS); + await multiAcceptCurator(api, indexes, aliceSigner()); indexes.pop(); - await multiAwardBounty(api, indexes, SIGNERS); + await multiAwardBounty(api, indexes, aliceSigner()); await multiWaitForClaim(api, indexes); indexes.pop(); - await multiClaimBounty(api, indexes, SIGNERS); + await multiClaimBounty(api, indexes, aliceSigner()); await api.disconnect(); })().catch((err) => console.error(err)); diff --git a/packages/test-support/scripts/lib/changeBountyStateFunctions.ts b/packages/test-support/scripts/lib/changeBountyStateFunctions.ts index 9672993552..784b19eeef 100644 --- a/packages/test-support/scripts/lib/changeBountyStateFunctions.ts +++ b/packages/test-support/scripts/lib/changeBountyStateFunctions.ts @@ -31,7 +31,6 @@ export async function proposeBounty (api: ApiPromise, value: BN, title: string, export async function proposeCurator (api: ApiPromise, index: number, signer: KeyringPair): Promise { await proposeMotion(api, api.tx.bounties.proposeCurator(index, signer.address, 10), signer); - const bountyProposal = await getMotion(api, index); await acceptMotion(api, bountyProposal.hash, bountyProposal.votes?.index.toNumber() ?? 0); diff --git a/packages/test-support/scripts/lib/helpers.ts b/packages/test-support/scripts/lib/helpers.ts index 29e1ea06cc..5204efe031 100644 --- a/packages/test-support/scripts/lib/helpers.ts +++ b/packages/test-support/scripts/lib/helpers.ts @@ -7,10 +7,7 @@ import { ApiPromise } from '@polkadot/api'; import { SubmittableExtrinsic } from '@polkadot/api/types'; import { DeriveCollectiveProposal } from '@polkadot/api-derive/types'; import { KeyringPair } from '@polkadot/keyring/types'; -import { charlieSigner, - daveSigner, - eveSigner, - ferdieSigner } from '@polkadot/test-support/keyring'; +import { charlieSigner, daveSigner, eveSigner, ferdieSigner } from '@polkadot/test-support/keyring'; import { execute } from '@polkadot/test-support/transaction'; import { Hash } from '@polkadot/types/interfaces'; @@ -27,7 +24,7 @@ export async function acceptMotion (api: ApiPromise, hash: Hash, index: number): } export async function fillTreasury (api: ApiPromise, signer: KeyringPair): Promise { - await execute(api.tx.balances.transfer(TREASURY_ADDRESS, new BN(5_000_000_000_000_000)), signer); + await execute(api.tx.balances.transfer(TREASURY_ADDRESS, new BN('50000000000000000')), signer); } export async function proposeMotion (api: ApiPromise, submittableExtrinsic: SubmittableExtrinsic<'promise'>, signer: KeyringPair): Promise { @@ -40,3 +37,57 @@ export async function getMotion (api: ApiPromise, index: number): Promise[], signer: KeyringPair): Promise { + const proposeExtrinsicArray = + submittableExtrinsicArray.map((extrinsic) => + api.tx.council.propose(4, extrinsic, LENGTH_BOUND)); + + await execute(api.tx.utility.batch(proposeExtrinsicArray), signer); +} + +export async function multiGetMotion (api: ApiPromise, indexes: number[]): Promise { + const bounties = await api.derive.bounties.bounties(); + const bountyProposals = + indexes.map((index) => + bounties.find((bounty) => + (bounty.index.toNumber() === index))?.proposals as DeriveCollectiveProposal[]); + + return bountyProposals.map((arr) => arr[0]); +} + +async function multiVoteAye (acceptMotionSigners: KeyringPair[], api: ApiPromise, indexes: number[], hashes: Hash[]) { + await Promise.all( + acceptMotionSigners.map((signer) => + execute( + api.tx.utility.batch( + indexes.map((bountyIndex, i) => api.tx.council.vote(hashes[i], bountyIndex, true)) + ), + signer + ) + ) + ); +} + +async function multiCloseMotion (api: ApiPromise, indexes: number[], hashes: Hash[]) { + await execute( + api.tx.utility.batch( + indexes.map((bountyIndex, i) => api.tx.council.close(hashes[i], bountyIndex, WEIGHT_BOUND, LENGTH_BOUND))), + charlieSigner() + ); +} + +export async function multiAcceptMotion (api: ApiPromise, hashes: Hash[], indexes: number[]): Promise { + const acceptMotionSigners = [charlieSigner(), daveSigner(), eveSigner(), ferdieSigner()]; + + await multiVoteAye(acceptMotionSigners, api, indexes, hashes); + await multiCloseMotion(api, indexes, hashes); +} + +export function extractIndexesFromProposals (bountyProposals: DeriveCollectiveProposal[]): number[] { + return bountyProposals.map((proposal) => proposal.votes?.index.toNumber() ?? 0); +} + +export function extractHashesFromProposals (bountyProposals: DeriveCollectiveProposal[]): Hash[] { + return bountyProposals.map((proposal) => proposal.hash); +} diff --git a/packages/test-support/scripts/lib/multiFunctions.ts b/packages/test-support/scripts/lib/multiFunctions.ts index 707d2ec656..125471ce0e 100644 --- a/packages/test-support/scripts/lib/multiFunctions.ts +++ b/packages/test-support/scripts/lib/multiFunctions.ts @@ -5,26 +5,44 @@ import BN from 'bn.js'; import { ApiPromise } from '@polkadot/api'; import { KeyringPair } from '@polkadot/keyring/types'; -import { aliceSigner } from '@polkadot/test-support/keyring'; +import { execute } from '@polkadot/test-support/transaction'; import { waitForBountyState, waitForClaim } from './bountyWaitFunctions'; -import { acceptCurator, approveBounty, awardBounty, claimBounty, proposeBounty, proposeCurator } from './changeBountyStateFunctions'; import { FUNDING_TIME, PAYOUT_TIME } from './constants'; +import { extractHashesFromProposals, + extractIndexesFromProposals, + fillTreasury, + multiAcceptMotion, + multiGetMotion, + multiProposeMotion } from './helpers'; -export async function multiProposeBounty (api: ApiPromise, numberOfBounties: number): Promise { - const indexes = []; +export async function multiProposeBounty (api: ApiPromise, numberOfBounties: number, signer: KeyringPair): Promise { + const initialIndex = await api.query.bounties.bountyCount(); - for (let i = 0; i < numberOfBounties; i++) { - indexes.push(await proposeBounty(api, new BN(500_000_000_000_000), `new bounty no ${i}`, aliceSigner())); + const arr = Array.from({ length: numberOfBounties }, (_, i) => api.tx.bounties.proposeBounty(new BN(500_000_000_000_000), `new bounty no ${i}`)); + + await execute( + api.tx.utility.batch(arr), + signer + ); + const endIndex = await api.query.bounties.bountyCount(); + + if ((endIndex.sub(initialIndex)).toNumber() !== numberOfBounties) { + throw new Error('Multi Propose Failed'); } - return indexes; + return Array.from({ length: numberOfBounties }, (_, i) => i + initialIndex.toNumber()); } -export async function multiApproveBounty (api: ApiPromise, bountyIndexes: number[]): Promise { - for (const bountyIndex of bountyIndexes) { - await approveBounty(api, bountyIndex, aliceSigner()); - } +export async function multiApproveBounty (api: ApiPromise, bountyIndexes: number[], signer: KeyringPair): Promise { + const extrinsicArray = bountyIndexes.map((index) => api.tx.bounties.approveBounty(index)); + + await multiProposeMotion(api, extrinsicArray, signer); + + const bountyProposals = await multiGetMotion(api, bountyIndexes); + + await fillTreasury(api, signer); + await multiAcceptMotion(api, extractHashesFromProposals(bountyProposals), extractIndexesFromProposals(bountyProposals)); } export async function multiWaitForBountyFunded (api: ApiPromise, bountyIndexes: number[]): Promise { @@ -34,24 +52,28 @@ export async function multiWaitForBountyFunded (api: ApiPromise, bountyIndexes: await Promise.all(waitFunctions); } -export async function multiProposeCurator (api: ApiPromise, bountyIndexes: number[], signers: KeyringPair[]): Promise { - for (let i = 0; i < bountyIndexes.length; i++) { - await proposeCurator(api, bountyIndexes[i], signers[i]); - } +export async function multiProposeCurator (api: ApiPromise, bountyIndexes: number[], signer: KeyringPair): Promise { + const extrinsicArray = bountyIndexes.map((index) => api.tx.bounties.proposeCurator(index, signer.address, 10)); + + await multiProposeMotion(api, extrinsicArray, signer); + + const bountyProposals = await multiGetMotion(api, bountyIndexes); + + await multiAcceptMotion(api, extractHashesFromProposals(bountyProposals), extractIndexesFromProposals(bountyProposals)); } -export async function multiAcceptCurator (api: ApiPromise, bountyIndexes: number[], signers: KeyringPair[]): Promise { - const acceptFunctions = bountyIndexes.map((bountyIndex, index) => - acceptCurator(api, bountyIndex, signers[index])); - - await Promise.all(acceptFunctions); +export async function multiAcceptCurator (api: ApiPromise, bountyIndexes: number[], signer: KeyringPair): Promise { + await execute( + api.tx.utility.batch(bountyIndexes.map((bountyIndex) => api.tx.bounties.acceptCurator(bountyIndex))), + signer + ); } -export async function multiAwardBounty (api: ApiPromise, bountyIndexes: number[], signers: KeyringPair[]): Promise { - const awardFunctions = bountyIndexes.map((bountyIndex, index) => - awardBounty(api, bountyIndex, signers[index])); - - await Promise.all(awardFunctions); +export async function multiAwardBounty (api: ApiPromise, bountyIndexes: number[], signer: KeyringPair): Promise { + await execute( + api.tx.utility.batch(bountyIndexes.map((bountyIndex) => api.tx.bounties.awardBounty(bountyIndex, signer.address))), + signer + ); } export async function multiWaitForClaim (api: ApiPromise, bountyIndexes: number[]): Promise { @@ -60,9 +82,9 @@ export async function multiWaitForClaim (api: ApiPromise, bountyIndexes: number[ } } -export async function multiClaimBounty (api: ApiPromise, bountyIndexes: number[], signers: KeyringPair[]): Promise { - const awardFunctions = bountyIndexes.map((bountyIndex, index) => - claimBounty(api, bountyIndex, signers[index])); - - await Promise.all(awardFunctions); +export async function multiClaimBounty (api: ApiPromise, bountyIndexes: number[], signer: KeyringPair): Promise { + await execute( + api.tx.utility.batch(bountyIndexes.map((bountyIndex) => api.tx.bounties.claimBounty(bountyIndex))), + signer + ); }