@@ -7,7 +7,7 @@ import type { ApiPromise } from '@polkadot/api'
77import { decodeAddress , encodeAddress } from '@polkadot/keyring'
88import type { KeyringPair } from '@polkadot/keyring/types'
99import type { EventRecord } from '@polkadot/types/interfaces'
10- import type { PalletStakingValidatorPrefs } from '@polkadot/types/lookup'
10+ import type { FrameSystemAccountInfo , PalletStakingValidatorPrefs } from '@polkadot/types/lookup'
1111import type { IsEvent } from '@polkadot/types/metadata/decorate/types'
1212import type { AnyTuple , Codec , IEvent } from '@polkadot/types/types'
1313import type { HexString } from '@polkadot/util/types'
@@ -107,6 +107,79 @@ export type BlockProvider = 'Local' | 'NonLocal'
107107/** Whether async backing is enabled or disabled on the querying parachain. */
108108export type AsyncBacking = 'Enabled' | 'Disabled'
109109
110+ /**
111+ * Given a PJS client and a call, modify the `scheduler` pallet's `agenda` storage to execute the list of extrinsics
112+ * in the next block.
113+ *
114+ * The calls can be either inline calls or lookup calls, which in the latter case *must* have been noted
115+ * in the storage of the chain's `preimage` pallet with a `notePreimage` extrinsic.
116+ *
117+ * @param blockProvider Whether the calls are being scheduled on a chain that uses a local or nonlocal block provider.
118+ * This chain's runtime *must* have the scheduler pallet available.
119+ */
120+ export async function scheduleCallListWithOrigin (
121+ client : {
122+ api : ApiPromise
123+ dev : {
124+ setStorage : ( values : StorageValues , blockHash ?: string ) => Promise < any >
125+ }
126+ } ,
127+ calls : {
128+ call :
129+ | { Inline : any }
130+ | {
131+ Lookup : {
132+ hash : any
133+ len : any
134+ }
135+ }
136+ origin : any
137+ } [ ] ,
138+ blockProvider : BlockProvider = 'Local' ,
139+ ) {
140+ const scheduledBlock = await match ( blockProvider )
141+ . with ( 'Local' , async ( ) => ( await client . api . rpc . chain . getHeader ( ) ) . number . toNumber ( ) + 1 )
142+ . with ( 'NonLocal' , async ( ) =>
143+ ( ( await client . api . query . parachainSystem . lastRelayChainBlockNumber ( ) ) as any ) . toNumber ( ) ,
144+ )
145+ . exhaustive ( )
146+
147+ const agenda = calls . map ( ( { call, origin } ) => [
148+ [ scheduledBlock ] ,
149+ [
150+ {
151+ call,
152+ origin : origin ,
153+ } ,
154+ ] ,
155+ ] )
156+
157+ await client . dev . setStorage ( {
158+ Scheduler : {
159+ agenda : agenda ,
160+ } ,
161+ } )
162+ }
163+
164+ /**
165+ * Given a PJS client and a list of inline calls with the same origin, modify the `scheduler`
166+ * pallet's `agenda` storage to execute the extrinsic in the next block.
167+ */
168+ export async function scheduleInlineCallListWithSameOrigin (
169+ client : {
170+ api : ApiPromise
171+ dev : {
172+ setStorage : ( values : StorageValues , blockHash ?: string ) => Promise < any >
173+ }
174+ } ,
175+ encodedCall : HexString [ ] ,
176+ origin : any ,
177+ blockProvider : BlockProvider = 'Local' ,
178+ ) {
179+ const callList = encodedCall . map ( ( call ) => ( { call : { Inline : call } , origin } ) )
180+ await scheduleCallListWithOrigin ( client , callList , blockProvider )
181+ }
182+
110183/**
111184 * Given a PJS client and a call, modify the `scheduler` pallet's `agenda` storage to execute the extrinsic in the next
112185 * block.
@@ -135,28 +208,7 @@ export async function scheduleCallWithOrigin(
135208 origin : any ,
136209 blockProvider : BlockProvider = 'Local' ,
137210) {
138- const scheduledBlock = await match ( blockProvider )
139- . with ( 'Local' , async ( ) => ( await client . api . rpc . chain . getHeader ( ) ) . number . toNumber ( ) + 1 )
140- . with ( 'NonLocal' , async ( ) =>
141- ( ( await client . api . query . parachainSystem . lastRelayChainBlockNumber ( ) ) as any ) . toNumber ( ) ,
142- )
143- . exhaustive ( )
144-
145- await client . dev . setStorage ( {
146- Scheduler : {
147- agenda : [
148- [
149- [ scheduledBlock ] ,
150- [
151- {
152- call,
153- origin : origin ,
154- } ,
155- ] ,
156- ] ,
157- ] ,
158- } ,
159- } )
211+ await scheduleCallListWithOrigin ( client , [ { call, origin } ] , blockProvider )
160212}
161213
162214/**
@@ -487,6 +539,22 @@ export function sortAddressesByBytes(addresses: string[], addressEncoding: numbe
487539 . map ( ( bytes ) => encodeAddress ( bytes , addressEncoding ) )
488540}
489541
542+ /**
543+ * Get the free funds of an account.
544+ */
545+ export async function getFreeFunds ( client : Client < any , any > , address : any ) : Promise < number > {
546+ const account = ( await client . api . query . system . account ( address ) ) as FrameSystemAccountInfo
547+ return account . data . free . toNumber ( )
548+ }
549+
550+ /**
551+ * Get the reserved funds of an account.
552+ */
553+ export async function getReservedFunds ( client : Client < any , any > , address : any ) : Promise < number > {
554+ const account = ( await client . api . query . system . account ( address ) ) as FrameSystemAccountInfo
555+ return account . data . reserved . toNumber ( )
556+ }
557+
490558/**
491559 * Configuration for relay chain tests.
492560 */
0 commit comments