Skip to content

Commit

Permalink
Merge pull request #43 from curvefi/one-tx-reapprove
Browse files Browse the repository at this point in the history
One tx reapprove
  • Loading branch information
Macket authored Nov 5, 2024
2 parents 695c5c1 + 79675ae commit a13dfa6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@curvefi/lending-api",
"version": "2.3.5",
"version": "2.3.6",
"description": "JavaScript library for Curve Lending",
"main": "lib/index.js",
"author": "Macket",
Expand Down
2 changes: 1 addition & 1 deletion src/constants/aliases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const ALIASES_ETHEREUM = lowerCaseValues({
"leverage_zap": "0x3294514B78Df4Bb90132567fcf8E5e99f390B687",
"leverage_markets_start_id": "9",
"crvUSD": "0xf939E0A03FB07F59A73314E73794Be0E57ac1b4E",
"st_crvUSD": "0xcea18a8752bb7e7817f9ae7565328fe415c0f2ca", // TODO It's Yearn vault for tests, change to real
"st_crvUSD": "0x0655977FEb2f289A4aB78af67BAB0d17aAb84367",
});

export const ALIASES_POLYGON = lowerCaseValues({
Expand Down
32 changes: 8 additions & 24 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,20 +234,16 @@ export const hasAllowance = async (coins: string[], amounts: (number | string)[]
return _allowance.map((a, i) => a >= _amounts[i]).reduce((a, b) => a && b);
}

export const _ensureAllowance = async (coins: string[], amounts: bigint[], spender: string, isMax = true): Promise<string[]> => {
export const _ensureAllowance = async (coins: string[], _amounts: bigint[], spender: string, isMax = true): Promise<string[]> => {
const address = lending.signerAddress;
const allowance: bigint[] = await _getAllowance(coins, address, spender);
const _allowance: bigint[] = await _getAllowance(coins, address, spender);

const txHashes: string[] = []
for (let i = 0; i < allowance.length; i++) {
if (allowance[i] < amounts[i]) {
for (let i = 0; i < _allowance.length; i++) {
if (_allowance[i] < _amounts[i]) {
const contract = lending.contracts[coins[i]].contract;
const _approveAmount = isMax ? MAX_ALLOWANCE : amounts[i];
const _approveAmount = isMax ? MAX_ALLOWANCE : _amounts[i];
await lending.updateFeeData();
if (allowance[i] > lending.parseUnits("0")) {
const gasLimit = _mulBy1_3(DIGas(await contract.approve.estimateGas(spender, lending.parseUnits("0"), lending.constantOptions)));
txHashes.push((await contract.approve(spender, lending.parseUnits("0"), { ...lending.options, gasLimit })).hash);
}
const gasLimit = _mulBy1_3(DIGas(await contract.approve.estimateGas(spender, _approveAmount, lending.constantOptions)));
txHashes.push((await contract.approve(spender, _approveAmount, { ...lending.options, gasLimit })).hash);
}
Expand All @@ -261,27 +257,15 @@ export const ensureAllowanceEstimateGas = async (coins: string[], amounts: (numb
const coinAddresses = _getCoinAddresses(coins);
const decimals = _getCoinDecimals(coinAddresses);
const _amounts = amounts.map((a, i) => parseUnits(a, decimals[i]));
const address = lending.signerAddress;
const _allowance: bigint[] = await _getAllowance(coinAddresses, address, spender);
const _allowance: bigint[] = await _getAllowance(coinAddresses, lending.signerAddress, spender);

let gas = [0,0];
for (let i = 0; i < _allowance.length; i++) {
if (_allowance[i] < _amounts[i]) {
const contract = lending.contracts[coinAddresses[i]].contract;
const _approveAmount = isMax ? MAX_ALLOWANCE : _amounts[i];
if (_allowance[i] > lending.parseUnits("0")) {
let currentGas = smartNumber(await contract.approve.estimateGas(spender, lending.parseUnits("0"), lending.constantOptions));
// For some coins (crv for example ) we can't estimate the second tx gas (approve: 0 --> amount), so we assume it will cost the same amount of gas
if (typeof currentGas === "number") {
currentGas = currentGas * 2;
} else {
currentGas = currentGas.map((g) => g * 2)
}
gas = gasSum(gas, currentGas);
} else {
const currentGas = smartNumber(await contract.approve.estimateGas(spender, _approveAmount, lending.constantOptions));
gas = gasSum(gas, currentGas);
}
const currentGas = smartNumber(await contract.approve.estimateGas(spender, _approveAmount, lending.constantOptions));
gas = gasSum(gas, currentGas);
}
}

Expand Down

0 comments on commit a13dfa6

Please sign in to comment.