Skip to content

Commit

Permalink
Merge pull request #415 from curvefi/fix/deposit-and-stake-ng-only
Browse files Browse the repository at this point in the history
Fix: deposit and stake ng only
  • Loading branch information
Macket authored Nov 20, 2024
2 parents 984a824 + 7d3f109 commit 798a740
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 33 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/api",
"version": "2.65.8",
"version": "2.65.9",
"description": "JavaScript library for curve.fi",
"main": "lib/index.js",
"author": "Macket",
Expand Down
50 changes: 50 additions & 0 deletions src/constants/abis/deposit_and_stake_ng_only.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[
{
"stateMutability": "payable",
"type": "function",
"name": "deposit_and_stake",
"inputs": [
{
"name": "deposit",
"type": "address"
},
{
"name": "lp_token",
"type": "address"
},
{
"name": "gauge",
"type": "address"
},
{
"name": "n_coins",
"type": "uint256"
},
{
"name": "coins",
"type": "address[]"
},
{
"name": "amounts",
"type": "uint256[]"
},
{
"name": "min_mint_amount",
"type": "uint256"
},
{
"name": "use_dynarray",
"type": "bool"
},
{
"name": "pool",
"type": "address"
}
],
"outputs": []
},
{
"stateMutability": "payable",
"type": "fallback"
}
]
12 changes: 10 additions & 2 deletions src/curve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { getFactoryPoolData } from "./factory/factory.js";
import { getFactoryPoolsDataFromApi } from "./factory/factory-api.js";
import { getCryptoFactoryPoolData } from "./factory/factory-crypto.js";
import { getTricryptoFactoryPoolData } from "./factory/factory-tricrypto.js";
import {IPoolData, IDict, ICurve, INetworkName, IChainId, IFactoryPoolType, Abi, INetworkConstants} from "./interfaces";
import {IPoolData, IDict, ICurve, IChainId, IFactoryPoolType, Abi, INetworkConstants} from "./interfaces";
import ERC20Abi from './constants/abis/ERC20.json' assert { type: 'json' };
import cERC20Abi from './constants/abis/cERC20.json' assert { type: 'json' };
import yERC20Abi from './constants/abis/yERC20.json' assert { type: 'json' };
Expand All @@ -30,6 +30,7 @@ import feeDistributorABI from './constants/abis/fee_distributor.json' assert { t
import feeDistributorCrvUSDABI from './constants/abis/fee_distributor_crvusd.json' assert { type: 'json' };
import gaugeControllerABI from './constants/abis/gaugecontroller.json' assert { type: 'json' };
import depositAndStakeABI from './constants/abis/deposit_and_stake.json' assert { type: 'json' };
import depositAndStakeNgOnlyABI from './constants/abis/deposit_and_stake_ng_only.json' assert { type: 'json' };
import cryptoCalcZapABI from './constants/abis/crypto_calc.json' assert { type: 'json'};
import StableCalcZapABI from './constants/abis/stable_calc.json' assert { type: 'json' };
import routerABI from './constants/abis/router.json' assert { type: 'json' };
Expand All @@ -56,6 +57,9 @@ import { L2Networks } from "./constants/L2Networks.js";
import { getTwocryptoFactoryPoolData } from "./factory/factory-twocrypto.js";
import {getNetworkConstants} from "./utils.js";


export const OLD_CHAINS = [1, 10, 56, 100, 137, 250, 1284, 2222, 8453, 42161, 42220, 43114, 1313161554]; // these chains have non-ng pools

export const memoizedContract = (): (address: string, abi: any, provider: BrowserProvider | JsonRpcProvider | Signer) => Contract => {
const cache: Record<string, Contract> = {};
return (address: string, abi: any, provider: BrowserProvider | JsonRpcProvider | Signer): Contract => {
Expand Down Expand Up @@ -369,7 +373,11 @@ class Curve implements ICurve {
this.setContract(this.constants.ALIASES.router, routerNgPoolsOnlyABI);
}

this.setContract(this.constants.ALIASES.deposit_and_stake, depositAndStakeABI);
if (OLD_CHAINS.includes(this.chainId)) {
this.setContract(this.constants.ALIASES.deposit_and_stake, depositAndStakeABI);
} else {
this.setContract(this.constants.ALIASES.deposit_and_stake, depositAndStakeNgOnlyABI);
}

this.setContract(this.constants.ALIASES.crypto_calc, cryptoCalcZapABI);

Expand Down
86 changes: 59 additions & 27 deletions src/pools/PoolTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
PERIODS,
} from '../utils.js';
import {IDict, IProfit} from '../interfaces';
import { curve } from "../curve.js";
import { curve, OLD_CHAINS } from "../curve.js";
import ERC20Abi from '../constants/abis/ERC20.json' assert { type: 'json' };
import {CorePool} from "./subClasses/corePool.js";
import {StatsPool} from "./subClasses/statsPool.js";
Expand Down Expand Up @@ -1227,6 +1227,7 @@ export class PoolTemplate extends CorePool {

const contract = curve.contracts[curve.constants.ALIASES.deposit_and_stake].contract;
const useUnderlying = isUnderlying && (this.isLending || (this.isCrypto && !this.isPlain)) && (!this.zap || this.id == 'avaxcrypto');
const useDynarray = (!this.isCrypto && this.isNg && this.isPlain) || (isUnderlying && this.isMeta && (new PoolTemplate(this.basePool)).isNg);
const _expectedLpTokenAmount = isUnderlying ?
curve.parseUnits(await this.depositAndStakeExpected(amounts)) :
curve.parseUnits(await this.depositAndStakeWrappedExpected(amounts));
Expand All @@ -1235,37 +1236,68 @@ export class PoolTemplate extends CorePool {
const ethIndex = getEthIndex(coinAddresses);
const value = _amounts[ethIndex] || curve.parseUnits("0");

const _gas = (await contract.deposit_and_stake.estimateGas(
depositAddress,
this.lpToken,
this.gauge.address,
coins.length,
coinAddresses,
_amounts,
_minMintAmount,
useUnderlying,
(!this.isCrypto && this.isNg && this.isPlain) || (isUnderlying && this.isMeta && (new PoolTemplate(this.basePool)).isNg),
this.isMetaFactory && isUnderlying ? this.address : curve.constants.ZERO_ADDRESS,
{ ...curve.constantOptions, value }
))
let _gas = BigInt(0)
if (OLD_CHAINS.includes(curve.chainId)) {
_gas = (await contract.deposit_and_stake.estimateGas(
depositAddress,
this.lpToken,
this.gauge.address,
coins.length,
coinAddresses,
_amounts,
_minMintAmount,
useUnderlying, // <--- DIFFERENCE
useDynarray,
this.isMetaFactory && isUnderlying ? this.address : curve.constants.ZERO_ADDRESS,
{...curve.constantOptions, value}
))
} else {
_gas = (await contract.deposit_and_stake.estimateGas(
depositAddress,
this.lpToken,
this.gauge.address,
coins.length,
coinAddresses,
_amounts,
_minMintAmount,
useDynarray,
this.isMetaFactory && isUnderlying ? this.address : curve.constants.ZERO_ADDRESS,
{...curve.constantOptions, value}
))
}

if (estimateGas) return smartNumber(_gas)

await curve.updateFeeData();
const gasLimit = DIGas(_gas) * curve.parseUnits("200", 0) / curve.parseUnits("100", 0);
return (await contract.deposit_and_stake(
depositAddress,
this.lpToken,
this.gauge.address,
coins.length,
coinAddresses,
_amounts,
_minMintAmount,
useUnderlying,
(!this.isCrypto && this.isNg && this.isPlain) || (isUnderlying && this.isMeta && (new PoolTemplate(this.basePool)).isNg),
this.isMetaFactory && isUnderlying ? this.address : curve.constants.ZERO_ADDRESS,
{ ...curve.options, gasLimit, value }
)).hash
if (OLD_CHAINS.includes(curve.chainId)) {
return (await contract.deposit_and_stake(
depositAddress,
this.lpToken,
this.gauge.address,
coins.length,
coinAddresses,
_amounts,
_minMintAmount,
useUnderlying, // <--- DIFFERENCE
useDynarray,
this.isMetaFactory && isUnderlying ? this.address : curve.constants.ZERO_ADDRESS,
{...curve.options, gasLimit, value}
)).hash
} else {
return (await contract.deposit_and_stake(
depositAddress,
this.lpToken,
this.gauge.address,
coins.length,
coinAddresses,
_amounts,
_minMintAmount,
useDynarray,
this.isMetaFactory && isUnderlying ? this.address : curve.constants.ZERO_ADDRESS,
{...curve.options, gasLimit, value}
)).hash
}
}

// ---------------- WITHDRAW ----------------
Expand Down
4 changes: 1 addition & 3 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios from "axios";
import memoize from "memoizee";
import BigNumber from "bignumber.js";
import {ethers} from "ethers";
import {curve} from "./curve.js";
import { curve, OLD_CHAINS } from "./curve.js";
import {IDict, IRoute, IRouteOutputAndCost, IRouteStep} from "./interfaces";
import {
_cutZeros,
Expand Down Expand Up @@ -35,8 +35,6 @@ import {IRouteGraphInput, routeGraphWorker, routeGraphWorkerCode} from "./route-
const MAX_STEPS = 5;
const ROUTE_LENGTH = (MAX_STEPS * 2) + 1;

const OLD_CHAINS = [1, 10, 56, 100, 137, 250, 1284, 2222, 8453, 42161, 42220, 43114, 1313161554]; // these chains have non-ng pools

const _getTVL = memoize(
async (poolId: string) => Number(await (getPool(poolId)).stats.totalLiquidity()),
{
Expand Down

0 comments on commit 798a740

Please sign in to comment.