From 6e01259d0813a33db9d05a28a5c91aa8560720f4 Mon Sep 17 00:00:00 2001 From: Elmar Kenigs Date: Thu, 30 Mar 2023 13:33:34 +0300 Subject: [PATCH] Equilibrium integration (#66) --- .../config/src/balance/balance.interfaces.ts | 12 +++-- packages/config/src/balance/balance.ts | 26 ++++++++++ .../config/src/config/moonbase/assets/dev.ts | 21 ++++++++ .../config/src/config/moonbase/assets/eq.ts | 41 +++++++++++++++ .../config/src/config/moonbase/assets/eqd.ts | 49 ++++++++++++++++++ .../src/config/moonbase/moonbase.assets.ts | 12 +++++ .../src/config/moonbase/moonbase.chains.ts | 17 ++++++- .../config/src/config/moonbase/moonbase.ts | 4 ++ packages/config/src/constants/assets.ts | 4 +- packages/config/src/constants/chains.ts | 1 + .../__snapshots__/eqBalances.test.ts.snap | 34 +++++++++++++ .../eqBalances/eqBalances.constants.ts | 13 +++++ .../eqBalances/eqBalances.interfaces.ts | 51 +++++++++++++++++++ .../extrinsic/eqBalances/eqBalances.test.ts | 37 ++++++++++++++ .../src/extrinsic/eqBalances/eqBalances.ts | 50 ++++++++++++++++++ .../config/src/extrinsic/eqBalances/index.ts | 3 ++ .../src/extrinsic/extrinsic.constants.ts | 1 + packages/config/src/extrinsic/extrinsic.ts | 2 + packages/config/src/interfaces.ts | 1 + packages/sdk/src/polkadot/PolkadotService.ts | 26 +++++----- packages/sdk/src/sdk/sdk.deposit.ts | 30 +++++------ packages/sdk/src/sdk/sdk.interfaces.ts | 10 ++-- packages/sdk/src/sdk/sdk.withdraw.ts | 2 + 23 files changed, 406 insertions(+), 41 deletions(-) create mode 100644 packages/config/src/config/moonbase/assets/eq.ts create mode 100644 packages/config/src/config/moonbase/assets/eqd.ts create mode 100644 packages/config/src/extrinsic/eqBalances/__snapshots__/eqBalances.test.ts.snap create mode 100644 packages/config/src/extrinsic/eqBalances/eqBalances.constants.ts create mode 100644 packages/config/src/extrinsic/eqBalances/eqBalances.interfaces.ts create mode 100644 packages/config/src/extrinsic/eqBalances/eqBalances.test.ts create mode 100644 packages/config/src/extrinsic/eqBalances/eqBalances.ts create mode 100644 packages/config/src/extrinsic/eqBalances/index.ts diff --git a/packages/config/src/balance/balance.interfaces.ts b/packages/config/src/balance/balance.interfaces.ts index 93cf1340..e29cbed7 100644 --- a/packages/config/src/balance/balance.interfaces.ts +++ b/packages/config/src/balance/balance.interfaces.ts @@ -1,4 +1,4 @@ -import { Struct, u128 } from '@polkadot/types'; +import { Struct, Type, u128 } from '@polkadot/types'; import { PalletBalancesAccountData } from '@polkadot/types/lookup'; import { AssetSymbol } from '../constants'; import { AssetId } from '../interfaces'; @@ -11,17 +11,23 @@ import { export type BalanceConfig = | SystemBalanceConfig | AssetsBalanceConfig + | EquilibriumSystemBalanceConfig | OrmlTokensBalanceConfig | TokensBalanceConfig; -export interface SystemBalanceConfig { +export interface SystemBalanceConfig { pallet: BalancePallet.System; function: BalanceFunction.Account; path: ['data']; getParams: (account: string) => [string]; - calc: (data: PalletBalancesAccountData) => bigint; + calc: (data: Data) => bigint; } +export type EquilibriumSystemBalanceConfig = SystemBalanceConfig; +export type EquilibriumSystemBalanceData = Array< + [number, { positive: number }] +>; + export interface AssetsBalanceConfig { pallet: BalancePallet.Assets; function: BalanceFunction.Account; diff --git a/packages/config/src/balance/balance.ts b/packages/config/src/balance/balance.ts index 63c486f8..8a3e33de 100644 --- a/packages/config/src/balance/balance.ts +++ b/packages/config/src/balance/balance.ts @@ -11,6 +11,8 @@ import { } from './balance.constants'; import { AssetsBalanceConfig, + EquilibriumSystemBalanceConfig, + EquilibriumSystemBalanceData, MinBalanceAssetRegistryConfig, MinBalanceAssetsConfig, OrmlTokensBalanceConfig, @@ -32,6 +34,7 @@ export function createBalanceBuilder< minCurrencyMetadata, ormlTokens, system, + systemEquilibrium, tokens: () => tokens(), }; } @@ -100,6 +103,29 @@ function system(): SystemBalanceConfig { }; } +function systemEquilibrium(id: number): EquilibriumSystemBalanceConfig { + return { + pallet: BalancePallet.System, + function: BalanceFunction.Account, + path: ['data'], + getParams: (account: string) => [account], + calc: (data) => { + if (data.isEmpty) { + return 0n; + } + + const balances = data.toJSON() as any as EquilibriumSystemBalanceData; + const balance = balances.find(([assetId]) => assetId === id); + + if (!balance) { + return 0n; + } + + return BigInt(balance[1].positive); + }, + }; +} + function tokens() { return { foreignAsset: (asset: AssetId | Symbols) => diff --git a/packages/config/src/config/moonbase/assets/dev.ts b/packages/config/src/config/moonbase/assets/dev.ts index 3e45d966..9f1e27dd 100644 --- a/packages/config/src/config/moonbase/assets/dev.ts +++ b/packages/config/src/config/moonbase/assets/dev.ts @@ -3,6 +3,10 @@ import { XTokensExtrinsicCurrencyTypes, XTokensExtrinsicSuccessEvent, } from '../../../extrinsic'; +import { + EqBalancesFee, + EqBalancesSuccessEvent, +} from '../../../extrinsic/eqBalances'; import { getMoonAssetId } from '../../config.utils'; import { assets, @@ -15,14 +19,26 @@ import { import { MoonbaseXcmConfig } from '../moonbase.interfaces'; const asset = assets[AssetSymbol.DEV]; +const eqa = chains[ChainKey.EquilibriumAlphanet]; const pioneer = chains[ChainKey.BitCountryPioneer]; const pioneerDevId = getMoonAssetId(pioneer); +const eqaDevId = getMoonAssetId(eqa) as number; export const DEV: MoonbaseXcmConfig = { asset, origin: moonbase, deposit: { + [eqa.key]: { + source: eqa, + balance: balance.systemEquilibrium(eqaDevId), + extrinsic: extrinsic + .eqBalances() + .xcmTransfer() + .successEvent(EqBalancesSuccessEvent.ExtrinsicSuccess) + .asset(eqaDevId) + .fee(EqBalancesFee.SovereignAccWillPay), + }, [pioneer.key]: { source: pioneer, balance: balance.tokens().fungibleToken(pioneerDevId), @@ -36,6 +52,11 @@ export const DEV: MoonbaseXcmConfig = { }, }, withdraw: { + [eqa.key]: withdraw.xTokens({ + balance: balance.systemEquilibrium(eqaDevId), + destination: eqa, + feePerWeight: 0.1, + }), [pioneer.key]: withdraw.xTokens({ balance: balance.tokens().fungibleToken(pioneerDevId), destination: pioneer, diff --git a/packages/config/src/config/moonbase/assets/eq.ts b/packages/config/src/config/moonbase/assets/eq.ts new file mode 100644 index 00000000..587c341c --- /dev/null +++ b/packages/config/src/config/moonbase/assets/eq.ts @@ -0,0 +1,41 @@ +import { AssetSymbol, ChainKey } from '../../../constants'; +import { + EqBalancesFee, + EqBalancesSuccessEvent, +} from '../../../extrinsic/eqBalances'; +import { + assets, + balance, + chains, + extrinsic, + withdraw, +} from '../moonbase.common'; +import { MoonbaseXcmConfig } from '../moonbase.interfaces'; + +const asset = assets[AssetSymbol.EQ]; +const eqa = chains[ChainKey.EquilibriumAlphanet]; +const eqId = 25_969; + +export const EQ: MoonbaseXcmConfig = { + asset, + origin: eqa, + deposit: { + [eqa.key]: { + source: eqa, + balance: balance.systemEquilibrium(eqId), + extrinsic: extrinsic + .eqBalances() + .xcmTransfer() + .successEvent(EqBalancesSuccessEvent.ExtrinsicSuccess) + .asset(eqId) + .fee(EqBalancesFee.ThisAccWillPay), + }, + }, + withdraw: { + [eqa.key]: withdraw.xTokens({ + balance: balance.systemEquilibrium(eqId), + destination: eqa, + feePerWeight: 3.5, + }), + }, +}; diff --git a/packages/config/src/config/moonbase/assets/eqd.ts b/packages/config/src/config/moonbase/assets/eqd.ts new file mode 100644 index 00000000..b7984b1c --- /dev/null +++ b/packages/config/src/config/moonbase/assets/eqd.ts @@ -0,0 +1,49 @@ +import { AssetSymbol, ChainKey } from '../../../constants'; +import { + EqBalancesFee, + EqBalancesSuccessEvent, +} from '../../../extrinsic/eqBalances'; +import { + assets, + balance, + chains, + extrinsic, + moonbase, + withdraw, +} from '../moonbase.common'; +import { MoonbaseXcmConfig } from '../moonbase.interfaces'; + +const asset = assets[AssetSymbol.EQD]; +const feeAsset = assets[AssetSymbol.EQ]; +const eqa = chains[ChainKey.EquilibriumAlphanet]; +const eqId = 25_969; +const eqdId = 6_648_164; + +export const EQD: MoonbaseXcmConfig = { + asset, + origin: moonbase, + deposit: { + [eqa.key]: { + source: eqa, + balance: balance.systemEquilibrium(eqdId), + sourceFeeBalance: balance.systemEquilibrium(eqId), + xcmFeeAsset: { + asset: feeAsset, + balance: balance.systemEquilibrium(eqId), + }, + extrinsic: extrinsic + .eqBalances() + .xcmTransfer() + .successEvent(EqBalancesSuccessEvent.ExtrinsicSuccess) + .asset(eqdId) + .fee(EqBalancesFee.ThisAccWillPay), + }, + }, + withdraw: { + [eqa.key]: withdraw.xTokens({ + balance: balance.systemEquilibrium(eqdId), + destination: eqa, + feePerWeight: 0.05, + }), + }, +}; diff --git a/packages/config/src/config/moonbase/moonbase.assets.ts b/packages/config/src/config/moonbase/moonbase.assets.ts index 898b57cc..eba0011d 100644 --- a/packages/config/src/config/moonbase/moonbase.assets.ts +++ b/packages/config/src/config/moonbase/moonbase.assets.ts @@ -6,6 +6,8 @@ export const MOONBASE_ASSETS = [ AssetSymbol.AUQ, AssetSymbol.BIT, AssetSymbol.DEV, + AssetSymbol.EQ, + AssetSymbol.EQD, AssetSymbol.LIT, AssetSymbol.NEER, AssetSymbol.PARING, @@ -30,6 +32,16 @@ export const MOONBASE_ASSETS_MAP: AssetsMap = { originSymbol: AssetSymbol.DEV, isNative: true, }, + [AssetSymbol.EQ]: { + id: '190590555344745888270686124937537713878', + erc20Id: '0xffffffff8f6267e040d8a0638c576dfba4f0f6d6', + originSymbol: AssetSymbol.EQ, + }, + [AssetSymbol.EQD]: { + id: '187224307232923873519830480073807488153', + erc20Id: '0xffffffff8cda1707baf23834d211b08726b1e499', + originSymbol: AssetSymbol.EQD, + }, [AssetSymbol.LIT]: { id: '65216491554813189869575508812319036608', erc20Id: '0xffffffff31103d490325bb0a8e40ef62e2f614c0', diff --git a/packages/config/src/config/moonbase/moonbase.chains.ts b/packages/config/src/config/moonbase/moonbase.chains.ts index 44946d51..45f6f585 100644 --- a/packages/config/src/config/moonbase/moonbase.chains.ts +++ b/packages/config/src/config/moonbase/moonbase.chains.ts @@ -1,9 +1,10 @@ -import { ChainKey } from '../../constants'; +import { AssetSymbol, ChainKey } from '../../constants'; import { ChainsMap } from '../config.interfaces'; import { MoonbaseChains } from './moonbase.interfaces'; export const MOONBASE_CHAINS = [ ChainKey.AlphanetRelay, + ChainKey.EquilibriumAlphanet, ChainKey.LitentryAlphanet, ChainKey.BitCountryPioneer, ChainKey.DarwiniaPangoro, @@ -44,6 +45,20 @@ export const MOONBASE_CHAINS_MAP: ChainsMap = { genesisHash: '0xaaa8b33b723b30b44e45e4e6c01936cc92e7559b4184fb0cee2853d55610fcbf', }, + [ChainKey.EquilibriumAlphanet]: { + key: ChainKey.EquilibriumAlphanet, + name: 'EquilibriumAlphanet', + ws: 'wss://devnet.equilab.io/alphanet/collator/api/wss', + weight: 1_000_000_000, + parachainId: 2011, + moonAssetId: 1735159154, + ss58Format: 68, + genesisHash: + '0x0ba4508078664cfd04f00bf17892dd1a0764ce61d90857f76c6a2ea61f008247', + assetsDecimals: { + [AssetSymbol.DEV]: 9, + }, + }, [ChainKey.LitentryAlphanet]: { key: ChainKey.LitentryAlphanet, name: 'Litentry Alphanet', diff --git a/packages/config/src/config/moonbase/moonbase.ts b/packages/config/src/config/moonbase/moonbase.ts index aba43b73..1092a42f 100644 --- a/packages/config/src/config/moonbase/moonbase.ts +++ b/packages/config/src/config/moonbase/moonbase.ts @@ -4,6 +4,8 @@ import { MoonbaseXcmConfigs } from './moonbase.interfaces'; import { AUQ } from './assets/auq'; import { BIT } from './assets/bit'; import { DEV } from './assets/dev'; +import { EQ } from './assets/eq'; +import { EQD } from './assets/eqd'; import { LIT } from './assets/lit'; import { NEER } from './assets/neer'; import { PARING } from './assets/paring'; @@ -14,6 +16,8 @@ export const MOONBASE_CONFIGS: MoonbaseXcmConfigs = { [AssetSymbol.AUQ]: AUQ, [AssetSymbol.BIT]: BIT, [AssetSymbol.DEV]: DEV, + [AssetSymbol.EQ]: EQ, + [AssetSymbol.EQD]: EQD, [AssetSymbol.LIT]: LIT, [AssetSymbol.NEER]: NEER, [AssetSymbol.PARING]: PARING, diff --git a/packages/config/src/constants/assets.ts b/packages/config/src/constants/assets.ts index bea34dff..70b3e319 100644 --- a/packages/config/src/constants/assets.ts +++ b/packages/config/src/constants/assets.ts @@ -1,14 +1,16 @@ export enum AssetSymbol { ACA = 'ACA', ASTR = 'ASTR', - AUSD = 'AUSD', AUQ = 'AUQ', + AUSD = 'AUSD', BIT = 'BIT', BNC = 'BNC', CRAB = 'CRAB', CSM = 'CSM', DEV = 'DEV', DOT = 'DOT', + EQ = 'EQ', + EQD = 'EQD', GLMR = 'GLMR', HKO = 'HKO', IBTC = 'IBTC', diff --git a/packages/config/src/constants/chains.ts b/packages/config/src/constants/chains.ts index 47b6918a..b90424d6 100644 --- a/packages/config/src/constants/chains.ts +++ b/packages/config/src/constants/chains.ts @@ -53,6 +53,7 @@ export enum ChainKey { CrustShadow = 'CrustShadow', Darwinia = 'Darwinia', DarwiniaPangoro = 'DarwiniaPangoro', + EquilibriumAlphanet = 'EquilibriumAlphanet', Integritee = 'Integritee', Interlay = 'Interlay', Karura = 'Karura', diff --git a/packages/config/src/extrinsic/eqBalances/__snapshots__/eqBalances.test.ts.snap b/packages/config/src/extrinsic/eqBalances/__snapshots__/eqBalances.test.ts.snap new file mode 100644 index 00000000..de70675c --- /dev/null +++ b/packages/config/src/extrinsic/eqBalances/__snapshots__/eqBalances.test.ts.snap @@ -0,0 +1,34 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`eqBalances xcmTransfer should be correct config 1`] = ` +{ + "extrinsic": "xcmTransfer", + "getParams": [Function], + "pallet": "eqBalances", + "successEvent": "ExtrinsicSuccess", +} +`; + +exports[`eqBalances xcmTransfer should get correct params 1`] = ` +[ + 25969, + 1000n, + { + "interior": { + "X2": [ + { + "Parachain": 1000, + }, + { + "AccountKey20": { + "key": "0xeF46c7649270C912704fB09B75097f6E32208b85", + "network": "Any", + }, + }, + ], + }, + "parents": 1, + }, + "SovereignAccWillPay", +] +`; diff --git a/packages/config/src/extrinsic/eqBalances/eqBalances.constants.ts b/packages/config/src/extrinsic/eqBalances/eqBalances.constants.ts new file mode 100644 index 00000000..6b633e7d --- /dev/null +++ b/packages/config/src/extrinsic/eqBalances/eqBalances.constants.ts @@ -0,0 +1,13 @@ +export enum EqBalancesExtrinsic { + XcmTransfer = 'xcmTransfer', +} + +export enum EqBalancesSuccessEvent { + ExtrinsicSuccess = 'ExtrinsicSuccess', +} + +export enum EqBalancesFee { + SovereignAccWillPay = 'SovereignAccWillPay', + TargetChainAccWillPay = 'TargetChainAccWillPay', + ThisAccWillPay = 'ThisAccWillPay', +} diff --git a/packages/config/src/extrinsic/eqBalances/eqBalances.interfaces.ts b/packages/config/src/extrinsic/eqBalances/eqBalances.interfaces.ts new file mode 100644 index 00000000..2376c570 --- /dev/null +++ b/packages/config/src/extrinsic/eqBalances/eqBalances.interfaces.ts @@ -0,0 +1,51 @@ +import { XcmExtrinsicGetParams } from '../common.interfaces'; +import { ExtrinsicPallet } from '../extrinsic.constants'; +import { + EqBalancesExtrinsic, + EqBalancesFee, + EqBalancesSuccessEvent, +} from './eqBalances.constants'; + +export interface EqBalancesPallet { + pallet: ExtrinsicPallet.EqBalances; + extrinsic: EqBalancesExtrinsic; + successEvent: EqBalancesSuccessEvent; + getParams: (params: XcmExtrinsicGetParams) => EqBalancesPalletParams; +} + +export type EqBalancesPalletParams = [ + /** + * asset + */ + number, + /** + * amount + */ + bigint, + /** + * destination + */ + { + parents: 1; + interior: { + X2: [ + { + Parachain: number; + }, + { + AccountKey20: { + network: 'Any'; + /** + * account + */ + key: string; + }; + }, + ]; + }; + }, + /** + * Fee type + */ + EqBalancesFee, +]; diff --git a/packages/config/src/extrinsic/eqBalances/eqBalances.test.ts b/packages/config/src/extrinsic/eqBalances/eqBalances.test.ts new file mode 100644 index 00000000..14724ff8 --- /dev/null +++ b/packages/config/src/extrinsic/eqBalances/eqBalances.test.ts @@ -0,0 +1,37 @@ +import { MoonChainKey } from '../../constants'; +import { MoonChain } from '../../interfaces'; +import { eqBalances } from './eqBalances'; +import { EqBalancesFee, EqBalancesSuccessEvent } from './eqBalances.constants'; + +describe('eqBalances', () => { + const amount = 1000n; + const account = '0xeF46c7649270C912704fB09B75097f6E32208b85'; + const eqId = 25_969; + const chain: MoonChain = { + key: MoonChainKey.MoonbaseAlpha, + name: 'Moonbase Alpha', + ws: 'wss://wss.api.moonbase.moonbeam.network', + parachainId: 1000, + decimals: 10, + chainId: 1287, + unitsPerSecond: 100n, + }; + + const extrinsic = eqBalances(chain); + + describe('xcmTransfer', () => { + const cfg = extrinsic + .xcmTransfer() + .successEvent(EqBalancesSuccessEvent.ExtrinsicSuccess) + .asset(eqId) + .fee(EqBalancesFee.SovereignAccWillPay); + + it('should be correct config', () => { + expect(cfg).toMatchSnapshot(); + }); + + it('should get correct params', () => { + expect(cfg.getParams({ account, amount })).toMatchSnapshot(); + }); + }); +}); diff --git a/packages/config/src/extrinsic/eqBalances/eqBalances.ts b/packages/config/src/extrinsic/eqBalances/eqBalances.ts new file mode 100644 index 00000000..52e9713a --- /dev/null +++ b/packages/config/src/extrinsic/eqBalances/eqBalances.ts @@ -0,0 +1,50 @@ +import { MoonChain } from '../../interfaces'; +import { ExtrinsicPallet } from '../extrinsic.constants'; +import { + EqBalancesExtrinsic, + EqBalancesFee, + EqBalancesSuccessEvent, +} from './eqBalances.constants'; +import { EqBalancesPallet } from './eqBalances.interfaces'; + +/* eslint-disable @typescript-eslint/no-use-before-define */ +export function eqBalances(chain: MoonChain) { + return { + xcmTransfer: () => xcmTransfer(chain), + }; +} + +function xcmTransfer(chain: MoonChain) { + return { + successEvent: (event: EqBalancesSuccessEvent) => ({ + asset: (id: number) => ({ + fee: (fee: EqBalancesFee): EqBalancesPallet => ({ + pallet: ExtrinsicPallet.EqBalances, + extrinsic: EqBalancesExtrinsic.XcmTransfer, + successEvent: event, + getParams: ({ account, amount }) => [ + id, + amount, + { + parents: 1, + interior: { + X2: [ + { + Parachain: chain.parachainId, + }, + { + AccountKey20: { + network: 'Any', + key: account, + }, + }, + ], + }, + }, + fee, + ], + }), + }), + }), + }; +} diff --git a/packages/config/src/extrinsic/eqBalances/index.ts b/packages/config/src/extrinsic/eqBalances/index.ts new file mode 100644 index 00000000..d62769d0 --- /dev/null +++ b/packages/config/src/extrinsic/eqBalances/index.ts @@ -0,0 +1,3 @@ +export * from './eqBalances'; +export * from './eqBalances.constants'; +export * from './eqBalances.interfaces'; diff --git a/packages/config/src/extrinsic/extrinsic.constants.ts b/packages/config/src/extrinsic/extrinsic.constants.ts index 6ad6e109..20cf63d7 100644 --- a/packages/config/src/extrinsic/extrinsic.constants.ts +++ b/packages/config/src/extrinsic/extrinsic.constants.ts @@ -3,4 +3,5 @@ export enum ExtrinsicPallet { XcmPallet = 'xcmPallet', XTokens = 'xTokens', XTransfer = 'xTransfer', + EqBalances = 'eqBalances', } diff --git a/packages/config/src/extrinsic/extrinsic.ts b/packages/config/src/extrinsic/extrinsic.ts index 29f69b24..17213b22 100644 --- a/packages/config/src/extrinsic/extrinsic.ts +++ b/packages/config/src/extrinsic/extrinsic.ts @@ -1,5 +1,6 @@ import { AssetSymbol, ChainKey } from '../constants'; import { MoonChain } from '../interfaces'; +import { eqBalances } from './eqBalances'; import { polkadotXcm } from './polkadotXcm'; import { xcmPallet } from './xcmPallet'; import { xTokens } from './xTokens'; @@ -14,5 +15,6 @@ export function createExtrinsicBuilder< xcmPallet: () => xcmPallet(chain), xTokens: () => xTokens(chain), xTransfer: () => xTransfer(chain), + eqBalances: () => eqBalances(chain), }; } diff --git a/packages/config/src/interfaces.ts b/packages/config/src/interfaces.ts index f010ec5e..73049320 100644 --- a/packages/config/src/interfaces.ts +++ b/packages/config/src/interfaces.ts @@ -52,6 +52,7 @@ export interface Chain ss58Format?: number; usesEthereumAccounts?: boolean; genesisHash: string; + assetsDecimals?: Record; } export interface XcmFeeAsset { diff --git a/packages/sdk/src/polkadot/PolkadotService.ts b/packages/sdk/src/polkadot/PolkadotService.ts index d5f15256..19b57e2b 100644 --- a/packages/sdk/src/polkadot/PolkadotService.ts +++ b/packages/sdk/src/polkadot/PolkadotService.ts @@ -54,12 +54,13 @@ export class PolkadotService< } getExistentialDeposit(): bigint { + const existentialDeposit = this.#api.consts.balances + ?.existentialDeposit as unknown as u128 | undefined; + const eqExistentialDeposit = this.#api.consts.eqBalances + ?.existentialDeposit as unknown as u128 | undefined; + return ( - ( - this.#api.consts.balances?.existentialDeposit as unknown as - | u128 - | undefined - )?.toBigInt() || 0n + existentialDeposit?.toBigInt() || eqExistentialDeposit?.toBigInt() || 0n ); } @@ -135,15 +136,14 @@ export class PolkadotService< primaryAccount?: string, ): SubmittableExtrinsic<'promise'> { const call = this.#api.tx[pallet][extrinsic]; + const params = getParams({ + account, + amount, + extrinsicCall: call, + fee, + }); - let transferExtrinsic = call( - ...getParams({ - account, - amount, - extrinsicCall: call, - fee, - }), - ); + let transferExtrinsic = call(...params); if (primaryAccount) { transferExtrinsic = this.#api.tx.proxy.proxy( diff --git a/packages/sdk/src/sdk/sdk.deposit.ts b/packages/sdk/src/sdk/sdk.deposit.ts index 49d0cf7c..83482ad3 100644 --- a/packages/sdk/src/sdk/sdk.deposit.ts +++ b/packages/sdk/src/sdk/sdk.deposit.ts @@ -10,7 +10,10 @@ import { createExtrinsicEventHandler, ExtrinsicEventsCallback, } from '@moonbeam-network/xcm-utils'; -import type { Signer as PolkadotSigner } from '@polkadot/api/types'; +import type { + Signer as PolkadotSigner, + SubmittableExtrinsic, +} from '@polkadot/api/types'; import type { IKeyringPair } from '@polkadot/types/types'; import { isUndefined } from '@polkadot/util'; import { PolkadotService } from '../polkadot'; @@ -37,7 +40,7 @@ export function getCreateExtrinsic< primaryAccount, fee, }: CreateExtrinsicOptions) { - return (amount: bigint): any => + return (amount: bigint): SubmittableExtrinsic<'promise'> => foreignPolkadot.getXcmExtrinsic( account, amount, @@ -91,13 +94,12 @@ export async function getDepositData< }; const [ - assetDecimals, + decimals, existentialDeposit, sourceBalance, sourceFeeBalance, sourceMinBalance = 0n, xcmFee, - xcmFeeBalance, xcmFeeDecimals, ] = await Promise.all([ // assetDecimals @@ -126,11 +128,6 @@ export async function getDepositData< config.source.weight, moonChain, ), - // xcmFeeBalance - foreignPolkadot.getGenericBalance( - primaryAccount || sourceAddress, - xcmFeeAssetConfig.balance, - ), // xcmFeeDecimals polkadot.getAssetDecimals(xcmFeeAssetConfig.asset), ]); @@ -147,15 +144,14 @@ export async function getDepositData< }); return { - asset: { ...asset, decimals: assetDecimals }, + asset: { ...asset, decimals }, existentialDeposit, min, moonChainFee: { - balance: xcmFeeBalance, + amount: xcmFee, decimals: xcmFeeAssetConfig.asset.isNative ? moonChain.decimals : xcmFeeDecimals, - fee: xcmFee, symbol: xcmFeeAssetConfig.asset.originSymbol, }, native: { ...nativeAsset, decimals: meta.decimals }, @@ -164,13 +160,15 @@ export async function getDepositData< ...config.source, }, sourceBalance, + sourceAssetDecimals: + config.source.assetsDecimals?.[asset.originSymbol] || decimals, sourceFeeBalance: !isUndefined(sourceFeeBalance) ? { ...meta, balance: sourceFeeBalance } : undefined, sourceMinBalance, getFee: async (amount = sourceBalance): Promise => { const extrinsic = createExtrinsic(amount); - const info = await extrinsic.paymentInfo(sourceAccount); + const info = await extrinsic.paymentInfo(sourceAccount, { nonce: -1 }); return info.partialFee.toBigInt(); }, @@ -183,14 +181,14 @@ export async function getDepositData< sourceAccount, { signer: polkadotSigner, + nonce: -1, }, - cb && - createExtrinsicEventHandler( + (createExtrinsicEventHandler( config.extrinsic.pallet, config.extrinsic.successEvent, cb, - ), + ) as any), ); return hash.toString(); diff --git a/packages/sdk/src/sdk/sdk.interfaces.ts b/packages/sdk/src/sdk/sdk.interfaces.ts index 40edd746..f63a2faa 100644 --- a/packages/sdk/src/sdk/sdk.interfaces.ts +++ b/packages/sdk/src/sdk/sdk.interfaces.ts @@ -102,11 +102,12 @@ export interface DepositTransferData< asset: AssetWithDecimals; existentialDeposit: bigint; min: bigint; - moonChainFee: XcmFeeWithBalance; + moonChainFee: XcmFeeWithDecimals; native: AssetWithDecimals; origin: MoonChain | Chain; source: Chain; sourceBalance: bigint; + sourceAssetDecimals: number; sourceFeeBalance?: Balance; sourceMinBalance: bigint; getFee: (amount?: bigint) => Promise; @@ -120,6 +121,7 @@ export interface WithdrawTransferData< asset: AssetWithDecimals; destination: Chain; destinationBalance: bigint; + destinationAssetDecimals: number; destinationFee: bigint; existentialDeposit: bigint; min: bigint; @@ -148,12 +150,6 @@ export interface Balance { symbol: Symbols; } -export interface XcmFeeWithBalance< - Symbols extends string | AssetSymbol = AssetSymbol, -> extends Balance { - fee: bigint; -} - export interface SdkOptions { ethersSigner?: EthersSigner; polkadotSigner?: PolkadotSigner; diff --git a/packages/sdk/src/sdk/sdk.withdraw.ts b/packages/sdk/src/sdk/sdk.withdraw.ts index c7d14472..04b8d934 100644 --- a/packages/sdk/src/sdk/sdk.withdraw.ts +++ b/packages/sdk/src/sdk/sdk.withdraw.ts @@ -115,6 +115,8 @@ export async function getWithdrawData< ...config.destination, }, destinationBalance, + destinationAssetDecimals: + config.destination.assetsDecimals?.[asset.originSymbol] || decimals, destinationFee, existentialDeposit, min,