UNPKG

@ledgerhq/coin-filecoin

Version:
74 lines 3.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateIntent = validateIntent; const errors_1 = require("@ledgerhq/ledger-wallet-framework/errors"); const addresses_1 = require("../network/addresses"); function validateRecipient(intent, isTokenTransfer, errors) { if (!intent.recipient) { errors.recipient = new errors_1.RecipientRequired(); return; } const recipientValid = (0, addresses_1.validateAddress)(intent.recipient); if (!recipientValid.isValid || (isTokenTransfer && !(0, addresses_1.isRecipientValidForTokenTransfer)(intent.recipient))) { errors.recipient = new errors_1.InvalidAddress("", { currencyName: "Filecoin" }); } } function validateTokenAmount(intent, balances, estimatedFees, errors) { // Find the matching token balance by lowercase assetReference const assetRef = "assetReference" in intent.asset ? intent.asset.assetReference.toLowerCase() : ""; const tokenBalance = balances.find(b => "assetReference" in b.asset && b.asset.assetReference.toLowerCase() === assetRef); const tokenAvailable = tokenBalance?.value ?? 0n; const amount = intent.useAllAmount ? tokenAvailable : intent.amount; if (amount <= 0n) { errors.amount = intent.useAllAmount ? new errors_1.NotEnoughBalance() : new errors_1.AmountRequired(); } else if (amount > tokenAvailable) { errors.amount = new errors_1.NotEnoughBalance(); } // Token transfers still require native FIL to cover gas fees const nativeBal = balances.find(b => b.asset.type === "native"); const nativeAvailable = nativeBal?.value ?? 0n; if (estimatedFees > 0n && estimatedFees > nativeAvailable) { errors.amount = new errors_1.NotEnoughBalance(); } return amount; } function validateNativeAmount(intent, balances, estimatedFees, errors, warnings) { const nativeBalance = balances.find(b => b.asset.type === "native"); const available = nativeBalance?.value ?? 0n; let amount = intent.amount; if (intent.useAllAmount) { amount = available > estimatedFees ? available - estimatedFees : 0n; } if (amount <= 0n) { errors.amount = intent.useAllAmount ? new errors_1.NotEnoughBalance() : new errors_1.AmountRequired(); } else if (amount + estimatedFees > available) { errors.amount = new errors_1.NotEnoughBalance(); } // Fee-too-high warning: warn when fee exceeds balance (not a hard error) if (estimatedFees > 0n && estimatedFees > available) { warnings.feeTooHigh = new errors_1.FeeTooHigh(); } return amount; } async function validateIntent(intent, balances, customFees) { const errors = {}; const warnings = {}; const estimatedFees = customFees?.value ?? 0n; const isTokenTransfer = intent.asset.type !== "native"; validateRecipient(intent, isTokenTransfer, errors); const amount = isTokenTransfer ? validateTokenAmount(intent, balances, estimatedFees, errors) : validateNativeAmount(intent, balances, estimatedFees, errors, warnings); const totalSpent = isTokenTransfer ? amount : amount + estimatedFees; return { errors, warnings, estimatedFees, amount, totalSpent, }; } //# sourceMappingURL=validateIntent.js.map