@@ -105,6 +105,40 @@ const tradeProvider: Provider = {
105
105
} ,
106
106
} ;
107
107
108
+ async function calculatePnl ( client : RESTClient , amount : number ) : Promise < number > {
109
+ const accounts = JSON . parse ( await client . listAccounts ( { } ) ) . accounts ;
110
+ elizaLogger . info ( "Accounts:" , accounts ) ;
111
+
112
+ const btcAccount = accounts . find ( account => account . currency === "BTC" ) ;
113
+ elizaLogger . info ( "BTC Account:" , btcAccount ) ;
114
+ const btcBalance = parseFloat ( btcAccount ?. available_balance ?. value ) ;
115
+ elizaLogger . info ( "BTC Balance:" , btcBalance ) ;
116
+
117
+ const ethAccount = accounts . find ( account => account . currency === "ETH" ) ;
118
+ elizaLogger . info ( "ETH Account:" , ethAccount ) ;
119
+ const ethBalance = parseFloat ( ethAccount ?. available_balance ?. value ) ;
120
+ elizaLogger . info ( "ETH Balance:" , ethBalance ) ;
121
+
122
+ const usdAccount = accounts . find ( account => account . currency === "USD" ) ;
123
+ elizaLogger . info ( "USD Account:" , usdAccount ) ;
124
+ const usdBalance = parseFloat ( usdAccount ?. available_balance ?. value ) ;
125
+ elizaLogger . info ( "USD Balance:" , usdBalance ) ;
126
+
127
+ const btcPrice = await getPrice ( client , "BTC-USD" ) ;
128
+ elizaLogger . info ( "BTC Price:" , btcPrice ) ;
129
+ const ethPrice = await getPrice ( client , "ETH-USD" ) ;
130
+ elizaLogger . info ( "ETH Price:" , ethPrice ) ;
131
+
132
+ const btcUSD = btcBalance * btcPrice ;
133
+ elizaLogger . info ( "BTC USD:" , btcUSD ) ;
134
+ const ethUSD = ethBalance * ethPrice ;
135
+ const totalValueUSD = btcUSD + ethUSD + usdBalance ;
136
+ elizaLogger . info ( "Total Value USD:" , totalValueUSD ) ;
137
+
138
+ const pnl = totalValueUSD - amount ;
139
+ return pnl ;
140
+ }
141
+
108
142
export const pnlProvider : Provider = {
109
143
get : async ( runtime : IAgentRuntime , _message : Memory ) => {
110
144
const client = new RESTClient (
@@ -115,36 +149,14 @@ export const pnlProvider: Provider = {
115
149
elizaLogger . info ( "Portfolios:" , portfolios ) ;
116
150
const portfolioId = portfolios . portfolios [ 0 ] . uuid ;
117
151
elizaLogger . info ( "Portfolio ID:" , portfolioId ) ;
118
- return { realizedPnl : 0 , unrealizedPnl : 0 } ;
119
152
if ( ! portfolioId ) {
120
153
elizaLogger . error ( "Portfolio ID is not set" ) ;
121
154
return { realizedPnl : 0 , unrealizedPnl : 0 } ;
122
155
}
123
-
124
- let btcPosition : Position , ethPosition : Position ;
125
- try {
126
- btcPosition = JSON . parse ( await client . getPortfolioBalances ( { portfolioUuid : portfolioId , symbol : "BTC-USD" } ) ) . position ;
127
- ethPosition = JSON . parse ( await client . getPortfolioBalances ( { portfolioUuid : portfolioId , symbol : "ETH-USD" } ) ) . position ;
128
- elizaLogger . info ( "BTC Position:" , btcPosition ) ;
129
- elizaLogger . info ( "ETH Position:" , ethPosition ) ;
130
- } catch ( error ) {
131
- elizaLogger . error ( "Error fetching portfolio balances:" , error . message ) ;
132
- return { realizedPnl : 0 , unrealizedPnl : 0 } ;
133
- }
134
-
135
- const btcUnrealizedPnl = btcPosition ?. unrealized_pnl || 0 ;
136
- const ethUnrealizedPnl = ethPosition ?. unrealized_pnl || 0 ;
137
- const ethAggregatedPnl = ethPosition ?. aggregated_pnl || 0 ;
138
- const btcAggregatedPnl = btcPosition ?. aggregated_pnl || 0 ;
139
- elizaLogger . info ( "BTC Unrealized PNL:" , JSON . stringify ( btcUnrealizedPnl ) ) ;
140
- elizaLogger . info ( "ETH Unrealized PNL:" , JSON . stringify ( ethUnrealizedPnl ) ) ;
141
- elizaLogger . info ( "ETH Aggregated PNL:" , JSON . stringify ( ethAggregatedPnl ) ) ;
142
- return {
143
- btcUnrealizedPnl : JSON . stringify ( btcUnrealizedPnl ) ,
144
- ethUnrealizedPnl : JSON . stringify ( ethUnrealizedPnl ) ,
145
- ethAggregatedPnl : JSON . stringify ( ethAggregatedPnl ) ,
146
- btcAggregatedPnl : JSON . stringify ( btcAggregatedPnl )
147
- }
156
+ // Need to manually set the amount and calculate as endpoints not accessible for portfolios
157
+ const pnl = await calculatePnl ( client , 1000 ) ;
158
+ elizaLogger . info ( "Pnl:" , pnl ) ;
159
+ return pnl
148
160
}
149
161
}
150
162
0 commit comments