From 14d6c32238331d479e592bb84961f4bc6df62f5a Mon Sep 17 00:00:00 2001 From: Pablo Labarta Date: Tue, 4 Feb 2025 11:57:37 -0300 Subject: [PATCH 1/3] initial costs tests --- .../test-assets/test-foreign-assets-costs.ts | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 test/suites/dev/moonbase/test-assets/test-foreign-assets-costs.ts diff --git a/test/suites/dev/moonbase/test-assets/test-foreign-assets-costs.ts b/test/suites/dev/moonbase/test-assets/test-foreign-assets-costs.ts new file mode 100644 index 00000000000..343a078eb7d --- /dev/null +++ b/test/suites/dev/moonbase/test-assets/test-foreign-assets-costs.ts @@ -0,0 +1,152 @@ +import "@moonbeam-network/api-augment"; +import "@moonbeam-network/api-augment/moonbase"; +import { describeSuite, expect, beforeAll, fetchCompiledContract } from "@moonwall/cli"; +import { + ARBITRARY_ASSET_ID, + PARA_1000_SOURCE_LOCATION_V4, + RELAY_SOURCE_LOCATION_V4, + foreignAssetBalance, + mockAssetBalance, + patchLocationV4recursively, + registerForeignAsset, + relayAssetMetadata, +} from "../../../../helpers"; +import { parseAbi } from "viem"; +import exp from "constants"; +import { alith, ALITH_ADDRESS, BALTATHAR_ADDRESS } from "@moonwall/util"; + +describeSuite({ + id: "D010111", + title: "XCM - Costs of managing foreign assets", + foundationMethods: "dev", + testCases: ({ context, it, log }) => { + let address: string; + let assetId: bigint; + let assetLocation = RELAY_SOURCE_LOCATION_V4; + let xcmLoc = patchLocationV4recursively(assetLocation); + let decimals = 18; + + beforeAll(async () => { + }); + + // Cases + // 1. Cost of creating an asset + // 2. Cost of changing an asset's location + // 3. Cost of freezing an asset + // 4. Cost of unfreezing an asset + // 6. Size of the created contract + // 7. Cost of sending an asset + it({ + id: "T01", + "title": "Cost of creating an asset", + test: async function () { + + const createAsset = context.polkadotJs().tx.sudo.sudo( + context.polkadotJs().tx.evmForeignAssets.createForeignAsset(assetId, xcmLoc, decimals, "test", "test") + ); + + const { weight, proofSize } = await calculateWeight(context, createAsset); + + expect(weight).toMatchInlineSnapshot(`6287470000`); + expect(proofSize).toMatchInlineSnapshot(`3284782`); + } + }) + + it({ + id: "T01", + "title": "Cost of changing an asset location", + test: async function () { + + const changeLocation = context.polkadotJs().tx.sudo.sudo( + context.polkadotJs().tx.evmForeignAssets.changeXcmLocation(assetId, xcmLoc) + ); + + const { weight, proofSize } = await calculateWeight(context, changeLocation); + + expect(weight).toMatchInlineSnapshot(`479200000`); + expect(proofSize).toMatchInlineSnapshot(`9906`); + } + }) + + it({ + id: "T01", + "title": "Cost of freezing an asset", + test: async function () { + + const freezeAsset = context.polkadotJs().tx.sudo.sudo( + context.polkadotJs().tx.evmForeignAssets.freezeForeignAsset(assetId, false) + ); + + const { weight, proofSize } = await calculateWeight(context, freezeAsset); + + expect(weight).toMatchInlineSnapshot(`4945940000`); + expect(proofSize).toMatchInlineSnapshot(`3302014`); + } + }) + + it({ + id: "T01", + "title": "Cost of unfreezing an asset", + test: async function () { + + const unfreezeAsset = context.polkadotJs().tx.sudo.sudo( + context.polkadotJs().tx.evmForeignAssets.freezeForeignAsset(assetId, true) + ); + + const { weight, proofSize } = await calculateWeight(context, unfreezeAsset); + + expect(weight).toMatchInlineSnapshot(`4945940000`); + expect(proofSize).toMatchInlineSnapshot(`3302014`); + } + }) + + it({ + id: "T01", + "title": "Size of the created contract", + test: async function () { + const someBalance = 100_000_000_000_000n; + const assetLocation = RELAY_SOURCE_LOCATION_V4; + const assetId = 1n; + + // Register the asset + const {contractAddress, registeredAssetId } = await registerForeignAsset(context, assetId, assetLocation, relayAssetMetadata); + // Mock asset balance + await mockAssetBalance(context, someBalance, assetId, alith, ALITH_ADDRESS); + + const newBalance = await foreignAssetBalance(context, assetId, ALITH_ADDRESS); + expect(newBalance).toBe(someBalance); + + + const { request } = await context.viem().simulateContract({ + address: contractAddress as `0x${string}`, + abi: fetchCompiledContract("ERC20Instance").abi, + functionName: "transfer", + args: [BALTATHAR_ADDRESS, someBalance / 2n], + }); + + const hash = await context.viem().writeContract(request); + + await context.createBlock(); + + const receipt = await context.viem().getTransactionReceipt({hash}); + + console.log(receipt); + + expect(receipt.status).toBe('success'); + expect(receipt.gasUsed).toMatchInlineSnapshot(`154976n`); + + const transferredBalance = await foreignAssetBalance(context, assetId, BALTATHAR_ADDRESS); + expect(transferredBalance).toBe(someBalance / 2n); + + } + }) + }, +}); + +async function calculateWeight(context, transaction) { + const info = await context.polkadotJs().call.transactionPaymentApi.queryInfo(transaction.toU8a(), transaction.encodedLength); + return { + weight: info.weight.refTime, + proofSize: info.weight.proofSize + } +} \ No newline at end of file From 372897e774369f25e7f53f069a55972b6ecba2e5 Mon Sep 17 00:00:00 2001 From: Pablo Labarta Date: Tue, 4 Feb 2025 12:25:32 -0300 Subject: [PATCH 2/3] update snapshots & rm comments --- .../test-assets/test-foreign-assets-costs.ts | 31 ++++++------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/test/suites/dev/moonbase/test-assets/test-foreign-assets-costs.ts b/test/suites/dev/moonbase/test-assets/test-foreign-assets-costs.ts index 343a078eb7d..af32578c142 100644 --- a/test/suites/dev/moonbase/test-assets/test-foreign-assets-costs.ts +++ b/test/suites/dev/moonbase/test-assets/test-foreign-assets-costs.ts @@ -19,23 +19,12 @@ describeSuite({ id: "D010111", title: "XCM - Costs of managing foreign assets", foundationMethods: "dev", - testCases: ({ context, it, log }) => { - let address: string; + testCases: ({ context, it }) => { let assetId: bigint; let assetLocation = RELAY_SOURCE_LOCATION_V4; let xcmLoc = patchLocationV4recursively(assetLocation); let decimals = 18; - beforeAll(async () => { - }); - - // Cases - // 1. Cost of creating an asset - // 2. Cost of changing an asset's location - // 3. Cost of freezing an asset - // 4. Cost of unfreezing an asset - // 6. Size of the created contract - // 7. Cost of sending an asset it({ id: "T01", "title": "Cost of creating an asset", @@ -47,8 +36,8 @@ describeSuite({ const { weight, proofSize } = await calculateWeight(context, createAsset); - expect(weight).toMatchInlineSnapshot(`6287470000`); - expect(proofSize).toMatchInlineSnapshot(`3284782`); + expect(weight).toMatchInlineSnapshot(`2435166000`); + expect(proofSize).toMatchInlineSnapshot(`14519`); } }) @@ -63,8 +52,8 @@ describeSuite({ const { weight, proofSize } = await calculateWeight(context, changeLocation); - expect(weight).toMatchInlineSnapshot(`479200000`); - expect(proofSize).toMatchInlineSnapshot(`9906`); + expect(weight).toMatchInlineSnapshot(`443464000`); + expect(proofSize).toMatchInlineSnapshot(`7594`); } }) @@ -79,8 +68,8 @@ describeSuite({ const { weight, proofSize } = await calculateWeight(context, freezeAsset); - expect(weight).toMatchInlineSnapshot(`4945940000`); - expect(proofSize).toMatchInlineSnapshot(`3302014`); + expect(weight).toMatchInlineSnapshot(`1279934000`); + expect(proofSize).toMatchInlineSnapshot(`31612`); } }) @@ -95,8 +84,8 @@ describeSuite({ const { weight, proofSize } = await calculateWeight(context, unfreezeAsset); - expect(weight).toMatchInlineSnapshot(`4945940000`); - expect(proofSize).toMatchInlineSnapshot(`3302014`); + expect(weight).toMatchInlineSnapshot(`1279934000`); + expect(proofSize).toMatchInlineSnapshot(`31612`); } }) @@ -109,7 +98,7 @@ describeSuite({ const assetId = 1n; // Register the asset - const {contractAddress, registeredAssetId } = await registerForeignAsset(context, assetId, assetLocation, relayAssetMetadata); + const {contractAddress} = await registerForeignAsset(context, assetId, assetLocation, relayAssetMetadata); // Mock asset balance await mockAssetBalance(context, someBalance, assetId, alith, ALITH_ADDRESS); From 7848e0459320b97fcc6d596a5521adf916580dba Mon Sep 17 00:00:00 2001 From: Pablo Labarta Date: Tue, 4 Feb 2025 12:37:48 -0300 Subject: [PATCH 3/3] appease biome --- .../test-assets/test-foreign-assets-costs.ts | 219 +++++++++--------- 1 file changed, 112 insertions(+), 107 deletions(-) diff --git a/test/suites/dev/moonbase/test-assets/test-foreign-assets-costs.ts b/test/suites/dev/moonbase/test-assets/test-foreign-assets-costs.ts index af32578c142..34c900ecf8f 100644 --- a/test/suites/dev/moonbase/test-assets/test-foreign-assets-costs.ts +++ b/test/suites/dev/moonbase/test-assets/test-foreign-assets-costs.ts @@ -1,9 +1,7 @@ import "@moonbeam-network/api-augment"; import "@moonbeam-network/api-augment/moonbase"; -import { describeSuite, expect, beforeAll, fetchCompiledContract } from "@moonwall/cli"; +import { describeSuite, expect, fetchCompiledContract } from "@moonwall/cli"; import { - ARBITRARY_ASSET_ID, - PARA_1000_SOURCE_LOCATION_V4, RELAY_SOURCE_LOCATION_V4, foreignAssetBalance, mockAssetBalance, @@ -11,8 +9,6 @@ import { registerForeignAsset, relayAssetMetadata, } from "../../../../helpers"; -import { parseAbi } from "viem"; -import exp from "constants"; import { alith, ALITH_ADDRESS, BALTATHAR_ADDRESS } from "@moonwall/util"; describeSuite({ @@ -21,121 +17,130 @@ describeSuite({ foundationMethods: "dev", testCases: ({ context, it }) => { let assetId: bigint; - let assetLocation = RELAY_SOURCE_LOCATION_V4; - let xcmLoc = patchLocationV4recursively(assetLocation); - let decimals = 18; + const assetLocation = RELAY_SOURCE_LOCATION_V4; + const xcmLoc = patchLocationV4recursively(assetLocation); + const decimals = 18; it({ - id: "T01", - "title": "Cost of creating an asset", - test: async function () { - - const createAsset = context.polkadotJs().tx.sudo.sudo( - context.polkadotJs().tx.evmForeignAssets.createForeignAsset(assetId, xcmLoc, decimals, "test", "test") - ); - - const { weight, proofSize } = await calculateWeight(context, createAsset); - - expect(weight).toMatchInlineSnapshot(`2435166000`); - expect(proofSize).toMatchInlineSnapshot(`14519`); - } - }) + id: "T01", + title: "Cost of creating an asset", + test: async function () { + const createAsset = context + .polkadotJs() + .tx.sudo.sudo( + context + .polkadotJs() + .tx.evmForeignAssets.createForeignAsset(assetId, xcmLoc, decimals, "test", "test") + ); + + const { weight, proofSize } = await calculateWeight(context, createAsset); + + expect(weight).toMatchInlineSnapshot(`2435166000`); + expect(proofSize).toMatchInlineSnapshot(`14519`); + }, + }); it({ - id: "T01", - "title": "Cost of changing an asset location", - test: async function () { - - const changeLocation = context.polkadotJs().tx.sudo.sudo( - context.polkadotJs().tx.evmForeignAssets.changeXcmLocation(assetId, xcmLoc) - ); - - const { weight, proofSize } = await calculateWeight(context, changeLocation); - - expect(weight).toMatchInlineSnapshot(`443464000`); - expect(proofSize).toMatchInlineSnapshot(`7594`); - } - }) + id: "T01", + title: "Cost of changing an asset location", + test: async function () { + const changeLocation = context + .polkadotJs() + .tx.sudo.sudo( + context.polkadotJs().tx.evmForeignAssets.changeXcmLocation(assetId, xcmLoc) + ); + + const { weight, proofSize } = await calculateWeight(context, changeLocation); + + expect(weight).toMatchInlineSnapshot(`443464000`); + expect(proofSize).toMatchInlineSnapshot(`7594`); + }, + }); it({ - id: "T01", - "title": "Cost of freezing an asset", - test: async function () { - - const freezeAsset = context.polkadotJs().tx.sudo.sudo( - context.polkadotJs().tx.evmForeignAssets.freezeForeignAsset(assetId, false) - ); - - const { weight, proofSize } = await calculateWeight(context, freezeAsset); - - expect(weight).toMatchInlineSnapshot(`1279934000`); - expect(proofSize).toMatchInlineSnapshot(`31612`); - } - }) + id: "T01", + title: "Cost of freezing an asset", + test: async function () { + const freezeAsset = context + .polkadotJs() + .tx.sudo.sudo( + context.polkadotJs().tx.evmForeignAssets.freezeForeignAsset(assetId, false) + ); + + const { weight, proofSize } = await calculateWeight(context, freezeAsset); + + expect(weight).toMatchInlineSnapshot(`1279934000`); + expect(proofSize).toMatchInlineSnapshot(`31612`); + }, + }); it({ - id: "T01", - "title": "Cost of unfreezing an asset", - test: async function () { + id: "T01", + title: "Cost of unfreezing an asset", + test: async function () { + const unfreezeAsset = context + .polkadotJs() + .tx.sudo.sudo(context.polkadotJs().tx.evmForeignAssets.freezeForeignAsset(assetId, true)); - const unfreezeAsset = context.polkadotJs().tx.sudo.sudo( - context.polkadotJs().tx.evmForeignAssets.freezeForeignAsset(assetId, true) - ); + const { weight, proofSize } = await calculateWeight(context, unfreezeAsset); - const { weight, proofSize } = await calculateWeight(context, unfreezeAsset); - - expect(weight).toMatchInlineSnapshot(`1279934000`); - expect(proofSize).toMatchInlineSnapshot(`31612`); - } - }) + expect(weight).toMatchInlineSnapshot(`1279934000`); + expect(proofSize).toMatchInlineSnapshot(`31612`); + }, + }); it({ - id: "T01", - "title": "Size of the created contract", - test: async function () { - const someBalance = 100_000_000_000_000n; - const assetLocation = RELAY_SOURCE_LOCATION_V4; - const assetId = 1n; - - // Register the asset - const {contractAddress} = await registerForeignAsset(context, assetId, assetLocation, relayAssetMetadata); - // Mock asset balance - await mockAssetBalance(context, someBalance, assetId, alith, ALITH_ADDRESS); - - const newBalance = await foreignAssetBalance(context, assetId, ALITH_ADDRESS); - expect(newBalance).toBe(someBalance); - - - const { request } = await context.viem().simulateContract({ - address: contractAddress as `0x${string}`, - abi: fetchCompiledContract("ERC20Instance").abi, - functionName: "transfer", - args: [BALTATHAR_ADDRESS, someBalance / 2n], - }); - - const hash = await context.viem().writeContract(request); - - await context.createBlock(); - - const receipt = await context.viem().getTransactionReceipt({hash}); - - console.log(receipt); - - expect(receipt.status).toBe('success'); - expect(receipt.gasUsed).toMatchInlineSnapshot(`154976n`); - - const transferredBalance = await foreignAssetBalance(context, assetId, BALTATHAR_ADDRESS); - expect(transferredBalance).toBe(someBalance / 2n); - - } - }) + id: "T01", + title: "Size of the created contract", + test: async function () { + const someBalance = 100_000_000_000_000n; + const assetLocation = RELAY_SOURCE_LOCATION_V4; + const assetId = 1n; + + // Register the asset + const { contractAddress } = await registerForeignAsset( + context, + assetId, + assetLocation, + relayAssetMetadata + ); + // Mock asset balance + await mockAssetBalance(context, someBalance, assetId, alith, ALITH_ADDRESS); + + const newBalance = await foreignAssetBalance(context, assetId, ALITH_ADDRESS); + expect(newBalance).toBe(someBalance); + + const { request } = await context.viem().simulateContract({ + address: contractAddress as `0x${string}`, + abi: fetchCompiledContract("ERC20Instance").abi, + functionName: "transfer", + args: [BALTATHAR_ADDRESS, someBalance / 2n], + }); + + const hash = await context.viem().writeContract(request); + + await context.createBlock(); + + const receipt = await context.viem().getTransactionReceipt({ hash }); + + console.log(receipt); + + expect(receipt.status).toBe("success"); + expect(receipt.gasUsed).toMatchInlineSnapshot(`154976n`); + + const transferredBalance = await foreignAssetBalance(context, assetId, BALTATHAR_ADDRESS); + expect(transferredBalance).toBe(someBalance / 2n); + }, + }); }, }); async function calculateWeight(context, transaction) { - const info = await context.polkadotJs().call.transactionPaymentApi.queryInfo(transaction.toU8a(), transaction.encodedLength); - return { - weight: info.weight.refTime, - proofSize: info.weight.proofSize - } -} \ No newline at end of file + const info = await context + .polkadotJs() + .call.transactionPaymentApi.queryInfo(transaction.toU8a(), transaction.encodedLength); + return { + weight: info.weight.refTime, + proofSize: info.weight.proofSize, + }; +}