From b552b638fde345066a9a6b6ef32203b257a1fd85 Mon Sep 17 00:00:00 2001 From: Mario J Maurello Date: Wed, 18 Sep 2024 12:52:40 +0200 Subject: [PATCH] -wip- fix some types --- packages/builder/src/fee/FeeBuilder.ts | 19 ++++---- packages/builder/src/fee/FeeBuilder.utils.ts | 50 +++++++++++--------- 2 files changed, 38 insertions(+), 31 deletions(-) diff --git a/packages/builder/src/fee/FeeBuilder.ts b/packages/builder/src/fee/FeeBuilder.ts index f533a0b9..5b759f03 100644 --- a/packages/builder/src/fee/FeeBuilder.ts +++ b/packages/builder/src/fee/FeeBuilder.ts @@ -56,12 +56,13 @@ function xcmPaymentApi() { // TODO mjm rename assetId? const versionedAssetId = await getVersionedAssetId(api, asset); console.log('versionedAssetId', versionedAssetId); + const assets = [versionedAssetId]; const instructions = [ - getWithdrawAssetInstruction([versionedAssetId]), + getWithdrawAssetInstruction(assets), getClearOriginInstruction(), getBuyExecutionInstruction(versionedAssetId), - getDepositAssetInstruction(address), + getDepositAssetInstruction(address, assets), ]; return getFeeForXcmInstructionsAndAsset( @@ -78,12 +79,13 @@ function xcmPaymentApi() { api, call: async (): Promise => { const versionedAssetId = await getVersionedAssetId(api, asset); + const assets = [versionedAssetId]; const instructions = [ - getWithdrawAssetInstruction([versionedAssetId]), + getWithdrawAssetInstruction(assets), getClearOriginInstruction(), getBuyExecutionInstruction(versionedAssetId), - getDepositAssetInstruction(address), + getDepositAssetInstruction(address, assets), ]; return getFeeForXcmInstructionsAndAsset( @@ -105,14 +107,13 @@ function xcmPaymentApi() { transferAsset, ); + const assets = [versionedAssetId, versionedTransferAssetId]; + const instructions = [ - getWithdrawAssetInstruction([ - versionedAssetId, - versionedTransferAssetId, - ]), + getWithdrawAssetInstruction(assets), getClearOriginInstruction(), getBuyExecutionInstruction(versionedAssetId), - getDepositAssetInstruction(address), + getDepositAssetInstruction(address, assets), ]; return getFeeForXcmInstructionsAndAsset( diff --git a/packages/builder/src/fee/FeeBuilder.utils.ts b/packages/builder/src/fee/FeeBuilder.utils.ts index 5d4d6114..d9febb5f 100644 --- a/packages/builder/src/fee/FeeBuilder.utils.ts +++ b/packages/builder/src/fee/FeeBuilder.utils.ts @@ -10,7 +10,7 @@ const DEFAULT_AMOUNT = 10 * 18; const moonChainNativeAssetId = '0x0000000000000000000000000000000000000802'; -export function getWithdrawAssetInstruction(assetTypes: any[]) { +export function getWithdrawAssetInstruction(assetTypes: object[]) { return { WithdrawAsset: assetTypes.map((assetType) => ({ fun: { @@ -21,12 +21,12 @@ export function getWithdrawAssetInstruction(assetTypes: any[]) { }; } -export function getReserveAssetDepositedInstruction(assetType) { +export function getReserveAssetDepositedInstruction(assetType: object) { return { ReserveAssetDeposited: [ { fun: { - Fungible: '1', + Fungible: DEFAULT_AMOUNT, }, id: { ...assetType }, }, @@ -40,7 +40,7 @@ export function getClearOriginInstruction() { }; } -export function getBuyExecutionInstruction(assetType) { +export function getBuyExecutionInstruction(assetType: object) { return { BuyExecution: { fees: { @@ -58,12 +58,12 @@ export function getBuyExecutionInstruction(assetType) { }; } -export function getDepositAssetInstruction(address: string) { +export function getDepositAssetInstruction(address: string, assets: object[]) { return { DepositAsset: { assets: { Wild: { - AllCounted: 1, + AllCounted: assets.length, }, }, beneficiary: { @@ -86,9 +86,10 @@ export async function getAssetIdType( api: ApiPromise, asset: ChainAssetId, ): Promise> { - const type = (await api.query.assetManager.assetIdType( - asset, - )) as unknown as Option; + const type = + await api.query.assetManager.assetIdType< + Option + >(asset); if (type.isNone || !type.unwrap().isXcm) { throw new Error(`No asset type found for asset ${asset}`); @@ -101,15 +102,17 @@ export async function getVersionedAssetId( api: ApiPromise, asset: ChainAssetId, ) { - const acceptablePaymentAssetsResult: Result< - Vec, - PolkadotError - > = await api.call.xcmPaymentApi.queryAcceptablePaymentAssets(3); + const acceptablePaymentAssetsResult = + await api.call.xcmPaymentApi.queryAcceptablePaymentAssets< + Result, PolkadotError> + >(3); const acceptablePaymentAssets = acceptablePaymentAssetsResult.isOk ? acceptablePaymentAssetsResult.asOk.map((value) => value.asV3) : []; - - // console.log('acceptablePaymentAssets', acceptablePaymentAssets); + console.log( + 'acceptablePaymentAssets', + acceptablePaymentAssets.map((value) => value.toHuman()), + ); // TODO mjm verify that the assets Id are in the acceptablePaymentAssets if (asset === moonChainNativeAssetId) { @@ -156,12 +159,13 @@ export async function getVersionedAssetId( export async function getFeeForXcmInstructionsAndAsset( api: ApiPromise, instructions: AnyJson, - versionedAssetId: any, // TODO mjm + versionedAssetId: object, // TODO mjm ) { - const xcmToWeightResult: Result = - await api.call.xcmPaymentApi.queryXcmWeight({ - V3: instructions, - }); + const xcmToWeightResult = await api.call.xcmPaymentApi.queryXcmWeight< + Result + >({ + V3: instructions, + }); console.log('xcmToWeightResult', xcmToWeightResult.toHuman()); if (!xcmToWeightResult.isOk) { throw new Error( @@ -170,8 +174,10 @@ export async function getFeeForXcmInstructionsAndAsset( } const xcmToWeight = xcmToWeightResult.asOk; - const weightToForeingAssets: Result = - await api.call.xcmPaymentApi.queryWeightToAssetFee(xcmToWeight, { + const weightToForeingAssets = + await api.call.xcmPaymentApi.queryWeightToAssetFee< + Result + >(xcmToWeight, { V3: { ...versionedAssetId, },