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

USDC from Wormhole in Zeitgeist #179

Merged
merged 12 commits into from
Jan 31, 2024
7 changes: 7 additions & 0 deletions .changeset/wild-singers-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@moonbeam-network/xcm-config': patch
'@moonbeam-network/xcm-types': patch
'@moonbeam-network/xcm-sdk': patch
---

USDC wh integration with Moonbeam
14 changes: 11 additions & 3 deletions packages/config/src/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,13 +454,11 @@ export const interlay = new Parachain({
asset: intr,
decimals: 10,
id: { Token: intr.originSymbol },
metadataId: 0,
},
{
asset: ibtc,
decimals: 8,
id: { Token: ibtc.originSymbol },
metadataId: 0,
},
],
ecosystem: Ecosystem.Polkadot,
Expand Down Expand Up @@ -549,7 +547,6 @@ export const kintsugi = new Parachain({
asset: kbtc,
decimals: 8,
id: { Token: kbtc.originSymbol },
metadataId: 0,
},
],
ecosystem: Ecosystem.Kusama,
Expand Down Expand Up @@ -1400,13 +1397,24 @@ export const uniqueAlpha = new Parachain({
});

export const zeitgeist = new Parachain({
assetsData: [
{
asset: usdcwh,
id: { ForeignAsset: 1 },
},
{
asset: glmr,
id: { ForeignAsset: 3 },
},
],
ecosystem: Ecosystem.Polkadot,
genesisHash:
'0x1bf2a2ecb4a868de66ea8610f2ce7c8c43706561b6476031315f6640fe38e060',
key: 'zeitgeist',
name: 'Zeitgeist',
parachainId: 2092,
ss58Format: 73,
usesChainDecimals: true,
ws: 'wss://zeitgeist-rpc.dwellir.com',
});

Expand Down
28 changes: 27 additions & 1 deletion 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 @@ -515,6 +526,21 @@ export const moonbeamConfig = new ChainConfig({
balance: BalanceBuilder().substrate().system().account(),
},
}),
new AssetConfig({
asset: usdcwh,
balance: BalanceBuilder().evm().erc20(),
contract: ContractBuilder().Xtokens().transfer(),
destination: zeitgeist,
destinationFee: {
amount: 0.101,
asset: usdcwh,
balance: BalanceBuilder().evm().erc20(),
},
fee: {
asset: glmr,
balance: BalanceBuilder().substrate().system().account(),
},
}),
new AssetConfig({
asset: usdtwh,
balance: BalanceBuilder().evm().erc20(),
Expand Down
28 changes: 27 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 { 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 @@ -21,6 +21,32 @@ export const zeitgeistConfig = new ChainConfig({
},
extrinsic: ExtrinsicBuilder().xTokens().transfer(),
}),
new AssetConfig({
asset: usdcwh,
balance: BalanceBuilder().substrate().tokens().accounts(),
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(),
}),
],
chain: zeitgeist,
});
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
65 changes: 47 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,40 @@ 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 feeBalance = await polkadot.query(
feeConfig.balance.build({
address,
asset: polkadot.chain.getBalanceAssetId(feeConfig.asset),
}) as SubstrateQueryConfig,
);

return chain.usesChainDecimals
? 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 +213,7 @@ export interface GetFeeParams {

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

const xcmDeliveryFee = getXcmDeliveryFee(decimals, feeConfig);

return extrinsicFee + xcmDeliveryFee;
const totalFee = extrinsicFee + xcmDeliveryFee;

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

throw new Error('Either contract or extrinsic must be provided');
Expand Down
23 changes: 16 additions & 7 deletions packages/sdk/src/getTransferData/getTransferData.utils.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
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 balance = await polkadot.query(cfg as SubstrateQueryConfig);
return chain.usesChainDecimals
? convertDecimals(balance, polkadot.decimals, decimals)
: balance;
}

if (!evmSigner) {
Expand All @@ -44,7 +53,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
Loading
Loading