Skip to content

Commit

Permalink
--wip-- [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
ekenigs committed Sep 11, 2024
1 parent 4644b6d commit 1c32e11
Show file tree
Hide file tree
Showing 19 changed files with 313 additions and 36 deletions.
3 changes: 2 additions & 1 deletion packages/builder/src/balance/BalanceBuilder.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import type { ChainAsset } from '@moonbeam-network/xcm-types';
import type { Struct, u128 } from '@polkadot/types';
import type { ConfigBuilder } from '../builder.interfaces';
import type { ContractConfig } from '../contract';
import type { EvmQueryConfig } from '../types/evm/EvmQueryConfig';
import type { SubstrateQueryConfig } from '../types/substrate/SubstrateQueryConfig';

export type BalanceConfigBuilder = ConfigBuilder<
ContractConfig | SubstrateQueryConfig,
ContractConfig | SubstrateQueryConfig | EvmQueryConfig,
BalanceBuilderPrams
>;

Expand Down
13 changes: 13 additions & 0 deletions packages/builder/src/balance/BalanceBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import type {
PalletBalancesAccountData,
} from '@polkadot/types/lookup';
import { evmToAddress } from '@polkadot/util-crypto';
import type { Address } from 'viem';
import { ContractConfig } from '../contract';
import { EvmQueryConfig } from '../types/evm/EvmQueryConfig';
import { SubstrateQueryConfig } from '../types/substrate/SubstrateQueryConfig';
import type {
BalanceConfigBuilder,
Expand All @@ -25,6 +27,7 @@ export function BalanceBuilder() {
export function evm() {
return {
erc20,
native,
};
}

Expand All @@ -46,6 +49,16 @@ function erc20(): BalanceConfigBuilder {
};
}

function native(): BalanceConfigBuilder {
return {
build: ({ address }) => {
return new EvmQueryConfig({
func: 'getBalance',
args: [{ address: address as Address }],
});
},
};
}
export function substrate() {
return {
assets,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigBuilder } from '../builder.interfaces';
import type { ContractConfig } from '../types/ContractConfig';
import type { ContractConfig } from '../types/evm/ContractConfig';

export type ContractConfigBuilder = ConfigBuilder<ContractConfig>;
2 changes: 1 addition & 1 deletion packages/builder/src/contract/contracts/Xtokens/Xtokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type AnyParachain, EvmParachain } from '@moonbeam-network/xcm-types';
import { formatAssetIdToERC20 } from '@moonbeam-network/xcm-utils';
import { u8aToHex } from '@polkadot/util';
import { decodeAddress, evmToAddress } from '@polkadot/util-crypto';
import { ContractConfig } from '../../../types/ContractConfig';
import { ContractConfig } from '../../../types/evm/ContractConfig';
import type { ContractConfigBuilder } from '../../ContractBuilder.interfaces';
import { XTOKENS_ABI } from './XtokensABI';

Expand Down
2 changes: 1 addition & 1 deletion packages/builder/src/contract/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from '../types/evm/ContractConfig';
export * from './ContractBuilder';
export * from './ContractBuilder.interfaces';
export * from '../types/ContractConfig';
3 changes: 1 addition & 2 deletions packages/builder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ export * from './contract';
export * from './extrinsic';
export * from './fee';
export * from './mrl';
export * from './types/BaseConfig';
export * from './types/substrate/SubstrateQueryConfig';
export * from './types';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type Address, encodeFunctionData } from 'viem';
import { ERC20_ABI } from '../../../../../balance/Erc20Abi';
import type { ContractConfig } from '../../../../../types/ContractConfig';
import type { ContractConfig } from '../../../../../types/evm/ContractConfig';
import { ExtrinsicConfig } from '../../../../../types/substrate/ExtrinsicConfig';
import { BATCH_CONTRACT_ADDRESS } from '../../../../MrlBuilder.constants';
import type { MrlConfigBuilder } from '../../../../MrlBuilder.interfaces';
Expand Down
4 changes: 2 additions & 2 deletions packages/builder/src/mrl/providers/wormhole/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { contract } from './contract';
import { extrinsic } from './extrinsic';
import { wormhole as whBuilder } from './wormhole';

export * from './wormhole/wormholeFactory';
export * from './wormhole/WormholeConfig';
export * from './extrinsic/ethereumXcm/BatchContractAbi';
export * from './wormhole/WormholeConfig';
export * from './wormhole/wormholeFactory';

export function wormhole() {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import type { Network, Wormhole } from '@wormhole-foundation/sdk-connect';

export type WormholeTransferFunctions = 'tokenTransfer';
export type Args = Parameters<Wormhole<Network>[WormholeTransferFunctions]>;
export type WormholeFunctionArgs = Parameters<
Wormhole<Network>[WormholeTransferFunctions]
>;

export interface WormholeConfigConstructorParams {
args: Args;
args: WormholeFunctionArgs;
func: WormholeTransferFunctions;
}

export class WormholeConfig {
readonly args: Args;
readonly args: WormholeFunctionArgs;

readonly func: WormholeTransferFunctions;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Abi, encodeFunctionData } from 'viem';
import { BaseConfig, type BaseConfigConstructorParams } from './BaseConfig';
import { BaseConfig, type BaseConfigConstructorParams } from '../BaseConfig';

export interface ContractConfigConstructorParams
extends BaseConfigConstructorParams {
Expand Down
25 changes: 25 additions & 0 deletions packages/builder/src/types/evm/EvmQueryConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { HttpTransport, PublicClient } from 'viem';

export type EvmQueryFunctions = 'getBalance';
export type EvmFunctionArgs = Parameters<
PublicClient<HttpTransport>[EvmQueryFunctions]
>;

export interface EvmQueryConfigParams {
readonly args: EvmFunctionArgs;
readonly func: EvmQueryFunctions;
}

export class EvmQueryConfig {
readonly args: EvmFunctionArgs;
readonly func: EvmQueryFunctions;

static is(obj: unknown): obj is EvmQueryConfig {
return obj instanceof EvmQueryConfig;
}

constructor({ args, func }: EvmQueryConfigParams) {
this.args = args;
this.func = func;
}
}
2 changes: 2 additions & 0 deletions packages/builder/src/types/evm/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './ContractConfig';
export * from './EvmQueryConfig';
2 changes: 2 additions & 0 deletions packages/builder/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './evm';
export * from './substrate';
3 changes: 3 additions & 0 deletions packages/builder/src/types/substrate/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './ExtrinsicConfig';
export * from './SubstrateCallConfig';
export * from './SubstrateQueryConfig';
30 changes: 16 additions & 14 deletions packages/config/src/mrl-configs/fantomTestnet.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import {
BalanceBuilder,
FeeBuilder,
MrlBuilder,
} from '@moonbeam-network/xcm-builder';
import { ftmwh, hdx } from '../assets';
import { fantomTestnet, hydrationAlphanet } from '../chains';
import { BalanceBuilder, MrlBuilder } from '@moonbeam-network/xcm-builder';
import { dev, ftmwh } from '../assets';
import { fantomTestnet, peaqAlphanet } from '../chains';
import { ChainRoutes } from '../types/ChainRoutes';

export const fantomTestnetRoutes = new ChainRoutes({
Expand All @@ -13,21 +9,27 @@ export const fantomTestnetRoutes = new ChainRoutes({
{
asset: ftmwh,
source: {
balance: BalanceBuilder().substrate().system().account(), // TODO:
balance: BalanceBuilder().evm().native(),
destinationFee: {
balance: BalanceBuilder().evm().native(),
},
},
destination: {
chain: hydrationAlphanet,
balance: BalanceBuilder().substrate().system().account(), // TODO:
chain: peaqAlphanet,
balance: BalanceBuilder().substrate().assets().account(),
fee: {
// TODO:
amount: FeeBuilder().assetManager().assetTypeUnitsPerSecond(),
asset: hdx,
balance: BalanceBuilder().substrate().system().account(),
asset: ftmwh,
amount: 0.01,
},
},
mrl: {
isAutomatic: true,
transfer: MrlBuilder().wormhole().wormhole().tokenTransfer(),
moonChainFee: {
asset: dev,
amount: 0.1,
balance: BalanceBuilder().substrate().system().account(),
},
},
},
],
Expand Down
5 changes: 5 additions & 0 deletions packages/config/src/types/AssetRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export interface FeeConfig {
export interface MrlConfig {
isAutomatic: boolean;
transfer: MrlConfigBuilder;
moonChainFee: {
asset: Asset;
amount: number | FeeConfigBuilder;
balance: BalanceConfigBuilder;
};
}

export interface DestinationFeeConfig
Expand Down
19 changes: 13 additions & 6 deletions packages/sdk/src/getTransferData/getTransferData.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
type AssetMinConfigBuilder,
type BalanceConfigBuilder,
ContractConfig,
EvmQueryConfig,
type ExtrinsicConfig,
type FeeConfigBuilder,
SubstrateQueryConfig,
Expand Down Expand Up @@ -51,14 +52,20 @@ export async function getBalance({
return amount.copyWith({ amount: converted });
}

if (
ContractConfig.is(config) &&
(EvmChain.is(chain) || EvmParachain.is(chain))
) {
if (EvmChain.is(chain) || EvmParachain.is(chain)) {
const evm = EvmService.create(chain);
const balance = await evm.getBalance(address, config);

return amount.copyWith({ amount: balance });
if (ContractConfig.is(config)) {
const balance = await evm.getBalance(address, config);

return amount.copyWith({ amount: balance });
}

if (EvmQueryConfig.is(config)) {
const balance = await evm.query(config);

return amount.copyWith({ amount: balance });
}
}

throw new Error(
Expand Down
11 changes: 9 additions & 2 deletions packages/sdk/src/services/evm/EvmService.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import type { ContractConfig } from '@moonbeam-network/xcm-builder';
import type {
ContractConfig,
EvmQueryConfig,
} from '@moonbeam-network/xcm-builder';
import type { EvmChain, EvmParachain } from '@moonbeam-network/xcm-types';
import {
createPublicClient,
http,
type Address,
type Hash,
type HttpTransport,
type PublicClient,
createPublicClient,
} from 'viem';
import type { EvmSigner } from '../../sdk.interfaces';

Expand All @@ -27,6 +30,10 @@ export class EvmService {
});
}

async query(query: EvmQueryConfig) {
return this.client[query.func](...query.args);
}

async getFee(address: string, contract: ContractConfig): Promise<bigint> {
const gas = await this.client.estimateContractGas({
abi: contract.abi,
Expand Down
Loading

0 comments on commit 1c32e11

Please sign in to comment.