Skip to content

Commit

Permalink
implement GLMR integration with Zeitgeist and add param to use chain …
Browse files Browse the repository at this point in the history
…decimals internally
  • Loading branch information
mmaurello committed Jan 31, 2024
1 parent 9f843ed commit 8cc3f76
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 30 deletions.
6 changes: 5 additions & 1 deletion packages/config/src/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1400,9 +1400,12 @@ export const zeitgeist = new Parachain({
assetsData: [
{
asset: usdcwh,
decimals: 10,
id: { ForeignAsset: 1 },
},
{
asset: glmr,
id: { ForeignAsset: 3 },
},
],
ecosystem: Ecosystem.Polkadot,
genesisHash:
Expand All @@ -1411,6 +1414,7 @@ export const zeitgeist = new Parachain({
name: 'Zeitgeist',
parachainId: 2092,
ss58Format: 73,
usesOwnDecimalsInternally: true,
ws: 'wss://zeitgeist-rpc.dwellir.com',
});

Expand Down
15 changes: 13 additions & 2 deletions packages/config/src/configs/moonbeam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,18 @@ export const moonbeamConfig = new ChainConfig({
contract: ContractBuilder().Xtokens().transfer(),
destination: pendulum,
destinationFee: {
amount: 0.0002, //
amount: 0.0002,
asset: glmr,
balance: BalanceBuilder().substrate().system().account(),
},
}),
new AssetConfig({
asset: glmr,
balance: BalanceBuilder().substrate().system().account(),
contract: ContractBuilder().Xtokens().transfer(),
destination: zeitgeist,
destinationFee: {
amount: 0.3,
asset: glmr,
balance: BalanceBuilder().substrate().system().account(),
},
Expand Down Expand Up @@ -521,7 +532,7 @@ export const moonbeamConfig = new ChainConfig({
contract: ContractBuilder().Xtokens().transfer(),
destination: zeitgeist,
destinationFee: {
amount: 0.001,
amount: 0.101,
asset: usdcwh,
balance: BalanceBuilder().evm().erc20(),
},
Expand Down
17 changes: 16 additions & 1 deletion packages/config/src/configs/zeitgeist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
ExtrinsicBuilder,
FeeBuilder,
} from '@moonbeam-network/xcm-builder';
import { usdcwh, ztg } from '../assets';
import { glmr, usdcwh, ztg } from '../assets';
import { moonbeam, zeitgeist } from '../chains';
import { AssetConfig } from '../types/AssetConfig';
import { ChainConfig } from '../types/ChainConfig';
Expand All @@ -27,7 +27,22 @@ export const zeitgeistConfig = new ChainConfig({
destination: moonbeam,
destinationFee: {
amount: 0.04,
asset: glmr,
balance: BalanceBuilder().substrate().tokens().accounts(),
},
extrinsic: ExtrinsicBuilder().xTokens().transferMultiCurrencies(),
fee: {
asset: ztg,
balance: BalanceBuilder().substrate().system().account(),
},
}),
new AssetConfig({
asset: glmr,
balance: BalanceBuilder().substrate().tokens().accounts(),
destination: moonbeam,
destinationFee: {
amount: 0.01,
asset: glmr,
balance: BalanceBuilder().substrate().tokens().accounts(),
},
extrinsic: ExtrinsicBuilder().xTokens().transfer(),
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/getTransferData/getDestinationData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export async function getDestinationData({
amount: 0n,
decimals: await getDecimals({
address: destinationAddress,
chain,
config,
evmSigner,
polkadot,
Expand All @@ -37,7 +38,9 @@ export async function getDestinationData({

const balance = await getBalance({
address: destinationAddress,
chain,
config,
decimals: zeroAmount.decimals,
evmSigner,
polkadot,
});
Expand All @@ -53,7 +56,6 @@ export async function getDestinationData({
polkadot,
});
const minAmount = zeroAmount.copyWith({ amount: min });

return {
balance: balanceAmount,
chain,
Expand Down
68 changes: 50 additions & 18 deletions packages/sdk/src/getTransferData/getSourceData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ import {
SubstrateQueryConfig,
} from '@moonbeam-network/xcm-builder';
import { FeeAssetConfig, TransferConfig } from '@moonbeam-network/xcm-config';
import { AssetAmount } from '@moonbeam-network/xcm-types';
import { AnyChain, AssetAmount } from '@moonbeam-network/xcm-types';
import { convertDecimals, toBigInt } from '@moonbeam-network/xcm-utils';
import Big from 'big.js';
import { TransferContractInterface, createContract } from '../contract';
import { PolkadotService } from '../polkadot';
import { EvmSigner, SourceChainTransferData } from '../sdk.interfaces';
import { getBalance, getDecimals, getMin } from './getTransferData.utils';
import {
GetBalancesParams,
getBalance,
getDecimals,
getMin,
} from './getTransferData.utils';

export interface GetSourceDataParams {
transferConfig: TransferConfig;
Expand Down Expand Up @@ -39,6 +44,7 @@ export async function getSourceData({
amount: 0n,
decimals: await getDecimals({
address: destinationAddress,
chain,
config,
evmSigner,
polkadot,
Expand All @@ -50,6 +56,7 @@ export async function getSourceData({
decimals: await getDecimals({
address: destinationAddress,
asset: config.fee.asset,
chain,
config,
evmSigner,
polkadot,
Expand All @@ -62,6 +69,7 @@ export async function getSourceData({
decimals: await getDecimals({
address: destinationAddress,
asset: config.destinationFee.asset,
chain,
config,
evmSigner,
polkadot,
Expand All @@ -71,14 +79,18 @@ export async function getSourceData({

const balance = await getBalance({
address: sourceAddress,
chain,
config,
decimals: zeroAmount.decimals,
evmSigner,
polkadot,
});

const feeBalance = await getFeeBalances({
const feeBalance = await getFeeBalance({
address: sourceAddress,
balance,
chain,
decimals: zeroFeeAmount.decimals,
feeConfig: config.fee,
polkadot,
});
Expand All @@ -87,9 +99,11 @@ export async function getSourceData({
config.destinationFee.asset,
)
? balance
: await getFeeBalances({
: await getFeeBalance({
address: sourceAddress,
balance,
chain,
decimals: zeroDestinationFeeAmount.decimals,
feeConfig: config.destinationFee,
polkadot,
});
Expand Down Expand Up @@ -117,6 +131,7 @@ export async function getSourceData({
});
const fee = await getFee({
balance,
chain,
contract,
decimals: zeroFeeAmount.decimals,
evmSigner,
Expand All @@ -134,6 +149,7 @@ export async function getSourceData({
amount: destinationFeeBalance,
});
const minAmount = zeroAmount.copyWith({ amount: min });

const maxAmount = getMax({
balanceAmount,
existentialDeposit,
Expand All @@ -153,32 +169,41 @@ export async function getSourceData({
};
}

export interface GetBalancesParams {
address: string;
export interface GetFeeBalanceParams
extends Omit<GetBalancesParams, 'config' | 'evmSigner'> {
balance: bigint;
feeConfig: FeeAssetConfig | undefined;
polkadot: PolkadotService;
}

export async function getFeeBalances({
export async function getFeeBalance({
address,
balance,
chain,
decimals,
feeConfig,
polkadot,
}: GetBalancesParams) {
return feeConfig
? polkadot.query(
feeConfig.balance.build({
address,
asset: polkadot.chain.getBalanceAssetId(feeConfig.asset),
}) as SubstrateQueryConfig,
)
: balance;
}: GetFeeBalanceParams) {
if (!feeConfig) {
return balance;
}

const convertAmount = chain.usesOwnDecimalsInternally;
const feeBalance = await polkadot.query(
feeConfig.balance.build({
address,
asset: polkadot.chain.getBalanceAssetId(feeConfig.asset),
}) as SubstrateQueryConfig,
);

return convertAmount
? convertDecimals(feeBalance, polkadot.decimals, decimals)
: feeBalance;
}

export interface GetFeeParams {
balance: bigint;
contract?: ContractConfig;
chain: AnyChain;
decimals: number;
evmSigner?: EvmSigner;
extrinsic?: ExtrinsicConfig;
Expand All @@ -189,6 +214,7 @@ export interface GetFeeParams {

export async function getFee({
balance,
chain,
contract,
decimals,
evmSigner,
Expand All @@ -215,7 +241,13 @@ export async function getFee({

const xcmDeliveryFee = getXcmDeliveryFee(decimals, feeConfig);

return extrinsicFee + xcmDeliveryFee;
const convertAmount = chain.usesOwnDecimalsInternally;

const totalFee = extrinsicFee + xcmDeliveryFee;

return convertAmount
? convertDecimals(totalFee, polkadot.decimals, decimals)
: totalFee;
}

throw new Error('Either contract or extrinsic must be provided');
Expand Down
24 changes: 17 additions & 7 deletions packages/sdk/src/getTransferData/getTransferData.utils.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
import { CallType, SubstrateQueryConfig } from '@moonbeam-network/xcm-builder';
import { AssetConfig } from '@moonbeam-network/xcm-config';
import { Asset } from '@moonbeam-network/xcm-types';
import { toBigInt } from '@moonbeam-network/xcm-utils';
import { AnyChain, Asset } from '@moonbeam-network/xcm-types';
import { convertDecimals, toBigInt } from '@moonbeam-network/xcm-utils';
import { BalanceContractInterface, createContract } from '../contract';
import { PolkadotService } from '../polkadot';
import { EvmSigner } from '../sdk.interfaces';

export interface GetFeeBalancesParams {
export interface GetBalancesParams {
address: string;
asset?: Asset;
chain: AnyChain;
config: AssetConfig;
decimals: number;
evmSigner?: EvmSigner;
polkadot: PolkadotService;
asset?: Asset;
}

export type GetDecimalsParams = Omit<GetBalancesParams, 'decimals'>;

export async function getBalance({
address,
chain,
config,
decimals,
evmSigner,
polkadot,
}: GetFeeBalancesParams) {
}: GetBalancesParams) {
const cfg = config.balance.build({
address,
asset: polkadot.chain.getBalanceAssetId(config.asset),
});

if (cfg.type === CallType.Substrate) {
return polkadot.query(cfg as SubstrateQueryConfig);
const convertAmount = chain.usesOwnDecimalsInternally;
const balance = await polkadot.query(cfg as SubstrateQueryConfig);
return convertAmount
? convertDecimals(balance, polkadot.decimals, decimals)
: balance;
}

if (!evmSigner) {
Expand All @@ -44,7 +54,7 @@ export async function getDecimals({
config,
evmSigner,
polkadot,
}: GetFeeBalancesParams) {
}: GetDecimalsParams) {
const cfg = config.balance.build({
address,
asset: polkadot.chain.getBalanceAssetId(asset || config.asset),
Expand Down
5 changes: 5 additions & 0 deletions packages/types/src/chain/parachain/Parachain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface ParachainConstructorParams
genesisHash: string;
parachainId: number;
ss58Format: number;
usesOwnDecimalsInternally?: boolean;
weight?: number;
ws: string;
}
Expand All @@ -23,6 +24,8 @@ export class Parachain extends Chain {

readonly ss58Format: number;

readonly usesOwnDecimalsInternally: boolean;

readonly weight: number | undefined;

readonly ws: string;
Expand All @@ -31,6 +34,7 @@ export class Parachain extends Chain {
assetsData,
genesisHash,
parachainId,
usesOwnDecimalsInternally,
ss58Format,
weight,
ws,
Expand All @@ -46,6 +50,7 @@ export class Parachain extends Chain {
this.genesisHash = genesisHash;
this.parachainId = parachainId;
this.ss58Format = ss58Format;
this.usesOwnDecimalsInternally = !!usesOwnDecimalsInternally;
this.weight = weight;
this.ws = ws;
}
Expand Down

0 comments on commit 8cc3f76

Please sign in to comment.