Skip to content

feat:trades via 0x #681

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

feat:trades via 0x #681

wants to merge 1 commit into from

Conversation

phdargen
Copy link
Contributor

Description

Added 0x Protocol support for token swaps on EVM networks

New ZeroXActionProvider:

  • get_swap_price_quote_from_0x: fetches price quote for token swaps
  • execute_swap_on_0x: executes token swaps with automatic permit2 approvals if needed
  • Supports native ETH and ERC20 tokens
  • requires 0x API key: https://0x.org/docs/

Tests

Chatbot: typescript/examples/langchain-cdp-chatbot/chatbot.ts
Network: Base Mainnet

Prompt: get quote for swapping 0.001 ETH (0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee) to USDC (0x833589fcd6edb6e08f4c7c32d4f71b54bda02913) 

-------------------
{"sellAmount":"0.001","sellToken":"0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","buyAmount":"1.589364","minBuyAmount":"1.573522","buyToken":"0x833589fcd6edb6e08f4c7c32d4f71b54bda02913","totalNetworkFeeInETH":"0.000007152843753845","issues":{"allowance":null,"balance":null,"simulationIncomplete":false,"invalidSourcesPassed":[]},"liquidityAvailable":true,"priceOfBuyTokenInSellToken":"0.0006291824906063054","priceOfSellTokenInBuyToken":"1589.364"}
-------------------
The quote for swapping 0.001 ETH to USDC is as follows:

- **Sell Amount**: 0.001 ETH
- **Estimated Buy Amount**: 1.589364 USDC
- **Minimum Buy Amount**: 1.573522 USDC
- **Total Network Fee**: 0.000007152843753845 ETH
- **Price of USDC in ETH**: 0.0006291824906063054 ETH per USDC
- **Price of ETH in USDC**: 1589.364 USDC per ETH

Liquidity is available for this swap. Would you like to proceed with the swap?
-------------------

Prompt: yes swap it

-------------------
{"success":true,"sellAmount":"0.001","sellToken":"0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","buyAmount":"1.590409","minBuyAmount":"1.574577","buyToken":"0x833589fcd6edb6e08f4c7c32d4f71b54bda02913","totalNetworkFeeInETH":"0.0000031379987515","priceOfBuyTokenInSellToken":"0.0006287690776397769","priceOfSellTokenInBuyToken":"1590.4089999999999","permit2ApprovalTxHash":null,"swapTxHash":"0xce83863f396bd893324b61147a875ad1e2ea157e50638a12854aa3b87b2f3acb"}
-------------------
The swap was successful! Here are the details:

- **Sell Amount**: 0.001 ETH
- **Estimated Buy Amount**: 1.590409 USDC
- **Minimum Buy Amount**: 1.574577 USDC
- **Total Network Fee**: 0.0000031379987515 ETH
- **Price of USDC in ETH**: 0.0006287690776397769 ETH per USDC
- **Price of ETH in USDC**: 1590.409 USDC per ETH
- **Transaction Hash**: [0xce83863f396bd893324b61147a875ad1e2ea157e50638a12854aa3b87b2f3acb](https://basescan.org//tx/0xce83863f396bd893324b61147a875ad1e2ea157e50638a12854aa3b87b2f3acb)

If you need anything else, feel free to ask!
-------------------

Prompt: swap 1 USDC to ETH

-------------------
{"success":true,"sellAmount":"1","sellToken":"0x833589fcd6edb6e08f4c7c32d4f71b54bda02913","buyAmount":"0.000626747268589479","minBuyAmount":"0.0006204797958942","buyToken":"0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","totalNetworkFeeInETH":"0.000003327900437011","priceOfBuyTokenInSellToken":"1595.5394624224562","priceOfSellTokenInBuyToken":"0.000626747268589479","permit2ApprovalTxHash":"0x379e20d8d582c5b093c12e645df7fcfdc0052daf398d791cd287e92731581d18","swapTxHash":"0xeddf6ba025ac81de2fe23fcb9856242527b51a001bf51643c40ecad40ef49440"}
-------------------
The swap was successful! Here are the details:

- **Sell Amount**: 1 USDC
- **Estimated Buy Amount**: 0.000626747268589479 ETH
- **Minimum Buy Amount**: 0.0006204797958942 ETH
- **Total Network Fee**: 0.000003327900437011 ETH
- **Price of ETH in USDC**: 1595.5394624224562 USDC per ETH
- **Price of USDC in ETH**: 0.000626747268589479 ETH per USDC
- **Transaction Hash**: [0xeddf6ba025ac81de2fe23fcb9856242527b51a001bf51643c40ecad40ef49440](https://basescan.org//tx/0xeddf6ba025ac81de2fe23fcb9856242527b51a001bf51643c40ecad40ef49440)

Checklist

A couple of things to include in your PR for completeness:

  • Added documentation to all relevant README.md files
  • Added a changelog entry

@phdargen phdargen requested a review from murrlincoln as a code owner April 18, 2025 13:19
@cb-heimdall
Copy link

cb-heimdall commented Apr 18, 2025

🟡 Heimdall Review Status

Requirement Status More Info
Reviews 🟡 0/1
Denominator calculation
Show calculation
1 if user is bot 0
1 if user is external 0
From .codeflow.yml 1
Additional review requirements
Show calculation
Max 0
0
From CODEOWNERS 0
Global minimum 0
Max 1
1
1 if commit is unverified 0
Sum 1

@github-actions github-actions bot added documentation Improvements or additions to documentation action provider New action provider example New example agent typescript labels Apr 18, 2025
@phdargen phdargen changed the title feat:swaps via 0x feat:trades via 0x Apr 18, 2025
@github-actions github-actions bot removed the example New example agent label Apr 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
action provider New action provider documentation Improvements or additions to documentation typescript
Development

Successfully merging this pull request may close these issues.

2 participants