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

Adjustments after implementing mrl package in dapp #401

Merged
merged 8 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions examples/sdk-simple/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,24 @@ console.log(`Substrate address: ${pair.address}`);

export function logBalances(data: TransferData): void {
console.log(
`Balance on ${data.source.chain.name} ${data.source.balance.toDecimal()} ${
data.source.balance.symbol
}`,
`Balance on ${data.source.chain.name} ${data.source.balance.toDecimal()} ${data.source.balance.getSymbol()}`,
);
console.log(
`Balance on ${
data.destination.chain.name
} ${data.destination.balance.toDecimal()} ${
data.destination.balance.symbol
}`,
} ${data.destination.balance.toDecimal()} ${data.destination.balance.getSymbol()}`,
);
}

export function logTxDetails(data: TransferData): void {
console.log(
`\nYou can send min: ${data.min.toDecimal()} ${
data.min.symbol
} and max: ${data.max.toDecimal()} ${data.max.symbol} from ${
`\nYou can send min: ${data.min.toDecimal()} ${data.min.getSymbol()} and max: ${data.max.toDecimal()} ${data.max.getSymbol()} from ${
data.source.chain.name
} to ${
data.destination.chain.name
}. You will pay ${data.source.fee.toDecimal()} ${
data.source.fee.symbol
} fee on ${
}. You will pay ${data.source.fee.toDecimal()} ${data.source.fee.getSymbol()} fee on ${
data.source.chain.name
} and ${data.destination.fee.toDecimal()} ${
data.destination.fee.symbol
} fee on ${data.destination.chain.name}.`,
} and ${data.destination.fee.toDecimal()} ${data.destination.fee.getSymbol()} fee on ${data.destination.chain.name}.`,
);
}

Expand Down
27 changes: 15 additions & 12 deletions packages/mrl/src/getTransferData/getMoonChainData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ export async function getMoonChainData({
}

const moonChain = getMoonChain(route.source.chain);
const asset = moonChain.getChainAsset(route.mrl.moonChain.asset);
const isDestinationMoonChain = route.destination.chain.isEqual(moonChain);
// TODO is this used for something? do we need the balance?
// const asset = moonChain.getChainAsset(route.mrl.moonChain.asset);
// const isDestinationMoonChain = route.destination.chain.isEqual(moonChain);

if (isDestinationMoonChain) {
return {
balance: destinationData.balance,
chain: destinationData.chain,
fee: destinationData.fee,
};
}
// TODO technically not correct
// if (isDestinationMoonChain) {
// return {
// balance: destinationData.balance,
// chain: destinationData.chain,
// fee: destinationData.fee,
// };
// }

const fee = await getDestinationFee({
address: sourceAddress, // TODO not correct
Expand All @@ -56,15 +58,16 @@ export async function getMoonChainData({
address = address20;
}

const balance = await getBalance({
const feeBalance = await getBalance({
address,
asset,
asset: moonChain.getChainAsset(route.mrl.moonChain.fee.asset),
builder: route.mrl.moonChain.fee.balance,
chain: moonChain,
});

return {
balance,
// TODO technically feeBalance
balance: feeBalance,
chain: moonChain,
fee,
};
Expand Down
5 changes: 4 additions & 1 deletion packages/mrl/src/getTransferData/getSourceData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
type EvmChain,
type EvmParachain,
} from '@moonbeam-network/xcm-types';
import { toBigInt } from '@moonbeam-network/xcm-utils';
import type { SourceTransferData } from '../mrl.interfaces';
import { WormholeService } from '../services/wormhole';
import {
Expand Down Expand Up @@ -239,11 +240,13 @@ async function getWormholeFee({
config,
}: GetWormholeFeeParams): Promise<AssetAmount | undefined> {
if (WormholeConfig.is(config)) {
const safetyAmount = toBigInt(0.000001, asset.decimals);

const wh = WormholeService.create(chain as EvmChain | EvmParachain);
const fee = await wh.getFee(config);

return AssetAmount.fromChainAsset(chain.getChainAsset(asset), {
amount: fee.relayFee?.amount || 0n,
amount: fee.relayFee ? fee.relayFee.amount + safetyAmount : 0n,
});
}

Expand Down
5 changes: 5 additions & 0 deletions packages/mrl/src/getTransferData/getTransferData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ interface GetTransferDataParams {
route: MrlAssetRoute;
sourceAddress: string;
destinationAddress: string;
isAutomatic: boolean;
}

export async function getTransferData({
route,
sourceAddress,
destinationAddress,
isAutomatic,
}: GetTransferDataParams): Promise<TransferData> {
if (!route.mrl) {
throw new Error(
Expand Down Expand Up @@ -98,13 +100,15 @@ export async function getTransferData({
destinationData,
moonChainData,
sourceData,
isAutomatic,
}),
moonChain: moonChainData,
source: sourceData,
async transfer(
amount,
isAutomatic,
{ evmSigner, polkadotSigner }: Partial<Signers>,
statusCallback,
): Promise<string[]> {
const source = route.source.chain;

Expand Down Expand Up @@ -152,6 +156,7 @@ export async function getTransferData({
sourceAddress,
transfer,
polkadotSigner,
statusCallback,
);

return [hash];
Expand Down
9 changes: 6 additions & 3 deletions packages/mrl/src/getTransferData/getTransferData.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ interface DataParams {
destinationData: DestinationChainTransferData;
moonChainData: MoonChainTransferData;
sourceData: SourceTransferData;
isAutomatic?: boolean;
}

export function getMoonChainFeeValueOnSource({
Expand Down Expand Up @@ -74,6 +75,7 @@ export function getMrlMin({
destinationData,
moonChainData,
sourceData,
isAutomatic,
}: DataParams): AssetAmount {
const minInDestination = getMin(destinationData);
const min = AssetAmount.fromChainAsset(
Expand All @@ -87,9 +89,10 @@ export function getMrlMin({
moonChainData,
sourceData,
});
const relayerFee = sourceData.relayerFee?.amount
? sourceData.relayerFee.toBig()
: Big(0);
const relayerFee =
sourceData.relayerFee?.amount && isAutomatic
? sourceData.relayerFee.toBig()
: Big(0);

return min.copyWith({
amount: BigInt(min.toBig().add(moonChainFee).add(relayerFee).toFixed()),
Expand Down
3 changes: 2 additions & 1 deletion packages/mrl/src/mrl.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
} from '@moonbeam-network/xcm-sdk';
import type { AnyChain, AssetAmount } from '@moonbeam-network/xcm-types';
import type { Signer } from '@polkadot/api/types';
import type { IKeyringPair } from '@polkadot/types/types';
import type { IKeyringPair, ISubmittableResult } from '@polkadot/types/types';
import type { TokenTransfer } from '@wormhole-foundation/sdk-connect';
import type { WalletClient } from 'viem';

Expand All @@ -24,6 +24,7 @@ export interface TransferData {
amount: bigint | number | string,
isAutomatic: boolean,
signers: Signers,
statusCallback?: (params: ISubmittableResult) => void,
): Promise<string[]>;
}

Expand Down
30 changes: 17 additions & 13 deletions packages/mrl/src/mrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,24 @@ export function Mrl(options?: MrlOptions) {
source,
destination,
});

return {
setAddresses({
sourceAddress,
destinationAddress,
}: {
sourceAddress: string;
destinationAddress: string;
}) {
return getTransferData({
route,
sourceAddress,
destinationAddress,
});
setIsAutomatic(isAutomatic: boolean) {
return {
setAddresses({
sourceAddress,
destinationAddress,
}: {
sourceAddress: string;
destinationAddress: string;
}) {
return getTransferData({
route,
sourceAddress,
destinationAddress,
isAutomatic,
});
},
};
},
};
},
Expand Down
37 changes: 28 additions & 9 deletions packages/sdk/src/services/polkadot/PolkadotService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,37 @@ export class PolkadotService {
account: string,
config: ExtrinsicConfig,
signer: PolkadotSigner | IKeyringPair,
statusCallback?: (params: ISubmittableResult) => void,
): Promise<string> {
const extrinsic = this.getExtrinsic(config);
const hash = await extrinsic.signAndSend(
this.#isSigner(signer) ? account : signer,
{
nonce: -1,
signer: this.#isSigner(signer) ? signer : undefined,
withSignedTransaction: true,
},
);

return hash.toString();
const hash = await new Promise<string>((resolve, reject) => {
Rihyx marked this conversation as resolved.
Show resolved Hide resolved
extrinsic
.signAndSend(
this.#isSigner(signer) ? account : signer,
{
nonce: -1,
signer: this.#isSigner(signer) ? signer : undefined,
withSignedTransaction: true,
},
(result) => {
if (result.isError || result.dispatchError) {
reject(
new Error(
result.dispatchError?.toString() || 'Transaction failed',
),
);
}
if (result.txHash) {
resolve(result.txHash.toString());
}
statusCallback?.(result);
},
)
.catch(reject);
});

return hash;
}

#isSigner(signer: PolkadotSigner | IKeyringPair): signer is PolkadotSigner {
Expand Down