Skip to content

Commit

Permalink
Configure minimal balance for GLMR and MOVR on Acala and Karura (#36)
Browse files Browse the repository at this point in the history
* [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 <[email protected]>
  • Loading branch information
mmaurello and Elmar Kenigs authored Oct 21, 2022
1 parent 2578ec1 commit 19f2d56
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 17 deletions.
17 changes: 16 additions & 1 deletion packages/config/src/balance/__snapshots__/balance.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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],
Expand Down
2 changes: 2 additions & 0 deletions packages/config/src/balance/balance.constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export enum BalancePallet {
Assets = 'assets',
AssetRegistry = 'assetRegistry',
OrmlTokens = 'ormlTokens',
System = 'system',
Tokens = 'tokens',
Expand All @@ -9,4 +10,5 @@ export enum BalanceFunction {
Account = 'account',
Accounts = 'accounts',
Asset = 'asset',
AssetMetadatas = 'assetMetadatas',
}
13 changes: 12 additions & 1 deletion packages/config/src/balance/balance.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }];
}
8 changes: 5 additions & 3 deletions packages/config/src/balance/balance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

Expand Down
17 changes: 14 additions & 3 deletions packages/config/src/balance/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { AssetId } from '../interfaces';
import { BalanceFunction, BalancePallet } from './balance.constants';
import {
AssetsBalanceConfig,
MinBalanceConfig,
MinBalanceAssetRegistryConfig,
MinBalanceAssetsConfig,
OrmlTokensBalanceConfig,
SystemBalanceConfig,
TokensBalanceConfig,
Expand All @@ -20,7 +21,8 @@ export function createBalanceBuilder<
>() {
return {
assets,
min,
minAssetPallet,
minAssetRegistryPallet,
ormlTokens,
system,
tokens: (asset: number | bigint | Symbols | 'MOVR' | 'KUSD' | 'AUSD') =>
Expand All @@ -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,
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/config/src/config/moonbase/assets/tt1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
}),
Expand Down
1 change: 1 addition & 0 deletions packages/config/src/config/moonbeam/assets/glmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion packages/config/src/config/moonbeam/assets/usdt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions packages/config/src/config/moonriver/assets/movr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions packages/config/src/config/moonriver/assets/rmrk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
}),
Expand Down
4 changes: 2 additions & 2 deletions packages/config/src/config/moonriver/assets/usdt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
}),
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/polkadot/PolkadotService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ export class PolkadotService<
params,
path,
}: MinBalanceConfig): Promise<bigint> {
const details = await this.#api.query[pallet][fn](...params);
const details = (await this.#api.query[pallet][fn](
...params,
)) as Option<any>;

if (details.isEmpty) {
return 0n;
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/sdk/sdk.withdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 19f2d56

Please sign in to comment.