@qso-soft/shared
Version:
Shared library for QSO-soft
64 lines • 2.79 kB
JavaScript
import { encodePacked } from 'viem';
import { explorerLinks } from '../../constants';
import { calculateAmount, estimateLayerzeroFee, getClientByNetwork, getFeePerGasOptions, intToDecimal, shuffleArray, transactionWorker, } from '../../helpers';
import { MERKLY_ABI, MERKLY_CONTRACTS } from './data';
import { getDstChainId } from './helpers';
import { merklySrcNetworks } from './types';
export const execMakeMerklyRefuel = async (params) => transactionWorker({
...params,
startLogMessage: 'Execute make merkly refuel...',
transactionCallback: makeMerklyRefuel,
});
const makeMerklyRefuel = async ({ wallet, minAndMaxAmount, merklyNetworkPairs, merklyRandomNetworks, usePercentBalance, gweiRange, logger, }) => {
if (!merklyNetworkPairs) {
throw new Error('merklyNetworkPairs can not be empty');
}
const [srcNetwork, dstNetwork] = merklyNetworkPairs;
if (!minAndMaxAmount) {
throw new Error('minAndMaxAmount can not be empty');
}
if (!srcNetwork || !dstNetwork) {
throw new Error('merkly destination and source chainns can not be empty');
}
let selectedSrcNetwork;
if (srcNetwork === 'random') {
const randomNetworks = merklyRandomNetworks || merklySrcNetworks;
selectedSrcNetwork = shuffleArray([...randomNetworks])[0];
}
else {
selectedSrcNetwork = srcNetwork;
}
const mercklyContract = MERKLY_CONTRACTS[selectedSrcNetwork];
const destChainId = getDstChainId(dstNetwork);
const client = getClientByNetwork(selectedSrcNetwork, wallet.privKey, logger);
const nativeBalance = await client.getNativeBalance();
const amountToRefuelInt = calculateAmount({
balance: nativeBalance.int,
minAndMaxAmount,
usePercentBalance,
});
const amountToRefuelWei = intToDecimal({ amount: amountToRefuelInt });
logger.info(`Refuel [${amountToRefuelInt.toFixed(7)}] from ${client.walletClient.chain.name} to ${dstNetwork}`);
const adapterParams = encodePacked(['uint16', 'uint', 'uint', 'address'], [2, BigInt('250000'), amountToRefuelWei, wallet.walletAddress]);
const lzFee = await estimateLayerzeroFee({
adapterParams,
abi: MERKLY_ABI,
publicClient: client.publicClient,
contract: mercklyContract,
destNetwork: destChainId,
});
const txHash = await client.walletClient.writeContract({
address: mercklyContract,
abi: MERKLY_ABI,
functionName: 'bridgeGas',
args: [destChainId, wallet.walletAddress, adapterParams],
value: lzFee,
...getFeePerGasOptions(gweiRange),
});
return {
txHash,
explorerLink: explorerLinks[selectedSrcNetwork],
status: 'success',
};
};
//# sourceMappingURL=make-merkly-refuel.js.map