|
| 1 | +import { |
| 2 | + Action, |
| 3 | + IAgentRuntime, |
| 4 | + Memory, |
| 5 | + HandlerCallback, |
| 6 | + State, |
| 7 | + composeContext, |
| 8 | + ModelClass, |
| 9 | + elizaLogger, |
| 10 | + ActionExample, |
| 11 | + generateObjectDeprecated, |
| 12 | +} from "@elizaos/core"; |
| 13 | + |
| 14 | +import { swapTemplate } from "../templates"; |
| 15 | +import { SendTransactionParams, SwapParams } from "../types"; |
| 16 | +import { |
| 17 | + initWalletProvider, |
| 18 | + WalletProvider, |
| 19 | +} from "../providers/walletProvider"; |
| 20 | +import { validateHoldStationConfig } from "../environment"; |
| 21 | +import { HOLDSTATION_ROUTER_ADDRESS, NATIVE_ADDRESS } from "../constants"; |
| 22 | +import { parseUnits } from "viem"; |
| 23 | + |
| 24 | +export class SwapAction { |
| 25 | + constructor(private walletProvider: WalletProvider) {} |
| 26 | + |
| 27 | + async swap(params: SwapParams): Promise<any> { |
| 28 | + const { items: tokens } = await this.walletProvider.fetchPortfolio(); |
| 29 | + |
| 30 | + if (!params.inputTokenCA && !params.inputTokenSymbol) { |
| 31 | + throw new Error("Input token not provided"); |
| 32 | + } |
| 33 | + |
| 34 | + const filters = tokens.filter( |
| 35 | + (t) => |
| 36 | + t.symbol === params.inputTokenSymbol || |
| 37 | + t.address === params.inputTokenCA, |
| 38 | + ); |
| 39 | + if (filters.length != 1) { |
| 40 | + throw new Error( |
| 41 | + "Multiple tokens or no tokens found with the symbol", |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + // fill in token info |
| 46 | + params.inputTokenCA = filters[0].address; |
| 47 | + params.inputTokenSymbol = filters[0].symbol; |
| 48 | + const decimals = filters[0].decimals ?? 18; |
| 49 | + |
| 50 | + // parse amount out |
| 51 | + const tokenAmount = parseUnits(params.amount.toString(), decimals); |
| 52 | + |
| 53 | + if (!params.outputTokenCA && !params.outputTokenSymbol) { |
| 54 | + throw new Error("Output token not provided"); |
| 55 | + } |
| 56 | + |
| 57 | + if (!params.outputTokenCA || !params.outputTokenSymbol) { |
| 58 | + const tokens = await this.walletProvider.fetchAllTokens(); |
| 59 | + const filters = tokens.filter( |
| 60 | + (t) => |
| 61 | + t.symbol === params.outputTokenSymbol || |
| 62 | + t.address === params.outputTokenCA, |
| 63 | + ); |
| 64 | + if (filters.length != 1) { |
| 65 | + throw new Error( |
| 66 | + "Multiple tokens or no tokens found with the symbol", |
| 67 | + ); |
| 68 | + } |
| 69 | + params.outputTokenCA = filters[0].address; |
| 70 | + params.outputTokenSymbol = filters[0].symbol; |
| 71 | + } |
| 72 | + |
| 73 | + elizaLogger.info("--- Swap params:", params); |
| 74 | + |
| 75 | + // fetch swap tx data |
| 76 | + const walletAddress = this.walletProvider.getAddress(); |
| 77 | + const deadline = Math.floor(Date.now() / 1000) + 10 * 60; |
| 78 | + const swapUrl = `https://swap.hold.so/api/swap?src=${params.inputTokenCA}&dst=${params.outputTokenCA}&amount=${tokenAmount}&receiver=${walletAddress}&deadline=${deadline}`; |
| 79 | + elizaLogger.info("swapUrl:", swapUrl); |
| 80 | + const swapResponse = await fetch(swapUrl); |
| 81 | + const swapData = await swapResponse.json(); |
| 82 | + if (!swapData || swapData.error) { |
| 83 | + elizaLogger.error("Swap error:", swapData); |
| 84 | + throw new Error( |
| 85 | + `Failed to fetch swap: ${swapData?.error || "Unknown error"}`, |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + // generate nonce |
| 90 | + const nonce = await this.walletProvider |
| 91 | + .getPublicClient() |
| 92 | + .getTransactionCount({ |
| 93 | + address: walletAddress, |
| 94 | + }); |
| 95 | + |
| 96 | + const populatedTx: SendTransactionParams = { |
| 97 | + to: HOLDSTATION_ROUTER_ADDRESS, |
| 98 | + data: swapData.tx.data, |
| 99 | + nonce: nonce, |
| 100 | + }; |
| 101 | + |
| 102 | + if ( |
| 103 | + params.inputTokenCA.toLowerCase() !== NATIVE_ADDRESS.toLowerCase() |
| 104 | + ) { |
| 105 | + const allowance = await this.walletProvider.getAllowace( |
| 106 | + params.inputTokenCA, |
| 107 | + walletAddress, |
| 108 | + HOLDSTATION_ROUTER_ADDRESS, |
| 109 | + ); |
| 110 | + if (allowance < tokenAmount) { |
| 111 | + await this.walletProvider.approve( |
| 112 | + HOLDSTATION_ROUTER_ADDRESS, |
| 113 | + params.inputTokenCA, |
| 114 | + tokenAmount, |
| 115 | + ); |
| 116 | + } |
| 117 | + } else { |
| 118 | + populatedTx.value = tokenAmount; |
| 119 | + } |
| 120 | + |
| 121 | + const hash = await this.walletProvider.sendTransaction(populatedTx); |
| 122 | + |
| 123 | + return { |
| 124 | + hash, |
| 125 | + ...params, |
| 126 | + }; |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +export const swapAction: Action = { |
| 131 | + name: "TOKEN_SWAP_BY_HOLDSTATION", |
| 132 | + similes: [ |
| 133 | + "SWAP_TOKEN", |
| 134 | + "SWAP_TOKEN_BY_HOLDSTATION_SWAP", |
| 135 | + "EXCHANGE_TOKENS", |
| 136 | + "EXCHANGE_TOKENS_BY_HOLDSTATION_SWAP", |
| 137 | + "CONVERT_TOKENS", |
| 138 | + "CONVERT_TOKENS_BY_HOLDSTATION_SWAP", |
| 139 | + ], |
| 140 | + validate: async (runtime: IAgentRuntime, _message: Memory) => { |
| 141 | + await validateHoldStationConfig(runtime); |
| 142 | + return true; |
| 143 | + }, |
| 144 | + description: "Perform swapping of tokens on ZKsync by HoldStation swap.", |
| 145 | + handler: async ( |
| 146 | + runtime: IAgentRuntime, |
| 147 | + message: Memory, |
| 148 | + state: State, |
| 149 | + _options: any, |
| 150 | + callback: HandlerCallback, |
| 151 | + ) => { |
| 152 | + elizaLogger.log("Starting HoldStation Wallet TOKEN_SWAP handler..."); |
| 153 | + |
| 154 | + const walletProvider = await initWalletProvider(runtime); |
| 155 | + const action = new SwapAction(walletProvider); |
| 156 | + |
| 157 | + // compose state |
| 158 | + if (!state) { |
| 159 | + state = (await runtime.composeState(message)) as State; |
| 160 | + } else { |
| 161 | + state = await runtime.updateRecentMessageState(state); |
| 162 | + } |
| 163 | + |
| 164 | + // compose swap context |
| 165 | + const swapContext = composeContext({ |
| 166 | + state, |
| 167 | + template: swapTemplate, |
| 168 | + }); |
| 169 | + |
| 170 | + // generate swap content |
| 171 | + const content = await generateObjectDeprecated({ |
| 172 | + runtime, |
| 173 | + context: swapContext, |
| 174 | + modelClass: ModelClass.SMALL, |
| 175 | + }); |
| 176 | + |
| 177 | + elizaLogger.info("generate swap content:", content); |
| 178 | + |
| 179 | + try { |
| 180 | + const { |
| 181 | + hash, |
| 182 | + inputTokenCA, |
| 183 | + inputTokenSymbol, |
| 184 | + outputTokenCA, |
| 185 | + outputTokenSymbol, |
| 186 | + amount, |
| 187 | + } = await action.swap(content); |
| 188 | + |
| 189 | + elizaLogger.success( |
| 190 | + `Swap completed successfully from ${amount} ${inputTokenSymbol} (${inputTokenCA}) to ${outputTokenSymbol} (${outputTokenCA})!\nTransaction Hash: ${hash}`, |
| 191 | + ); |
| 192 | + |
| 193 | + if (callback) { |
| 194 | + callback({ |
| 195 | + text: `Swap completed successfully from ${amount} ${inputTokenSymbol} (${inputTokenCA}) to ${outputTokenSymbol} (${outputTokenCA})!\nTransaction Hash: ${hash}`, |
| 196 | + content: { |
| 197 | + success: true, |
| 198 | + hash: hash, |
| 199 | + }, |
| 200 | + }); |
| 201 | + } |
| 202 | + return true; |
| 203 | + } catch (error) { |
| 204 | + elizaLogger.error("Error during token swap:", error); |
| 205 | + if (callback) { |
| 206 | + callback({ |
| 207 | + text: `Error during token swap: ${error.message}`, |
| 208 | + content: { error: error.message }, |
| 209 | + }); |
| 210 | + } |
| 211 | + return false; |
| 212 | + } |
| 213 | + }, |
| 214 | + examples: [ |
| 215 | + [ |
| 216 | + { |
| 217 | + user: "{{user1}}", |
| 218 | + content: { |
| 219 | + text: "Swap 100 USDC for HOLD", |
| 220 | + }, |
| 221 | + }, |
| 222 | + { |
| 223 | + user: "{{agent}}", |
| 224 | + content: { |
| 225 | + text: "Sure, I'll do swap 100 USDC for HOLD now.", |
| 226 | + action: "TOKEN_SWAP", |
| 227 | + }, |
| 228 | + }, |
| 229 | + { |
| 230 | + user: "{{agent}}", |
| 231 | + content: { |
| 232 | + text: "Swap completed 100 USDC for HOLD successfully! Transaction: ...", |
| 233 | + }, |
| 234 | + }, |
| 235 | + ], |
| 236 | + ] as ActionExample[][], |
| 237 | +}; |
0 commit comments