@@ -761,7 +761,77 @@ export async function smalltipperTryingToSpendMoreThanTheOriginAllows<
761761 assert ( dispatchError . isModule )
762762 expect ( relayClient . api . errors . treasury . InsufficientPermission . is ( dispatchError . asModule ) ) . toBeTruthy ( )
763763
764- await relayClient . teardown ( )
764+ await assetHubClient . teardown ( )
765+ }
766+
767+ /**
768+ * Test: Check treasury payouts which are already approved can be paid
769+ *
770+ * Verifies that the treasury's payout mechanism correctly processes already approved spends
771+ * and properly disburses funds to beneficiaries. This test ensures that approved treasury
772+ * spends can be successfully paid out
773+ *
774+ * Test Structure:
775+ * 1. Get all the spends which are pending or failed and is neither expired nor early payout
776+ * 2. Call payout tx for each pending or failed spend
777+ * 3. Verify the Paid event is emitted
778+ * 4. Verify the spend status is attempted
779+ */
780+ export async function checkTreasuryPayoutsWhichAreAlreadyApprovedCanBePaid <
781+ TCustom extends Record < string , unknown > | undefined ,
782+ TInitStoragesRelay extends Record < string , Record < string , any > > | undefined ,
783+ TInitStoragesPara extends Record < string , Record < string , any > > | undefined ,
784+ > ( relayChain : Chain < TCustom , TInitStoragesRelay > , ahChain : Chain < TCustom , TInitStoragesPara > ) {
785+ const [ assetHubClient , relayClient ] = await setupNetworks ( ahChain , relayChain )
786+
787+ await setupTestAccounts ( assetHubClient , [ 'alice' , 'bob' ] )
788+
789+ // get all the spends
790+ const spends = await assetHubClient . api . query . treasury . spends . entries ( )
791+
792+ // get current relay chain block number
793+ const currentRelayChainBlockNumber = ( await relayClient . api . query . system . number ( ) ) . toNumber ( )
794+
795+ // filter those spends which are pending or failed and is neither expired nor early payout
796+ const pendingOrFailedSpends = spends . filter ( ( spend ) => {
797+ const spendData = spend [ 1 ] ?. unwrap ( )
798+ return (
799+ ( spendData ?. status . isPending || spendData ?. status . isFailed ) && // not pending or failed
800+ spendData ?. validFrom . toNumber ( ) < currentRelayChainBlockNumber && //not early payout
801+ spendData ?. expireAt . toNumber ( ) > currentRelayChainBlockNumber // not expired
802+ )
803+ } )
804+
805+ await assetHubClient . dev . newBlock ( )
806+
807+ const spendIndices : number [ ] = [ ]
808+
809+ // call payout tx for each pending or failed spend
810+ for ( const spend of pendingOrFailedSpends ) {
811+ const spendIndex = spend [ 0 ] . toHuman ?.( ) as number
812+ spendIndices . push ( spendIndex )
813+ const payoutTx = assetHubClient . api . tx . treasury . payout ( spendIndex )
814+ await sendTransaction ( payoutTx . signAsync ( testAccounts . alice ) )
815+
816+ await assetHubClient . dev . newBlock ( )
817+
818+ // verify the Paid event is emitted
819+ const treasuryEvents = await assetHubClient . api . query . system . events ( )
820+ const paidEvent = treasuryEvents . find ( ( record ) => {
821+ const { event } = record
822+ return event . section === 'treasury' && event . method === 'Paid'
823+ } )
824+ assert ( paidEvent )
825+ assert ( assetHubClient . api . events . treasury . Paid . is ( paidEvent . event ) )
826+ }
827+
828+ // verify the spends status is attempted
829+ for ( const spendIndex of spendIndices ) {
830+ const spend = await assetHubClient . api . query . treasury . spends ( spendIndex )
831+ expect ( spend ?. unwrap ( ) ?. status . isAttempted ) . toBe ( true )
832+ }
833+
834+ await assetHubClient . teardown ( )
765835}
766836
767837export function baseTreasuryE2ETests <
@@ -815,7 +885,12 @@ export function baseTreasuryE2ETests<
815885 {
816886 kind : 'test' ,
817887 label : 'Smalltipper trying to spend more than the origin allows emits `InsufficientPermission` error' ,
818- testFn : async ( ) => await smalltipperTryingToSpendMoreThanTheOriginAllows ( relayChain ) ,
888+ testFn : async ( ) => await smalltipperTryingToSpendMoreThanTheOriginAllows ( ahChain ) ,
889+ } ,
890+ {
891+ kind : 'test' ,
892+ label : 'Check treasury payouts which are already approved can be paid' ,
893+ testFn : async ( ) => await checkTreasuryPayoutsWhichAreAlreadyApprovedCanBePaid ( relayChain , ahChain ) ,
819894 } ,
820895 ] ,
821896 }
0 commit comments