Skip to content

Commit

Permalink
Equilibrium integration (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
Elmar Kenigs authored Mar 30, 2023
1 parent 6a69234 commit 6e01259
Show file tree
Hide file tree
Showing 23 changed files with 406 additions and 41 deletions.
12 changes: 9 additions & 3 deletions packages/config/src/balance/balance.interfaces.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -11,17 +11,23 @@ import {
export type BalanceConfig<Symbols extends AssetSymbol = AssetSymbol> =
| SystemBalanceConfig
| AssetsBalanceConfig
| EquilibriumSystemBalanceConfig
| OrmlTokensBalanceConfig
| TokensBalanceConfig<Symbols>;

export interface SystemBalanceConfig {
export interface SystemBalanceConfig<Data = PalletBalancesAccountData> {
pallet: BalancePallet.System;
function: BalanceFunction.Account;
path: ['data'];
getParams: (account: string) => [string];
calc: (data: PalletBalancesAccountData) => bigint;
calc: (data: Data) => bigint;
}

export type EquilibriumSystemBalanceConfig = SystemBalanceConfig<Type>;
export type EquilibriumSystemBalanceData = Array<
[number, { positive: number }]
>;

export interface AssetsBalanceConfig {
pallet: BalancePallet.Assets;
function: BalanceFunction.Account;
Expand Down
26 changes: 26 additions & 0 deletions packages/config/src/balance/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
} from './balance.constants';
import {
AssetsBalanceConfig,
EquilibriumSystemBalanceConfig,
EquilibriumSystemBalanceData,
MinBalanceAssetRegistryConfig,
MinBalanceAssetsConfig,
OrmlTokensBalanceConfig,
Expand All @@ -32,6 +34,7 @@ export function createBalanceBuilder<
minCurrencyMetadata,
ormlTokens,
system,
systemEquilibrium,
tokens: () => tokens<Symbols>(),
};
}
Expand Down Expand Up @@ -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<Symbols extends AssetSymbol = AssetSymbol>() {
return {
foreignAsset: (asset: AssetId | Symbols) =>
Expand Down
21 changes: 21 additions & 0 deletions packages/config/src/config/moonbase/assets/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import {
XTokensExtrinsicCurrencyTypes,
XTokensExtrinsicSuccessEvent,
} from '../../../extrinsic';
import {
EqBalancesFee,
EqBalancesSuccessEvent,
} from '../../../extrinsic/eqBalances';
import { getMoonAssetId } from '../../config.utils';
import {
assets,
Expand All @@ -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),
Expand All @@ -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,
Expand Down
41 changes: 41 additions & 0 deletions packages/config/src/config/moonbase/assets/eq.ts
Original file line number Diff line number Diff line change
@@ -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,
}),
},
};
49 changes: 49 additions & 0 deletions packages/config/src/config/moonbase/assets/eqd.ts
Original file line number Diff line number Diff line change
@@ -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,
}),
},
};
12 changes: 12 additions & 0 deletions packages/config/src/config/moonbase/moonbase.assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export const MOONBASE_ASSETS = <const>[
AssetSymbol.AUQ,
AssetSymbol.BIT,
AssetSymbol.DEV,
AssetSymbol.EQ,
AssetSymbol.EQD,
AssetSymbol.LIT,
AssetSymbol.NEER,
AssetSymbol.PARING,
Expand All @@ -30,6 +32,16 @@ export const MOONBASE_ASSETS_MAP: AssetsMap<MoonbaseAssets> = {
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',
Expand Down
17 changes: 16 additions & 1 deletion packages/config/src/config/moonbase/moonbase.chains.ts
Original file line number Diff line number Diff line change
@@ -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 = <const>[
ChainKey.AlphanetRelay,
ChainKey.EquilibriumAlphanet,
ChainKey.LitentryAlphanet,
ChainKey.BitCountryPioneer,
ChainKey.DarwiniaPangoro,
Expand Down Expand Up @@ -44,6 +45,20 @@ export const MOONBASE_CHAINS_MAP: ChainsMap<MoonbaseChains> = {
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',
Expand Down
4 changes: 4 additions & 0 deletions packages/config/src/config/moonbase/moonbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion packages/config/src/constants/assets.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
1 change: 1 addition & 0 deletions packages/config/src/constants/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export enum ChainKey {
CrustShadow = 'CrustShadow',
Darwinia = 'Darwinia',
DarwiniaPangoro = 'DarwiniaPangoro',
EquilibriumAlphanet = 'EquilibriumAlphanet',
Integritee = 'Integritee',
Interlay = 'Interlay',
Karura = 'Karura',
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
]
`;
13 changes: 13 additions & 0 deletions packages/config/src/extrinsic/eqBalances/eqBalances.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export enum EqBalancesExtrinsic {
XcmTransfer = 'xcmTransfer',
}

export enum EqBalancesSuccessEvent {
ExtrinsicSuccess = 'ExtrinsicSuccess',
}

export enum EqBalancesFee {
SovereignAccWillPay = 'SovereignAccWillPay',
TargetChainAccWillPay = 'TargetChainAccWillPay',
ThisAccWillPay = 'ThisAccWillPay',
}
Loading

0 comments on commit 6e01259

Please sign in to comment.