Skip to main content

Langchain extension for BNB Chain

Project description

BNB-chain-agentkit

This is a Langchain extension for BNB Chain.

This toolkit equips LLM agents with the ability to interact with BNB Chain and execute on-chain operations, including getting balances, transferring tokens, swapping tokens, staking, bridging, and deploying different types of ERC tokens.

Setup

Prerequisites

  • Python 3.12+

Installation

pip install bnb-chain-agentkit

Environment Variables

cp .env.example .env
  • PRIVATE_KEY: Your private key for the BNB Chain account.
  • BSC_PROVIDER_URL: The URL of the BNB Smart Chain provider.
  • OPBNB_PROVIDER_URL: The URL of the Optimism BNB Smart Chain provider.

You can set your own RPC endpoint or use a public one listed here: https://docs.bnbchain.org/bnb-smart-chain/developers/json_rpc/json-rpc-endpoint/

Usage

Basic Setup

from bnb_chain_agentkit.agent_toolkits import BnbChainToolkit
from bnb_chain_agentkit.utils import BnbChainAPIWrapper

# Configure BNB Chain Langchain Extension.
bnb_chain = BnbChainAPIWrapper()

# Initialize BNB Chain Toolkit and get tools.
bnb_chain_toolkit = BnbChainToolkit.from_bnb_chain_api_wrapper(bnb_chain)

View available tools:

tools = bnb_chain_toolkit.get_tools()
print('Supported tools:')
for tool in tools:
    print(tool.name)

The toolkit provides the following tools:

  1. get_balance - Get balance for a specific token of given account
  2. transfer - Transfer tokens to a specific address
  3. faucet - Request test tokens from faucet
  4. swap - Swap tokens on PancakeSwap
  5. stake - Stake BNB to ListaDao
  6. bridge - Bridge tokens between BSC and opBNB
  7. deploy - Deploy a new ERC20/ERC7721/ERC1155 token

Using with an Agent

from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent

# Initialize LLM
llm = ChatOpenAI(model="gpt-4o-mini")

# Get tools and create agent
tools = bnb_chain_toolkit.get_tools()
agent_executor = create_react_agent(llm, tools)

# Example usage
events = agent_executor.stream(
    {"messages": [("user", "Check BNB balance of 0x1234")]},
    stream_mode="values"
)

for event in events:
    event["messages"][-1].pretty_print()

Expected output:

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

Check my BNB balance
================================== Ai Message ==================================
Tool Calls:
get_balance (call_w4HfhOLX9d5lH7emf1QKQCF4)
Call ID: call_w4HfhOLX9d5lH7emf1QKQCF4
Args:
    account: 0x1234
    token: BNB
================================= Tool Message =================================
Name: get_balance

Balances for account 0x1234 of BNB:
6145615640500000000 (decimals: 18)
================================== Ai Message ==================================

Your BNB balance is 6.1456156405 BNB.

Please refer to examples/chatbot for more detailed usage.

Credits

Special thanks to CDP (Coinbase Developer Platform). The architecture and implementation patterns of this toolkit were inspired by their excellent work in building AI agent.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bnb_chain_agentkit-0.0.1.tar.gz (135.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bnb_chain_agentkit-0.0.1-py3-none-any.whl (82.6 kB view details)

Uploaded Python 3

File details

Details for the file bnb_chain_agentkit-0.0.1.tar.gz.

File metadata

  • Download URL: bnb_chain_agentkit-0.0.1.tar.gz
  • Upload date:
  • Size: 135.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.0

File hashes

Hashes for bnb_chain_agentkit-0.0.1.tar.gz
Algorithm Hash digest
SHA256 d76bad7d1a7b3225487d4f7e9bdc6e192fbf81bfcb3b37265fc39404948d48b2
MD5 24f17b0865d8ad853e5f471c37c89537
BLAKE2b-256 19d5e7abbcf0973a2f07e5ea8abc47f2638c8a2423a492fe5050f6ad5add5f4c

See more details on using hashes here.

File details

Details for the file bnb_chain_agentkit-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for bnb_chain_agentkit-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e2d94eb51394a1f00b30a67ab7cc4928a5bc0dc920ce6b31234620dbfd89f2a7
MD5 7b34b73935181c4d104230257cdc143b
BLAKE2b-256 6df694271cf2d00053bb0a1327f2ddbbbccbfec86383f0a2a693e8bedef9b632

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page