Skip to content

Commit ae7b086

Browse files
committed
refactor: make isETH dependent on an instance of the curve
1 parent d467fa2 commit ae7b086

File tree

8 files changed

+35
-40
lines changed

8 files changed

+35
-40
lines changed

src/curve.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ import {
7070
import {_getHiddenPools} from "./external-api.js";
7171
import {L2Networks} from "./constants/L2Networks.js";
7272
import {getTwocryptoFactoryPoolData} from "./factory/factory-twocrypto.js";
73-
import {getNetworkConstants, setNativeTokenAddress} from "./utils.js";
73+
import {getNetworkConstants} from "./utils.js";
7474
import {_setPoolsFromApi} from "./cached.js";
7575

7676
export const OLD_CHAINS = [1, 10, 56, 100, 137, 250, 1284, 2222, 8453, 42161, 42220, 43114, 1313161554]; // these chains have non-ng pools
@@ -119,6 +119,7 @@ export class Curve implements ICurve {
119119
options: { gasPrice?: number | bigint, maxFeePerGas?: number | bigint, maxPriorityFeePerGas?: number | bigint };
120120
L1WeightedGasPrice?: number;
121121
constants: INetworkConstants;
122+
nativeTokenAddress: string;
122123

123124
constructor() {
124125
this.provider = null!;
@@ -132,6 +133,7 @@ export class Curve implements ICurve {
132133
this.feeData = {}
133134
this.constantOptions = { gasLimit: 12000000 }
134135
this.options = {};
136+
this.nativeTokenAddress = NETWORK_CONSTANTS[1].NATIVE_COIN.address;
135137
this.constants = {
136138
NATIVE_TOKEN: NETWORK_CONSTANTS[1].NATIVE_COIN,
137139
NETWORK_NAME: 'ethereum',
@@ -171,6 +173,7 @@ export class Curve implements ICurve {
171173
this.feeData = {}
172174
this.constantOptions = { gasLimit: 12000000 }
173175
this.options = {};
176+
this.nativeTokenAddress = NETWORK_CONSTANTS[1].NATIVE_COIN.address;
174177
this.constants = {
175178
NATIVE_TOKEN: NETWORK_CONSTANTS[1].NATIVE_COIN,
176179
NETWORK_NAME: 'ethereum',
@@ -256,7 +259,7 @@ export class Curve implements ICurve {
256259

257260
const network_constants = await getNetworkConstants.call(this, this.chainId);
258261
this.constants.NATIVE_TOKEN = network_constants.NATIVE_COIN;
259-
setNativeTokenAddress(network_constants.NATIVE_COIN.address);
262+
this.nativeTokenAddress = network_constants.NATIVE_COIN.address;
260263
this.constants.NETWORK_NAME = network_constants.NAME;
261264
this.constants.ALIASES = network_constants.ALIASES;
262265
this.constants.ALIASES.anycall = "0x37414a8662bc1d25be3ee51fb27c2686e2490a89";
@@ -837,6 +840,14 @@ export class Curve implements ICurve {
837840
getIsLiteChain = (): boolean => {
838841
return this.isLiteChain
839842
}
843+
844+
isEth = (address: string): boolean => {
845+
return address.toLowerCase() === this.nativeTokenAddress.toLowerCase();
846+
}
847+
848+
getEthIndex = (addresses: string[]): number => {
849+
return addresses.map((address: string) => address.toLowerCase()).indexOf(this.nativeTokenAddress.toLowerCase());
850+
}
840851
}
841852

842853
export const curve = new Curve();

src/pools/PoolTemplate.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
ensureAllowanceEstimateGas,
1919
findAbiFunction,
2020
fromBN,
21-
getEthIndex,
2221
hasAllowance,
2322
mulBy1_3,
2423
parseUnits,
@@ -1251,7 +1250,7 @@ export class PoolTemplate extends CorePool {
12511250
this.curve.parseUnits(await this.depositAndStakeWrappedExpected(amounts));
12521251
const minAmountBN = toBN(_expectedLpTokenAmount).times(100 - slippage).div(100);
12531252
const _minMintAmount = fromBN(minAmountBN);
1254-
const ethIndex = getEthIndex(coinAddresses);
1253+
const ethIndex = this.curve.getEthIndex(coinAddresses);
12551254
const value = _amounts[ethIndex] || this.curve.parseUnits("0");
12561255

12571256
const _gas = OLD_CHAINS.includes(this.curve.chainId) ? (await contract.deposit_and_stake.estimateGas(

src/pools/mixins/depositMixins.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
_ensureAllowance,
44
DIGas,
55
fromBN,
6-
getEthIndex,
76
hasAllowance,
87
mulBy1_3,
98
parseUnits,
@@ -43,7 +42,7 @@ export const depositMetaFactoryMixin = {
4342
if (!estimateGas) await _ensureAllowance.call(this.curve, this.underlyingCoinAddresses, _amounts, this.zap as string);
4443

4544
const _minMintAmount = await _depositMinAmount.call(this, _amounts, slippage);
46-
const ethIndex = getEthIndex(this.underlyingCoinAddresses);
45+
const ethIndex = this.curve.getEthIndex(this.underlyingCoinAddresses);
4746
const value = _amounts[ethIndex] || parseUnits("0");
4847
const contract = this.curve.contracts[this.zap as string].contract;
4948

@@ -70,7 +69,7 @@ export const depositCryptoMetaFactoryMixin = {
7069
if (!estimateGas) await _ensureAllowance.call(this.curve, this.underlyingCoinAddresses, _amounts, this.zap as string);
7170

7271
const _minMintAmount = await _depositMinAmount.call(this, _amounts, slippage);
73-
const ethIndex = getEthIndex(this.underlyingCoinAddresses);
72+
const ethIndex = this.curve.getEthIndex(this.underlyingCoinAddresses);
7473
const value = _amounts[ethIndex] || this.curve.parseUnits("0");
7574
const contract = this.curve.contracts[this.zap as string].contract;
7675

@@ -97,7 +96,7 @@ export const depositZapMixin = {
9796
if (!estimateGas) await _ensureAllowance.call(this.curve, this.underlyingCoinAddresses, _amounts, this.zap as string);
9897

9998
const _minMintAmount = await _depositMinAmount.call(this, _amounts, slippage);
100-
const ethIndex = getEthIndex(this.underlyingCoinAddresses);
99+
const ethIndex = this.curve.getEthIndex(this.underlyingCoinAddresses);
101100
const value = _amounts[ethIndex] || this.curve.parseUnits("0");
102101
const contract = this.curve.contracts[this.zap as string].contract;
103102

@@ -126,7 +125,7 @@ export const depositLendingOrCryptoMixin = {
126125
if (!estimateGas) await _ensureAllowance.call(this.curve, this.underlyingCoinAddresses, _amounts, this.address);
127126

128127
const _minMintAmount = await _depositMinAmount.call(this, _amounts, slippage);
129-
const ethIndex = getEthIndex(this.underlyingCoinAddresses);
128+
const ethIndex = this.curve.getEthIndex(this.underlyingCoinAddresses);
130129
const value = _amounts[ethIndex] || this.curve.parseUnits("0");
131130
const contract = this.curve.contracts[this.address].contract;
132131

@@ -152,7 +151,7 @@ export const depositPlainMixin = {
152151
async _deposit(this: PoolTemplate, _amounts: bigint[], slippage?: number, estimateGas = false): Promise<string | number | number[]> {
153152
if (!estimateGas) await _ensureAllowance.call(this.curve, this.wrappedCoinAddresses, _amounts, this.address);
154153
const _minMintAmount = await _depositMinAmount.call(this, _amounts, slippage);
155-
const ethIndex = getEthIndex(this.wrappedCoinAddresses);
154+
const ethIndex = this.curve.getEthIndex(this.wrappedCoinAddresses);
156155
const value = _amounts[ethIndex] || this.curve.parseUnits("0");
157156
const contract = this.curve.contracts[this.address].contract;
158157

src/pools/mixins/depositWrappedMixins.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
_ensureAllowance,
44
DIGas,
55
fromBN,
6-
getEthIndex,
76
hasAllowance,
87
mulBy1_3,
98
parseUnits,
@@ -48,7 +47,7 @@ export const depositWrapped2argsMixin = {
4847
if (!estimateGas) await _ensureAllowance.call(this.curve, this.wrappedCoinAddresses, _amounts, this.address);
4948

5049
const _minMintAmount = await _depositWrappedMinAmount.call(this, _amounts, slippage);
51-
const ethIndex = getEthIndex(this.wrappedCoinAddresses);
50+
const ethIndex = this.curve.getEthIndex(this.wrappedCoinAddresses);
5251
const value = _amounts[ethIndex] || this.curve.parseUnits("0");
5352
const contract = this.curve.contracts[this.address].contract;
5453

@@ -75,7 +74,7 @@ export const depositWrapped3argsMixin = {
7574
if (!estimateGas) await _ensureAllowance.call(this.curve, this.wrappedCoinAddresses, _amounts, this.address);
7675

7776
const _minMintAmount = await _depositWrappedMinAmount.call(this, _amounts, slippage);
78-
const ethIndex = getEthIndex(this.wrappedCoinAddresses);
77+
const ethIndex = this.curve.getEthIndex(this.wrappedCoinAddresses);
7978
const value = _amounts[ethIndex] || this.curve.parseUnits("0");
8079
const contract = this.curve.contracts[this.address].contract;
8180

src/pools/mixins/swapMixins.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
DIGas,
77
fromBN,
88
hasAllowance,
9-
isEth,
109
mulBy1_3,
1110
parseUnits,
1211
smartNumber,
@@ -56,7 +55,7 @@ export const swapTricrypto2Mixin = {
5655
const _minRecvAmount = await _swapMinAmount.call(this, i, j, _amount, slippage);
5756
const contract = this.curve.contracts[contractAddress].contract;
5857
const exchangeMethod = 'exchange_underlying' in contract ? 'exchange_underlying' : 'exchange';
59-
const value = isEth(this.underlyingCoinAddresses[i]) ? _amount : this.curve.parseUnits("0");
58+
const value = this.curve.isEth(this.underlyingCoinAddresses[i]) ? _amount : this.curve.parseUnits("0");
6059

6160
const gas = await contract[exchangeMethod].estimateGas(i, j, _amount, _minRecvAmount, true, { ...this.curve.constantOptions, value });
6261
if (estimateGas) return smartNumber(gas);
@@ -84,7 +83,7 @@ export const swapMetaFactoryMixin = {
8483
const _minRecvAmount = await _swapMinAmount.call(this, i, j, _amount, slippage);
8584
const contract = this.curve.contracts[contractAddress].contract;
8685
const exchangeMethod = 'exchange_underlying' in contract ? 'exchange_underlying' : 'exchange';
87-
const value = isEth(this.underlyingCoinAddresses[i]) ? _amount : this.curve.parseUnits("0");
86+
const value = this.curve.isEth(this.underlyingCoinAddresses[i]) ? _amount : this.curve.parseUnits("0");
8887

8988
const gas = await contract[exchangeMethod].estimateGas(this.address, i, j, _amount, _minRecvAmount, { ...this.curve.constantOptions, value });
9089
if (estimateGas) return smartNumber(gas);
@@ -112,7 +111,7 @@ export const swapCryptoMetaFactoryMixin = {
112111
const _minRecvAmount = await _swapMinAmount.call(this, i, j, _amount, slippage);
113112
const contract = this.curve.contracts[contractAddress].contract;
114113
const exchangeMethod = 'exchange_underlying' in contract ? 'exchange_underlying' : 'exchange';
115-
const value = isEth(this.underlyingCoinAddresses[i]) ? _amount : this.curve.parseUnits("0");
114+
const value = this.curve.isEth(this.underlyingCoinAddresses[i]) ? _amount : this.curve.parseUnits("0");
116115

117116
const gas = await contract[exchangeMethod].estimateGas(this.address, i, j, _amount, _minRecvAmount, true, { ...this.curve.constantOptions, value });
118117
if (estimateGas) return smartNumber(gas);
@@ -140,7 +139,7 @@ export const swapMixin = {
140139
const _minRecvAmount = await _swapMinAmount.call(this, i, j, _amount, slippage);
141140
const contract = this.curve.contracts[contractAddress].contract;
142141
const exchangeMethod = 'exchange_underlying' in contract ? 'exchange_underlying' : 'exchange';
143-
const value = isEth(this.underlyingCoinAddresses[i]) ? _amount : this.curve.parseUnits("0");
142+
const value = this.curve.isEth(this.underlyingCoinAddresses[i]) ? _amount : this.curve.parseUnits("0");
144143

145144
const gas = await contract[exchangeMethod].estimateGas(i, j, _amount, _minRecvAmount, { ...this.curve.constantOptions, value });
146145
if (estimateGas) return smartNumber(gas);

src/pools/mixins/swapWrappedMixins.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
ensureAllowanceEstimateGas,
99
fromBN,
1010
hasAllowance,
11-
isEth,
1211
mulBy1_3,
1312
parseUnits,
1413
smartNumber,
@@ -55,7 +54,7 @@ export const swapWrappedTricrypto2Mixin= {
5554

5655
const _minRecvAmount = await _swapWrappedMinAmount.call(this, i, j, _amount, slippage);
5756
const contract = this.curve.contracts[this.address].contract;
58-
const value = isEth(this.wrappedCoinAddresses[i]) ? _amount : this.curve.parseUnits("0");
57+
const value = this.curve.isEth(this.wrappedCoinAddresses[i]) ? _amount : this.curve.parseUnits("0");
5958

6059
const gas = await contract.exchange.estimateGas(i, j, _amount, _minRecvAmount, false, { ...this.curve.constantOptions, value });
6160
if (estimateGas) return smartNumber(gas);
@@ -81,7 +80,7 @@ export const swapWrappedMixin= {
8180

8281
const _minRecvAmount = await _swapWrappedMinAmount.call(this, i, j, _amount, slippage);
8382
const contract = this.curve.contracts[this.address].contract;
84-
const value = isEth(this.wrappedCoinAddresses[i]) ? _amount : this.curve.parseUnits("0");
83+
const value = this.curve.isEth(this.wrappedCoinAddresses[i]) ? _amount : this.curve.parseUnits("0");
8584

8685
const gas = await contract.exchange.estimateGas(i, j, _amount, _minRecvAmount, { ...this.curve.constantOptions, value });
8786
if (estimateGas) return smartNumber(gas);

src/router.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
getGasPriceFromL1,
2020
getTxCostsUsd,
2121
hasAllowance,
22-
isEth,
2322
parseUnits,
2423
populateApprove,
2524
runWorker,
@@ -142,7 +141,7 @@ async function _estimateGasForDifferentRoutes(this: Curve, routes: IRoute[], inp
142141

143142
const contract = this.contracts[this.constants.ALIASES.router].contract;
144143
const gasPromises: Promise<bigint | bigint[]>[] = [];
145-
const value = isEth(inputCoinAddress) ? _amount : this.parseUnits("0");
144+
const value = this.isEth(inputCoinAddress) ? _amount : this.parseUnits("0");
146145
for (const route of routes) {
147146
const routeKey = _getRouteKey(route, inputCoinAddress, outputCoinAddress);
148147
let gasPromise: Promise<bigint | bigint[]>;
@@ -440,7 +439,7 @@ async function prepareSwap(this: Curve, inputCoin: string, outputCoin: string, a
440439
const _minRecvAmount = fromBN(minRecvAmountBN, outputCoinDecimals);
441440

442441
const contract = this.contracts[this.constants.ALIASES.router].contract;
443-
const value = isEth(inputCoinAddress) ? _amount : this.parseUnits("0");
442+
const value = this.isEth(inputCoinAddress) ? _amount : this.parseUnits("0");
444443

445444
await this.updateFeeData();
446445
if (_pools) {
@@ -489,7 +488,7 @@ export async function populateSwap(this: Curve, inputCoin: string, outputCoin: s
489488
return await contract.exchange.populateTransaction(...[
490489
_route, _swapParams, _amount, _minRecvAmount,
491490
..._pools ? [_pools] : [],
492-
{ value: isEth(inputCoinAddress) ? _amount : this.parseUnits("0") },
491+
{ value: this.isEth(inputCoinAddress) ? _amount : this.parseUnits("0") },
493492
])
494493
}
495494

src/utils.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ export const ETH_ADDRESS = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
3434
// export const MAX_ALLOWANCE = curve.parseUnits(new BigNumber(2).pow(256).minus(1).toFixed(), 0);
3535
export const MAX_ALLOWANCE = BigInt("115792089237316195423570985008687907853269984665640564039457584007913129639935"); // 2**256 - 1
3636

37-
// Global variable for current native token address (updated during Curve.init)
38-
let CURRENT_NATIVE_TOKEN_ADDRESS = ETH_ADDRESS;
39-
4037

4138
// Formatting numbers
4239

@@ -79,13 +76,6 @@ export const fromBN = (bn: BigNumber, decimals = 18): bigint => {
7976

8077
// -------------------
8178

82-
83-
export const setNativeTokenAddress = (address: string): void => {
84-
CURRENT_NATIVE_TOKEN_ADDRESS = address;
85-
};
86-
87-
export const isEth = (address: string): boolean => address.toLowerCase() === CURRENT_NATIVE_TOKEN_ADDRESS.toLowerCase();
88-
export const getEthIndex = (addresses: string[]): number => addresses.map((address: string) => address.toLowerCase()).indexOf(CURRENT_NATIVE_TOKEN_ADDRESS.toLowerCase());
8979
export const mulBy1_3 = (n: bigint): bigint => n * parseUnits("130", 0) / parseUnits("100", 0);
9080

9181
export const smartNumber = (abstractNumber: bigint | bigint[]): number | number[] => {
@@ -150,7 +140,7 @@ export async function _getBalances(this: Curve, coins: string[], addresses: stri
150140
const coinAddresses = _getCoinAddresses.call(this, coins);
151141
const decimals = _getCoinDecimals.call(this, coinAddresses);
152142

153-
const ethIndex = getEthIndex(coinAddresses);
143+
const ethIndex = this.getEthIndex(coinAddresses);
154144
if (ethIndex !== -1) {
155145
coinAddresses.splice(ethIndex, 1);
156146
}
@@ -207,7 +197,7 @@ export async function getBalances(this: Curve, coins: string[], ...addresses: st
207197

208198
export async function _getAllowance(this: Curve, coins: string[], address: string, spender: string): Promise<bigint[]> {
209199
const _coins = [...coins]
210-
const ethIndex = getEthIndex(_coins);
200+
const ethIndex = this.getEthIndex(_coins);
211201
if (ethIndex !== -1) {
212202
_coins.splice(ethIndex, 1);
213203

@@ -443,11 +433,11 @@ export async function _getUsdRate(this: Curve, assetId: string): Promise<number>
443433

444434
// Special case for chainId 988 native and wrapped tokens are USDT
445435
if (this.chainId === 988) {
446-
if (isEth(assetId) || assetId.toLowerCase() === this.constants.NATIVE_TOKEN.wrappedAddress.toLowerCase()) {
436+
if (this.isEth(assetId) || assetId.toLowerCase() === this.constants.NATIVE_TOKEN.wrappedAddress.toLowerCase()) {
447437
assetId = 'tether';
448438
}
449439
} else {
450-
assetId = isEth(assetId) ? nativeTokenName : assetId.toLowerCase();
440+
assetId = this.isEth(assetId) ? nativeTokenName : assetId.toLowerCase();
451441
}
452442

453443
// No EURT on Coingecko Polygon
@@ -701,7 +691,7 @@ export async function getCoinsData(this: Curve, ...coins: string[] | string[][])
701691
const coinAddresses = _getCoinAddressesNoCheck.call(this, coins);
702692
console.log(coinAddresses);
703693

704-
const ethIndex = getEthIndex(coinAddresses);
694+
const ethIndex = this.getEthIndex(coinAddresses);
705695
if (ethIndex !== -1) {
706696
coinAddresses.splice(ethIndex, 1);
707697
}

0 commit comments

Comments
 (0)