Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
OurBuilding Ambient Agents with LangGraph course is now available on LangChain Academy!
Open In ColabOpen on GitHub

Compass LangChain Toolkit

Interact with various DeFi protocols. Non-custodial.Tools returnunsigned 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 orborrowing assets using protocols on Aave
  • Providing liquidity on Aerodrome and Uniswap
  • Transferring funds between wallets.
  • Querying balances, portfolios andmonitoring 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 between addresses.

Setup

Here we will:

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

Installation

This toolkit lives in thelangchain-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.toolkitsimport LangchainCompassToolkit

toolkit= LangchainCompassToolkit(compass_api_key=None)

Tools

Viewavailable tools:

tools= toolkit.get_tools()
for toolin tools:
print(tool.name)
# Expected output:

aave_supply
aave_borrow
aave_repay
aave_withdraw
aave_asset_price_get
aave_liquidity_change_get
aave_user_position_summary_get
...

Invocation

To invoke a single tool programmatically:

tool_name="generic_ens_get"
tool=next(toolfor toolin toolsif tool.name== tool_name)
tool.invoke({"ens_name":"vitalik.eth","chain":"ethereum:mainnet"})
EnsNameInfoResponse(wallet_address='0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045', registrant='0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045')

Use within an agent

We will need a LLM or chat model:

from dotenvimport load_dotenv
from langchain_openaiimport ChatOpenAI

load_dotenv()

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

Initialize the agent with the tools:

from langgraph.prebuiltimport create_react_agent

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

Example usage:

example_query="please set an allowance on Uniswap of 10 WETH for vitalic.eth."# spelt wrong intentionally

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

Expected output:

================================ Human Message =================================

please set an allowance on Uniswap of 10 WETH for vitalic.eth.
================================== Ai Message ==================================
Tool Calls:
generic_ens_get (call_MHIXRXxWH0L7iUEYHwvDUdU1)
Call ID: call_MHIXRXxWH0L7iUEYHwvDUdU1
Args:
chain: ethereum:mainnet
ens_name: vitalic.eth
================================= Tool Message =================================
Name: generic_ens_get

wallet_address='0x44761Ef63FaD902D8f8dC77e559Ab116929881Db' registrant='0x44761Ef63FaD902D8f8dC77e559Ab116929881Db'
================================== Ai Message ==================================
Tool Calls:
generic_allowance_set (call_IEBftbtBfKCkI1zFXXtEY8tq)
Call ID: call_IEBftbtBfKCkI1zFXXtEY8tq
Args:
amount: 10
chain: ethereum:mainnet
contract_name: UniswapV3Router
sender: 0x44761Ef63FaD902D8f8dC77e559Ab116929881Db
token: WETH
================================= Tool Message =================================
Name: generic_allowance_set

{"type": "unsigned_transaction", "content": {"chainId": 1, "data": "0x095ea7b300000000000000000000000068b3465833fb72a70ecdf485e0e4c7bd8665fc450000000000000000000000000000000000000000000000008ac7230489e80000", "from": "0x44761Ef63FaD902D8f8dC77e559Ab116929881Db", "gas": 46434, "to": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", "value": 0, "nonce": 79, "maxFeePerGas": 2265376912, "maxPriorityFeePerGas": 6400594}}

API reference

langchain-compass is built on top of the Compass API. Each tool corresponds to an API endpoint.Please check out the docs here

Related


[8]ページ先頭

©2009-2025 Movatter.jp