hummingbot / gateway

Middleware that standardizes DEX API endpoints on different blockchain networks
Apache License 2.0
74 stars 130 forks source link

The this.ethereum of uniswapLP is not initialized. #228

Open johna1203 opened 11 months ago

johna1203 commented 11 months ago

When I use uniswapLP in a script, this.ethereum.storedTokenList is always empty. I researched the reason and found a possibility of a bug.

It appears that this.ethereum.init() is not being called, meaning it is not being initialized. This prevents it from loading tokens.

I changed 3 lines in uniswap.lp.helper.ts and it worked for me.

Steps To Reproduce

1, create a Script with markets = {"uniswapLP_polygon_mainnet": "XXXX-XXX"} 2, then, call the await self.connectors["uniswapLP_polygon_mainnet"].get_price(trading_pair, fee_tier)

Screenshots

uniswap.lp.helper.ts

  public getTokenByAddress(address: string): Token {
    console.log('getTokenByAddress', {
      'this.tokenList': this.tokenList,
    });

    return this.tokenList[getAddress(address)];
  }

  public async init() {
    if (this._chain == 'ethereum' && !this.ethereum.ready())
      throw new InitializationError(
        SERVICE_UNITIALIZED_ERROR_MESSAGE('ETH'),
        SERVICE_UNITIALIZED_ERROR_CODE
      );
    for (const token of this.ethereum.storedTokenList) {
      this.tokenList[token.address] = new Token(
        this.chainId,
        token.address,
        token.decimals,
        token.symbol,
        token.name
      );
    }
    this._ready = true;
  }

image

Release version { "name": "hummingbot-gateway", "version": "1.21.0", }

uniswap.lp.helper.ts

Here is a solution that is working for me. I am calling await this.ethereum.init(); if ethereum is not ready.

  public async init() {
    if (!this.ethereum.ready()) {
      await this.ethereum.init();
    }

    if (this._chain == 'ethereum' && !this.ethereum.ready())
      throw new InitializationError(
        SERVICE_UNITIALIZED_ERROR_MESSAGE('ETH'),
        SERVICE_UNITIALIZED_ERROR_CODE
      );
    for (const token of this.ethereum.storedTokenList) {
      this.tokenList[token.address] = new Token(
        this.chainId,
        token.address,
        token.decimals,
        token.symbol,
        token.name
      );
    }
    this._ready = true;
  }

Optional: your discord username:johna1203

johna1203 commented 11 months ago

Hi, The solution I wrote above is not correct. I am still researching to see if I can find the problem.

johna1203 commented 11 months ago

The problem is similar to this issue. It seems that the UniswapLPHelper is not creating the correct object when the chain is Polygon. https://github.com/hummingbot/gateway/blob/main/src/connectors/uniswap/uniswap.ts#L61

my solution is check the chain and create a correct Object for this.ethereum

uniswap.lp.helper.ts

  constructor(chain: string, network: string) {
    if (chain === 'ethereum') {
      this.ethereum = Ethereum.getInstance(network);
    } else if (chain === 'polygon') {
      this.ethereum = Polygon.getInstance(network);
    } else {
      throw new Error('Chain not supported');
    }
emusol commented 11 months ago

The problem is similar to this issue. It seems that the UniswapLPHelper is not creating the correct object when the chain is Polygon. https://github.com/hummingbot/gateway/blob/main/src/connectors/uniswap/uniswap.ts#L61

my solution is check the chain and create a correct Object for this.ethereum

uniswap.lp.helper.ts

  constructor(chain: string, network: string) {
    if (chain === 'ethereum') {
      this.ethereum = Ethereum.getInstance(network);
    } else if (chain === 'polygon') {
      this.ethereum = Polygon.getInstance(network);
    } else {
      throw new Error('Chain not supported');
    }

Hey, does the solution works for you?

vic-en commented 11 months ago

@johna1203 @emusol what chain and network are you trying to connect to?

emusol commented 11 months ago

@johna1203 @emusol what chain and network are you trying to connect to?

hey the error is when I try to work with polygon, on arbitrum it work perfectly

vic-en commented 11 months ago

@emusol I see that there has been some refactoring to some evm networks.

So reconnect to uniswapLP using ethereum as chain and polygon as network.

emusol commented 11 months ago

image Hey thats what I get when i try this way

@emusol I see that there has been some refactoring to some evm networks.

So reconnect to uniswapLP using ethereum as chain and polygon as network.

johna1203 commented 11 months ago

@vic-en

@johna1203 @emusol what chain and network are you trying to connect to?

Sorry for the delay, I was trying to connect to Polygon.

https://github.com/hummingbot/gateway/issues/228#issuecomment-1799353579

The code I wrote above worked, I had to add a check for Polygon.

johna1203 commented 11 months ago

image Hey thats what I get when i try this way

@emusol I see that there has been some refactoring to some evm networks. So reconnect to uniswapLP using ethereum as chain and polygon as network.

This error does not happen to me. What version of the gateway are you using?

johna1203 commented 11 months ago

image Hey thats what I get when i try this way

@emusol I see that there has been some refactoring to some evm networks. So reconnect to uniswapLP using ethereum as chain and polygon as network.

This error does not happen to me. What version of the gateway are you using?

In fact, it is not an error... you are choosing the wrong network In the question "Which chain do you want uniswapLP...." you need to choose polygon instead of ethereum.

image