@molecularlabs/nucleus-frontend
Version:
Nucleus BoringVault Frontend SDK
117 lines • 4.15 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var exchange_rate_exports = {};
__export(exchange_rate_exports, {
getDepositExchangeRate: () => getDepositExchangeRate,
getWithdrawExchangeRate: () => getWithdrawExchangeRate
});
module.exports = __toCommonJS(exchange_rate_exports);
var import_accountant = require("../api/accountant");
var import_bigint = require("../utils/bigint");
var import_vaults = require("../vaults");
const getDepositExchangeRate = async ({
vaultKey,
sourceChainId,
depositTokenSymbol,
minimumFractionDigits,
maximumFractionDigits = 3
}) => {
const vault = await (0, import_vaults.getVaultByKey)(vaultKey);
const sourceChain = vault.deposit.sourceChains[sourceChainId];
if (!sourceChain) {
throw new Error(
`Source chain ${sourceChainId} not found for vault ${vaultKey}`
);
}
const depositToken = sourceChain.depositTokens?.[depositTokenSymbol];
if (!depositToken) {
throw new Error(
`Deposit token ${depositTokenSymbol} not found for chain ${sourceChainId}`
);
}
const tokenAddress = depositToken.address;
if (!tokenAddress) {
throw new Error(`Token address not found for ${depositTokenSymbol}`);
}
const [decimals, rate] = await (0, import_accountant.getRateInQuoteWithAssetDecimals)({
assetAddress: tokenAddress,
accountantAddress: vault.contracts.accountant,
chainId: sourceChainId
});
if (rate.status === "failure") {
throw new Error(`Failed to get rate: ${rate.error.message}`);
}
if (decimals.status === "failure") {
throw new Error(`Failed to get decimals: ${decimals.error.message}`);
}
return (0, import_bigint.bigIntToNumberAsString)(rate.result, {
decimals: decimals.result,
minimumFractionDigits,
maximumFractionDigits
});
};
const getWithdrawExchangeRate = async ({
vaultKey,
sourceChainId,
destinationChainId,
wantTokenSymbol,
minimumFractionDigits,
maximumFractionDigits = 3
}) => {
const vault = await (0, import_vaults.getVaultByKey)(vaultKey);
const sourceChain = vault.withdraw.sourceChains[sourceChainId];
if (!sourceChain) {
throw new Error(
`Source chain ${sourceChainId} not found for vault ${vaultKey}`
);
}
const destinationChain = sourceChain.destinationChains[destinationChainId];
if (!destinationChain) {
throw new Error(
`Destination chain ${destinationChainId} not found for source chain ${sourceChainId}`
);
}
const wantToken = destinationChain.wantTokens?.[wantTokenSymbol];
if (!wantToken) {
throw new Error(
`Want token ${wantTokenSymbol} not found for chain ${destinationChainId}`
);
}
const tokenAddress = wantToken.address;
if (!tokenAddress) {
throw new Error(`Token address not found for ${wantTokenSymbol}`);
}
const [decimals, rate] = await (0, import_accountant.getRateInQuoteWithAssetDecimals)({
assetAddress: tokenAddress,
accountantAddress: vault.contracts.accountant,
chainId: sourceChainId
});
if (rate.status === "failure") {
throw new Error(`Failed to get rate: ${rate.error.message}`);
}
if (decimals.status === "failure") {
throw new Error(`Failed to get decimals: ${decimals.error.message}`);
}
return (0, import_bigint.bigIntToNumberAsString)(rate.result, {
decimals: decimals.result,
minimumFractionDigits,
maximumFractionDigits
});
};
//# sourceMappingURL=exchange-rate.js.map