Skip to content

Commit

Permalink
Bifrost integration with Moonbeam [xcBNC and GLMR] (#45)
Browse files Browse the repository at this point in the history
* Bifrost integration with moonbeam

* add unit test for Token2
  • Loading branch information
mmaurello authored Nov 16, 2022
1 parent 957a389 commit bcaec2a
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,12 @@ exports[`balance tokens should be correct params with Token 1`] = `
},
]
`;
exports[`balance tokens should be correct params with Token2 1`] = `
[
"<ACCOUNT>",
{
"Token2": 1,
},
]
`;
1 change: 1 addition & 0 deletions packages/config/src/balance/balance.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export enum BalanceFunction {
Accounts = 'accounts',
Asset = 'asset',
AssetMetadatas = 'assetMetadatas',
CurrencyMetadatas = 'currencyMetadatas',
}
5 changes: 3 additions & 2 deletions packages/config/src/balance/balance.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface TokensBalanceConfig<
Token: Symbols | 'MOVR' | 'KUSD';
}
| { ForeignAsset: AssetId }
| { Token2: AssetId }
),
];
calc: (data: TokensPalletAccountData) => bigint;
Expand All @@ -71,7 +72,7 @@ export interface MinBalanceAssetsConfig {

export interface MinBalanceAssetRegistryConfig {
pallet: BalancePallet.AssetRegistry;
function: BalanceFunction.AssetMetadatas;
function: BalanceFunction.AssetMetadatas | BalanceFunction.CurrencyMetadatas;
path: ['minimalBalance'];
params: [{ ForeignAssetId: AssetId }];
params: [{ ForeignAssetId: AssetId } | { Token2: AssetId }];
}
6 changes: 6 additions & 0 deletions packages/config/src/balance/balance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,11 @@ describe('balance', () => {

expect(cfg2.getParams(account)).toMatchSnapshot();
});

it('should be correct params with Token2', () => {
const cfg2 = balance.tokens2(1);

expect(cfg2.getParams(account)).toMatchSnapshot();
});
});
});
30 changes: 28 additions & 2 deletions packages/config/src/balance/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ export function createBalanceBuilder<
assets,
minAssetPallet,
minAssetRegistryPallet,
minCurrencyMetadata,
ormlTokens,
system,
tokens: (asset: number | bigint | Symbols | 'MOVR' | 'KUSD' | 'AUSD') =>
tokens<Symbols>(asset),
tokens2: (asset: AssetId) => tokens2<Symbols>(asset),
};
}

Expand Down Expand Up @@ -58,6 +60,15 @@ function minAssetRegistryPallet(asset: AssetId): MinBalanceAssetRegistryConfig {
};
}

function minCurrencyMetadata(asset: AssetId): MinBalanceAssetRegistryConfig {
return {
pallet: BalancePallet.AssetRegistry,
function: BalanceFunction.CurrencyMetadatas,
path: ['minimalBalance'],
params: [{ Token2: asset }],
};
}

function ormlTokens(asset: AssetId): OrmlTokensBalanceConfig {
return {
pallet: BalancePallet.OrmlTokens,
Expand All @@ -80,6 +91,10 @@ function system(): SystemBalanceConfig {
};
}

function calcTokensBalance({ free, frozen }: TokensPalletAccountData): bigint {
return BigInt(free.sub(frozen).toString());
}

function tokens<Symbols extends AssetSymbol = AssetSymbol>(
asset: number | bigint | Symbols | 'MOVR' | 'KUSD' | 'AUSD',
): TokensBalanceConfig<Symbols> {
Expand All @@ -93,7 +108,18 @@ function tokens<Symbols extends AssetSymbol = AssetSymbol>(
? { ForeignAsset: asset as number }
: { Token: asset as Symbols },
],
calc: ({ free, frozen }: TokensPalletAccountData) =>
BigInt(free.sub(frozen).toString()),
calc: calcTokensBalance,
};
}

