@molecularlabs/nucleus-frontend
Version:
Nucleus BoringVault Frontend SDK
97 lines • 3.09 kB
JavaScript
import { getRateInQuoteWithAssetDecimals } from "../api/accountant";
import { bigIntToNumberAsString } from "../utils/bigint";
import { getVaultByKey } from "../vaults";
const getDepositExchangeRate = async ({
vaultKey,
sourceChainId,
depositTokenSymbol,
minimumFractionDigits,
maximumFractionDigits = 3
}) => {
const vault = await 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 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 bigIntToNumberAsString(rate.result, {
decimals: decimals.result,
minimumFractionDigits,
maximumFractionDigits
});
};
const getWithdrawExchangeRate = async ({
vaultKey,
sourceChainId,
destinationChainId,
wantTokenSymbol,
minimumFractionDigits,
maximumFractionDigits = 3
}) => {
const vault = await 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 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 bigIntToNumberAsString(rate.result, {
decimals: decimals.result,
minimumFractionDigits,
maximumFractionDigits
});
};
export {
getDepositExchangeRate,
getWithdrawExchangeRate
};
//# sourceMappingURL=exchange-rate.mjs.map