@biconomy/sdk
Version:
SDK for Biconomy integration with support for account abstraction, smart accounts, ERC-4337.
100 lines • 4.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildBridgeInstructions = void 0;
const toAcrossPlugin_1 = require("../utils/toAcrossPlugin.js");
const queryBridge_1 = require("./queryBridge.js");
const buildBridgeInstructions = async (params) => {
const { account, amount: targetAmount, toChain, unifiedBalance, bridgingPlugins = [(0, toAcrossPlugin_1.toAcrossPlugin)()], feeData } = params;
const tokenMapping = {
on: (chainId) => unifiedBalance.mcToken.deployments.get(chainId) || "0x",
deployments: Array.from(unifiedBalance.mcToken.deployments.entries(), ([chainId, address]) => ({
chainId,
address
}))
};
const destinationBalance = unifiedBalance.breakdown.find((b) => b.chainId === toChain.id)?.balance ||
0n;
if (destinationBalance >= targetAmount) {
return {
instructions: [],
meta: {
bridgingInstructions: [],
totalAvailableOnDestination: destinationBalance
}
};
}
const amountToBridge = targetAmount - destinationBalance;
const sourceBalances = unifiedBalance.breakdown
.filter((balance) => balance.chainId !== toChain.id)
.map((balance) => {
const isFeeChain = feeData && feeData.txFeeChainId === balance.chainId;
const availableBalance = isFeeChain && "txFeeAmount" in feeData
? balance.balance > feeData.txFeeAmount
? balance.balance - feeData.txFeeAmount
: 0n
: balance.balance;
return {
chainId: balance.chainId,
balance: availableBalance
};
})
.filter((balance) => balance.balance > 0n);
const chains = Object.fromEntries(account.deployments.map((deployment) => {
const chain = deployment.client.chain;
if (!chain) {
throw new Error(`Client not configured with chain for deployment at ${deployment.address}`);
}
return [chain.id, chain];
}));
const bridgeQueries = sourceBalances.flatMap((source) => {
const fromChain = chains[source.chainId];
if (!fromChain)
return [];
return bridgingPlugins.map((plugin) => (0, queryBridge_1.queryBridge)({
fromChain,
toChain,
plugin,
amount: source.balance,
account,
tokenMapping
}));
});
const bridgeResults = (await Promise.all(bridgeQueries))
.filter((result) => result !== null)
.sort((a, b) => Number((b.receivedAtDestination * 10000n) / b.amount) -
Number((a.receivedAtDestination * 10000n) / a.amount));
const bridgingInstructions = [];
const instructions = [];
let totalBridged = 0n;
let remainingNeeded = amountToBridge;
for (const result of bridgeResults) {
if (remainingNeeded <= 0n)
break;
const amountToTake = result.amount >= remainingNeeded ? remainingNeeded : result.amount;
const receivedFromRoute = (result.receivedAtDestination * amountToTake) / result.amount;
instructions.push(result.userOp);
bridgingInstructions.push({
userOp: result.userOp,
receivedAtDestination: receivedFromRoute,
bridgingDurationExpectedMs: result.bridgingDurationExpectedMs
});
totalBridged += receivedFromRoute;
remainingNeeded -= amountToTake;
}
if (remainingNeeded > 0n) {
throw new Error(`Insufficient balance for bridging:
Required: ${targetAmount.toString()}
Available to bridge: ${totalBridged.toString()}
Shortfall: ${remainingNeeded.toString()}`);
}
return {
instructions,
meta: {
bridgingInstructions,
totalAvailableOnDestination: destinationBalance + totalBridged
}
};
};
exports.buildBridgeInstructions = buildBridgeInstructions;
exports.default = exports.buildBridgeInstructions;
//# sourceMappingURL=buildBridgeInstructions.js.map