Skip to content

Commit

Permalink
feat:Support for BNC and vDOT assets (#658)
Browse files Browse the repository at this point in the history
* feat:Support for BNC and vDOT assets

* Fix some errors

* Update BifrostXcmRepository.ts

* Add some constants
  • Loading branch information
hqwangningbo authored Jan 12, 2023
1 parent ea9e50b commit 4d81d91
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/modules/xcm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions src/modules/xcm/tokens/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]: [
{
Expand Down
2 changes: 2 additions & 0 deletions src/v2/config/xcm/XcmRepositoryConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down
2 changes: 2 additions & 0 deletions src/v2/models/XcmModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export enum Chain {
CRUST_SHADOW = 'Crust-shadow',
KHALA = 'Khala',
PHALA = 'Phala',
BIFROST_POLKADOT = 'Bifrost-polkadot',
}

export enum parachainIds {
Expand All @@ -33,6 +34,7 @@ export enum parachainIds {
CRUST_SHADOW = 2012,
KHALA = 2004,
PHALA = 2035,
BIFROST_POLKADOT = 2030,
}

export interface XcmChain {
Expand Down
1 change: 1 addition & 0 deletions src/v2/repositories/implementations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export * from './xcm/PhalaXcmRepository';
export * from './TokenApiRepository';
export * from './EvmAssetsRepository';
export * from './xcm/CrustShadowXcmRepository';
export * from './xcm/BifrostXcmRepository';
104 changes: 104 additions & 0 deletions src/v2/repositories/implementations/xcm/BifrostXcmRepository.ts
Original file line number Diff line number Diff line change
@@ -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<IApi>(Symbols.DefaultApi);
const apiFactory = container.get<IApiFactory>(Symbols.ApiFactory);
const registeredTokens = container.get<XcmTokenInformation[]>(Symbols.RegisteredTokens);
super(defaultApi, apiFactory, registeredTokens);
this.astarTokens = {
ASTR: 3,
};
}

public async getTransferCall(
from: XcmChain,
to: XcmChain,
recipientAddress: string,
token: Asset,
amount: BN
): Promise<ExtrinsicPayload> {
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<string> {
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<TokensAccounts>(address, vDOT);
return bal.free.toString();
} else {
const bal = await api.query.tokens.accounts<TokensAccounts>(address, ASTR);
return bal.free.toString();
}
} catch (e) {
console.error(e);
return '0';
}
}
}

0 comments on commit 4d81d91

Please sign in to comment.