@catalabs/catalyst-sdk
Version:
Catalyst AMM SDK
131 lines • 6.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EvmTokensModuleV5 = void 0;
const permit2_sdk_1 = require("@uniswap/permit2-sdk");
const ethers_v5_1 = require("ethers-v5");
const contracts_v5_1 = require("../contracts-v5");
const time_utils_1 = require("../utils/time.utils");
class EvmTokensModuleV5 {
sdk;
permitAllowanceProvider;
constructor(sdk) {
this.sdk = sdk;
this.permitAllowanceProvider = new permit2_sdk_1.AllowanceProvider(this.sdk.provider, permit2_sdk_1.PERMIT2_ADDRESS);
}
async checkBalance(token, owner) {
const result = await contracts_v5_1.Erc20__factory.connect(token, this.sdk.provider).balanceOf(owner);
return BigInt(result.toString());
}
async checkBalances(tokens, owner) {
const balances = {};
const lens = contracts_v5_1.CatalystLens__factory.connect(this.sdk.addresses.lens, this.sdk.provider);
const result = await lens.fetchTokenBalances(owner, tokens);
result.forEach((r) => (balances[r.token] = r.amount));
return balances;
}
async checkAllowance(token, owner, spender) {
const result = await contracts_v5_1.Erc20__factory.connect(token, this.sdk.provider).allowance(owner, spender);
return BigInt(result.toString());
}
async increaseAllowance(token, spender, amount, options) {
if (!this.sdk.signer) {
throw new Error('increaseAllowance requires a signer, try calling connectSigner first');
}
const contract = contracts_v5_1.Erc20__factory.connect(token, this.sdk.signer);
const tx = await contract.approve(spender, amount, {
gasPrice: options?.gasPrice,
maxFeePerGas: options?.maxFeePerGas,
maxPriorityFeePerGas: options?.maxPriorityFeePerGas,
});
return {
hash: tx.hash,
wait: async () => {
await tx.wait();
},
};
}
async checkPermitAmount(token, owner, spender) {
const { amount, expiration } = await this.permitAllowanceProvider.getAllowanceData(token, owner, spender);
if (expiration <= (Date.now() + 5 * time_utils_1.ONE_MIN_MS) / 1000) {
return 0n;
}
return amount.toBigInt();
}
async ensurePermit2Allowance(token, options) {
if (!this.sdk.signer || !this.sdk.address) {
throw new Error('ensurePermit2Allowance requires a signer, try calling connectSigner first');
}
return this.ensureAllowance(token, this.sdk.address, permit2_sdk_1.PERMIT2_ADDRESS, BigInt(ethers_v5_1.ethers.constants.MaxUint256.toString()), options);
}
async generatePermitData(token, spender, amount) {
if (!this.sdk.signer || !this.sdk.address) {
throw new Error('generatePermitData requires a signer, try calling connectSigner first');
}
const nonce = await this.permitAllowanceProvider.getNonce(token, this.sdk.address, spender);
const permitSingle = {
details: {
token,
amount: amount ?? permit2_sdk_1.MaxAllowanceTransferAmount,
expiration: (0, time_utils_1.toDeadline)(1000 * 60 * 60 * 24 * 30),
nonce,
},
spender,
sigDeadline: (0, time_utils_1.toDeadline)(1000 * 60 * 60 * 30),
};
const chainId = await this.sdk.signer.getChainId();
const { domain, types, values } = permit2_sdk_1.AllowanceTransfer.getPermitData(permitSingle, permit2_sdk_1.PERMIT2_ADDRESS, chainId);
const signature = await this.sdk.signer._signTypedData(domain, types, values);
return { signature, permit: permitSingle };
}
async generatePermitBatchData(assets, spender) {
if (!this.sdk.signer || !this.sdk.address) {
throw new Error('generatePermitBatchData requires a signer, try calling connectSigner first');
}
const permitDetailsArray = [];
const expiration30Days = (0, time_utils_1.toDeadline)(1000 * 60 * 60 * 24 * 30);
for (const { token, amount } of assets) {
const nonce = await this.permitAllowanceProvider.getNonce(token, this.sdk.address, spender);
permitDetailsArray.push({
token,
amount: amount ?? permit2_sdk_1.MaxAllowanceTransferAmount,
expiration: expiration30Days,
nonce,
});
}
const expiration30Mins = (0, time_utils_1.toDeadline)(1000 * 60 * 60 * 30);
const permitBatch = {
details: permitDetailsArray,
spender,
sigDeadline: expiration30Mins,
};
const chainId = await this.sdk.signer.getChainId();
const { domain, types, values } = permit2_sdk_1.AllowanceTransfer.getPermitData(permitBatch, permit2_sdk_1.PERMIT2_ADDRESS, chainId);
const signature = await this.sdk.signer._signTypedData(domain, types, values);
return { signature, permit: permitBatch };
}
async revoke(token, spender) {
if (!this.sdk.signer) {
throw new Error('revoke requires a signer, try calling connectSigner first');
}
const contract = contracts_v5_1.Erc20__factory.connect(token, this.sdk.signer);
const tx = await contract.approve(spender, 0);
return {
hash: tx.hash,
wait: async () => {
await tx.wait();
},
};
}
async getTotalSupply(address) {
const result = await contracts_v5_1.Erc20__factory.connect(address, this.sdk.provider).totalSupply();
return BigInt(result.toString());
}
async ensureAllowance(token, owner, spender, amount, options) {
const allowance = await this.checkAllowance(token, owner, spender);
if (allowance < amount) {
return this.increaseAllowance(token, spender, amount, options);
}
}
}
exports.EvmTokensModuleV5 = EvmTokensModuleV5;
//# sourceMappingURL=evm-tokens.module-v5.js.map