function tokens2<Symbols extends AssetSymbol = AssetSymbol>(
asset: AssetId,
): TokensBalanceConfig<Symbols> {
return {
pallet: BalancePallet.Tokens,
function: BalanceFunction.Accounts,
path: [],
getParams: (account: string) => [account, { Token2: asset }],
calc: calcTokensBalance,
};
}
42 changes: 42 additions & 0 deletions packages/config/src/config/moonbeam/assets/bnc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { AssetSymbol, ChainKey } from '../../../constants';
import {
XTokensExtrinsicCurrencyTypes,
XTokensExtrinsicSuccessEvent,
} from '../../../extrinsic';
import {
assets,
balance,
chains,
extrinsic,
withdraw,
} from '../moonbeam.common';
import { MoonbeamXcmConfig } from '../moonbeam.interfaces';

const asset = assets[AssetSymbol.BNC];
const origin = chains[ChainKey.BifrostPolkadot];

export const BNC: MoonbeamXcmConfig = {
asset,
origin,
deposit: {
[origin.key]: {
source: origin,
balance: balance.system(),
extrinsic: extrinsic
.xTokens()
.transfer()
.successEvent(XTokensExtrinsicSuccessEvent.TransferredMultiAssets)
.origin(origin)
.asset({
[XTokensExtrinsicCurrencyTypes.Native]: asset.originSymbol,
}),
},
},
withdraw: {
[origin.key]: withdraw.xTokens({
balance: balance.system(),
destination: origin,
feePerWeight: 64,
}),
},
};
21 changes: 21 additions & 0 deletions packages/config/src/config/moonbeam/assets/glmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ import { MoonbeamXcmConfig } from '../moonbeam.interfaces';
const asset = assets[AssetSymbol.GLMR];
const acala = chains[ChainKey.Acala];
const astar = chains[ChainKey.Astar];
const bifrost = chains[ChainKey.BifrostPolkadot];
const parallel = chains[ChainKey.Parallel];
const phala = chains[ChainKey.Phala];

const acalaGlmrId = getMoonAssetId(acala);
const astarGlmrId = getMoonAssetId(astar);
const bifrostGlmrId = getMoonAssetId(bifrost);
const parallelGlmrId = getMoonAssetId(parallel);
const phalaGlmrId = getMoonAssetId(phala);

