UNPKG

@arcana/ca-sdk

Version:

Arcana Network's chain abstraction SDK for unified balance in Web3 apps

61 lines (60 loc) 2.39 kB
import { Universe } from "@arcana/ca-common"; import Decimal from "decimal.js"; import { FUEL_BASE_ASSET_ID, isNativeAddress, ZERO_ADDRESS, } from "../constants"; import { getLogger } from "../logger"; import { convertTo32BytesHex, mulDecimals } from "./common.utils"; const logger = getLogger(); const getSourcesAndDestinationsForRFF = (intent, chainList, destinationUniverse) => { const sources = []; const universes = new Set(); const sourceCounts = { [Universe.ETHEREUM]: 0, [Universe.FUEL]: 0, [Universe.SOLANA]: 0, [Universe.UNRECOGNIZED]: 0, }; for (const source of intent.sources) { if (source.chainID == intent.destination.chainID) { continue; } const token = chainList.getTokenByAddress(source.chainID, source.tokenContract); if (!token) { logger.error("Token not found", { source }); throw new Error("token not found"); } sourceCounts[source.universe] += 1; universes.add(source.universe); sources.push({ chainID: BigInt(source.chainID), tokenAddress: convertTo32BytesHex(source.tokenContract), universe: source.universe, value: mulDecimals(source.amount, token.decimals), }); } universes.add(intent.destination.universe); const destinations = [ { tokenAddress: convertTo32BytesHex(intent.destination.tokenContract), universe: intent.destination.universe, value: BigInt(intent.destination.amount .mul(Decimal.pow(10, intent.destination.decimals)) .toFixed(0, Decimal.ROUND_FLOOR)), }, ]; if (intent.destination.gas != 0n) { if (isNativeAddress(intent.destination.universe, intent.destination.tokenContract)) { destinations[0].value = destinations[0].value + intent.destination.gas; } else { destinations.push({ tokenAddress: convertTo32BytesHex(destinationUniverse === Universe.FUEL ? FUEL_BASE_ASSET_ID : ZERO_ADDRESS), universe: intent.destination.universe, value: intent.destination.gas, }); } } return { destinations, sourceCounts, sources, universes }; }; export { getSourcesAndDestinationsForRFF };