Skip to content

Commit fe0410e

Browse files
committed
add getTokensInWallet and add the logic to resolve CA from symbol based on existing symbol in wallet
Signed-off-by: MarcoMandar <malicemandar@gmail.com>
1 parent 50f3a4d commit fe0410e

File tree

1 file changed

+69
-7
lines changed

1 file changed

+69
-7
lines changed

core/src/actions/swap.ts

+69-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { getTokenDecimals } from "./swapUtils.ts";
2222
import settings from "../core/settings.ts";
2323
import { bs58 } from "@coral-xyz/anchor/dist/cjs/utils/bytes/index.js";
2424
import BigNumber from "bignumber.js";
25+
import { WalletProvider } from "../providers/wallet.ts";
2526

2627
async function swapToken(
2728
connection: Connection,
@@ -141,6 +142,34 @@ Respond with a JSON markdown block containing only the extracted values. Use nul
141142

142143
// if we get the token symbol but not the CA, check walet for matching token, and if we have, get the CA for it
143144

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+
144173
// swapToken should took CA, not symbol
145174

146175
export const executeSwap: Action = {
@@ -193,13 +222,46 @@ export const executeSwap: Action = {
193222

194223
// if both contract addresses are set, lets execute the swap
195224
// 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+
}
203265
}
204266

205267
if (!response.amount) {

0 commit comments

Comments
 (0)