Expand Down Expand Up @@ -56,6 +58,19 @@ export const GLMR: MoonbeamXcmConfig = {
.V1()
.X2(getPalletInstance(astar)),
},
[bifrost.key]: {
source: bifrost,
balance: balance.tokens2(bifrostGlmrId),
sourceFeeBalance: balance.system(),
extrinsic: extrinsic
.xTokens()
.transfer()
.successEvent(XTokensExtrinsicSuccessEvent.TransferredMultiAssets)
.origin(bifrost)
.asset({
[XTokensExtrinsicCurrencyTypes.Token2]: bifrostGlmrId,
}),
},
[parallel.key]: {
source: parallel,
balance: balance.assets(parallelGlmrId),
Expand Down Expand Up @@ -91,6 +106,12 @@ export const GLMR: MoonbeamXcmConfig = {
destination: astar,
feePerWeight: 50_000,
}),
[bifrost.key]: withdraw.xTokens({
balance: balance.tokens2(bifrostGlmrId),
destination: bifrost,
feePerWeight: 0.8,
sourceMinBalance: balance.minCurrencyMetadata(bifrostGlmrId),
}),
[parallel.key]: withdraw.xTokens({
balance: balance.assets(parallelGlmrId),
destination: parallel,
Expand Down
6 changes: 6 additions & 0 deletions packages/config/src/config/moonbeam/moonbeam.assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const MOONBEAM_ASSETS = <const>[
AssetSymbol.ACA,
AssetSymbol.ASTR,
AssetSymbol.AUSD,
AssetSymbol.BNC,
AssetSymbol.DOT,
AssetSymbol.GLMR,
AssetSymbol.IBTC,
Expand All @@ -32,6 +33,11 @@ export const MOONBEAM_ASSETS_MAP: AssetsMap<MoonbeamAssets> = {
erc20Id: '0xffffffff52c56a9257bb97f4b2b6f7b2d624ecda',
originSymbol: AssetSymbol.AUSD,
},
[AssetSymbol.BNC]: {
id: '165823357460190568952172802245839421906',
erc20Id: '0xffffffff7cc06abdf7201b350a1265c62c8601d2',
originSymbol: AssetSymbol.BNC,
},
[AssetSymbol.DOT]: {
id: '42259045809535163221576417993425387648',
erc20Id: '0xffffffff1fcacbd218edc0eba20fc2308c778080',
Expand Down
9 changes: 9 additions & 0 deletions packages/config/src/config/moonbeam/moonbeam.chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { MoonbeamChains } from './moonbeam.interfaces';
export const MOONBEAM_CHAINS = <const>[
ChainKey.Acala,
ChainKey.Astar,
ChainKey.BifrostPolkadot,
ChainKey.Darwinia,
ChainKey.Interlay,
ChainKey.Parallel,
Expand All @@ -31,6 +32,14 @@ export const MOONBEAM_CHAINS_MAP: ChainsMap<MoonbeamChains> = {
moonAssetId: 18446744073709551619n,
palletInstance: 10,
},
[ChainKey.BifrostPolkadot]: {
key: ChainKey.BifrostPolkadot,
name: 'Bifrost',
ws: 'wss://hk.p.bifrost-rpc.liebi.com/ws',
weight: 1_000_000_000,
parachainId: 2030,
moonAssetId: 1,
},
[ChainKey.Darwinia]: {
key: ChainKey.Darwinia,
name: 'Darwinia',
Expand Down
2 changes: 2 additions & 0 deletions packages/config/src/config/moonbeam/moonbeam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { MoonbeamXcmConfigs } from './moonbeam.interfaces';
import { ACA } from './assets/aca';
import { ASTR } from './assets/astr';
import { AUSD } from './assets/ausd';
import { BNC } from './assets/bnc';
import { DOT } from './assets/dot';
import { GLMR } from './assets/glmr';
import { IBTC } from './assets/ibtc';
Expand All @@ -17,6 +18,7 @@ export const MOONBEAM_CONFIGS: MoonbeamXcmConfigs = {
[AssetSymbol.ACA]: ACA,
[AssetSymbol.ASTR]: ASTR,
[AssetSymbol.AUSD]: AUSD,
[AssetSymbol.BNC]: BNC,
[AssetSymbol.DOT]: DOT,
[AssetSymbol.GLMR]: GLMR,
[AssetSymbol.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 @@ -46,6 +46,7 @@ export enum ChainKey {
AlphanetRelay = 'AlphanetRelay',
Astar = 'Astar',
Bifrost = 'Bifrost',
BifrostPolkadot = 'BifrostPolkadot',
Calamari = 'Calamari',
CloverAlphanet = 'CloverAlphanet',
Crab = 'Crab',
Expand Down
1 change: 1 addition & 0 deletions packages/config/src/extrinsic/xTokens/xTokens.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export enum XTokensExtrinsicSuccessEvent {

export enum XTokensExtrinsicCurrencyTypes {
Token = 'Token',
Token2 = 'Token2',
Native = 'Native',
ForeignAsset = 'ForeignAsset',
MantaCurrency = 'MantaCurrency',
Expand Down
3 changes: 3 additions & 0 deletions packages/config/src/extrinsic/xTokens/xTokens.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ export type XTokensTransferExtrinsicParamsAsset<
| {
[XTokensExtrinsicCurrencyTypes.Token]: Symbols | 'KUSD' | 'MOVR';
}
| {
[XTokensExtrinsicCurrencyTypes.Token2]: AssetId;
}
| {
[XTokensExtrinsicCurrencyTypes.Native]: Symbols;
}
Expand Down

0 comments on commit bcaec2a

Please sign in to comment.