@getclave/lifi-sdk
Version:
LI.FI Any-to-Any Cross-Chain-Swap SDK
45 lines (44 loc) • 1.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkPermitSupport = void 0;
const types_1 = require("@lifi/types");
const config_1 = require("../../config");
const getActionWithFallback_1 = require("./getActionWithFallback");
const getAllowance_1 = require("./getAllowance");
const getNativePermit_1 = require("./permits/getNativePermit");
const publicClient_1 = require("./publicClient");
/**
* Checks what permit types are supported for a token on a specific chain.
* Checks in order:
* 1. Native permit (EIP-2612) support
* 2. Permit2 availability and allowance
*
* @param chain - The chain to check permit support on
* @param tokenAddress - The token address to check
* @param ownerAddress - The address that would sign the permit
* @param amount - The amount to check allowance against for Permit2
* @returns Object indicating which permit types are supported
*/
const checkPermitSupport = async ({ chain, tokenAddress, ownerAddress, amount, }) => {
const provider = config_1.config.getProvider(types_1.ChainType.EVM);
let client = await provider?.getWalletClient?.();
if (!client) {
client = await (0, publicClient_1.getPublicClient)(chain.id);
}
const nativePermit = await (0, getActionWithFallback_1.getActionWithFallback)(client, getNativePermit_1.getNativePermit, 'getNativePermit', {
chainId: chain.id,
tokenAddress,
spenderAddress: chain.permit2Proxy,
amount,
});
let permit2Allowance;
// Check Permit2 allowance if available on chain
if (chain.permit2) {
permit2Allowance = await (0, getAllowance_1.getAllowance)(client, tokenAddress, ownerAddress, chain.permit2);
}
return {
nativePermitSupported: !!nativePermit,
permit2AllowanceSufficient: !!permit2Allowance && permit2Allowance >= amount,
};
};
exports.checkPermitSupport = checkPermitSupport;