Skip to content

Commit 8cc3f76

Browse files
committed
implement GLMR integration with Zeitgeist and add param to use chain decimals internally
1 parent 9f843ed commit 8cc3f76

File tree

7 files changed

+109
-30
lines changed

7 files changed

+109
-30
lines changed

packages/config/src/chains.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1400,9 +1400,12 @@ export const zeitgeist = new Parachain({
14001400
assetsData: [
14011401
{
14021402
asset: usdcwh,
1403-
decimals: 10,
14041403
id: { ForeignAsset: 1 },
14051404
},
1405+
{
1406+
asset: glmr,
1407+
id: { ForeignAsset: 3 },
1408+
},
14061409
],
14071410
ecosystem: Ecosystem.Polkadot,
14081411
genesisHash:
@@ -1411,6 +1414,7 @@ export const zeitgeist = new Parachain({
14111414
name: 'Zeitgeist',
14121415
parachainId: 2092,
14131416
ss58Format: 73,
1417+
usesOwnDecimalsInternally: true,
14141418
ws: 'wss://zeitgeist-rpc.dwellir.com',
14151419
});
14161420

packages/config/src/configs/moonbeam.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,18 @@ export const moonbeamConfig = new ChainConfig({
165165
contract: ContractBuilder().Xtokens().transfer(),
166166
destination: pendulum,
167167
destinationFee: {
168-
amount: 0.0002, //
168+
amount: 0.0002,
169+
asset: glmr,
170+
balance: BalanceBuilder().substrate().system().account(),
171+
},
172+
}),
173+
new AssetConfig({
174+
asset: glmr,
175+
balance: BalanceBuilder().substrate().system().account(),
176+
contract: ContractBuilder().Xtokens().transfer(),
177+
destination: zeitgeist,
178+
destinationFee: {
179+
amount: 0.3,
169180
asset: glmr,
170181
balance: BalanceBuilder().substrate().system().account(),
171182
},
@@ -521,7 +532,7 @@ export const moonbeamConfig = new ChainConfig({
521532
contract: ContractBuilder().Xtokens().transfer(),
522533
destination: zeitgeist,
523534
destinationFee: {
524-
amount: 0.001,
535+
amount: 0.101,
525536
asset: usdcwh,
526537
balance: BalanceBuilder().evm().erc20(),
527538
},

packages/config/src/configs/zeitgeist.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
ExtrinsicBuilder,
44
FeeBuilder,
55
} from '@moonbeam-network/xcm-builder';
6-
import { usdcwh, ztg } from '../assets';
6+
import { glmr, usdcwh, ztg } from '../assets';
77
import { moonbeam, zeitgeist } from '../chains';
88
import { AssetConfig } from '../types/AssetConfig';
99
import { ChainConfig } from '../types/ChainConfig';
@@ -27,7 +27,22 @@ export const zeitgeistConfig = new ChainConfig({
2727
destination: moonbeam,
2828
destinationFee: {
2929
amount: 0.04,
30+
asset: glmr,
31+
balance: BalanceBuilder().substrate().tokens().accounts(),
32+
},
33+
extrinsic: ExtrinsicBuilder().xTokens().transferMultiCurrencies(),
34+
fee: {
3035
asset: ztg,
36+
balance: BalanceBuilder().substrate().system().account(),
37+
},
38+
}),
39+
new AssetConfig({
40+
asset: glmr,
41+
balance: BalanceBuilder().substrate().tokens().accounts(),
42+
destination: moonbeam,
43+
destinationFee: {
44+
amount: 0.01,
45+
asset: glmr,
3146
balance: BalanceBuilder().substrate().tokens().accounts(),
3247
},
3348
extrinsic: ExtrinsicBuilder().xTokens().transfer(),

packages/sdk/src/getTransferData/getDestinationData.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export async function getDestinationData({
2929
amount: 0n,
3030
decimals: await getDecimals({
3131
address: destinationAddress,
32+
chain,
3233
config,
3334
evmSigner,
3435
polkadot,
@@ -37,7 +38,9 @@ export async function getDestinationData({
3738

3839
const balance = await getBalance({
3940
address: destinationAddress,
41+
chain,
4042
config,
43+
decimals: zeroAmount.decimals,
4144
evmSigner,
4245
polkadot,
4346
});
@@ -53,7 +56,6 @@ export async function getDestinationData({
5356
polkadot,
5457
});
5558
const minAmount = zeroAmount.copyWith({ amount: min });
56-
5759
return {
5860
balance: balanceAmount,
5961
chain,

packages/sdk/src/getTransferData/getSourceData.ts

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ import {
55
SubstrateQueryConfig,
66
} from '@moonbeam-network/xcm-builder';
77
import { FeeAssetConfig, TransferConfig } from '@moonbeam-network/xcm-config';
8-
import { AssetAmount } from '@moonbeam-network/xcm-types';
8+
import { AnyChain, AssetAmount } from '@moonbeam-network/xcm-types';
99
import { convertDecimals, toBigInt } from '@moonbeam-network/xcm-utils';
1010
import Big from 'big.js';
1111
import { TransferContractInterface, createContract } from '../contract';
1212
import { PolkadotService } from '../polkadot';
1313
import { EvmSigner, SourceChainTransferData } from '../sdk.interfaces';
14-
import { getBalance, getDecimals, getMin } from './getTransferData.utils';
14+
import {
15+
GetBalancesParams,
16+
getBalance,
17+
getDecimals,
18+
getMin,
19+
} from './getTransferData.utils';
1520

1621
export interface GetSourceDataParams {
1722
transferConfig: TransferConfig;
@@ -39,6 +44,7 @@ export async function getSourceData({
3944
amount: 0n,
4045
decimals: await getDecimals({
4146
address: destinationAddress,
47+
chain,
4248
config,
4349
evmSigner,
4450
polkadot,
@@ -50,6 +56,7 @@ export async function getSourceData({
5056
decimals: await getDecimals({
5157
address: destinationAddress,
5258
asset: config.fee.asset,
59+
chain,
5360
config,
5461
evmSigner,
5562
polkadot,
@@ -62,6 +69,7 @@ export async function getSourceData({
6269
decimals: await getDecimals({
6370
address: destinationAddress,
6471
asset: config.destinationFee.asset,
72+
chain,
6573
config,
6674
evmSigner,
6775
polkadot,
@@ -71,14 +79,18 @@ export async function getSourceData({
7179

7280
const balance = await getBalance({
7381
address: sourceAddress,
82+
chain,
7483
config,
84+
decimals: zeroAmount.decimals,
7585
evmSigner,
7686
polkadot,
7787
});
7888

79-
const feeBalance = await getFeeBalances({
89+
const feeBalance = await getFeeBalance({
8090
address: sourceAddress,
8191
balance,
92+
chain,
93+
decimals: zeroFeeAmount.decimals,
8294
feeConfig: config.fee,
8395
polkadot,
8496
});
@@ -87,9 +99,11 @@ export async function getSourceData({
8799
config.destinationFee.asset,
88100
)
89101
? balance
90-
: await getFeeBalances({
102+
: await getFeeBalance({
91103
address: sourceAddress,
92104
balance,
105+
chain,
106+
decimals: zeroDestinationFeeAmount.decimals,
93107
feeConfig: config.destinationFee,
94108
polkadot,
95109
});
@@ -117,6 +131,7 @@ export async function getSourceData({
117131
});
118132
const fee = await getFee({
119133
balance,
134+
chain,
120135
contract,
121136
decimals: zeroFeeAmount.decimals,
122137
evmSigner,
@@ -134,6 +149,7 @@ export async function getSourceData({
134149
amount: destinationFeeBalance,
135150
});
136151
const minAmount = zeroAmount.copyWith({ amount: min });
152+
137153
const maxAmount = getMax({
138154
balanceAmount,
139155
existentialDeposit,
@@ -153,32 +169,41 @@ export async function getSourceData({
153169
};
154170
}
155171

156-
export interface GetBalancesParams {
157-
address: string;
172+
export interface GetFeeBalanceParams
173+
extends Omit<GetBalancesParams, 'config' | 'evmSigner'> {
158174
balance: bigint;
159175
feeConfig: FeeAssetConfig | undefined;
160-
polkadot: PolkadotService;
161176
}
162177

163-
export async function getFeeBalances({
178+
export async function getFeeBalance({
164179
address,
165180
balance,
181+
chain,
182+
decimals,
166183
feeConfig,
167184
polkadot,
168-
}: GetBalancesParams) {
169-
return feeConfig
170-
? polkadot.query(
171-
feeConfig.balance.build({
172-
address,
173-
asset: polkadot.chain.getBalanceAssetId(feeConfig.asset),
174-
}) as SubstrateQueryConfig,
175-
)
176-
: balance;
185+
}: GetFeeBalanceParams) {
186+
if (!feeConfig) {
187+
return balance;
188+
}
189+
190+
const convertAmount = chain.usesOwnDecimalsInternally;
191+
const feeBalance = await polkadot.query(
192+
feeConfig.balance.build({
193+
address,
194+
asset: polkadot.chain.getBalanceAssetId(feeConfig.asset),
195+
}) as SubstrateQueryConfig,
196+
);
197+
198+
return convertAmount
199+
? convertDecimals(feeBalance, polkadot.decimals, decimals)
200+
: feeBalance;
177201
}
178202

179203
export interface GetFeeParams {
180204
balance: bigint;
181205
contract?: ContractConfig;
206+
chain: AnyChain;
182207
decimals: number;
183208
evmSigner?: EvmSigner;
184209
extrinsic?: ExtrinsicConfig;
@@ -189,6 +214,7 @@ export interface GetFeeParams {
189214

190215
export async function getFee({
191216
balance,
217+
chain,
192218
contract,
193219
decimals,
194220
evmSigner,
@@ -215,7 +241,13 @@ export async function getFee({
215241

216242
const xcmDeliveryFee = getXcmDeliveryFee(decimals, feeConfig);
217243

218-
return extrinsicFee + xcmDeliveryFee;
244+
const convertAmount = chain.usesOwnDecimalsInternally;
245+
246+
const totalFee = extrinsicFee + xcmDeliveryFee;
247+
248+
return convertAmount
249+
? convertDecimals(totalFee, polkadot.decimals, decimals)
250+
: totalFee;
219251
}
220252

221253
throw new Error('Either contract or extrinsic must be provided');

packages/sdk/src/getTransferData/getTransferData.utils.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,42 @@
11
import { CallType, SubstrateQueryConfig } from '@moonbeam-network/xcm-builder';
22
import { AssetConfig } from '@moonbeam-network/xcm-config';
3-
import { Asset } from '@moonbeam-network/xcm-types';
4-
import { toBigInt } from '@moonbeam-network/xcm-utils';
3+
import { AnyChain, Asset } from '@moonbeam-network/xcm-types';
4+
import { convertDecimals, toBigInt } from '@moonbeam-network/xcm-utils';
55
import { BalanceContractInterface, createContract } from '../contract';
66
import { PolkadotService } from '../polkadot';
77
import { EvmSigner } from '../sdk.interfaces';
88

9-
export interface GetFeeBalancesParams {
9+
export interface GetBalancesParams {
1010
address: string;
11+
asset?: Asset;
12+
chain: AnyChain;
1113
config: AssetConfig;
14+
decimals: number;
1215
evmSigner?: EvmSigner;
1316
polkadot: PolkadotService;
14-
asset?: Asset;
1517
}
1618

19+
export type GetDecimalsParams = Omit<GetBalancesParams, 'decimals'>;
20+
1721
export async function getBalance({
1822
address,
23+
chain,
1924
config,
25+
decimals,
2026
evmSigner,
2127
polkadot,
22-
}: GetFeeBalancesParams) {
28+
}: GetBalancesParams) {
2329
const cfg = config.balance.build({
2430
address,
2531
asset: polkadot.chain.getBalanceAssetId(config.asset),
2632
});
2733

2834
if (cfg.type === CallType.Substrate) {
29-
return polkadot.query(cfg as SubstrateQueryConfig);
35+
const convertAmount = chain.usesOwnDecimalsInternally;
36+
const balance = await polkadot.query(cfg as SubstrateQueryConfig);
37+
return convertAmount
38+
? convertDecimals(balance, polkadot.decimals, decimals)
39+
: balance;
3040
}
3141

3242
if (!evmSigner) {
@@ -44,7 +54,7 @@ export async function getDecimals({
4454
config,
4555
evmSigner,
4656
polkadot,
47-
}: GetFeeBalancesParams) {
57+
}: GetDecimalsParams) {
4858
const cfg = config.balance.build({
4959
address,
5060
asset: polkadot.chain.getBalanceAssetId(asset || config.asset),

packages/types/src/chain/parachain/Parachain.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface ParachainConstructorParams
1010
genesisHash: string;
1111
parachainId: number;
1212
ss58Format: number;
13+
usesOwnDecimalsInternally?: boolean;
1314
weight?: number;
1415
ws: string;
1516
}
@@ -23,6 +24,8 @@ export class Parachain extends Chain {
2324

2425
readonly ss58Format: number;
2526

27+
readonly usesOwnDecimalsInternally: boolean;
28+
2629
readonly weight: number | undefined;
2730

2831
readonly ws: string;
@@ -31,6 +34,7 @@ export class Parachain extends Chain {
3134
assetsData,
3235
genesisHash,
3336
parachainId,
37+
usesOwnDecimalsInternally,
3438
ss58Format,
3539
weight,
3640
ws,
@@ -46,6 +50,7 @@ export class Parachain extends Chain {
4650
this.genesisHash = genesisHash;
4751
this.parachainId = parachainId;
4852
this.ss58Format = ss58Format;
53+
this.usesOwnDecimalsInternally = !!usesOwnDecimalsInternally;
4954
this.weight = weight;
5055
this.ws = ws;
5156
}

0 commit comments

Comments
 (0)