UNPKG

@ledgerhq/coin-filecoin

Version:
71 lines 3.2 kB
import { AmountRequired, FeeTooHigh, InvalidAddress, NotEnoughBalance, RecipientRequired, } from "@ledgerhq/ledger-wallet-framework/errors"; import { isRecipientValidForTokenTransfer, validateAddress } from "../network/addresses"; function validateRecipient(intent, isTokenTransfer, errors) { if (!intent.recipient) { errors.recipient = new RecipientRequired(); return; } const recipientValid = validateAddress(intent.recipient); if (!recipientValid.isValid || (isTokenTransfer && !isRecipientValidForTokenTransfer(intent.recipient))) { errors.recipient = new 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 NotEnoughBalance() : new AmountRequired(); } else if (amount > tokenAvailable) { errors.amount = new 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 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 NotEnoughBalance() : new AmountRequired(); } else if (amount + estimatedFees > available) { errors.amount = new NotEnoughBalance(); } // Fee-too-high warning: warn when fee exceeds balance (not a hard error) if (estimatedFees > 0n && estimatedFees > available) { warnings.feeTooHigh = new FeeTooHigh(); } return amount; } export 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