@@ -22,6 +22,7 @@ import { getTokenDecimals } from "./swapUtils.ts";
22
22
import settings from "../core/settings.ts" ;
23
23
import { bs58 } from "@coral-xyz/anchor/dist/cjs/utils/bytes/index.js" ;
24
24
import BigNumber from "bignumber.js" ;
25
+ import { WalletProvider } from "../providers/wallet.ts" ;
25
26
26
27
async function swapToken (
27
28
connection : Connection ,
@@ -141,6 +142,34 @@ Respond with a JSON markdown block containing only the extracted values. Use nul
141
142
142
143
// if we get the token symbol but not the CA, check walet for matching token, and if we have, get the CA for it
143
144
145
+ // get all the tokens in the wallet using the wallet provider
146
+ async function getTokensInWallet ( runtime : IAgentRuntime ) {
147
+ const walletProvider = new WalletProvider (
148
+ new Connection ( "https://api.mainnet-beta.solana.com" ) ,
149
+ new PublicKey ( runtime . getSetting ( "WALLET_PUBLIC_KEY" ) )
150
+ ) ;
151
+ const walletInfo = await walletProvider . fetchPortfolioValue ( runtime ) ;
152
+ const items = walletInfo . items ;
153
+ return items ;
154
+ }
155
+
156
+ // check if the token symbol is in the wallet
157
+ async function checkTokenInWallet ( runtime : IAgentRuntime , tokenSymbol : string ) {
158
+ try {
159
+ const items = await getTokensInWallet ( runtime ) ;
160
+ const token = items . find ( ( item ) => item . symbol === tokenSymbol ) ;
161
+
162
+ if ( token ) {
163
+ return token . address ;
164
+ } else {
165
+ return null ;
166
+ }
167
+ } catch ( error ) {
168
+ console . error ( "Error checking token in wallet:" , error ) ;
169
+ return null ;
170
+ }
171
+ }
172
+
144
173
// swapToken should took CA, not symbol
145
174
146
175
export const executeSwap : Action = {
@@ -193,13 +222,46 @@ export const executeSwap: Action = {
193
222
194
223
// if both contract addresses are set, lets execute the swap
195
224
// TODO: try to resolve CA from symbol based on existing symbol in wallet
196
- if ( ! response . inputTokenCA || ! response . outputTokenCA ) {
197
- console . log ( "No contract addresses provided, skipping swap" ) ;
198
- const responseMsg = {
199
- text : "I need the contract addresses to perform the swap" ,
200
- } ;
201
- callback ?.( responseMsg ) ;
202
- return true ;
225
+ if ( ! response . inputTokenCA && response . inputTokenSymbol ) {
226
+ console . log (
227
+ `Attempting to resolve CA for input token symbol: ${ response . inputTokenSymbol } `
228
+ ) ;
229
+ response . inputTokenCA = await checkTokenInWallet (
230
+ runtime ,
231
+ response . inputTokenSymbol
232
+ ) ;
233
+ if ( response . inputTokenCA ) {
234
+ console . log ( `Resolved inputTokenCA: ${ response . inputTokenCA } ` ) ;
235
+ } else {
236
+ console . log ( "No contract addresses provided, skipping swap" ) ;
237
+ const responseMsg = {
238
+ text : "I need the contract addresses to perform the swap" ,
239
+ } ;
240
+ callback ?.( responseMsg ) ;
241
+ return true ;
242
+ }
243
+ }
244
+
245
+ if ( ! response . outputTokenCA && response . outputTokenSymbol ) {
246
+ console . log (
247
+ `Attempting to resolve CA for output token symbol: ${ response . outputTokenSymbol } `
248
+ ) ;
249
+ response . outputTokenCA = await checkTokenInWallet (
250
+ runtime ,
251
+ response . outputTokenSymbol
252
+ ) ;
253
+ if ( response . outputTokenCA ) {
254
+ console . log (
255
+ `Resolved outputTokenCA: ${ response . outputTokenCA } `
256
+ ) ;
257
+ } else {
258
+ console . log ( "No contract addresses provided, skipping swap" ) ;
259
+ const responseMsg = {
260
+ text : "I need the contract addresses to perform the swap" ,
261
+ } ;
262
+ callback ?.( responseMsg ) ;
263
+ return true ;
264
+ }
203
265
}
204
266
205
267
if ( ! response . amount ) {
0 commit comments