|
| 1 | +import { |
| 2 | + type Action, |
| 3 | + type ActionExample, |
| 4 | + type IAgentRuntime, |
| 5 | + type Memory, |
| 6 | + type State, |
| 7 | + type HandlerCallback, |
| 8 | + composeContext, |
| 9 | + elizaLogger, |
| 10 | +} from "@elizaos/core"; |
| 11 | +import { accountSummaryTemplate } from "../templates"; |
| 12 | +import { ethers } from "ethers"; |
| 13 | +import { |
| 14 | + generateNonce, |
| 15 | + generateJwt, |
| 16 | + getSubaccount, |
| 17 | + getEndpoint, |
| 18 | + formatNumber, |
| 19 | +} from "../services/utils"; |
| 20 | +import { getSubaccountSummary } from "../services/account"; |
| 21 | + |
| 22 | +export const accountSummary: Action = { |
| 23 | + name: "GET_PERP_ACCOUNT_SUMMARY", |
| 24 | + similes: [ |
| 25 | + "CHECK_ACCOUNT", |
| 26 | + "CHECK_PERP_ACCOUNT", |
| 27 | + "ACCOUNT_SUMMARY", |
| 28 | + "PERP_ACCOUNT_SUMMARY", |
| 29 | + ], |
| 30 | + description: "Get the current account summary", |
| 31 | + validate: async (runtime: IAgentRuntime) => { |
| 32 | + return !!( |
| 33 | + runtime.getSetting("DESK_EXCHANGE_PRIVATE_KEY") && |
| 34 | + runtime.getSetting("DESK_EXCHANGE_NETWORK") |
| 35 | + ); |
| 36 | + }, |
| 37 | + handler: async ( |
| 38 | + runtime: IAgentRuntime, |
| 39 | + message: Memory, |
| 40 | + state: State, |
| 41 | + options: Record<string, unknown>, |
| 42 | + callback?: HandlerCallback |
| 43 | + ) => { |
| 44 | + // Initialize or update state |
| 45 | + state = !state |
| 46 | + ? await runtime.composeState(message) |
| 47 | + : await runtime.updateRecentMessageState(state); |
| 48 | + |
| 49 | + const context = composeContext({ |
| 50 | + state, |
| 51 | + template: accountSummaryTemplate, |
| 52 | + }); |
| 53 | + |
| 54 | + try { |
| 55 | + const endpoint = getEndpoint(runtime); |
| 56 | + const wallet = new ethers.Wallet( |
| 57 | + runtime.getSetting("DESK_EXCHANGE_PRIVATE_KEY") |
| 58 | + ); |
| 59 | + const jwt = await generateJwt(endpoint, wallet, 0, generateNonce()); |
| 60 | + |
| 61 | + const response = await getSubaccountSummary( |
| 62 | + endpoint, |
| 63 | + jwt, |
| 64 | + getSubaccount(wallet.address, 0) |
| 65 | + ); |
| 66 | + elizaLogger.info(response.data); |
| 67 | + |
| 68 | + const subaccountSummaryData = response.data.data; |
| 69 | + const positionSummary = |
| 70 | + subaccountSummaryData.positions.length > 0 |
| 71 | + ? subaccountSummaryData.positions |
| 72 | + .map((p) => { |
| 73 | + return `- ${p.side} ${formatNumber(p.quantity)} ${ |
| 74 | + p.symbol |
| 75 | + }`; |
| 76 | + }) |
| 77 | + .join("\n") |
| 78 | + : "- No active position"; |
| 79 | + const orderSummary = |
| 80 | + subaccountSummaryData.open_orders.length > 0 |
| 81 | + ? subaccountSummaryData.open_orders |
| 82 | + .map((o) => { |
| 83 | + return `- ${ |
| 84 | + o.side === "Long" ? "Buy" : "Sell" |
| 85 | + } ${formatNumber( |
| 86 | + Number(o.original_quantity) - |
| 87 | + Number(o.remaining_quantity) |
| 88 | + )}/${formatNumber(o.original_quantity)} ${ |
| 89 | + o.symbol |
| 90 | + } @${ |
| 91 | + Number(o.price) > 0 |
| 92 | + ? formatNumber(o.price) |
| 93 | + : formatNumber(o.trigger_price) |
| 94 | + }`; |
| 95 | + }) |
| 96 | + .join("\n") |
| 97 | + : "- No orders"; |
| 98 | + const collateralSummary = |
| 99 | + subaccountSummaryData.collaterals.length > 0 |
| 100 | + ? subaccountSummaryData.collaterals |
| 101 | + .map((c) => { |
| 102 | + return `- ${formatNumber(c.amount, 4)} ${ |
| 103 | + c.asset |
| 104 | + }`; |
| 105 | + }) |
| 106 | + .join("\n") |
| 107 | + : "- No collateral"; |
| 108 | + callback({ |
| 109 | + text: |
| 110 | + `Here is the summary of your account ${wallet.address}\n` + |
| 111 | + `Your positions:\n` + |
| 112 | + positionSummary + |
| 113 | + `\n` + |
| 114 | + `Your orders:\n` + |
| 115 | + orderSummary + |
| 116 | + `\n` + |
| 117 | + `Your collaterals:\n` + |
| 118 | + collateralSummary, |
| 119 | + content: subaccountSummaryData, |
| 120 | + }); |
| 121 | + |
| 122 | + return true; |
| 123 | + } catch (error) { |
| 124 | + elizaLogger.error("Error getting account summary:", { |
| 125 | + message: error.message, |
| 126 | + code: error.code, |
| 127 | + data: error.response?.data, |
| 128 | + }); |
| 129 | + if (callback) { |
| 130 | + callback({ |
| 131 | + text: `Error getting account summary: ${error.message} ${error.response?.data?.errors}`, |
| 132 | + content: { error: error.message }, |
| 133 | + }); |
| 134 | + } |
| 135 | + return false; |
| 136 | + } |
| 137 | + }, |
| 138 | + examples: [ |
| 139 | + [ |
| 140 | + { |
| 141 | + user: "{{user1}}", |
| 142 | + content: { |
| 143 | + text: "Check my account please", |
| 144 | + }, |
| 145 | + }, |
| 146 | + { |
| 147 | + user: "{{agent}}", |
| 148 | + content: { |
| 149 | + text: "Here is the summary of your account", |
| 150 | + action: "GET_PERP_ACCOUNT_SUMMARY", |
| 151 | + }, |
| 152 | + }, |
| 153 | + ], |
| 154 | + [ |
| 155 | + { |
| 156 | + user: "{{user1}}", |
| 157 | + content: { |
| 158 | + text: "How is my account doing?", |
| 159 | + }, |
| 160 | + }, |
| 161 | + { |
| 162 | + user: "{{agent}}", |
| 163 | + content: { |
| 164 | + text: "Here is the summary of your account", |
| 165 | + action: "GET_PERP_ACCOUNT_SUMMARY", |
| 166 | + }, |
| 167 | + }, |
| 168 | + ], |
| 169 | + [ |
| 170 | + { |
| 171 | + user: "{{user1}}", |
| 172 | + content: { |
| 173 | + text: "Account summary", |
| 174 | + }, |
| 175 | + }, |
| 176 | + { |
| 177 | + user: "{{agent}}", |
| 178 | + content: { |
| 179 | + text: "Here is the summary of your account", |
| 180 | + action: "GET_PERP_ACCOUNT_SUMMARY", |
| 181 | + }, |
| 182 | + }, |
| 183 | + ], |
| 184 | + ] as ActionExample[][], |
| 185 | +}; |
| 186 | + |
| 187 | +export default accountSummary; |
0 commit comments