Skip to content

Commit

Permalink
Optional evmSigner, PolkadotService asset fix (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
nohaapav authored Oct 11, 2023
1 parent 06d88d4 commit 02dafc0
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-clocks-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@moonbeam-network/xcm-sdk': minor
---

Sopport optional EvmSigner, Fix: PolkadotService asset get (not reflecting config service)
2 changes: 1 addition & 1 deletion packages/sdk/src/getTransferData/getDestinationData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { getBalance, getDecimals, getMin } from './getTransferData.utils';
export interface GetDestinationDataParams {
transferConfig: TransferConfig;
destinationAddress: string;
evmSigner: EvmSigner;
evmSigner?: EvmSigner;
polkadot: PolkadotService;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/getTransferData/getSourceData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface GetSourceDataParams {
transferConfig: TransferConfig;
destinationAddress: string;
destinationFee: AssetAmount;
evmSigner: EvmSigner;
evmSigner?: EvmSigner;
polkadot: PolkadotService;
sourceAddress: string;
}
Expand Down
17 changes: 8 additions & 9 deletions packages/sdk/src/getTransferData/getTransferData.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -15,26 +15,24 @@ import { getDestinationData } from './getDestinationData';
import { getSourceData } from './getSourceData';

export interface GetTransferDataParams extends Partial<Signers> {
configService: IConfigService;
destinationAddress: string;
sourceAddress: string;
transferConfig: TransferConfig;
}

export async function getTransferData({
configService,
destinationAddress,
evmSigner,
polkadotSigner,
sourceAddress,
transferConfig,
}: GetTransferDataParams): Promise<TransferData> {
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,
Expand Down Expand Up @@ -82,6 +80,7 @@ export async function getTransferData({
source,
async swap() {
return getTransferData({
configService,
destinationAddress: sourceAddress,
evmSigner,
polkadotSigner,
Expand Down
10 changes: 9 additions & 1 deletion packages/sdk/src/getTransferData/getTransferData.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { EvmSigner } from '../sdk.interfaces';
export interface GetFeeBalancesParams {
address: string;
config: AssetConfig;
evmSigner: EvmSigner;
evmSigner?: EvmSigner;
polkadot: PolkadotService;
asset?: Asset;
}
Expand All @@ -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();
Expand All @@ -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();
Expand Down
35 changes: 28 additions & 7 deletions packages/sdk/src/polkadot/PolkadotService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
SubstrateQueryConfig,
} from '@moonbeam-network/xcm-builder';
import {
assetsMap,
IConfigService,
darwiniaPangoro,
eq,
equilibriumAlphanet,
Expand All @@ -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<PolkadotService> {
return new PolkadotService(await getPolkadotApi(chain.ws), chain);
static async create(
chain: AnyParachain,
configService: IConfigService,
): Promise<PolkadotService> {
return new PolkadotService(
await getPolkadotApi(chain.ws),
chain,
configService,
);
}

static async createMulti(chains: AnyParachain[]): Promise<PolkadotService[]> {
return Promise.all(chains.map(PolkadotService.create));
static async createMulti(
chains: AnyParachain[],
configService: IConfigService,
): Promise<PolkadotService[]> {
return Promise.all(
chains.map((chain: AnyParachain) =>
PolkadotService.create(chain, configService),
),
);
}

get decimals(): number {
Expand All @@ -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}"`);
Expand Down
11 changes: 9 additions & 2 deletions packages/sdk/src/sdk.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -9,7 +13,8 @@ export interface SdkOptions extends Partial<Signers> {
}

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);
Expand All @@ -35,6 +40,7 @@ export function Sdk(options?: SdkOptions) {
): Promise<TransferData> {
return gtd({
...options,
configService,
destinationAddress,
evmSigner: signers?.evmSigner ?? signers?.ethersSigner,
sourceAddress,
Expand All @@ -61,6 +67,7 @@ export function Sdk(options?: SdkOptions) {
sourceKeyOrChain,
}: SdkTransferParams): Promise<TransferData> {
return gtd({
configService,
destinationAddress,
evmSigner: evmSigner ?? ethersSigner,
polkadotSigner,
Expand Down

0 comments on commit 02dafc0

Please sign in to comment.