Skip to content
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

feat(ts): add cross-chain swap action #156

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jlin27
Copy link

@jlin27 jlin27 commented Jan 24, 2025

What changed? Why?

  • Add typescript action cross-chain-swap enable cross-chain swaps with LiFi SDK

How was this tested?

  1. Via typescript chatbot example in cdp-langchain
  2. Asked Agent to make a cross-chain swap for WETH on base mainnet to USDC on optimism
  3. Checked final swapped amounts and the printed transaction hashes

Some excerpts from the chat:

Prompt: can you help me make a cross-chain swap?
I can assist you with a cross-chain transfer. Could you please provide me with the following details:

1. The source chain ID (e.g., for Base mainnet use 8453).
2. The destination chain ID (e.g., for Optimism mainnet use 10).
3. The contract address of the token on the source chain.
4. The contract address of the token on the destination chain.
5. The amount to be transferred (in the smallest unit of the token).

Once I have these details, I can help you proceed with the transfer!
-------------------

Prompt: I want to bridge 0.0001 WETH (0x4200000000000000000000000000000000000006) from my wallet (0x4D2A422dB44144996E855ce15FB581a477dbB947) on base mainnet to USDC (0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85) in my same wallet on optimism (10)

-------------------
Wallet address: 0x4D2A422dB44144996E855ce15FB581a477dbB947
Wallet address type: object
Quote request: {
  "fromChain": 8453,
  "toChain": 10,
  "fromToken": "0x4200000000000000000000000000000000000006",
  "toToken": "0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85",
  "fromAmount": "100000000000000",
  "fromAddress": "0x4D2A422dB44144996E855ce15FB581a477dbB947"
}
{
  fromToken: {
    address: '0x4200000000000000000000000000000000000006',
    chainId: 8453,
    symbol: 'WETH',
    decimals: 18,
    name: 'Wrapped Ether',
    coinKey: 'WETH',
    logoURI: 'https://static.debank.com/image/coin/logo_url/eth/d61441782d4a08a7479d54aea211679e.png',
    priceUSD: '3244.48'
  },
  toToken: {
    address: '0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85',
    chainId: 10,
    symbol: 'USDC',
    decimals: 6,
    name: 'USD Coin',
    coinKey: 'USDC',
    logoURI: 'https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48/logo.png',
    priceUSD: '0.9998000399920016'
  },
  fromAmount: '100000000000000',
  toAmount: '319786',
  id: 'c894b920-c006-4522-97f9-87797b35d827:0',
  fromChainId: 8453,
  toChainId: 10,
  fromAmountUSD: '0.3244',
  toAmountUSD: '0.3197',
  steps: [
    {
      type: 'lifi',
      id: 'c894b920-c006-4522-97f9-87797b35d827:0',
      tool: 'relay',
      toolDetails: [Object],
      action: [Object],
      estimate: [Object],
      includedSteps: [Array],
      integrator: 'cdp-agentkit',
      transactionRequest: [Object],
      execution: [Object]
    }
  ],
  toAmountMin: '318187',
  insurance: { state: 'NOT_INSURABLE', feeAmountUsd: '0' }
}

-------------------
The cross-chain transfer of **0.0001 WETH** from your wallet on Base mainnet to **USDC** on Optimism has been successfully executed. Here are the relevant details:

### Transfer Details:
- **From**: 0.0001 WETH (0x4200000000000000000000000000000000000006)
- **To**: Expected amount of USDC: 319,701 (0.319701 USDC)
- **Transfer Link on Base**: [Base Explorer](https://basescan.org/tx/0xbcd6c10ea34e935b9684b964e8f9aa6cf1457c5b3f54238de84813742690889c)
- **Transfer Link on Optimism**: [Optimism Explorer](https://optimistic.etherscan.io/tx/0xc7a7d012860e2d81c9cb3a609f07de35ff84492dcf84dd09f5a8f73b56a963a3)

### Additional Information:
- **Gas Fees**: Approximately 0.0146 USD worth of ETH was used for gas fees in the transaction.

Feel free to let me know if you need any further assistance!

Qualified Impact

Affected components would include cdp-agentkit-core/typescript and cdp-langchain/examples. If an error occurs, would recommend removing this action.

@cb-heimdall
Copy link

🟡 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 1
Sum 2

@murrlincoln
Copy link

Great, thank you very much for building this! Excited to get this in, going to take a look over the coming days and we'll be in touch shortly

Comment on lines +63 to +73
const walletAddress = await wallet.getDefaultAddress();
const privateKey = walletAddress.export();

console.log("Wallet address:", walletAddress.getId());
console.log("Wallet address type:", typeof walletAddress);

const client = createWalletClient({
account: privateKeyToAccount(privateKey as `0x${string}`),
chain: base,
transport: http("https://base-mainnet.g.alchemy.com/v2/2yW47qv-8qJlPfJEg0lzt3VPSSR51H2P"),
}).extend(publicActions);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution @jlin27 !

We are working on extending AgentKit's wallet support to a more generic interface that will simplify this flow. We anticipate releasing the wallet provider interface in next week's release. I will circle back to this PR once the wallet provider interface is in master.

cc: @murrlincoln

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great to hear about the simplified wallet support coming soon!

Will check in and update once it's out. Cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants