From bffb9465b913d26275f1728120c6e56cc51f32a5 Mon Sep 17 00:00:00 2001 From: cygaar Date: Tue, 24 Dec 2024 16:47:15 -0500 Subject: [PATCH] Add erc20 support --- .../plugin-abstract/src/actions/transfer.ts | 46 +++++++++++++++---- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/packages/plugin-abstract/src/actions/transfer.ts b/packages/plugin-abstract/src/actions/transfer.ts index bf877bbed4..24725815e7 100644 --- a/packages/plugin-abstract/src/actions/transfer.ts +++ b/packages/plugin-abstract/src/actions/transfer.ts @@ -13,7 +13,7 @@ import { } from "@elizaos/core"; import { validateAbstractConfig } from "../environment"; -import { Address, createWalletClient, http, parseEther } from "viem"; +import { Address, createWalletClient, erc20Abi, http, parseEther } from "viem"; import { abstractTestnet } from "viem/chains"; import { privateKeyToAccount } from "viem/accounts"; import { eip712WalletActions } from "viem/zksync"; @@ -78,6 +78,14 @@ Given the recent messages, extract the following information about the requested Respond with a JSON markdown block containing only the extracted values.`; +const ETH_ADDRESS = "0x000000000000000000000000000000000000800A"; +const ERC20_OVERRIDE_INFO = { + "0xe4c7fbb0a626ed208021ccaba6be1566905e2dfc": { + name: "USDC", + decimals: 6, + }, +}; + export default { name: "SEND_TOKEN", similes: [ @@ -147,13 +155,35 @@ export default { transport: http(), }).extend(eip712WalletActions()); - const hash = await walletClient.sendTransaction({ - account: account, - chain: abstractTestnet, - to: content.recipient as Address, - value: parseEther(content.amount.toString()), - kzg: undefined, - }); + let hash; + if ( + content.tokenAddress.toLowerCase() !== ETH_ADDRESS.toLowerCase() + ) { + // Convert amount to proper token decimals + const tokenInfo = + ERC20_OVERRIDE_INFO[content.tokenAddress.toLowerCase()]; + const decimals = tokenInfo?.decimals ?? 18; // Default to 18 decimals if not specified + const tokenAmount = + BigInt(content.amount) * BigInt(10 ** decimals); + + // Execute ERC20 transfer + hash = await walletClient.writeContract({ + account, + chain: abstractTestnet, + address: content.tokenAddress as Address, + abi: erc20Abi, + functionName: "transfer", + args: [content.recipient as Address, tokenAmount], + }); + } else { + hash = await walletClient.sendTransaction({ + account: account, + chain: abstractTestnet, + to: content.recipient as Address, + value: parseEther(content.amount.toString()), + kzg: undefined, + }); + } elizaLogger.success( "Transfer completed successfully! Transaction hash: " + hash