From 19f2d561ba5c66c8c047ba5e97f12d4d9aacb4e3 Mon Sep 17 00:00:00 2001 From: mmaurello <93129175+mmaurello@users.noreply.github.com> Date: Fri, 21 Oct 2022 11:22:46 +0200 Subject: [PATCH] Configure minimal balance for GLMR and MOVR on Acala and Karura (#36) * [WIP] solution for considering GLMR - Acala min balance * add min balance to movr-karura * Fixing types * Fixing tests * do not consider existential deposit when is native asset Co-authored-by: Elmar Kenigs --- .../balance/__snapshots__/balance.test.ts.snap | 17 ++++++++++++++++- .../config/src/balance/balance.constants.ts | 2 ++ .../config/src/balance/balance.interfaces.ts | 13 ++++++++++++- packages/config/src/balance/balance.test.ts | 8 +++++--- packages/config/src/balance/balance.ts | 17 ++++++++++++++--- .../config/src/config/moonbase/assets/tt1.ts | 4 ++-- .../config/src/config/moonbeam/assets/glmr.ts | 1 + .../config/src/config/moonbeam/assets/usdt.ts | 2 +- .../config/src/config/moonriver/assets/movr.ts | 1 + .../config/src/config/moonriver/assets/rmrk.ts | 4 ++-- .../config/src/config/moonriver/assets/usdt.ts | 4 ++-- packages/sdk/src/polkadot/PolkadotService.ts | 4 +++- packages/sdk/src/sdk/sdk.withdraw.ts | 2 +- 13 files changed, 62 insertions(+), 17 deletions(-) diff --git a/packages/config/src/balance/__snapshots__/balance.test.ts.snap b/packages/config/src/balance/__snapshots__/balance.test.ts.snap index 906ca290..3dded7de 100644 --- a/packages/config/src/balance/__snapshots__/balance.test.ts.snap +++ b/packages/config/src/balance/__snapshots__/balance.test.ts.snap @@ -19,7 +19,7 @@ exports[`balance assets should get correct params 1`] = ` ] `; -exports[`balance min should be correct min config 1`] = ` +exports[`balance min should be correct minAssetPallet config 1`] = ` { "function": "asset", "pallet": "assets", @@ -32,6 +32,21 @@ exports[`balance min should be correct min config 1`] = ` } `; +exports[`balance min should be correct minAssetRegistryPallet config 1`] = ` +{ + "function": "assetMetadatas", + "pallet": "assetRegistry", + "params": [ + { + "ForeignAssetId": 1, + }, + ], + "path": [ + "minimalBalance", + ], +} +`; + exports[`balance system should be correct system config 1`] = ` { "calc": [Function], diff --git a/packages/config/src/balance/balance.constants.ts b/packages/config/src/balance/balance.constants.ts index 3ceb5800..e76dc4a2 100644 --- a/packages/config/src/balance/balance.constants.ts +++ b/packages/config/src/balance/balance.constants.ts @@ -1,5 +1,6 @@ export enum BalancePallet { Assets = 'assets', + AssetRegistry = 'assetRegistry', OrmlTokens = 'ormlTokens', System = 'system', Tokens = 'tokens', @@ -9,4 +10,5 @@ export enum BalanceFunction { Account = 'account', Accounts = 'accounts', Asset = 'asset', + AssetMetadatas = 'assetMetadatas', } diff --git a/packages/config/src/balance/balance.interfaces.ts b/packages/config/src/balance/balance.interfaces.ts index 63d12005..2116e4d6 100644 --- a/packages/config/src/balance/balance.interfaces.ts +++ b/packages/config/src/balance/balance.interfaces.ts @@ -58,9 +58,20 @@ export interface TokensBalanceConfig< calc: (data: TokensPalletAccountData) => bigint; } -export interface MinBalanceConfig { +export type MinBalanceConfig = + | MinBalanceAssetsConfig + | MinBalanceAssetRegistryConfig; + +export interface MinBalanceAssetsConfig { pallet: BalancePallet.Assets; function: BalanceFunction.Asset; path: ['minBalance']; params: [AssetId]; } + +export interface MinBalanceAssetRegistryConfig { + pallet: BalancePallet.AssetRegistry; + function: BalanceFunction.AssetMetadatas; + path: ['minimalBalance']; + params: [{ ForeignAssetId: AssetId }]; +} diff --git a/packages/config/src/balance/balance.test.ts b/packages/config/src/balance/balance.test.ts index 256f1b43..43656518 100644 --- a/packages/config/src/balance/balance.test.ts +++ b/packages/config/src/balance/balance.test.ts @@ -17,10 +17,12 @@ describe('balance', () => { }); describe('min', () => { - const cfg = balance.min(1); + it('should be correct minAssetPallet config', () => { + expect(balance.minAssetPallet(1)).toMatchSnapshot(); + }); - it('should be correct min config', () => { - expect(cfg).toMatchSnapshot(); + it('should be correct minAssetRegistryPallet config', () => { + expect(balance.minAssetRegistryPallet(1)).toMatchSnapshot(); }); }); diff --git a/packages/config/src/balance/balance.ts b/packages/config/src/balance/balance.ts index c6db3811..b4bf3c99 100644 --- a/packages/config/src/balance/balance.ts +++ b/packages/config/src/balance/balance.ts @@ -7,7 +7,8 @@ import { AssetId } from '../interfaces'; import { BalanceFunction, BalancePallet } from './balance.constants'; import { AssetsBalanceConfig, - MinBalanceConfig, + MinBalanceAssetRegistryConfig, + MinBalanceAssetsConfig, OrmlTokensBalanceConfig, SystemBalanceConfig, TokensBalanceConfig, @@ -20,7 +21,8 @@ export function createBalanceBuilder< >() { return { assets, - min, + minAssetPallet, + minAssetRegistryPallet, ormlTokens, system, tokens: (asset: number | bigint | Symbols | 'MOVR' | 'KUSD' | 'AUSD') => @@ -38,7 +40,7 @@ function assets(asset: AssetId): AssetsBalanceConfig { }; } -function min(asset: AssetId): MinBalanceConfig { +function minAssetPallet(asset: AssetId): MinBalanceAssetsConfig { return { pallet: BalancePallet.Assets, function: BalanceFunction.Asset, @@ -47,6 +49,15 @@ function min(asset: AssetId): MinBalanceConfig { }; } +function minAssetRegistryPallet(asset: AssetId): MinBalanceAssetRegistryConfig { + return { + pallet: BalancePallet.AssetRegistry, + function: BalanceFunction.AssetMetadatas, + path: ['minimalBalance'], + params: [{ ForeignAssetId: asset }], + }; +} + function ormlTokens(asset: AssetId): OrmlTokensBalanceConfig { return { pallet: BalancePallet.OrmlTokens, diff --git a/packages/config/src/config/moonbase/assets/tt1.ts b/packages/config/src/config/moonbase/assets/tt1.ts index ca4986d3..162dcc0c 100644 --- a/packages/config/src/config/moonbase/assets/tt1.ts +++ b/packages/config/src/config/moonbase/assets/tt1.ts @@ -22,7 +22,7 @@ export const TT1: MoonbaseXcmConfig = { source: origin, balance: balance.assets(originAssetId), sourceFeeBalance: balance.system(), - sourceMinBalance: balance.min(originAssetId), + sourceMinBalance: balance.minAssetPallet(originAssetId), extrinsic: extrinsic .polkadotXcm() .limitedReserveTransferAssets() @@ -35,7 +35,7 @@ export const TT1: MoonbaseXcmConfig = { withdraw: { [origin.key]: withdraw.xTokens({ balance: balance.assets(originAssetId), - sourceMinBalance: balance.min(originAssetId), + sourceMinBalance: balance.minAssetPallet(originAssetId), destination: origin, feePerWeight: 1_265_000_000, }), diff --git a/packages/config/src/config/moonbeam/assets/glmr.ts b/packages/config/src/config/moonbeam/assets/glmr.ts index 5ca6bfef..a89a1ebb 100644 --- a/packages/config/src/config/moonbeam/assets/glmr.ts +++ b/packages/config/src/config/moonbeam/assets/glmr.ts @@ -84,6 +84,7 @@ export const GLMR: MoonbeamXcmConfig = { balance: balance.tokens(acalaGlmrId), destination: acala, feePerWeight: 8_000_000, + sourceMinBalance: balance.minAssetRegistryPallet(acalaGlmrId), }), [astar.key]: withdraw.xTokens({ balance: balance.assets(astarGlmrId), diff --git a/packages/config/src/config/moonbeam/assets/usdt.ts b/packages/config/src/config/moonbeam/assets/usdt.ts index 613e7178..538836af 100644 --- a/packages/config/src/config/moonbeam/assets/usdt.ts +++ b/packages/config/src/config/moonbeam/assets/usdt.ts @@ -16,7 +16,7 @@ export const USDT: MoonbeamXcmConfig = { source: origin, balance: balance.assets(originAssetId), sourceFeeBalance: balance.system(), - sourceMinBalance: balance.min(originAssetId), + sourceMinBalance: balance.minAssetPallet(originAssetId), extrinsic: extrinsic .polkadotXcm() .limitedReserveTransferAssets() diff --git a/packages/config/src/config/moonriver/assets/movr.ts b/packages/config/src/config/moonriver/assets/movr.ts index 874a0990..9e3ee8a0 100644 --- a/packages/config/src/config/moonriver/assets/movr.ts +++ b/packages/config/src/config/moonriver/assets/movr.ts @@ -103,6 +103,7 @@ export const MOVR: MoonriverXcmConfig = { balance: balance.tokens(karuraMovrId), destination: karura, feePerWeight: 50_000, + sourceMinBalance: balance.minAssetRegistryPallet(karuraMovrId), }), [khala.key]: withdraw.xTokens({ balance: balance.assets(khalaMovrId), diff --git a/packages/config/src/config/moonriver/assets/rmrk.ts b/packages/config/src/config/moonriver/assets/rmrk.ts index 50a1d945..2b46a9ee 100644 --- a/packages/config/src/config/moonriver/assets/rmrk.ts +++ b/packages/config/src/config/moonriver/assets/rmrk.ts @@ -22,7 +22,7 @@ export const RMRK: MoonriverXcmConfig = { source: origin, balance: balance.assets(originAssetId), sourceFeeBalance: balance.system(), - sourceMinBalance: balance.min(originAssetId), + sourceMinBalance: balance.minAssetPallet(originAssetId), extrinsic: extrinsic .polkadotXcm() .limitedReserveTransferAssets() @@ -35,7 +35,7 @@ export const RMRK: MoonriverXcmConfig = { withdraw: { [origin.key]: withdraw.xTokens({ balance: balance.assets(originAssetId), - sourceMinBalance: balance.min(originAssetId), + sourceMinBalance: balance.minAssetPallet(originAssetId), destination: origin, feePerWeight: 0.000126, }), diff --git a/packages/config/src/config/moonriver/assets/usdt.ts b/packages/config/src/config/moonriver/assets/usdt.ts index 91b709bf..69844ebc 100644 --- a/packages/config/src/config/moonriver/assets/usdt.ts +++ b/packages/config/src/config/moonriver/assets/usdt.ts @@ -22,7 +22,7 @@ export const USDT: MoonriverXcmConfig = { source: origin, balance: balance.assets(originAssetId), sourceFeeBalance: balance.system(), - sourceMinBalance: balance.min(originAssetId), + sourceMinBalance: balance.minAssetPallet(originAssetId), extrinsic: extrinsic .polkadotXcm() .limitedReserveTransferAssets() @@ -35,7 +35,7 @@ export const USDT: MoonriverXcmConfig = { withdraw: { [origin.key]: withdraw.xTokens({ balance: balance.assets(originAssetId), - sourceMinBalance: balance.min(originAssetId), + sourceMinBalance: balance.minAssetPallet(originAssetId), destination: origin, feePerWeight: 0.00000126, }), diff --git a/packages/sdk/src/polkadot/PolkadotService.ts b/packages/sdk/src/polkadot/PolkadotService.ts index 087cd686..b9047dfb 100644 --- a/packages/sdk/src/polkadot/PolkadotService.ts +++ b/packages/sdk/src/polkadot/PolkadotService.ts @@ -109,7 +109,9 @@ export class PolkadotService< params, path, }: MinBalanceConfig): Promise { - const details = await this.#api.query[pallet][fn](...params); + const details = (await this.#api.query[pallet][fn]( + ...params, + )) as Option; if (details.isEmpty) { return 0n; diff --git a/packages/sdk/src/sdk/sdk.withdraw.ts b/packages/sdk/src/sdk/sdk.withdraw.ts index cf2d91e2..b6766bcf 100644 --- a/packages/sdk/src/sdk/sdk.withdraw.ts +++ b/packages/sdk/src/sdk/sdk.withdraw.ts @@ -94,7 +94,7 @@ export async function getWithdrawData< await Promise.all([ asset.isNative ? moonChain.decimals : polkadot.getAssetDecimals(asset), destinationPolkadot.getGenericBalance(destinationAccount, config.balance), - destinationPolkadot.getExistentialDeposit(), + asset.isNative ? 0n : destinationPolkadot.getExistentialDeposit(), config.sourceMinBalance ? destinationPolkadot.getAssetMinBalance(config.sourceMinBalance) : 0n,