@@ -17,13 +17,46 @@ import {
17
17
18
18
import { getStarknetAccount , validateSettings } from "../utils/index.ts" ;
19
19
20
+ interface SwapContent {
21
+ sellTokenAddress : string ;
22
+ buyTokenAddress : string ;
23
+ sellAmount : string ;
24
+ }
25
+
26
+ export function isSwapContent ( content : SwapContent ) : content is SwapContent {
27
+ // Validate types
28
+ const validTypes =
29
+ typeof content . sellTokenAddress === "string" &&
30
+ typeof content . buyTokenAddress === "string" &&
31
+ typeof content . sellAmount === "string" ;
32
+ if ( ! validTypes ) {
33
+ return false ;
34
+ }
35
+
36
+ // Validate addresses (must be 32-bytes long with 0x prefix)
37
+ const validAddresses =
38
+ content . sellTokenAddress . startsWith ( "0x" ) &&
39
+ content . sellTokenAddress . length === 66 &&
40
+ content . buyTokenAddress . startsWith ( "0x" ) &&
41
+ content . buyTokenAddress . length === 66 ;
42
+
43
+ return validAddresses ;
44
+ }
45
+
20
46
const swapTemplate = `Respond with a JSON markdown block containing only the extracted values. Use null for any values that cannot be determined.
21
47
48
+ These are known addresses you will get asked to swap, use these addresses for sellTokenAddress and buyTokenAddress:
49
+ - BROTHER/brother/$brother: 0x03b405a98c9e795d427fe82cdeeeed803f221b52471e3a757574a2b4180793ee
50
+ - BTC/btc: 0x03fe2b97c1fd336e750087d68b9b867997fd64a2661ff3ca5a7c771641e8e7ac
51
+ - ETH/eth: 0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7
52
+ - STRK/strk: 0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d
53
+ - LORDS/lords: 0x0124aeb495b947201f5fac96fd1138e326ad86195b98df6dec9009158a533b49
54
+
22
55
Example response:
23
56
\`\`\`json
24
57
{
25
58
"sellTokenAddress": "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
26
- "buyTokenAddress": "0x053c91253bc9682c04929ca02ed00b3e423f6710d2ee7e0d5ebb06f3ecf368a8 ",
59
+ "buyTokenAddress": "0x124aeb495b947201f5fac96fd1138e326ad86195b98df6dec9009158a533b49 ",
27
60
"sellAmount": "1000000000000000000"
28
61
}
29
62
\`\`\`
@@ -48,7 +81,8 @@ export const executeSwap: Action = {
48
81
validate : async ( runtime : IAgentRuntime , message : Memory ) => {
49
82
return validateSettings ( runtime ) ;
50
83
} ,
51
- description : "Perform a token swap using Avnu." ,
84
+ description :
85
+ "Perform a token swap on starknet. Use this action when a user asks you to swap tokens anything." ,
52
86
handler : async (
53
87
runtime : IAgentRuntime ,
54
88
message : Memory ,
@@ -75,6 +109,11 @@ export const executeSwap: Action = {
75
109
76
110
console . log ( "Response:" , response ) ;
77
111
112
+ if ( ! isSwapContent ( response ) ) {
113
+ callback ?.( { text : "Invalid swap content, please try again." } ) ;
114
+ return false ;
115
+ }
116
+
78
117
try {
79
118
// Get quote
80
119
const quoteParams : QuoteRequest = {
@@ -105,7 +144,7 @@ export const executeSwap: Action = {
105
144
return true ;
106
145
} catch ( error ) {
107
146
console . error ( "Error during token swap:" , error ) ;
108
- callback ?.( { text : `Error during swap: ${ error . message } ` } ) ;
147
+ callback ?.( { text : `Error during swap:` } ) ;
109
148
return false ;
110
149
}
111
150
} ,
@@ -114,19 +153,41 @@ export const executeSwap: Action = {
114
153
{
115
154
user : "{{user1}}" ,
116
155
content : {
117
- text : "Swap 1 ETH for USDC on Starknet" ,
156
+ text : "Swap 10 ETH for LORDS" ,
157
+ } ,
158
+ } ,
159
+ {
160
+ user : "{{agent}}" ,
161
+ content : {
162
+ text : "Ok, I'll swap 10 ETH for LORDS" ,
163
+ } ,
164
+ } ,
165
+ ] ,
166
+ [
167
+ {
168
+ user : "{{user1}}" ,
169
+ content : {
170
+ text : "Swap 100 $lords on starknet" ,
118
171
} ,
119
172
} ,
120
173
{
121
- user : "{{user2}}" ,
174
+ user : "{{agent}}" ,
175
+ content : {
176
+ text : "Ok, I'll swap 100 $lords on starknet" ,
177
+ } ,
178
+ } ,
179
+ ] ,
180
+ [
181
+ {
182
+ user : "{{user1}}" ,
122
183
content : {
123
- text : "Buy LORDS on Starknet " ,
184
+ text : "Swap 0.5 BTC for LORDS " ,
124
185
} ,
125
186
} ,
126
187
{
127
- user : "{{user2 }}" ,
188
+ user : "{{agent }}" ,
128
189
content : {
129
- text : "Executing swap... " ,
190
+ text : "Ok, I'll swap 0.5 BTC for LORDS " ,
130
191
} ,
131
192
} ,
132
193
] ,
0 commit comments