@paxoslabs/earn-sdk
Version:
Paxos Labs Earn SDK
323 lines (317 loc) • 10.7 kB
JavaScript
;
var chunkTFV7VK2Y_js = require('./chunk-TFV7VK2Y.js');
var chunk7FQRRMLO_js = require('./chunk-7FQRRMLO.js');
var chunkNEVIH2LY_js = require('./chunk-NEVIH2LY.js');
var chunkOTTVE2OX_js = require('./chunk-OTTVE2OX.js');
var chunkU72Q2IH5_js = require('./chunk-U72Q2IH5.js');
var viem = require('viem');
var isDepositSpendApproved = async ({
vaultKey,
sourceChainId,
depositTokenSymbol,
userAddress
}) => {
const {
data: vault,
error: vaultError,
success: vaultSuccess
} = await chunkOTTVE2OX_js.tryCatch(chunk7FQRRMLO_js.getVaultByKey(vaultKey));
if (!vaultSuccess) {
throw new Error(`Vault not found for ${vaultKey}: ${vaultError.message}`);
}
if (!vault.contracts) {
throw new Error(`Contracts not found: ${vaultKey}`);
}
const normalizedSourceChainId = chunkNEVIH2LY_js.toChainId(sourceChainId);
const depositAssetAddress = vault.deposit.sourceChains[normalizedSourceChainId].depositTokens[depositTokenSymbol].address;
if (!depositAssetAddress) {
throw new Error(`Deposit asset address not found: ${depositTokenSymbol}`);
}
const { deposit } = vault;
const { sourceChains } = deposit;
const sourceChain = sourceChains[normalizedSourceChainId];
if (!sourceChain) {
throw new Error(`Source chain not found: ${normalizedSourceChainId}`);
}
const [allowance, decimals] = await chunkTFV7VK2Y_js.getErc20AllowanceWithDecimals({
chainId: normalizedSourceChainId,
tokenAddress: depositAssetAddress,
userAddress,
spenderAddress: vault.contracts.boringVault
});
if (allowance.status === "failure" || decimals.status === "failure") {
return {
isApproved: false,
allowance: "0",
allowanceAsBigInt: "0",
decimals: "0",
error: allowance.error || decimals.error
};
}
return {
isApproved: allowance.result > 0n,
allowance: viem.formatUnits(allowance.result, decimals.result),
allowanceAsBigInt: allowance.result.toString(),
decimals: decimals.result,
error: null
};
};
var isWithdrawalSpendApproved = async ({
vaultKey,
destinationChainId,
userAddress
}) => {
const {
data: vault,
error: vaultError,
success: vaultSuccess
} = await chunkOTTVE2OX_js.tryCatch(chunk7FQRRMLO_js.getVaultByKey(vaultKey));
if (!vaultSuccess) {
throw new Error(`Vault not found for ${vaultKey}: ${vaultError.message}`);
}
if (!vault.contracts) {
throw new Error(`Contracts not found for ${vaultKey}`);
}
const normalizedDestinationChainId = chunkNEVIH2LY_js.toChainId(destinationChainId);
const boringVaultAddress = vault.contracts.boringVault;
const [allowance, decimals] = await chunkTFV7VK2Y_js.getErc20AllowanceWithDecimals({
chainId: normalizedDestinationChainId,
tokenAddress: boringVaultAddress,
userAddress,
spenderAddress: chunkU72Q2IH5_js.ATOMIC_QUEUE_CONTRACT_ADDRESS
});
if (allowance.status === "failure" || decimals.status === "failure") {
return {
isApproved: false,
allowance: "0",
allowanceAsBigInt: "0",
decimals: "0",
error: allowance.error || decimals.error
};
}
return {
isApproved: allowance.result > 0n,
allowance: viem.formatUnits(allowance.result, decimals.result),
allowanceAsBigInt: allowance.result.toString(),
decimals: decimals.result,
error: null
};
};
// src/vaults/bridge.ts
var prepareBridgeContractArg = ({
bridgeChainIdentifier,
userAddress,
nativeTokenForBridgeFee = chunkU72Q2IH5_js.NATIVE_TOKEN_FOR_BRIDGE_FEE
}) => {
return {
chainSelector: bridgeChainIdentifier,
destinationChainReceiver: userAddress,
bridgeFeeToken: nativeTokenForBridgeFee,
messageGas: BigInt(1e5),
data: "0x"
};
};
var prepareBridgeTransactionData = async ({
vaultKey,
bridgeAmount,
sourceChainId,
destinationChainId,
userAddress
}) => {
const nativeTokenForBridgeFee = chunkU72Q2IH5_js.NATIVE_TOKEN_FOR_BRIDGE_FEE;
const vault = await chunk7FQRRMLO_js.getVaultByKey(vaultKey);
if (!vault) {
throw new Error(`Invalid vault key: ${vaultKey}`);
}
if (!vault.contracts) {
throw new Error(`Contracts not configured for vault ${vaultKey}`);
}
if (!vault.contracts.boringVault) {
throw new Error(
`BoringVault contract not configured for vault ${vaultKey}`
);
}
if (!vault.contracts.accountant) {
throw new Error(`Accountant contract not configured for vault ${vaultKey}`);
}
if (!vault.contracts.teller) {
throw new Error(`Teller contract not configured for vault ${vaultKey}`);
}
const normalizedSourceChainId = chunkNEVIH2LY_js.toChainId(sourceChainId);
const sourceChain = vault.withdraw.sourceChains[normalizedSourceChainId];
if (!sourceChain) {
throw new Error(`Source chain not configured for vault ${vaultKey}`);
}
const normalizedDestinationChainId = chunkNEVIH2LY_js.toChainId(destinationChainId);
const destinationChain = sourceChain.destinationChains[normalizedDestinationChainId];
if (!destinationChain) {
throw new Error(`Destination chain not configured for vault ${vaultKey}`);
}
const bridgeChainIdentifier = destinationChain.bridge.chainIdentifier;
const bridgeContractArg = prepareBridgeContractArg({
bridgeChainIdentifier,
userAddress,
nativeTokenForBridgeFee
});
const previewFee = await chunkTFV7VK2Y_js.getPreviewFee({
shareAmount: bridgeAmount,
bridgeData: bridgeContractArg,
contractAddress: vault.contracts.teller,
chainId: normalizedSourceChainId
});
return {
abi: chunkTFV7VK2Y_js.TellerAbi,
address: vault.contracts.teller,
functionName: "bridge",
args: [bridgeAmount, bridgeContractArg],
chainId: normalizedSourceChainId,
value: previewFee
};
};
// src/display/bridge-fees.ts
var getBridgeFee = async ({
vaultKey,
bridgeAmount,
sourceChainId,
destinationChainId,
userAddress,
nativeTokenForBridgeFee = chunkU72Q2IH5_js.NATIVE_TOKEN_FOR_BRIDGE_FEE
}) => {
const vault = await chunk7FQRRMLO_js.getVaultByKey(vaultKey);
if (!vault) {
throw new Error(`Invalid vault key: ${vaultKey}`);
}
if (!vault.contracts) {
throw new Error(`Contracts not configured for vault ${vaultKey}`);
}
if (!vault.contracts.boringVault) {
throw new Error(
`BoringVault contract not configured for vault ${vaultKey}`
);
}
if (!vault.contracts.accountant) {
throw new Error(`Accountant contract not configured for vault ${vaultKey}`);
}
if (!vault.contracts.teller) {
throw new Error(`Teller contract not configured for vault ${vaultKey}`);
}
const sourceChain = vault.withdraw.sourceChains[sourceChainId];
if (!sourceChain) {
throw new Error(`Source chain not configured for vault ${vaultKey}`);
}
const destinationChain = sourceChain.destinationChains[destinationChainId];
if (!destinationChain) {
throw new Error(`Destination chain not configured for vault ${vaultKey}`);
}
const bridgeChainIdentifier = destinationChain.bridge.chainIdentifier;
const bridgeContractArg = prepareBridgeContractArg({
bridgeChainIdentifier,
userAddress,
nativeTokenForBridgeFee
});
const previewFee = await chunkTFV7VK2Y_js.getPreviewFee({
shareAmount: bridgeAmount,
bridgeData: bridgeContractArg,
contractAddress: vault.contracts.teller,
chainId: sourceChainId
});
return previewFee;
};
// src/display/exchange-rate.ts
var getDepositExchangeRate = async ({
vaultKey,
sourceChainId,
depositTokenSymbol,
minimumFractionDigits,
maximumFractionDigits = 3
}) => {
const vault = await chunk7FQRRMLO_js.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 chunkTFV7VK2Y_js.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 chunkOTTVE2OX_js.bigIntToNumberAsString(rate.result, {
decimals: decimals.result,
minimumFractionDigits,
maximumFractionDigits
});
};
var getWithdrawExchangeRate = async ({
vaultKey,
sourceChainId,
destinationChainId,
wantTokenSymbol,
minimumFractionDigits,
maximumFractionDigits = 3
}) => {
const vault = await chunk7FQRRMLO_js.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 chunkTFV7VK2Y_js.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 chunkOTTVE2OX_js.bigIntToNumberAsString(rate.result, {
decimals: decimals.result,
minimumFractionDigits,
maximumFractionDigits
});
};
exports.getBridgeFee = getBridgeFee;
exports.getDepositExchangeRate = getDepositExchangeRate;
exports.getWithdrawExchangeRate = getWithdrawExchangeRate;
exports.isDepositSpendApproved = isDepositSpendApproved;
exports.isWithdrawalSpendApproved = isWithdrawalSpendApproved;
exports.prepareBridgeTransactionData = prepareBridgeTransactionData;
//# sourceMappingURL=chunk-QON5Z6CB.js.map
//# sourceMappingURL=chunk-QON5Z6CB.js.map