Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/Osmosis Chain/Connector #237

Closed

Conversation

chasevoorhees
Copy link
Contributor

Before submitting this PR, please make sure:

[x ] Your code builds clean without any errors or warnings
[x ] You are using approved title ("feat/", "fix/", "docs/", "refactor/")

A description of the changes proposed in the pull request:
Adds Osmosis (with LP) support as a chain, plus some updates to Cosmos chain.

Cosmos amm.routes.ts models don't match up well with the rest due to inherent differences in their data. This has been handled by allowing certain endpoints to receive Cosmos-specific models switched by chain, eg. AddLiquidityRequest | CosmosAddLiquidityRequest.

Docs for above endpoints are in Swagger now.

Tests performed by the developer:
Jest tests added for all Osmosis functions. Very little patch use, instead a testnet wallet is instantiated and trades/liquidity actions are performed with it.
yarn test:cov is ~80% for Osmosis, improved for Cosmos.

Tips for QA testing:
THIS LINE MUST to be added to HBOT client native_tokens[]
"osmosis": "OSMO",
in hummingbot/core/utils/gateway_config_utils.py

PR has been submitted to HBOT main for that file as well.

@chasevoorhees chasevoorhees changed the title Osmosis Chain/Connector feat/Osmosis Chain/Connector Nov 22, 2023
@chasevoorhees
Copy link
Contributor Author

I'll continue to test/update our swap fee estimation logic and look into the EIP-1559-style base fee RPC query posted in Osmosis' Telegram on 11/11

… changed reduceLiquidity() to return array of amount/base/symbol instead of static # of coins (for pools with >2 token types). also changed swap fees to use sim() by default.
@rapcmia
Copy link
Contributor

rapcmia commented Nov 30, 2023

Hi @chasevoorhees good day
We are trying to setup a wallet we are stuck on this page where it tries to look for kepler on testnet? just checking if this a known issue or can you recommend how we can run testnet?

image

  • when tried to setup mainnet, connected successfully 💹
  • Tried different browsers(chrome and edge) same result

Also when setting up the wallet on hummingbot, we are asked to add the pkey for cosmos, the passphrase we normally use does not work. Where can we get the pkey of osmosis wallet?
image

@nkhrs
Copy link
Contributor

nkhrs commented Dec 1, 2023

Hi @rapcmia, I can answer those for you.

We are trying to setup a wallet we are stuck on this page where it tries to look for kepler on testnet? just checking if this a known issue or can you recommend how we can run testnet?

We replicated the issue with the testnet.osmosis.zone connection - according to the Osmosis Discord there is an upgrade for the osmosis-testnet-5 pending for Friday Dec. 01, so we are waiting to see if that is the source.

Also when setting up the wallet on hummingbot, we are asked to add the pkey for cosmos, the passphrase we normally use does not work. Where can we get the pkey of osmosis wallet?

The Keplr wallet does not export the pkey unless you create an account linked with google (dont ask us why) so we use Leap Wallet to get a pkey. It is also possible to export the seed phrase from Keplr and use it to 'restore wallet' on Leap, then extract the pkey from Leap (if you want to use an existing cosmos wallet made with Keplr).

edit: Also, the exported pkey from Leap has a '0x' at the beginning which needs to be removed.

@fengtality
Copy link
Contributor

@nkhrs Note this PR in which I cleaned up some incorrect dependencies in the yarn.lock file. I think you'll have to resolve conflicts related to it: #246

@chasevoorhees
Copy link
Contributor Author

chasevoorhees commented Dec 7, 2023

Just a heads up:

In case anyone is running the tests right now, there seems to have been some breaking change introduced in the tendermint server within the last 1-2 days... I'm suddenly seeing this error a whole bunch:

image
Error: Invalid string. Length must be a multiple of 4

Presumably related to our version of @cosmjs/tendermint-rpc
This is the version we've been using:
"@cosmjs/tendermint-rpc": "^0.31.3",
And the error is still appearing on the latest version:
"@cosmjs/tendermint-rpc": "^0.32.1",

I'll debug into it a bit to see what's actually happening.

Some other threads with similar issues:
CosmWasm/CosmWasmJS#177
cosmos/cosmjs#1353
cosmos/cosmos-sdk#11997

