Compass LangChain Toolkit
The langchain-compass
toolkit contains tools that enable an LLM agent to interact with popular DeFi protocols non-custodially i.e. tools return unsigned transactions. 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
Class | Package | Serializable | JS support | Package latest |
---|---|---|---|---|
LangchainCompassToolkit | langchain-compass | ❌ | ❌ |
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 between addresses.
Setup
Here we will:
- Install the langchain package
- Import and instantiate the toolkit
- Pass the tools to your agent with
toolkit.get_tools()
Installation
This toolkit lives in the langchain-compass
package:
%pip install -qU langchain-compass
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:
# .env file
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)
Use within an agent
We will need a LLM or chat model:
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
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." # spelt wrong intentionally
events = agent_executor.stream(
{"messages": [("user", example_query)]},
stream_mode="values",
)
for event in events:
event["messages"][-1].pretty_print()
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.
Related
- Tool conceptual guide
- Tool how-to guides