|
| 1 | +import { assert, describe, expect, test } from 'vitest' |
| 2 | + |
| 3 | +import { sendTransaction } from '@acala-network/chopsticks-testing' |
| 4 | +import { type Chain, defaultAccountsSr25519 as devAccounts } from '@e2e-test/networks' |
| 5 | +import { setupNetworks } from '@e2e-test/shared' |
| 6 | + |
| 7 | +import { checkEvents, checkSystemEvents, scheduleInlineCallWithOrigin } from './helpers/index.js' |
| 8 | + |
| 9 | +/** |
| 10 | + * Test that a foreign asset spend from the Relay treasury is reflected on the AssetHub. |
| 11 | + * |
| 12 | + * 1. Approve a spend from the Relay treasury |
| 13 | + * 2. Payout the spend from the Relay treasury |
| 14 | + * 3. Check that the spend shows in the AssetHub |
| 15 | + */ |
| 16 | +export async function treasurySpendForeignAssetTest< |
| 17 | + TCustom extends Record<string, unknown> | undefined, |
| 18 | + TInitStoragesRelay extends Record<string, Record<string, any>> | undefined, |
| 19 | + TInitStoragesPara extends Record<string, Record<string, any>> | undefined, |
| 20 | +>(relayChain: Chain<TCustom, TInitStoragesRelay>, ahChain: Chain<TCustom, TInitStoragesPara>) { |
| 21 | + const [relayClient, assetHubClient] = await setupNetworks(relayChain, ahChain) |
| 22 | + |
| 23 | + await relayClient.dev.setStorage({ |
| 24 | + System: { |
| 25 | + account: [ |
| 26 | + // give Alice some DOTs so that she can sign a payout transaction. |
| 27 | + [[devAccounts.alice.address], { providers: 1, data: { free: 10000e10 } }], |
| 28 | + ], |
| 29 | + }, |
| 30 | + }) |
| 31 | + const USDT_ID = 1984 |
| 32 | + const balanceBefore = await assetHubClient.api.query.assets.account(USDT_ID, devAccounts.alice.address) |
| 33 | + |
| 34 | + // amount is encoded into the call |
| 35 | + const amount = 123123123123n |
| 36 | + const treasurySpendCall = |
| 37 | + '0x130504000100a10f0002043205011f07b3c3b5aa1c0400010100d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d00' |
| 38 | + await scheduleInlineCallWithOrigin(relayClient, treasurySpendCall, { system: 'Root' }) |
| 39 | + await relayClient.dev.newBlock() |
| 40 | + await checkSystemEvents(relayClient, { section: 'treasury', method: 'AssetSpendApproved' }) |
| 41 | + // values (e.g. index) inside data increase over time, |
| 42 | + // PET framework often rounds them. |
| 43 | + // Tests will be flaky if we don't redact them. |
| 44 | + .redact({ |
| 45 | + redactKeys: /expireAt|validFrom|index/, |
| 46 | + number: false, |
| 47 | + }) |
| 48 | + .toMatchSnapshot('treasury spend approval events') |
| 49 | + |
| 50 | + // filter events to find an index to payout |
| 51 | + const [assetSpendApprovedEvent] = (await relayClient.api.query.system.events()).filter( |
| 52 | + ({ event }) => event.section === 'treasury' && event.method === 'AssetSpendApproved', |
| 53 | + ) |
| 54 | + expect(assetSpendApprovedEvent).toBeDefined() |
| 55 | + assert(relayClient.api.events.treasury.AssetSpendApproved.is(assetSpendApprovedEvent.event)) |
| 56 | + const spendIndex = assetSpendApprovedEvent.event.data.index.toNumber() |
| 57 | + |
| 58 | + // payout |
| 59 | + const payoutEvents = await sendTransaction( |
| 60 | + relayClient.api.tx.treasury.payout(spendIndex).signAsync(devAccounts.alice), |
| 61 | + ) |
| 62 | + |
| 63 | + // create blocks on RC and AH to ensure that payout is properly processed |
| 64 | + await relayClient.dev.newBlock() |
| 65 | + await checkEvents(payoutEvents, { section: 'treasury', method: 'Paid' }) |
| 66 | + .redact({ redactKeys: /paymentId|index/ }) |
| 67 | + .toMatchSnapshot('payout events') |
| 68 | + const [paidEvent] = (await relayClient.api.query.system.events()).filter( |
| 69 | + ({ event }) => event.section === 'treasury' && event.method === 'Paid', |
| 70 | + ) |
| 71 | + expect(paidEvent).toBeDefined() |
| 72 | + assert(relayClient.api.events.treasury.Paid.is(paidEvent.event)) |
| 73 | + const payoutIndex = paidEvent.event.data.index.toNumber() |
| 74 | + expect(payoutIndex).toBe(spendIndex) |
| 75 | + |
| 76 | + // treasury spend does not emit any event on AH so we need to check that Alice's balance is increased by the `amount` directly |
| 77 | + await assetHubClient.dev.newBlock() |
| 78 | + const balanceAfter = await assetHubClient.api.query.assets.account(USDT_ID, devAccounts.alice.address) |
| 79 | + const balanceAfterAmount = balanceAfter.isNone ? 0n : balanceAfter.unwrap().balance.toBigInt() |
| 80 | + const balanceBeforeAmount = balanceBefore.isNone ? 0n : balanceBefore.unwrap().balance.toBigInt() |
| 81 | + expect(balanceAfterAmount - balanceBeforeAmount).toBe(amount) |
| 82 | +} |
| 83 | + |
| 84 | +export function treasuryE2ETests< |
| 85 | + TCustom extends Record<string, unknown> | undefined, |
| 86 | + TInitStoragesRelay extends Record<string, Record<string, any>> | undefined, |
| 87 | + TInitStoragesPara extends Record<string, Record<string, any>> | undefined, |
| 88 | +>( |
| 89 | + relayChain: Chain<TCustom, TInitStoragesRelay>, |
| 90 | + ahChain: Chain<TCustom, TInitStoragesPara>, |
| 91 | + testConfig: { testSuiteName: string; addressEncoding: number }, |
| 92 | +) { |
| 93 | + describe(testConfig.testSuiteName, () => { |
| 94 | + test('Foreign asset spend from Relay treasury is reflected on AssetHub', async () => { |
| 95 | + await treasurySpendForeignAssetTest(relayChain, ahChain) |
| 96 | + }) |
| 97 | + }) |
| 98 | +} |
0 commit comments