Skip to content

Commit

Permalink
address TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaurello committed Oct 7, 2024
1 parent 3f863e1 commit fa221e7
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 77 deletions.
116 changes: 54 additions & 62 deletions packages/builder/src/mrl/providers/wormhole/wormhole/wormhole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,77 +18,69 @@ export function wormhole() {
return {
tokenTransfer: (): MrlConfigBuilder => ({
provider: Provider.WORMHOLE,
build: (params): WormholeConfig => {
// TODO not needed to be extracted anymore
const args = getWormholeArgs(params);
build: ({
asset,
destination,
destinationAddress,
isAutomatic,
moonApi,
moonChain,
source,
sourceAddress,
}): WormholeConfig => {
const isNativeAsset = asset.isSame(source.nativeAsset);
const isDestinationMoonChain = destination.isEqual(moonChain);
const isDestinationEvmChain = EvmChain.is(destination);
const tokenAddress = isNativeAsset ? 'native' : asset.address;

const { address20: computedOriginAccount } =
getMultilocationDerivedAddresses({
address: sourceAddress,
paraId: moonChain.parachainId,
isParents: true,
});

if (!tokenAddress) {
throw new Error(`Asset ${asset.key} has no address`);
}

const wh = wormholeFactory(source);
const whSource = isDestinationEvmChain
? wh.getChain(moonChain.getWormholeName())
: wh.getChain(source.getWormholeName());
const whDestination = isDestinationEvmChain
? wh.getChain(destination.getWormholeName())
: wh.getChain(moonChain.getWormholeName());
const whAsset = Wormhole.tokenId(whSource.chain, tokenAddress);
const whSourceAddress = Wormhole.chainAddress(
whSource.chain,
isDestinationEvmChain ? computedOriginAccount : sourceAddress,
);
const whDestinationAddress = Wormhole.chainAddress(
whDestination.chain,
isDestinationMoonChain || isDestinationEvmChain
? destinationAddress
: GMP_CONTRACT_ADDRESS,
);

return new WormholeConfig({
args,
args: [
whAsset,
asset.amount,
whSourceAddress,
whDestinationAddress,
isAutomatic,
isDestinationMoonChain || isDestinationEvmChain
? undefined
: getPayload({ destination, destinationAddress, moonApi }),
],
func: 'tokenTransfer',
});
},
}),
};
}

export function getWormholeArgs({
asset,
destination,
destinationAddress,
isAutomatic,
moonApi,
moonChain,
source,
sourceAddress,
}: MrlBuilderParams): WormholeFunctionArgs {
const isNativeAsset = asset.isSame(source.nativeAsset);
const isDestinationMoonChain = destination.isEqual(moonChain);
const isDestinationEvmChain = EvmChain.is(destination);
const tokenAddress = isNativeAsset ? 'native' : asset.address;

const { address20: computedOriginAccount } = getMultilocationDerivedAddresses(
{
address: sourceAddress,
paraId: moonChain.parachainId,
isParents: true,
},
);

if (!tokenAddress) {
throw new Error(`Asset ${asset.key} has no address`);
}

const wh = wormholeFactory(source);
const whSource = isDestinationEvmChain
? wh.getChain(moonChain.getWormholeName())
: wh.getChain(source.getWormholeName());
const whDestination = isDestinationEvmChain
? wh.getChain(destination.getWormholeName())
: wh.getChain(moonChain.getWormholeName());
const whAsset = Wormhole.tokenId(whSource.chain, tokenAddress);
const whSourceAddress = Wormhole.chainAddress(
whSource.chain,
isDestinationEvmChain ? computedOriginAccount : sourceAddress,
);
const whDestinationAddress = Wormhole.chainAddress(
whDestination.chain,
isDestinationMoonChain || isDestinationEvmChain
? destinationAddress
: GMP_CONTRACT_ADDRESS,
);

return [
whAsset,
asset.amount,
whSourceAddress,
whDestinationAddress,
isAutomatic,
isDestinationMoonChain || isDestinationEvmChain
? undefined
: getPayload({ destination, destinationAddress, moonApi }),
];
}

/*
* Extrinsic to GMP precompile
* https://docs.moonbeam.network/builders/ethereum/precompiles/interoperability/gmp/
Expand Down
29 changes: 21 additions & 8 deletions packages/mrl/src/getTransferData/getSourceData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ export interface GetRelayFeeParams extends BuildTransferParams {
transfer: ContractConfig | ExtrinsicConfig | WormholeConfig;
}

export interface GetWormholeFeeParams {
asset: AssetAmount;
chain: AnyChain;
config: ContractConfig | ExtrinsicConfig | WormholeConfig;
}

export async function getFee({
balance,
feeBalance,
Expand Down Expand Up @@ -193,12 +199,7 @@ export async function getRelayFee({
transfer,
}: GetRelayFeeParams): Promise<AssetAmount | undefined> {
if (WormholeConfig.is(transfer)) {
const wh = WormholeService.create(chain as EvmChain | EvmParachain);
const fee = await wh.getFee(transfer);

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

if (route?.mrl?.transfer.provider === Provider.WORMHOLE) {
Expand All @@ -214,10 +215,22 @@ export async function getRelayFee({
.wormhole()
.wormhole()
.tokenTransfer()
.build(builderParams) as WormholeConfig; // TODO specify in the build function?
.build(builderParams);

return getWormholeFee({ asset, chain, config: wormholeConfig });
}

return;
}

async function getWormholeFee({
asset,
chain,
config,
}: GetWormholeFeeParams): Promise<AssetAmount | undefined> {
if (WormholeConfig.is(config)) {
const wh = WormholeService.create(chain as EvmChain | EvmParachain);
const fee = await wh.getFee(wormholeConfig);
const fee = await wh.getFee(config);

return AssetAmount.fromChainAsset(chain.getChainAsset(asset), {
amount: fee.relayFee?.amount || 0n,
Expand Down
12 changes: 6 additions & 6 deletions packages/mrl/src/getTransferData/getTransferData.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,17 @@ export interface BuildTransferParams {
}

export async function buildTransfer(params: BuildTransferParams) {
// TODO mjm unify with check below
if (!params.route.mrl) {
const { route } = params;
if (!route.mrl) {
throw new Error(
`MrlConfigBuilder is not defined for source chain ${params.route.source.chain.name} and asset ${params.route.source.asset.originSymbol}`,
`MrlConfigBuilder is not defined for source chain ${route.source.chain.name} and asset ${route.source.asset.originSymbol}`,
);
}
const builderParams = await getMrlBuilderParams(params);

return params.route.mrl.transfer.build({
return route.mrl.transfer.build({
...builderParams,
transact: EvmParachain.isAnyParachain(params.route.source.chain) // TODO deconstruct?
transact: EvmParachain.isAnyParachain(route.source.chain)
? await getTransact(builderParams)
: undefined,
});
Expand Down Expand Up @@ -140,7 +140,7 @@ export async function getMrlBuilderParams({
destinationAddress,
destinationApi,
fee: destinationFee,
isAutomatic: route.mrl.isAutomaticPossible, // TODO
isAutomatic: route.mrl.isAutomaticPossible,
moonApi,
moonAsset: moonChain.nativeAsset,
moonChain,
Expand Down
1 change: 0 additions & 1 deletion packages/mrl/src/mrl.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export interface TransferData {
): Promise<string[]>;
}

// TODO mjm is this needed?
export interface SourceTransferData extends SourceChainTransferData {
destinationFeeBalance: AssetAmount;
relayFee?: AssetAmount;
Expand Down

0 comments on commit fa221e7

Please sign in to comment.