Use utility batch in scripts (#4521)

This commit is contained in:
MiZiet 2021-01-28 15:53:14 +01:00 committed by GitHub
parent 69a9eb8d32
commit 7f7a9395e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 115 additions and 50 deletions

View File

@ -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));

View File

@ -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<void> {
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);

View File

@ -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<void> {
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<void> {
@ -40,3 +37,57 @@ export async function getMotion (api: ApiPromise, index: number): Promise<Derive
return bountyProposals[0];
}
export async function multiProposeMotion (api: ApiPromise, submittableExtrinsicArray: SubmittableExtrinsic<'promise'>[], signer: KeyringPair): Promise<void> {
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<DeriveCollectiveProposal[]> {
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<void> {
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);
}

View File

@ -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<number[]> {
const indexes = [];
export async function multiProposeBounty (api: ApiPromise, numberOfBounties: number, signer: KeyringPair): Promise<number[]> {
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<void> {
for (const bountyIndex of bountyIndexes) {
await approveBounty(api, bountyIndex, aliceSigner());
}
export async function multiApproveBounty (api: ApiPromise, bountyIndexes: number[], signer: KeyringPair): Promise<void> {
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<void> {
@ -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<void> {
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<void> {
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<void> {
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<void> {
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<void> {
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<void> {
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<void> {
@ -60,9 +82,9 @@ export async function multiWaitForClaim (api: ApiPromise, bountyIndexes: number[
}
}
export async function multiClaimBounty (api: ApiPromise, bountyIndexes: number[], signers: KeyringPair[]): Promise<void> {
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<void> {
await execute(
api.tx.utility.batch(bountyIndexes.map((bountyIndex) => api.tx.bounties.claimBounty(bountyIndex))),
signer
);
}