Skip to content

Commit 8ece1cf

Browse files
b-yapebma
andauthored
Add PEN and XLM tokens to Astar portal (#1348)
* first iteration * add PendulumXcmRepository.ts * fix build issues * update PEN png * PEN is native, so use `getNativeBalance` * remove console.log * use '0' instead of 'PEN' * add XLM and EURC * use triple '=' * update symbols and issuers * remove EURC * remove console * xcPEN instead of PEN * Add refactoring * changing minimum amount --------- Co-authored-by: Marcel Ebert <mail@marcel-ebert.de>
1 parent aeefe34 commit 8ece1cf

8 files changed

Lines changed: 183 additions & 0 deletions

File tree

src/assets/img/token/PEN.svg

Lines changed: 23 additions & 0 deletions
Loading

src/assets/img/token/XLM.svg

Lines changed: 20 additions & 0 deletions
Loading

src/modules/xcm/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,15 @@ export let xcmChainObj: XcmChainObj = {
364364
subscan: 'https://hydration.subscan.io',
365365
isAstarNativeToken: true,
366366
},
367+
[Chain.PENDULUM]: {
368+
name: Chain.PENDULUM,
369+
relayChain: Chain.POLKADOT,
370+
img: require('/src/assets/img/token/PEN.svg'),
371+
parachainId: parachainIds.PENDULUM,
372+
endpoints: ['wss://rpc-pendulum.prd.pendulumchain.tech:443'],
373+
subscan: 'https://pendulum.subscan.io/',
374+
isAstarNativeToken: false,
375+
},
367376
};
368377

369378
export const xcmChains = objToArray(xcmChainObj);

