Skip to content

Commit 48248b9

Browse files
authored
Treasury e2e follow up (#321)
* Move treasury test to its own E2E module * Update snapshots
1 parent 15b2dc4 commit 48248b9

File tree

7 files changed

+177
-94
lines changed

7 files changed

+177
-94
lines changed

packages/polkadot/src/__snapshots__/assetHubPolkadot.polkadot.test.ts.snap

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -225,26 +225,3 @@ exports[`asset hub & polkadot > Teleport DOT from Polkadot to Asset Hub > tx eve
225225
},
226226
]
227227
`;
228-
229-
exports[`polkadot & assetHub > Foreign asset spend from Relay treasury is reflected on AssetHub > payout events 1`] = `
230-
[
231-
{
232-
"data": {
233-
"index": 160,
234-
"paymentId": "(rounded 210)",
235-
},
236-
"method": "Paid",
237-
"section": "treasury",
238-
},
239-
]
240-
`;
241-
242-
exports[`polkadot & assetHub > Foreign asset spend from Relay treasury is reflected on AssetHub > treasury spend approval events 1`] = `
243-
[
244-
{
245-
"data": "(redacted)",
246-
"method": "AssetSpendApproved",
247-
"section": "treasury",
248-
},
249-
]
250-
`;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`Polkadot Treasury > Foreign asset spend from Relay treasury is reflected on AssetHub > payout events 1`] = `
4+
[
5+
{
6+
"data": {
7+
"index": "(redacted)",
8+
"paymentId": "(redacted)",
9+
},
10+
"method": "Paid",
11+
"section": "treasury",
12+
},
13+
]
14+
`;
15+
16+
exports[`Polkadot Treasury > Foreign asset spend from Relay treasury is reflected on AssetHub > treasury spend approval events 1`] = `
17+
[
18+
{
19+
"data": {
20+
"amount": "123,123,123,123",
21+
"assetKind": {
22+
"V4": {
23+
"assetId": {
24+
"interior": {
25+
"X2": [
26+
{
27+
"PalletInstance": "50",
28+
},
29+
{
30+
"GeneralIndex": "1,984",
31+
},
32+
],
33+
},
34+
"parents": "0",
35+
},
36+
"location": {
37+
"interior": {
38+
"X1": [
39+
{
40+
"Parachain": "1,000",
41+
},
42+
],
43+
},
44+
"parents": "0",
45+
},
46+
},
47+
},
48+
"beneficiary": {
49+
"V4": {
50+
"interior": {
51+
"X1": [
52+
{
53+
"AccountId32": {
54+
"id": "(hash)",
55+
"network": null,
56+
},
57+
},
58+
],
59+
},
60+
"parents": "0",
61+
},
62+
},
63+
"expireAt": "(redacted)",
64+
"index": "(redacted)",
65+
"validFrom": "(redacted)",
66+
},
67+
"method": "AssetSpendApproved",
68+
"section": "treasury",
69+
},
70+
]
71+
`;

packages/polkadot/src/assetHubPolkadot.polkadot.test.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
1-
import { describe, test } from 'vitest'
1+
import { describe } from 'vitest'
22

33
import { defaultAccounts } from '@e2e-test/networks'
44
import { assetHubPolkadot, polkadot } from '@e2e-test/networks/chains'
55
import { setupNetworks } from '@e2e-test/shared'
6-
import { treasurySpendForeignAssetTest } from '@e2e-test/shared'
76
import { query, tx } from '@e2e-test/shared/api'
87
import { runXcmPalletDown, runXcmPalletUp } from '@e2e-test/shared/xcm'
98

10-
describe('polkadot & assetHub', async () => {
11-
const [polkadotClient, assetHubClient] = await setupNetworks(polkadot, assetHubPolkadot)
12-
13-
test('Foreign asset spend from Relay treasury is reflected on AssetHub', async () => {
14-
await treasurySpendForeignAssetTest(polkadotClient, assetHubClient)
15-
})
16-
})
17-
189
describe('asset hub & polkadot', async () => {
1910
const [polkadotClient, ahClient] = await setupNetworks(polkadot, assetHubPolkadot)
2011

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { assetHubPolkadot, polkadot } from '@e2e-test/networks/chains'
2+
import { treasuryE2ETests } from '@e2e-test/shared'
3+
4+
treasuryE2ETests(polkadot, assetHubPolkadot, { testSuiteName: 'Polkadot Treasury', addressEncoding: 0 })

packages/shared/src/governance.ts

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BN } from 'bn.js'
2-
import { assert, describe, expect, test } from 'vitest'
2+
import { assert, describe, test } from 'vitest'
33

4-
import { type Chain, type Client as NetworkClient, defaultAccountsSr25519 } from '@e2e-test/networks'
4+
import { type Chain, defaultAccountsSr25519 } from '@e2e-test/networks'
55
import { type Client, setupNetworks } from '@e2e-test/shared'
66
import { check, checkEvents, checkSystemEvents, objectCmp, scheduleInlineCallWithOrigin } from './helpers/index.js'
77

@@ -893,62 +893,3 @@ export function governanceE2ETests<
893893
})
894894
})
895895
}
896-
897-
export async function treasurySpendForeignAssetTest(relayClient: NetworkClient, assetHubClient: NetworkClient) {
898-
await relayClient.dev.setStorage({
899-
System: {
900-
account: [
901-
// give Alice some DOTs so that she can sign a payout transaction.
902-
[[devAccounts.alice.address], { providers: 1, data: { free: 10000e10 } }],
903-
],
904-
},
905-
})
906-
const USDT_ID = 1984
907-
const balanceBefore = await assetHubClient.api.query.assets.account(USDT_ID, devAccounts.alice.address)
908-
909-
// amount is encoded into the call
910-
const amount = 123123123123n
911-
const treasurySpendCall =
912-
'0x130504000100a10f0002043205011f07b3c3b5aa1c0400010100d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d00'
913-
await scheduleInlineCallWithOrigin(relayClient, treasurySpendCall, { system: 'Root' })
914-
await relayClient.dev.newBlock()
915-
await checkSystemEvents(relayClient, { section: 'treasury', method: 'AssetSpendApproved' })
916-
// values (e.g. index) inside data increase over time,
917-
// PET framework often rounds them.
918-
// Tests will be flaky if we don't redact them.
919-
.redact({ hash: false, redactKeys: /data/ })
920-
.toMatchSnapshot('treasury spend approval events')
921-
922-
// filter events to find an index to payout
923-
const spendEvents = (await relayClient.api.query.system.events()).filter(
924-
({ event }) => event.section === 'treasury' && event.method === 'AssetSpendApproved',
925-
)
926-
expect(spendEvents.length).toBe(1)
927-
assert(relayClient.api.events.treasury.AssetSpendApproved.is(spendEvents[0].event))
928-
const spendEvent = spendEvents[0]
929-
const spendIndex = (spendEvent.event.data[0] as any).toNumber()
930-
931-
// payout
932-
const payoutEvents = await sendTransaction(
933-
relayClient.api.tx.treasury.payout(spendIndex).signAsync(devAccounts.alice),
934-
)
935-
936-
// create blocks on RC and AH to ensure that payout is properly processed
937-
await relayClient.dev.newBlock()
938-
await checkEvents(payoutEvents, { section: 'treasury', method: 'Paid' }).toMatchSnapshot('payout events')
939-
const events = (await relayClient.api.query.system.events()).filter(
940-
({ event }) => event.section === 'treasury' && event.method === 'Paid',
941-
)
942-
expect(events.length).toBe(1)
943-
assert(relayClient.api.events.treasury.Paid.is(events[0].event))
944-
const payoutEvent = events[0]
945-
const payoutIndex = (payoutEvent.event.data[0] as any).toNumber()
946-
expect(payoutIndex).toBe(spendIndex)
947-
948-
// treasury spend does not emit any event on AH so we need to check that Alice's balance is increased by the `amount` directly
949-
await assetHubClient.dev.newBlock()
950-
const balanceAfter = await assetHubClient.api.query.assets.account(USDT_ID, devAccounts.alice.address)
951-
const balanceAfterAmount = balanceAfter.isNone ? 0n : balanceAfter.unwrap().balance.toBigInt()
952-
const balanceBeforeAmount = balanceBefore.isNone ? 0n : balanceBefore.unwrap().balance.toBigInt()
953-
expect(balanceAfterAmount - balanceBeforeAmount).toBe(amount)
954-
}

packages/shared/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export * from './people.js'
88
export * from './proxy.js'
99
export * from './scheduler.js'
1010
export * from './staking.js'
11+
export * from './treasury.js'
1112
export * from './vesting.js'
1213

1314
export * from './types.js'

packages/shared/src/treasury.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

Comments
 (0)