From e42e42f173700c53d4eb31effd8e16bac5b0d890 Mon Sep 17 00:00:00 2001 From: vic-en Date: Sat, 22 Jul 2023 23:49:56 -0500 Subject: [PATCH 1/2] fix issue with unrecognised tokens --- src/connectors/uniswap/uniswap.lp.helper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/connectors/uniswap/uniswap.lp.helper.ts b/src/connectors/uniswap/uniswap.lp.helper.ts index bd50ac5d4c..670288292f 100644 --- a/src/connectors/uniswap/uniswap.lp.helper.ts +++ b/src/connectors/uniswap/uniswap.lp.helper.ts @@ -96,7 +96,7 @@ export class UniswapLPHelper { * @param address Token address */ public getTokenByAddress(address: string): Token { - return this.tokenList[address]; + return this.tokenList[address.toLowerCase()]; } public async init() { From bfe5d42fc676f210ccc65ca4471b81bc1d368767 Mon Sep 17 00:00:00 2001 From: vic-en Date: Sun, 23 Jul 2023 00:28:10 -0500 Subject: [PATCH 2/2] refactor evm connectors to use checksumed address --- src/chains/ethereum/ethereum-base.ts | 9 +++++++-- src/connectors/defikingdoms/defikingdoms.ts | 3 ++- src/connectors/defira/defira.ts | 3 ++- src/connectors/openocean/openocean.ts | 3 ++- src/connectors/pancakeswap/pancakeswap.ts | 3 ++- src/connectors/pangolin/pangolin.ts | 3 ++- src/connectors/perp/perp.ts | 3 ++- src/connectors/quickswap/quickswap.ts | 3 ++- src/connectors/sushiswap/sushiswap.ts | 3 ++- src/connectors/traderjoe/traderjoe.ts | 3 ++- src/connectors/uniswap/uniswap.lp.helper.ts | 3 ++- src/connectors/uniswap/uniswap.ts | 3 ++- src/connectors/zigzag/zigzag.ts | 19 +++++++++---------- 13 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/chains/ethereum/ethereum-base.ts b/src/chains/ethereum/ethereum-base.ts index 64aefbe3a8..55101b30ce 100644 --- a/src/chains/ethereum/ethereum-base.ts +++ b/src/chains/ethereum/ethereum-base.ts @@ -18,6 +18,7 @@ import fse from 'fs-extra'; import { ConfigManagerCertPassphrase } from '../../services/config-manager-cert-passphrase'; import { logger } from '../../services/logger'; import { ReferenceCountingCloseable } from '../../services/refcounting-closeable'; +import { getAddress } from 'ethers/lib/utils'; // information about an Ethereum token export interface TokenInfo { @@ -153,7 +154,7 @@ export class EthereumBase { tokenListSource: string, tokenListType: TokenListType ): Promise { - let tokens; + let tokens: TokenInfo[]; if (tokenListType === 'URL') { ({ data: { tokens }, @@ -161,7 +162,11 @@ export class EthereumBase { } else { ({ tokens } = JSON.parse(await fs.readFile(tokenListSource, 'utf8'))); } - return tokens; + const mappedTokens: TokenInfo[] = tokens.map((token) => { + token.address = getAddress(token.address); + return token; + }); + return mappedTokens; } public get nonceManager() { diff --git a/src/connectors/defikingdoms/defikingdoms.ts b/src/connectors/defikingdoms/defikingdoms.ts index a1d2460b07..2e8e1059dc 100644 --- a/src/connectors/defikingdoms/defikingdoms.ts +++ b/src/connectors/defikingdoms/defikingdoms.ts @@ -24,6 +24,7 @@ import { percentRegexp } from '../../services/config-manager-v2'; // import { Ethereum } from '../../chains/ethereum/ethereum'; import { ExpectedTrade, Uniswapish } from '../../services/common-interfaces'; import { Harmony } from '../../chains/harmony/harmony'; +import { getAddress } from 'ethers/lib/utils'; export class Defikingdoms implements Uniswapish { private static _instances: { [name: string]: Defikingdoms }; @@ -65,7 +66,7 @@ export class Defikingdoms implements Uniswapish { * @param address Token address */ public getTokenByAddress(address: string): Token { - return this.tokenList[address]; + return this.tokenList[getAddress(address)]; } public async init() { diff --git a/src/connectors/defira/defira.ts b/src/connectors/defira/defira.ts index a179310fcb..c91a3472dd 100644 --- a/src/connectors/defira/defira.ts +++ b/src/connectors/defira/defira.ts @@ -21,6 +21,7 @@ import { logger } from '../../services/logger'; import { percentRegexp } from '../../services/config-manager-v2'; import { Harmony } from '../../chains/harmony/harmony'; import { ExpectedTrade, Uniswapish } from '../../services/common-interfaces'; +import { getAddress } from 'ethers/lib/utils'; export class Defira implements Uniswapish { private static _instances: { [name: string]: Defira }; @@ -65,7 +66,7 @@ export class Defira implements Uniswapish { * @param address Token address */ public getTokenByAddress(address: string): Token { - return this.tokenList[address]; + return this.tokenList[getAddress(address)]; } public async init() { diff --git a/src/connectors/openocean/openocean.ts b/src/connectors/openocean/openocean.ts index b090df3d2e..a151c2dd88 100644 --- a/src/connectors/openocean/openocean.ts +++ b/src/connectors/openocean/openocean.ts @@ -28,6 +28,7 @@ import { UNKNOWN_ERROR_ERROR_CODE, UNKNOWN_ERROR_MESSAGE, } from '../../services/error-handler'; +import { getAddress } from 'ethers/lib/utils'; export function newFakeTrade( tokenIn: Token, @@ -110,7 +111,7 @@ export class Openocean implements Uniswapish { * @param address Token address */ public getTokenByAddress(address: string): Token { - return this.tokenList[address]; + return this.tokenList[getAddress(address)]; } public async init() { diff --git a/src/connectors/pancakeswap/pancakeswap.ts b/src/connectors/pancakeswap/pancakeswap.ts index 9296dad496..0ea4ab6242 100644 --- a/src/connectors/pancakeswap/pancakeswap.ts +++ b/src/connectors/pancakeswap/pancakeswap.ts @@ -29,6 +29,7 @@ import { logger } from '../../services/logger'; import { isFractionString } from '../../services/validators'; import { PancakeSwapConfig } from './pancakeswap.config'; import routerAbi from './pancakeswap_router_abi.json'; +import { getAddress } from 'ethers/lib/utils'; export class PancakeSwap implements Uniswapish { private static _instances: { [name: string]: PancakeSwap }; @@ -91,7 +92,7 @@ export class PancakeSwap implements Uniswapish { * @param address Token address */ public getTokenByAddress(address: string): Token { - return this.tokenList[address]; + return this.tokenList[getAddress(address)]; } /** diff --git a/src/connectors/pangolin/pangolin.ts b/src/connectors/pangolin/pangolin.ts index bc0a149e42..7528435185 100644 --- a/src/connectors/pangolin/pangolin.ts +++ b/src/connectors/pangolin/pangolin.ts @@ -23,6 +23,7 @@ import { import { logger } from '../../services/logger'; import { Avalanche } from '../../chains/avalanche/avalanche'; import { ExpectedTrade, Uniswapish } from '../../services/common-interfaces'; +import { getAddress } from 'ethers/lib/utils'; export class Pangolin implements Uniswapish { private static _instances: { [name: string]: Pangolin }; @@ -63,7 +64,7 @@ export class Pangolin implements Uniswapish { * @param address Token address */ public getTokenByAddress(address: string): Token { - return this.tokenList[address]; + return this.tokenList[getAddress(address)]; } public async init() { diff --git a/src/connectors/perp/perp.ts b/src/connectors/perp/perp.ts index 0590673c52..8a3c74e5d6 100644 --- a/src/connectors/perp/perp.ts +++ b/src/connectors/perp/perp.ts @@ -23,6 +23,7 @@ import { logger } from '../../services/logger'; import { percentRegexp } from '../../services/config-manager-v2'; import { Ethereum } from '../../chains/ethereum/ethereum'; import { Perpish } from '../../services/common-interfaces'; +import { getAddress } from 'ethers/lib/utils'; export interface PerpPosition { positionAmt: string; @@ -88,7 +89,7 @@ export class Perp implements Perpish { * @param address Token address */ public getTokenByAddress(address: string): Token { - return this.tokenList[address]; + return this.tokenList[getAddress(address)]; } public async init() { diff --git a/src/connectors/quickswap/quickswap.ts b/src/connectors/quickswap/quickswap.ts index c58b4d3708..fff24ad3b4 100644 --- a/src/connectors/quickswap/quickswap.ts +++ b/src/connectors/quickswap/quickswap.ts @@ -22,6 +22,7 @@ import { import { logger } from '../../services/logger'; import { Polygon } from '../../chains/polygon/polygon'; import { ExpectedTrade, Uniswapish } from '../../services/common-interfaces'; +import { getAddress } from 'ethers/lib/utils'; export class Quickswap implements Uniswapish { private static _instances: { [name: string]: Quickswap }; @@ -62,7 +63,7 @@ export class Quickswap implements Uniswapish { * @param address Token address */ public getTokenByAddress(address: string): Token { - return this.tokenList[address]; + return this.tokenList[getAddress(address)]; } public async init() { diff --git a/src/connectors/sushiswap/sushiswap.ts b/src/connectors/sushiswap/sushiswap.ts index 444264d657..110f2bada2 100644 --- a/src/connectors/sushiswap/sushiswap.ts +++ b/src/connectors/sushiswap/sushiswap.ts @@ -31,6 +31,7 @@ import { } from 'ethers'; import { percentRegexp } from '../../services/config-manager-v2'; import { logger } from '../../services/logger'; +import { getAddress } from 'ethers/lib/utils'; export class Sushiswap implements Uniswapish { private static _instances: { [name: string]: Sushiswap }; @@ -83,7 +84,7 @@ export class Sushiswap implements Uniswapish { * @param address Token address */ public getTokenByAddress(address: string): Token { - return this.tokenList[address]; + return this.tokenList[getAddress(address)]; } public async init() { diff --git a/src/connectors/traderjoe/traderjoe.ts b/src/connectors/traderjoe/traderjoe.ts index 45567697e0..fbec8ace3b 100644 --- a/src/connectors/traderjoe/traderjoe.ts +++ b/src/connectors/traderjoe/traderjoe.ts @@ -22,6 +22,7 @@ import { import { EVMTxBroadcaster } from '../../chains/ethereum/evm.broadcaster'; import { createPublicClient, http } from 'viem'; import { avalanche, avalancheFuji } from 'viem/chains'; +import { getAddress } from 'ethers/lib/utils'; const MAX_HOPS = 2; const BASES = ['USDT', 'USDC', 'WAVAX']; @@ -71,7 +72,7 @@ export class Traderjoe implements Uniswapish { * @param address Token address */ public getTokenByAddress(address: string): Token { - return this.tokenList[address]; + return this.tokenList[getAddress(address)]; } public async init() { diff --git a/src/connectors/uniswap/uniswap.lp.helper.ts b/src/connectors/uniswap/uniswap.lp.helper.ts index 670288292f..00fe102995 100644 --- a/src/connectors/uniswap/uniswap.lp.helper.ts +++ b/src/connectors/uniswap/uniswap.lp.helper.ts @@ -18,6 +18,7 @@ import { ReduceLiquidityData, } from './uniswap.lp.interfaces'; import * as math from 'mathjs'; +import { getAddress } from 'ethers/lib/utils'; export class UniswapLPHelper { protected ethereum: Ethereum; @@ -96,7 +97,7 @@ export class UniswapLPHelper { * @param address Token address */ public getTokenByAddress(address: string): Token { - return this.tokenList[address.toLowerCase()]; + return this.tokenList[getAddress(address)]; } public async init() { diff --git a/src/connectors/uniswap/uniswap.ts b/src/connectors/uniswap/uniswap.ts index 873cdc0469..4501f489fc 100644 --- a/src/connectors/uniswap/uniswap.ts +++ b/src/connectors/uniswap/uniswap.ts @@ -39,6 +39,7 @@ import { percentRegexp } from '../../services/config-manager-v2'; import { Ethereum } from '../../chains/ethereum/ethereum'; import { Polygon } from '../../chains/polygon/polygon'; import { ExpectedTrade, Uniswapish } from '../../services/common-interfaces'; +import { getAddress } from 'ethers/lib/utils'; export class Uniswap implements Uniswapish { private static _instances: { [name: string]: Uniswap }; @@ -107,7 +108,7 @@ export class Uniswap implements Uniswapish { * @param address Token address */ public getTokenByAddress(address: string): Token { - return this.tokenList[address]; + return this.tokenList[getAddress(address)]; } public async init() { diff --git a/src/connectors/zigzag/zigzag.ts b/src/connectors/zigzag/zigzag.ts index b45595a640..df05ccc5ce 100644 --- a/src/connectors/zigzag/zigzag.ts +++ b/src/connectors/zigzag/zigzag.ts @@ -10,6 +10,7 @@ import { ZigZagish } from '../../services/common-interfaces'; import { logger } from '../../services/logger'; import { EVMTxBroadcaster } from '../../chains/ethereum/evm.broadcaster'; import { abi } from './zigzag.exchange.abi.json'; +import { getAddress } from 'ethers/lib/utils'; // https://api.arbitrum.zigzag.exchange/v1/info @@ -121,7 +122,7 @@ export class ZigZag implements ZigZagish { // } public getTokenByAddress(address: string): Token { - return this.tokenList[address.toLowerCase()]; + return this.tokenList[getAddress(address)]; } public async init() { @@ -132,21 +133,19 @@ export class ZigZag implements ZigZagish { if (response.status === 200) { const zigZagData: ZigZagInfo = response.data; for (const token of zigZagData.verifiedTokens) { - this.tokenList[token.address.toLowerCase()] = new Token( + this.tokenList[getAddress(token.address)] = new Token( this._chain.chainId, - token.address.toLowerCase(), + getAddress(token.address), token.decimals, token.symbol, token.name ); } for (const market of zigZagData.markets) { - const base = this.tokenList[market.buyToken]; - const quote = this.tokenList[market.sellToken]; + const base = this.getTokenByAddress(market.buyToken); + const quote = this.getTokenByAddress(market.sellToken); // this.markets.push(base.symbol + '-' + quote.symbol); - this.markets.push( - base.address.toLowerCase() + '-' + quote.address.toLowerCase() - ); + this.markets.push(base.address + '-' + quote.address); } } @@ -284,8 +283,8 @@ export class ZigZag implements ZigZagish { const quoteBuyAmount = ethers.BigNumber.from(order.buyAmount); if (side === 'buy' && quoteSellAmount.lt(stepBuyAmount)) return; - const quoteSellToken = this.tokenList[order.sellToken]; - const quoteBuyToken = this.tokenList[order.buyToken]; + const quoteSellToken = this.getTokenByAddress(order.sellToken); + const quoteBuyToken = this.getTokenByAddress(order.buyToken); if (!quoteSellToken || !quoteBuyToken) return; const quoteSellAmountFormated = Number(