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

Return balance and address in MoonChainData #408

Merged
merged 6 commits into from
Dec 18, 2024
Merged
Changes from 1 commit
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
64 changes: 42 additions & 22 deletions packages/mrl/src/getTransferData/getMoonChainData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,52 +23,72 @@ export async function getMoonChainData({
}

const moonChain = getMoonChain(route.source.chain);
const isDestinationMoonChain = moonChain.isEqual(route.destination.chain);
let address = isDestinationMoonChain ? destinationAddress : sourceAddress;
const moonChainAddress = getMoonChainAddress({
route,
sourceAddress,
destinationAddress,
});

const fee = await getDestinationFee({
address,
address: moonChainAddress,
asset: route.source.asset,
destination: moonChain,
fee: route.mrl.moonChain.fee.amount,
feeAsset: route.mrl.moonChain.fee.asset,
});

if (
Parachain.is(route.source.chain) &&
!route.source.chain.isEqual(moonChain)
) {
const addressToUse = EvmParachain.is(route.source.chain)
? evmToAddress(sourceAddress)
: sourceAddress;
const { address20 } = getMultilocationDerivedAddresses({
address: addressToUse,
paraId: route.source.chain.parachainId,
isParents: true,
});

address = address20;
}

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

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

return {
address,
address: moonChainAddress,
balance,
feeBalance,
chain: moonChain,
fee,
};
}

function getMoonChainAddress({
mmaurello marked this conversation as resolved.
Show resolved Hide resolved
route: { source, destination },
sourceAddress,
destinationAddress,
}: GetMoonChainDataParams): string {
const moonChain = getMoonChain(source.chain);
const isDestinationMoonChain = moonChain.isEqual(destination.chain);
const isSourceMoonChain = moonChain.isEqual(source.chain);

let moonChainAddress = isDestinationMoonChain
? destinationAddress
: sourceAddress;

// for Parachain to EVM transactions, we use the computed origin account in the moonchain
if (Parachain.is(source.chain) && !isSourceMoonChain) {
const isSourceEvmSigner =
EvmParachain.is(source.chain) && source.chain.isEvmSigner;

const { address20: computedOriginAccount } =
getMultilocationDerivedAddresses({
address: isSourceEvmSigner
? evmToAddress(sourceAddress)
: sourceAddress,
paraId: source.chain.parachainId,
isParents: true,
});

moonChainAddress = computedOriginAccount;
}

return moonChainAddress;
}
Loading