Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:Support for BNC and vDOT assets #658

Merged
merged 5 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: '0',
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';

/**
* 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 = { Native: 'BNC' };
} else if (token.originAssetId == '0') {
tokenData = { VToken2: 0 };
} else if (token.originAssetId == 'ASTR') {
tokenData = { Token2: 3 };
}
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 == '0') {
hqwangningbo marked this conversation as resolved.
Show resolved Hide resolved
const bal = await api.query.tokens.accounts<TokensAccounts>(address, {
VToken2: token.originAssetId,
});
return bal.free.toString();
} else {
const bal = await api.query.tokens.accounts<TokensAccounts>(address, {
Token2: 3,
hqwangningbo marked this conversation as resolved.
Show resolved Hide resolved
});
return bal.free.toString();
}
} catch (e) {
console.error(e);
return '0';
}
}
}