Opened an issue with @cosmjs as well
cosmos/cosmjs#1517

@rapcmia
Copy link
Contributor

rapcmia commented Dec 7, 2023

Test commit 6b6a0ec43


Setup on client + gateway

  • Successfully connected Osmosis connector using Pkey from leap wallet
  • Run gateway balance
    image
    2023-12-07 19:52:28,046 - 3895 - hummingbot.core.utils.async_utils - ERROR - Unhandled error in background task: No module named 'hummingbot.connector.gateway.amm_lp.gateway_osmosis_amm_lp'
    Traceback (most recent call last):
      File "/Users/rapcomia/github/hummingbot/6685/hummingbot/core/utils/async_utils.py", line 9, in safe_wrapper
        return await c
      File "/Users/rapcomia/github/hummingbot/6685/hummingbot/client/command/gateway_command.py", line 416, in _get_balances
        all_ex_bals = await asyncio.wait_for(
      File "/Users/rapcomia/anaconda3/envs/hummingbot/lib/python3.10/asyncio/tasks.py", line 445, in wait_for
        return fut.result()
      File "/Users/rapcomia/github/hummingbot/6685/hummingbot/client/command/gateway_command.py", line 568, in all_balances_all_exc
        await self.update_exchange(client_config_map)
      File "/Users/rapcomia/github/hummingbot/6685/hummingbot/client/command/gateway_command.py", line 562, in update_exchange
        results = await safe_gather(*tasks)
      File "/Users/rapcomia/github/hummingbot/6685/hummingbot/core/utils/async_utils.py", line 22, in safe_gather
        return await asyncio.gather(*args, **kwargs)
      File "/Users/rapcomia/github/hummingbot/6685/hummingbot/client/command/gateway_command.py", line 523, in update_exchange_balances
        return await self.add_gateway_exchange(exchange_name, client_config_map, **api_keys)
      File "/Users/rapcomia/github/hummingbot/6685/hummingbot/client/command/gateway_command.py", line 501, in add_gateway_exchange
        market = GatewayCommand.connect_markets(exchange, client_config_map, **api_details)
      File "/Users/rapcomia/github/hummingbot/6685/hummingbot/client/command/gateway_command.py", line 462, in connect_markets
        connector_class = get_connector_class(exchange)
      File "/Users/rapcomia/github/hummingbot/6685/hummingbot/client/config/config_helpers.py", line 563, in get_connector_class
        mod = __import__(conn_setting.module_path(),
    ModuleNotFoundError: No module named 'hummingbot.connector.gateway.amm_lp.gateway_osmosis_amm_lp'
    

logs_hummingbot.log

@chasevoorhees
Copy link
Contributor Author

chasevoorhees commented Dec 7, 2023

osmosis-labs/osmojs#71

Small update on my issue note from above - definitely a versioning issue with the tendermint client.

@cosmjs/tendermint-rpc - I suddenly started getting an error after broadcastTx->decodeTx
"Error: Invalid string. Length must be a multiple of 4"

I opened an cosmos/cosmjs#1517 with cosmjs and they told me this:

Osmosis testnet was upgraded to Cosmos SDK 0.47 as far as I can tell. In that case, using the Tendermint34Client won't work anymore. You need the Tendermint37Client. Can you report this to osmojs where those things are defined?

It looks like we need to be able to specify whether we want to use Tendermint34Client or Tendermint37Client when calling getSigningOsmosisClient()

I'm looking into this personally to see if there's an easy fix but wanted to post this as it's a blocking issue for hummingbot's #237

TL;DR: testnet doesn't currently work with the Osmosis connector; you'll need to use mainnet until I update it to manually select the new Tendermint37Client

@chasevoorhees
Copy link
Contributor Author

chasevoorhees commented Dec 7, 2023

@rapcmia

Just pushed an update to fix the new RPC client balances issue. However, I've noticed that the client's 'gateway balance' is only querying for the OSMO balance instead of all tokens returned returned via the getTokens() endpoint - is that expected behavior?

Also, we're still seeing an issue with reading back signed transactions on testnet due to the RPC server update mentioned above. I should have that fixed on our side soon, though.

FYI to run this there are 2 changes required in the client:

1. osmosis needs to be added to the native_tokens array here:

hummingbot/core/utils/gateway_config_utils.py

native_tokens = {
    "osmosis": "OSMO",

2. In the hummingbot client, please place the attached file (placeholder strategy) in this location (unzip first):

hummingbot/connector/gateway/amm_lp/gateway_osmosis_amm_lp.py

gateway_osmosis_amm_lp.zip

…eparated pool types for CL vs SWAP coming back from RPC calls
@rapcmia
Copy link
Contributor

rapcmia commented Dec 11, 2023

Thanks for the update @chasevoorhees

  1. osmosis needs to be added to the native_tokens array here:
  1. In the hummingbot client, please place the attached file (placeholder strategy) in this location (unzip first):
  • yes this works now with gateway balance perhaps we can add to the PR6685?

Pending

  • Received mainnet test fund
    • API tests using CURL
    • Run this connector on uniswap_v3_lp strategy

nkhrs added a commit to pecuniafinance/hummingbot that referenced this pull request Dec 11, 2023
placeholder strategy added by request from gateway PR hummingbot#237
hummingbot/gateway#237
@@ -0,0 +1,85 @@
paths:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please don't override the default docs for these endpoints. currently, the Swagger addition causes duplicate entries in local docs when users go to localhost:8080
Screen Shot 2023-12-12 at 5 38 31 PM

if you want to show users how to use cosmos-specific endpoints, I suggest doing so on the doc page for this in the https://github.com/hummingbot/hummingbot-site repo.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @fengtality, we have a PR planned for the /hummingbot-site repo with pages for osmosis dex, osmosis chain, update to cosmos chain, and the change to index, as described in the contribution guidelines - just havent done the PR yet, its coming up.

@@ -40,6 +42,12 @@ export interface PoolPriceRequest extends NetworkSelectionRequest {
interval: number;
}

export interface CosmosPoolPriceRequest extends NetworkSelectionRequest {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Gateway is intended to be chain-agnostic, I don't think there should be Cosmos-specific interfaces for requests and responses for fetching pool prices, etc. Instead, please extend and modify the existing interfaces and adapt your code to them. These interfaces should cover how users interact with various exchange types within the AMM family.

Current:

export interface PoolPriceRequest extends NetworkSelectionRequest {
  token0: string;
  token1: string;
  fee: string;
  period: number;
  interval: number;
}
export interface CosmosPoolPriceRequest extends NetworkSelectionRequest {
  address: string;
  token0: string;
  token1: string;
}

New:

export interface PoolPriceRequest extends NetworkSelectionRequest {
  token0: string;
  token1: string;
  address?: string
  fee?: string;
  period?: number;
  interval?: number;
}

you can perform downstream validations in the Cosmos code.

token1FinalAmount?: string; // Cosmos only
}

export interface CosmosAddLiquidityResponse {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly please modify the existing addLiquidity response and request interfaces

@@ -309,6 +338,66 @@ export const validateRemoveLiquidityRequest: RequestValidator =
validateMaxPriorityFeePerGas,
]);

export const validateCosmosPriceRequest: RequestValidator = mkRequestValidator([
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the cosmos specific controllers will allow you should remove these validators as well. This folder should only contain generic AMM code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you want to handle the non-required vars then, just validate if not null? Then Uniswap will need to perform not-null checks inside its controllers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There shouldn't be much changes needed to the validators. If you make the params optional as in:

export interface PoolPriceRequest extends NetworkSelectionRequest {
  token0: string;
  token1: string;
  fee?: string;
  period?: number;
  interval?: number;
}

The mkRequestValidator function in the validators in amm.validators.ts will only test the required params:

export const validatePoolPriceRequest: RequestValidator = mkRequestValidator([
  validateConnector,
  validateChain,
  validateNetwork,
  validateToken0,
  validateToken1,
  validateFee,
  validateInterval,
  validatePeriod,
]);

This may cause validation issues for connectors that require certain arguments in this request, such as fee for Uniswap. If that's needed, the Uniswap connector should validate the presence of that argument in its dedicated controllers in its folder.

mainnet:
nodeURL: https://rpc.osmosis.zone/
tokenListType: URL
tokenListSource: https://raw.githubusercontent.com/osmosis-labs/assetlists/main/osmosis-1/osmosis-1.assetlist.json
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I strongly suggest copying this locally into the lists folder and using the FILE list type - see other chains/DEXs for examples. This prevents a whole class of network-related issues that affect users in production.

@fengtality
Copy link
Contributor

@chasevoorhees
Also, we recently merged in an upgrade to PancakeSwapV3 to development that also allows it to be used in the uniswap_v3_lp strategy. Please check if that PR #247 has caused any issues with this one.

@rapcmia
Copy link
Contributor

rapcmia commented Dec 13, 2023

PR update:

  • Setup amm_arb for osmosis, not available ❌
  • Setup uniswap_v3_lp strategy for osmosis LP ✅
    • However when starting the strategy we are stuck on connector not ready.
      image

Steps to reproduce:

  • Setup osmosis wallet on mainnet
  • Setup uniswap_v3_lp strategy then start, observe behavior

logs_test-osmolp.log
test-osmolp.yml.log


Manually open a liquidity position on osmosis exchange to observe the behavior and run API tests using curl. There where no curl scripts available for osmosis so we override the existing scripts and use it. However when adding liquidity we are getting:

curl -s -X POST -k --key $GATEWAY_KEY --cert $GATEWAY_CERT -H "Content-Type: application/json" -d "$(envsubst < ./requests/eth_uniswap_add_liquidity.json)" https://localhost:15888/amm/liquidity/add | jq add liquidity

{
  "address": "$OSMO_ADDRESS",
  "token0": "OSMO",
  "token1": "DAI",
  "amount0": "1",
  "amount1": "3.8",
  "fee": "LOW",
  "lowerPrice": "0.65",
  "upperPrice": "1.17",
  "chain": "osmosis",
  "network": "mainnet",
  "connector": "osmosis",
  "privateKey": "$OSMO_PRIVATE_KEY" # we have to add this on the json file since its returning a missing pkey
}

image

  • We are getting a Trade query failed

curl -s -X POST -k --key $GATEWAY_KEY --cert $GATEWAY_CERT -H "Content-Type: application/json" -d "$(envsubst < ./requests/eth_uniswap_position.json)" https://localhost:15888/amm/liquidity/remove | jq remove liquidity

{
  "address": "$OSMO_ADDRESS",
  "tokenId": 1066,
  "chain": "osmosis",
  "network": "mainnet",
  "connector": "osmosis",
  "privateKey": "$OSMO_PRIVATE_KEY"
}

image

  • Manually open a liquidity position on OSMO/DAI
  • Get the pool id and use it to remove liquidity using curl script however getting the same error Trade query failed
    image
TypeError: Cannot read properties of undefined (reading 'myLiquidity')
    at Osmosis.<anonymous> (/Users/rapcomia/github/gateway/237/dist/src/chains/osmosis/osmosis.js:1071:76)
    at Generator.next (<anonymous>)
    at fulfilled (/Users/rapcomia/github/gateway/237/dist/src/chains/osmosis/osmosis.js:5:58)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
2023-12-13 06:48:55 | error | 	Trade query failed:

Steps to reproduce:

  1. Setup testing using curl scripts
  2. Use existing scripts for LP connectors from curl.sh
  3. Run curl scripts

logs_gateway_app.log.2023-12-13.log

@haroondilshad
Copy link

Any updates here?

@nikspz
Copy link
Contributor

nikspz commented Jan 19, 2024

@roncan I think there's ongoing dev work here.

@chasevoorhees Could you please give a comment here and resolve branch conflicts?

@chasevoorhees
Copy link
Contributor Author

chasevoorhees commented Jan 20, 2024

https://github.com/pecuniafinance/gateway/tree/development

I went ahead and moved back to our development branch (had to merge everything in again anyways).

Are we alright moving the PR to a new one from that branch? Or would you prefer we work within this one?

@nkhrs * will be doing the curl tests etc, but we're all good with yarn test again and have the changes you requested @fengtality

@nikspz
Copy link
Contributor

nikspz commented Jan 22, 2024

Are we alright moving the PR to a new one from that branch? Or would you prefer we work within this one?

You're free to create new PR or making changes on this PR. Please notify here

@chasevoorhees
Copy link
Contributor Author

Creating a new PR via the development branch

@nkhrs
Copy link
Contributor

nkhrs commented Jan 30, 2024

New PR can be found here #277

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants