Skip to main content
Open In ColabOpen on GitHub

Compass LangChain Toolkit

The langchain-compass toolkit contains tools that enable an LLM agent to interact with popular DeFi protocols non-custodially. The toolkit is built on top of a Universal DeFi API (Compass API) allowing agents to perform financial operations like:

  • Swapping tokens on Uniswap and Aerodrome
  • Lending or borrowing assets using protocols on Aave
  • Providing liquidity on Aerodrome and Uniswap
  • Transferring funds between wallets.
  • Querying balances, portfolios and monitoring positions.

Overview

Integration details

ClassPackageSerializableJS supportPackage latest
LangchainCompassToolkitlangchain-compassPyPI - Version

Tool features

Here’s a sample of the tools this toolkit provides (subject to change daily):

  • aave_supply_: Supply assets to Aave to earn interest.
  • aave_borrow_: Borrow assets from Aave using collateral.
  • uniswap_swap_sell_exactly_: Swap a specific amount of one token on Uniswap.
  • generic_portfolio_get_: Retrieve a wallet’s portfolio in USD and token balances.
  • generic_transfer_erc20_: Transfer ERC20 tokens to another address.

Setup

At a high-level, we will:

  1. Install the langchain package
  2. Import and instantiate the toolkit
  3. Pass the tools to your agent with toolkit.get_tools()

To enable automated tracing of individual tools, set your LangSmith API key:

# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
# os.environ["LANGSMITH_TRACING"] = "true"

Installation

This toolkit lives in the langchain-compass package:

%pip install -qU langchain-compass
/Users/johncosnett/compass_labs/compass_ai/.venv/bin/python: No module named pip
Note: you may need to restart the kernel to use updated packages.

Environment Setup

To run these examples, ensure LangChain has access to an LLM service. For instance, if you're using GPT-4o, create a .env file containing:

# OPENAI_API_KEY=your_openai_api_key_here

Instantiation

Now we can instantiate our toolkit:

from langchain_compass.toolkits import LangchainCompassToolkit

toolkit = LangchainCompassToolkit(compass_api_key=None)

Tools

View available tools:

tools = toolkit.get_tools()
for tool in tools:
print(tool.name)
aave_supply_
aave_borrow_
aave_repay_
aave_withdraw_
aave_asset_price_get_
aave_liquidity_change_get_
aave_user_position_summary_get_
aave_user_position_per_token_get_
aerodrome_slipstream_swap_sell_exactly_
aerodrome_slipstream_swap_buy_exactly_
aerodrome_slipstream_liquidity_provision_mint_
aerodrome_slipstream_liquidity_provision_increase_
aerodrome_slipstream_liquidity_provision_withdraw_
aerodrome_slipstream_liquidity_provision_positions_get_
aerodrome_slipstream_pool_price_get_
generic_portfolio_get_
generic_visualize_portfolio_get_
generic_price_usd_get_
generic_supported_tokens_get_
generic_balance_get_
generic_allowance_get_
generic_ens_get_
generic_wrap_eth_
generic_unwrap_weth_
generic_transfer_erc20_
generic_transfer_native_token_
generic_allowance_set_
uniswap_swap_buy_exactly_
uniswap_swap_sell_exactly_
uniswap_liquidity_provision_increase_
uniswap_liquidity_provision_mint_
uniswap_liquidity_provision_withdraw_
uniswap_quote_buy_exactly_get_
uniswap_quote_sell_exactly_get_
uniswap_pool_price_get_
uniswap_liquidity_provision_in_range_get_
uniswap_liquidity_provision_positions_get_

Use within an agent

We will need a LLM or chat model:

from langchain_openai import ChatOpenAI
from dotenv import load_dotenv
load_dotenv()

llm = ChatOpenAI(model="gpt-4o")
API Reference:ChatOpenAI

Initialize the agent with the tools:

from langgraph.prebuilt import create_react_agent

tools = toolkit.get_tools()
agent_executor = create_react_agent(llm, tools)
API Reference:create_react_agent

Example usage:

example_query = "what is the balance of vitalic.eth."

events = agent_executor.stream(
{"messages": [("user", example_query)]},
stream_mode="values",
)
for event in events:
event["messages"][-1].pretty_print()
================================ Human Message =================================

what is the balance of vitalic.eth.
================================== Ai Message ==================================
Tool Calls:
generic_ens_get_ (call_kbgxVmdMNzaJa79srcI9wgTf)
Call ID: call_kbgxVmdMNzaJa79srcI9wgTf
Args:
chain: ethereum:mainnet
ens_name: vitalik.eth
================================= Tool Message =================================
Name: generic_ens_get_

wallet_address='0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045' registrant='0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'
================================== Ai Message ==================================
Tool Calls:
generic_portfolio_get_ (call_s5BMj4zrqy3ir1kmfxh7TQB7)
Call ID: call_s5BMj4zrqy3ir1kmfxh7TQB7
Args:
chain: ethereum:mainnet
user: 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045
================================= Tool Message =================================
Name: generic_portfolio_get_

total_value_in_usd='485515.4480892641322350842779' token_balances=[TokenBalance(amount='6.036695054183323219', decimals=18, token_symbol=<Token.field_1INCH: '1INCH'>, token_address='0x111111111117dC0aa78b770fA6A738034120C302', price='0.16903517000000001235804347743396647274494171142578125', token_value_in_usd='1.020413774722037326090352170'), TokenBalance(amount='0.01015207', decimals=18, token_symbol=<Token.AAVE: 'AAVE'>, token_address='0x7Fc66500c84A76Ad7e9c93437bFc5Ac33E2DDaE9', price='137.75825943000000961546902544796466827392578125', token_value_in_usd='1.398531492811520197616914629'), TokenBalance(amount='0.932065903445280347', decimals=18, token_symbol=<Token.BAL: 'BAL'>, token_address='0xba100000625a3754423978a60c9317c58a424e3D', price='1.0338727000000000888491058503859676420688629150390625', token_value_in_usd='0.9636374921729113774230490147'), TokenBalance(amount='0.775486845097093235', decimals=18, token_symbol=<Token.crvUSD: 'crvUSD'>, token_address='0xf939E0A03FB07F59A73314E73794Be0E57ac1b4E', price='0.9999861599999999572929709756863303482532501220703125', token_value_in_usd='0.7754761123591570581108883985'), TokenBalance(amount='317203.872440674356221452', decimals=18, token_symbol=<Token.DAI: 'DAI'>, token_address='0x6B175474E89094C44Da98b954EedeAC495271d0F', price='1.000280229999999992429593476117588579654693603515625', token_value_in_usd='317292.7624818484039949336722'), TokenBalance(amount='1144.036076063671431479', decimals=18, token_symbol=<Token.ENS: 'ENS'>, token_address='0xC18360217D8F7Ab5e7c516566761Ea12Ce7F9D72', price='14.2737870000000004466755854082293808460235595703125', token_value_in_usd='16329.72727004864496192932498'), TokenBalance(amount='1.7777777', decimals=18, token_symbol=<Token.LINK: 'LINK'>, token_address='0x514910771AF9Ca656af840dff83E8264EcF986CA', price='12.349140999999999479541656910441815853118896484375', token_value_in_usd='21.95402748395569907474076388'), TokenBalance(amount='0.000029997485791137', decimals=18, token_symbol=<Token.rsETH: 'rsETH'>, token_address='0xA1290d69c65A6Fe4DF752f95823fae25cB99e5A7', price='1692.762674802640558767553319', token_value_in_usd='0.05077862428515927237735794292'), TokenBalance(amount='0.0000168', decimals=18, token_symbol=<Token.UNI: 'UNI'>, token_address='0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984', price='5.3284966100000001887337930384092032909393310546875', token_value_in_usd='0.00008951874304800000317072772305'), TokenBalance(amount='123223.706565', decimals=6, token_symbol=<Token.USDC: 'USDC'>, token_address='0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', price='0.99992999999999998550492819049395620822906494140625', token_value_in_usd='123215.0809055404482138635247'), TokenBalance(amount='170.147549', decimals=6, token_symbol=<Token.USDT: 'USDT'>, token_address='0xdAC17F958D2ee523a2206206994597C13D831ec7', price='0.99984572999999998810238821533857844769954681396484375', token_value_in_usd='170.1213003376157679756505159'), TokenBalance(amount='0.00107182', decimals=8, token_symbol=<Token.WBTC: 'WBTC'>, token_address='0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599', price='84878.623000000006868503987789154052734375', token_value_in_usd='90.97460570386000736179994419'), TokenBalance(amount='16.395109787715287566', decimals=18, token_symbol=<Token.WETH: 'WETH'>, token_address='0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', price='1731.651628985061878308560486', token_value_in_usd='28390.61857128610975671394299')]
================================== Ai Message ==================================

The portfolio value of the wallet address associated with `vitalik.eth` is approximately $485,515.45 USD. Below is a detailed breakdown of the token balances:

1. **1INCH**: `6.04` tokens valued at approximately `$1.02` USD
2. **AAVE**: `0.0102` tokens valued at approximately `$1.40` USD
3. **BAL**: `0.9321` tokens valued at approximately `$0.96` USD
4. **crvUSD**: `0.7755` tokens valued at approximately `$0.78` USD
5. **DAI**: `317,203.87` tokens valued at approximately `$317,292.76` USD
6. **ENS**: `1,144.04` tokens valued at approximately `$16,329.73` USD
7. **LINK**: `1.78` tokens valued at approximately `$21.95` USD
8. **rsETH**: `0.00003` tokens valued at approximately `$0.05` USD
9. **UNI**: `0.000017` tokens valued at approximately `$0.00009` USD
10. **USDC**: `123,223.71` tokens valued at approximately `$123,215.08` USD
11. **USDT**: `170.15` tokens valued at approximately `$170.12` USD
12. **WBTC**: `0.00107` tokens valued at approximately `$90.97` USD
13. **WETH**: `16.40` tokens valued at approximately `$28,390.62` USD

The total value from all these assets comes to approximately $485,515.45 USD.

Expected output:

The portfolio value of the wallet address associated with `vitalik.eth` is approximately $485,515.45 USD. Below is a detailed breakdown of the token balances:

1. **1INCH**: `6.04` tokens valued at approximately `$1.02` USD
2. **AAVE**: `0.0102` tokens valued at approximately `$1.40` USD
3. **BAL**: `0.9321` tokens valued at approximately `$0.96` USD
4. **crvUSD**: `0.7755` tokens valued at approximately `$0.78` USD
5. **DAI**: `317,203.87` tokens valued at approximately `$317,292.76` USD
6. **ENS**: `1,144.04` tokens valued at approximately `$16,329.73` USD
7. **LINK**: `1.78` tokens valued at approximately `$21.95` USD
8. **rsETH**: `0.00003` tokens valued at approximately `$0.05` USD
9. **UNI**: `0.000017` tokens valued at approximately `$0.00009` USD
10. **USDC**: `123,223.71` tokens valued at approximately `$123,215.08` USD
11. **USDT**: `170.15` tokens valued at approximately `$170.12` USD
12. **WBTC**: `0.00107` tokens valued at approximately `$90.97` USD
13. **WETH**: `16.40` tokens valued at approximately `$28,390.62` USD

The total value from all these assets comes to approximately $485,515.45 USD.

Was this page helpful?