diff --git a/packages/sdk/src/getTransferData/getTransferData.ts b/packages/sdk/src/getTransferData/getTransferData.ts index 36c227c3..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,22 +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 { - 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, @@ -78,6 +80,7 @@ export async function getTransferData({ source, async swap() { return getTransferData({ + configService, destinationAddress: sourceAddress, evmSigner, polkadotSigner, diff --git a/packages/sdk/src/polkadot/PolkadotService.ts b/packages/sdk/src/polkadot/PolkadotService.ts index a617ac7b..01368451 100644 --- a/packages/sdk/src/polkadot/PolkadotService.ts +++ b/packages/sdk/src/polkadot/PolkadotService.ts @@ -5,6 +5,7 @@ import { SubstrateQueryConfig, } from '@moonbeam-network/xcm-builder'; import { + IConfigService, darwiniaPangoro, eq, equilibriumAlphanet, @@ -28,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 { @@ -64,7 +86,7 @@ export class PolkadotService { throw new Error('No native symbol key found'); } - const asset = this.chain.assetsData.get(key)?.asset; + 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,