@openocean.finance/widget-sdk
Version:
OpenOcean Any-to-Any Cross-Chain-Swap SDK
43 lines • 1.83 kB
JavaScript
import { ChainType } from '@openocean.finance/widget-types';
import { config } from '../../config.js';
import { getAllowance } from './getAllowance.js';
import { getNativePermit } from './permits/getNativePermit.js';
import { getPublicClient } from './publicClient.js';
/**
* 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
*/
export const checkPermitSupport = async ({ chain, tokenAddress, ownerAddress, amount, }) => {
const provider = config.getProvider(ChainType.EVM);
let client = await provider?.getWalletClient?.();
if (!client) {
client = await getPublicClient(chain.id);
}
let nativePermit;
// Try with wallet client first, fallback to public client
try {
nativePermit = await getNativePermit(client, chain.id, tokenAddress, chain.permit2Proxy, amount);
}
catch {
client = await getPublicClient(chain.id);
nativePermit = await getNativePermit(client, chain.id, tokenAddress, chain.permit2Proxy, amount);
}
let permit2Allowance;
// Check Permit2 allowance if available on chain
if (chain.permit2) {
permit2Allowance = await getAllowance(chain.id, tokenAddress, ownerAddress, chain.permit2);
}
return {
nativePermitSupported: !!nativePermit,
permit2AllowanceSufficient: !!permit2Allowance && permit2Allowance >= amount,
};
};
//# sourceMappingURL=checkPermitSupport.js.map