diff --git a/.changeset/brave-clocks-live.md b/.changeset/brave-clocks-live.md new file mode 100644 index 00000000..ddf886d3 --- /dev/null +++ b/.changeset/brave-clocks-live.md @@ -0,0 +1,5 @@ +--- +'@moonbeam-network/xcm-sdk': minor +--- + +Sopport optional EvmSigner, Fix: PolkadotService asset get (not reflecting config service) diff --git a/packages/sdk/src/getTransferData/getDestinationData.ts b/packages/sdk/src/getTransferData/getDestinationData.ts index 076792b0..1f59fd0b 100644 --- a/packages/sdk/src/getTransferData/getDestinationData.ts +++ b/packages/sdk/src/getTransferData/getDestinationData.ts @@ -10,7 +10,7 @@ import { getBalance, getDecimals, getMin } from './getTransferData.utils'; export interface GetDestinationDataParams { transferConfig: TransferConfig; destinationAddress: string; - evmSigner: EvmSigner; + evmSigner?: EvmSigner; polkadot: PolkadotService; } diff --git a/packages/sdk/src/getTransferData/getSourceData.ts b/packages/sdk/src/getTransferData/getSourceData.ts index 3e9b2eb6..7abb32ff 100644 --- a/packages/sdk/src/getTransferData/getSourceData.ts +++ b/packages/sdk/src/getTransferData/getSourceData.ts @@ -17,7 +17,7 @@ export interface GetSourceDataParams { transferConfig: TransferConfig; destinationAddress: string; destinationFee: AssetAmount; - evmSigner: EvmSigner; + evmSigner?: EvmSigner; polkadot: PolkadotService; sourceAddress: string; } diff --git a/packages/sdk/src/getTransferData/getTransferData.ts b/packages/sdk/src/getTransferData/getTransferData.ts index 90b49456..648e5eb6 100644 --- a/packages/sdk/src/getTransferData/getTransferData.ts +++ b/packages/sdk/src/getTransferData/getTransferData.ts @@ -1,6 +1,6 @@ /* eslint-disable @typescript-eslint/no-use-before-define */ import type { TransactionResponse } from '@ethersproject/abstract-provider'; -import { TransferConfig } from '@moonbeam-network/xcm-config'; +import { IConfigService, TransferConfig } from '@moonbeam-network/xcm-config'; import { AssetAmount } from '@moonbeam-network/xcm-types'; import { convertDecimals, toBigInt } from '@moonbeam-network/xcm-utils'; import Big from 'big.js'; @@ -15,26 +15,24 @@ import { getDestinationData } from './getDestinationData'; import { getSourceData } from './getSourceData'; export interface GetTransferDataParams extends Partial { + configService: IConfigService; destinationAddress: string; sourceAddress: string; transferConfig: TransferConfig; } export async function getTransferData({ + configService, destinationAddress, evmSigner, polkadotSigner, sourceAddress, transferConfig, }: GetTransferDataParams): Promise { - if (!evmSigner) { - throw new Error('EVM Signer must be provided'); - } - - const [destPolkadot, srcPolkadot] = await PolkadotService.createMulti([ - transferConfig.destination.chain, - transferConfig.source.chain, - ]); + const [destPolkadot, srcPolkadot] = await PolkadotService.createMulti( + [transferConfig.destination.chain, transferConfig.source.chain], + configService, + ); const destination = await getDestinationData({ destinationAddress, @@ -82,6 +80,7 @@ export async function getTransferData({ source, async swap() { return getTransferData({ + configService, destinationAddress: sourceAddress, evmSigner, polkadotSigner, diff --git a/packages/sdk/src/getTransferData/getTransferData.utils.ts b/packages/sdk/src/getTransferData/getTransferData.utils.ts index b1af6a52..f0c4b1ac 100644 --- a/packages/sdk/src/getTransferData/getTransferData.utils.ts +++ b/packages/sdk/src/getTransferData/getTransferData.utils.ts @@ -9,7 +9,7 @@ import { EvmSigner } from '../sdk.interfaces'; export interface GetFeeBalancesParams { address: string; config: AssetConfig; - evmSigner: EvmSigner; + evmSigner?: EvmSigner; polkadot: PolkadotService; asset?: Asset; } @@ -29,6 +29,10 @@ export async function getBalance({ return polkadot.query(cfg as SubstrateQueryConfig); } + if (!evmSigner) { + throw new Error('Evm signer must be provided'); + } + const contract = createContract(cfg, evmSigner) as BalanceContractInterface; return contract.getBalance(); @@ -50,6 +54,10 @@ export async function getDecimals({ return polkadot.getAssetDecimals(asset || config.asset); } + if (!evmSigner) { + throw new Error('Evm signer must be provided'); + } + const contract = createContract(cfg, evmSigner) as BalanceContractInterface; return contract.getDecimals(); diff --git a/packages/sdk/src/polkadot/PolkadotService.ts b/packages/sdk/src/polkadot/PolkadotService.ts index a7f4fa25..01368451 100644 --- a/packages/sdk/src/polkadot/PolkadotService.ts +++ b/packages/sdk/src/polkadot/PolkadotService.ts @@ -5,7 +5,7 @@ import { SubstrateQueryConfig, } from '@moonbeam-network/xcm-builder'; import { - assetsMap, + IConfigService, darwiniaPangoro, eq, equilibriumAlphanet, @@ -29,17 +29,38 @@ export class PolkadotService { readonly chain: AnyParachain; - constructor(api: ApiPromise, chain: AnyParachain) { + readonly configService: IConfigService; + + constructor( + api: ApiPromise, + chain: AnyParachain, + configService: IConfigService, + ) { this.api = api; this.chain = chain; + this.configService = configService; } - static async create(chain: AnyParachain): Promise { - return new PolkadotService(await getPolkadotApi(chain.ws), chain); + static async create( + chain: AnyParachain, + configService: IConfigService, + ): Promise { + return new PolkadotService( + await getPolkadotApi(chain.ws), + chain, + configService, + ); } - static async createMulti(chains: AnyParachain[]): Promise { - return Promise.all(chains.map(PolkadotService.create)); + static async createMulti( + chains: AnyParachain[], + configService: IConfigService, + ): Promise { + return Promise.all( + chains.map((chain: AnyParachain) => + PolkadotService.create(chain, configService), + ), + ); } get decimals(): number { @@ -65,7 +86,7 @@ export class PolkadotService { throw new Error('No native symbol key found'); } - const asset = assetsMap.get(key); + const asset = this.configService.getAsset(key); if (!asset) { throw new Error(`No asset found for key "${key}" and symbol "${symbol}"`); diff --git a/packages/sdk/src/sdk.ts b/packages/sdk/src/sdk.ts index 6969c4d1..76701b88 100644 --- a/packages/sdk/src/sdk.ts +++ b/packages/sdk/src/sdk.ts @@ -1,5 +1,9 @@ /* eslint-disable sort-keys */ -import { ConfigBuilder, IConfigService } from '@moonbeam-network/xcm-config'; +import { + ConfigBuilder, + ConfigService, + IConfigService, +} from '@moonbeam-network/xcm-config'; import { AnyChain, Asset, Ecosystem } from '@moonbeam-network/xcm-types'; import { getTransferData as gtd } from './getTransferData/getTransferData'; import { Signers, TransferData } from './sdk.interfaces'; @@ -9,7 +13,8 @@ export interface SdkOptions extends Partial { } export function Sdk(options?: SdkOptions) { - const configService = options?.configService; + const configService = options?.configService ?? new ConfigService(); + return { assets(ecosystem?: Ecosystem) { const { assets, asset } = ConfigBuilder(configService).assets(ecosystem); @@ -35,6 +40,7 @@ export function Sdk(options?: SdkOptions) { ): Promise { return gtd({ ...options, + configService, destinationAddress, evmSigner: signers?.evmSigner ?? signers?.ethersSigner, sourceAddress, @@ -61,6 +67,7 @@ export function Sdk(options?: SdkOptions) { sourceKeyOrChain, }: SdkTransferParams): Promise { return gtd({ + configService, destinationAddress, evmSigner: evmSigner ?? ethersSigner, polkadotSigner,