From 4d81d91643ce5791773ad87a9453993eb13a6719 Mon Sep 17 00:00:00 2001 From: NingBo Wang <2536935847@qq.com> Date: Thu, 12 Jan 2023 21:11:21 +0800 Subject: [PATCH] feat:Support for BNC and vDOT assets (#658) * feat:Support for BNC and vDOT assets * Fix some errors * Update BifrostXcmRepository.ts * Add some constants --- src/modules/xcm/index.ts | 9 ++ src/modules/xcm/tokens/index.ts | 20 ++++ .../config/xcm/XcmRepositoryConfiguration.ts | 2 + src/v2/models/XcmModels.ts | 2 + src/v2/repositories/implementations/index.ts | 1 + .../xcm/BifrostXcmRepository.ts | 104 ++++++++++++++++++ 6 files changed, 138 insertions(+) create mode 100644 src/v2/repositories/implementations/xcm/BifrostXcmRepository.ts diff --git a/src/modules/xcm/index.ts b/src/modules/xcm/index.ts index 240588144..78c2e5611 100644 --- a/src/modules/xcm/index.ts +++ b/src/modules/xcm/index.ts @@ -199,6 +199,15 @@ export const xcmChainObj: XcmChainObj = { subscan: 'https://phala.subscan.io', isAstarNativeToken: true, }, + [Chain.BIFROST_POLKADOT]: { + name: Chain.BIFROST_POLKADOT, + relayChain: Chain.POLKADOT, + img: 'https://polkadot.js.org/apps/static/bifrost.76510acf..svg', + parachainId: parachainIds.BIFROST_POLKADOT, + endpoint: 'wss://hk.p.bifrost-rpc.liebi.com/ws', + subscan: 'https://bifrost.subscan.io/', + isAstarNativeToken: true, + }, }; export const xcmChains = objToArray(xcmChainObj); diff --git a/src/modules/xcm/tokens/index.ts b/src/modules/xcm/tokens/index.ts index ae071a5fe..eea70ce90 100644 --- a/src/modules/xcm/tokens/index.ts +++ b/src/modules/xcm/tokens/index.ts @@ -104,6 +104,26 @@ export const xcmToken = { originChain: Chain.PHALA, minBridgeAmount: '0.2', }, + { + symbol: 'BNC', + isNativeToken: true, + assetId: '18446744073709551623', + originAssetId: 'BNC', + logo: 'https://bifrost-kusama.subscan.io/static/img/bifrost.42f1933c.svg', + isXcmCompatible: true, + originChain: Chain.BIFROST_POLKADOT, + minBridgeAmount: '0.1', + }, + { + symbol: 'vDOT', + isNativeToken: false, + assetId: '18446744073709551624', + originAssetId: 'vDOT', + logo: 'https://bifrost.subscan.io/static/img/vDOT.75c0b67b.svg', + isXcmCompatible: true, + originChain: Chain.BIFROST_POLKADOT, + minBridgeAmount: '0.1', + }, ], [endpointKey.SHIDEN]: [ { diff --git a/src/v2/config/xcm/XcmRepositoryConfiguration.ts b/src/v2/config/xcm/XcmRepositoryConfiguration.ts index 6d31376cf..a35ae346c 100644 --- a/src/v2/config/xcm/XcmRepositoryConfiguration.ts +++ b/src/v2/config/xcm/XcmRepositoryConfiguration.ts @@ -7,6 +7,7 @@ import { InterlayXcmRepository, CrustShadowXcmRepository, PhalaXcmRepository, + BifrostXcmRepository, } from 'src/v2/repositories/implementations'; import { Chain } from 'src/v2/models/XcmModels'; import { TypeMapping } from 'src/v2/config/types'; @@ -27,6 +28,7 @@ export const XcmRepositoryConfiguration: TypeMapping = { [Chain.CRUST_SHADOW]: CrustShadowXcmRepository, [Chain.KHALA]: PhalaXcmRepository, [Chain.PHALA]: PhalaXcmRepository, + [Chain.BIFROST_POLKADOT]: BifrostXcmRepository, }; export type AstarToken = 'ASTR' | 'SDN'; diff --git a/src/v2/models/XcmModels.ts b/src/v2/models/XcmModels.ts index cf518e4c3..9f9c2069a 100644 --- a/src/v2/models/XcmModels.ts +++ b/src/v2/models/XcmModels.ts @@ -17,6 +17,7 @@ export enum Chain { CRUST_SHADOW = 'Crust-shadow', KHALA = 'Khala', PHALA = 'Phala', + BIFROST_POLKADOT = 'Bifrost-polkadot', } export enum parachainIds { @@ -33,6 +34,7 @@ export enum parachainIds { CRUST_SHADOW = 2012, KHALA = 2004, PHALA = 2035, + BIFROST_POLKADOT = 2030, } export interface XcmChain { diff --git a/src/v2/repositories/implementations/index.ts b/src/v2/repositories/implementations/index.ts index 77ca4569b..d99760411 100644 --- a/src/v2/repositories/implementations/index.ts +++ b/src/v2/repositories/implementations/index.ts @@ -13,3 +13,4 @@ export * from './xcm/PhalaXcmRepository'; export * from './TokenApiRepository'; export * from './EvmAssetsRepository'; export * from './xcm/CrustShadowXcmRepository'; +export * from './xcm/BifrostXcmRepository'; diff --git a/src/v2/repositories/implementations/xcm/BifrostXcmRepository.ts b/src/v2/repositories/implementations/xcm/BifrostXcmRepository.ts new file mode 100644 index 000000000..00c38d5ed --- /dev/null +++ b/src/v2/repositories/implementations/xcm/BifrostXcmRepository.ts @@ -0,0 +1,104 @@ +import { BN } from '@polkadot/util'; +import { decodeAddress } from '@polkadot/util-crypto'; +import { XcmTokenInformation } from 'src/modules/xcm'; +import { container } from 'src/v2/common'; +import { ExtrinsicPayload, IApi, IApiFactory } from 'src/v2/integration'; +import { Asset } from 'src/v2/models'; +import { XcmChain } from 'src/v2/models/XcmModels'; +import { TokensAccounts } from 'src/v2/repositories/implementations/xcm/AcalaXcmRepository'; +import { Symbols } from 'src/v2/symbols'; +import { XcmRepository } from '../XcmRepository'; + +const BNC = { Native: 'BNC' }; +const vDOT = { VToken2: 0 }; +const ASTR = { Token2: 3 }; + +/** + * Used to transfer assets from Bifrost + */ +export class BifrostXcmRepository extends XcmRepository { + constructor() { + const defaultApi = container.get(Symbols.DefaultApi); + const apiFactory = container.get(Symbols.ApiFactory); + const registeredTokens = container.get(Symbols.RegisteredTokens); + super(defaultApi, apiFactory, registeredTokens); + this.astarTokens = { + ASTR: 3, + }; + } + + public async getTransferCall( + from: XcmChain, + to: XcmChain, + recipientAddress: string, + token: Asset, + amount: BN + ): Promise { + if (!to.parachainId) { + throw `Parachain id for ${to.name} is not defined`; + } + let tokenData; + if (token.originAssetId == 'BNC') { + tokenData = BNC; + } else if (token.originAssetId == 'vDOT') { + tokenData = vDOT; + } else if (token.originAssetId == 'ASTR') { + tokenData = ASTR; + } + const destination = { + V1: { + parents: '1', + interior: { + X2: [ + { + Parachain: to.parachainId, + }, + { + AccountId32: { + network: { + Any: null, + }, + id: decodeAddress(recipientAddress), + }, + }, + ], + }, + }, + }; + const destWeight = { + limited: new BN(10).pow(new BN(9)).muln(5), + }; + return await this.buildTxCall( + from, + 'xTokens', + 'transfer', + tokenData, + amount, + destination, + destWeight + ); + } + + public async getTokenBalance( + address: string, + chain: XcmChain, + token: Asset, + isNativeToken: boolean + ): Promise { + const api = await this.apiFactory.get(chain.endpoint); + try { + if (token.originAssetId == 'BNC') { + return (await this.getNativeBalance(address, chain)).toString(); + } else if (token.originAssetId == 'vDOT') { + const bal = await api.query.tokens.accounts(address, vDOT); + return bal.free.toString(); + } else { + const bal = await api.query.tokens.accounts(address, ASTR); + return bal.free.toString(); + } + } catch (e) { + console.error(e); + return '0'; + } + } +}