src/modules/xcm/tokens/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,26 @@ export const xcmToken = {
175175
originChain: Chain.HYDRATION,
176176
minBridgeAmount: '5',
177177
},
178+
{
179+
symbol: 'xcPEN',
180+
isNativeToken: true,
181+
assetId: '18446744073709551634',
182+
originAssetId: 'PEN',
183+
logo: require('/src/assets/img/token/PEN.svg'),
184+
isXcmCompatible: true,
185+
originChain: Chain.PENDULUM,
186+
minBridgeAmount: '2',
187+
},
188+
{
189+
symbol: 'XLM.s',
190+
isNativeToken: false,
191+
assetId: '18446744073709551635',
192+
originAssetId: 'XLM.s',
193+
logo: require('/src/assets/img/token/XLM.svg'),
194+
isXcmCompatible: true,
195+
originChain: Chain.PENDULUM,
196+
minBridgeAmount: '1',
197+
},
178198
],
179199
[endpointKey.SHIDEN]: [
180200
{

src/v2/config/xcm/XcmRepositoryConfiguration.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
EquilibriumXcmRepository,
1212
UniqueXcmRepository,
1313
HydrationXcmRepository,
14+
PendulumXcmRepository,
1415
} from 'src/v2/repositories/implementations';
1516
import { Chain } from 'src/v2/models/XcmModels';
1617
import { TypeMapping } from 'src/v2/config/types';
@@ -36,6 +37,7 @@ export const XcmRepositoryConfiguration: TypeMapping = {
3637
[Chain.EQUILIBRIUM]: EquilibriumXcmRepository,
3738
[Chain.UNIQUE]: UniqueXcmRepository,
3839
[Chain.HYDRATION]: HydrationXcmRepository,
40+
[Chain.PENDULUM]: PendulumXcmRepository,
3941
};
4042

4143
export type AstarToken = 'ASTR' | 'SDN';

src/v2/models/XcmModels.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export enum Chain {
2222
EQUILIBRIUM = 'Equilibrium',
2323
UNIQUE = 'Unique',
2424
HYDRATION = 'Hydration',
25+
PENDULUM = 'Pendulum',
2526
}
2627

2728
export enum parachainIds {
@@ -43,6 +44,7 @@ export enum parachainIds {
4344
EQUILIBRIUM = 2011,
4445
UNIQUE = 2037,
4546
HYDRATION = 2034,
47+
PENDULUM = 2094,
4648
}
4749

4850
export interface XcmChain {

src/v2/repositories/implementations/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export * from './xcm/BifrostXcmRepository';
1919
export * from './xcm/EquilibriumXcmRepository';
2020
export * from './xcm/UniqueXcmRepository';
2121
export * from './xcm/HydrationXcmRepository';
22+
export * from './xcm/PendulumXcmRepository';
2223
export * from './NftRepository';
2324
export * from './AccountUnificationRepository';
2425
export * from './InflationRepository';
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { BN } from '@polkadot/util';
2+
import { XcmTokenInformation } from 'src/modules/xcm';
3+
import { container } from 'src/v2/common';
4+
import { ExtrinsicPayload, IApi, IApiFactory } from 'src/v2/integration';
5+
import { Asset } from 'src/v2/models';
6+
import { Symbols } from 'src/v2/symbols';
7+
import { XcmRepository } from '../XcmRepository';
8+
import { XcmChain } from 'src/v2/models/XcmModels';
9+
import { TokensAccounts } from 'src/v2/repositories/implementations/xcm/AcalaXcmRepository';
10+
import { decodeAddress } from '@polkadot/util-crypto';
11+
12+
// Mapping object for token IDs
13+
const TOKEN_IDS: Record<string, any> = {
14+
PEN: 'Native',
15+
'XLM.s': { Stellar: 'StellarNative' },
16+
};
17+
18+
export class PendulumXcmRepository extends XcmRepository {
19+
constructor() {
20+
const defaultApi = container.get<IApi>(Symbols.DefaultApi);
21+
const apiFactory = container.get<IApiFactory>(Symbols.ApiFactory);
22+
const registeredTokens = container.get<XcmTokenInformation[]>(Symbols.RegisteredTokens);
23+
super(defaultApi, apiFactory, registeredTokens);
24+
}
25+
26+
public async getTransferCall(
27+
from: XcmChain,
28+
to: XcmChain,
29+
recipientAddress: string,
30+
token: Asset,
31+
amount: BN,
32+
endpoint: string
33+
): Promise<ExtrinsicPayload> {
34+
if (!to.parachainId) {
35+
throw `Parachain id for ${to.name} is not defined`;
36+
}
37+
38+
const tokenData = TOKEN_IDS[token.originAssetId];
39+
if (!tokenData) {
40+
throw `Token name for ${token.originAssetId} is not defined`;
41+
}
42+
43+
const version = 'V3';
44+
45+
const AccountId32 = {
46+
id: decodeAddress(recipientAddress),
47+
};
48+
49+
const destination = {
50+
[version]: {
51+
parents: '1',
52+
interior: {
53+
X2: [
54+
{
55+
Parachain: to.parachainId,
56+
},
57+
{
58+
AccountId32,
59+
},
60+
],
61+
},
62+
},
63+
};
64+
65+
const destWeight = { Unlimited: null };
66+
67+
return await this.buildTxCall(
68+
from,
69+
endpoint,
70+
'xTokens',
71+
'transfer',
72+
tokenData,
73+
amount,
74+
destination,
75+
destWeight
76+
);
77+
}
78+
79+
public async getTokenBalance(
80+
address: string,
81+
chain: XcmChain,
82+
token: Asset,
83+
isNativeToken: boolean,
84+
endpoint: string
85+
): Promise<string> {
86+
const api = await this.apiFactory.get(endpoint);
87+
88+
try {
89+
if (isNativeToken) {
90+
const nativeBalance = await this.getNativeBalance(address, chain, endpoint);
91+
return nativeBalance.toString();
92+
}
93+
94+
const currencyId = TOKEN_IDS[token.originAssetId];
95+
if (!currencyId) {
96+
console.error(`Token name for ${token.originAssetId} is not defined`);
97+
return '0';
98+
}
99+
const bal = await api.query.tokens.accounts<TokensAccounts>(address, currencyId);
100+
return bal.free.toString();
101+
} catch (e) {
102+
console.error(e);
103+
return '0';
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)