@@ -2,7 +2,10 @@ use std::{collections::HashMap, sync::Arc};
2
2
3
3
use dataloader:: non_cached:: Loader ;
4
4
use juniper:: { graphql_object, FieldResult , GraphQLInputObject , GraphQLObject } ;
5
- use ledger:: { Account , AccountId , FpExt } ;
5
+ use ledger:: {
6
+ scan_state:: currency:: { Balance , Magnitude , Slot } ,
7
+ Account , AccountId , FpExt , Timing ,
8
+ } ;
6
9
use mina_p2p_messages:: {
7
10
string:: { TokenSymbol , ZkAppUri } ,
8
11
v2:: {
@@ -49,11 +52,12 @@ pub(crate) fn create_account_loader(rpc_sender: RpcSender) -> AccountLoader {
49
52
50
53
#[ derive( Debug , Clone ) ]
51
54
pub ( crate ) struct GraphQLAccount {
55
+ inner : Account ,
52
56
public_key : String ,
53
57
token_id : String ,
54
58
token : String ,
55
59
token_symbol : String ,
56
- balance : GraphQLBalance ,
60
+ // balance: GraphQLBalance,
57
61
nonce : String ,
58
62
receipt_chain_hash : String ,
59
63
// Storing the key for later
@@ -70,6 +74,27 @@ pub(crate) struct GraphQLAccount {
70
74
zkapp_uri : Option < String > ,
71
75
}
72
76
77
+ impl GraphQLAccount {
78
+ fn min_balance ( & self , global_slot : Option < u32 > ) -> Option < Balance > {
79
+ global_slot. map ( |slot| match self . inner . timing {
80
+ Timing :: Untimed => Balance :: zero ( ) ,
81
+ Timing :: Timed { .. } => self . inner . min_balance_at_slot ( Slot :: from_u32 ( slot) ) ,
82
+ } )
83
+ }
84
+
85
+ fn liquid_balance ( & self , global_slot : Option < u32 > ) -> Option < Balance > {
86
+ let min_balance = self . min_balance ( global_slot) ;
87
+ let total = self . inner . balance ;
88
+ min_balance. map ( |mb| {
89
+ if total > mb {
90
+ total. checked_sub ( & mb) . expect ( "overflow" )
91
+ } else {
92
+ Balance :: zero ( )
93
+ }
94
+ } )
95
+ }
96
+ }
97
+
73
98
#[ graphql_object( context = Context ) ]
74
99
#[ graphql( description = "A Mina account" ) ]
75
100
impl GraphQLAccount {
@@ -89,8 +114,26 @@ impl GraphQLAccount {
89
114
& self . token_symbol
90
115
}
91
116
92
- fn balance ( & self ) -> & GraphQLBalance {
93
- & self . balance
117
+ async fn balance ( & self , context : & Context ) -> GraphQLBalance {
118
+ let best_tip = context. get_or_fetch_best_tip ( ) . await ;
119
+ let global_slot = best_tip. as_ref ( ) . map ( |bt| bt. global_slot ( ) ) ;
120
+
121
+ GraphQLBalance {
122
+ total : self . inner . balance . as_u64 ( ) . to_string ( ) ,
123
+ block_height : best_tip
124
+ . as_ref ( )
125
+ . map ( |bt| bt. height ( ) )
126
+ . unwrap_or_default ( )
127
+ . to_string ( ) ,
128
+ state_hash : best_tip. as_ref ( ) . map ( |bt| bt. hash ( ) . to_string ( ) ) ,
129
+ liquid : self
130
+ . liquid_balance ( global_slot)
131
+ . map ( |b| b. as_u64 ( ) . to_string ( ) ) ,
132
+ locked : self
133
+ . min_balance ( global_slot)
134
+ . map ( |b| b. as_u64 ( ) . to_string ( ) ) ,
135
+ unknown : self . inner . balance . as_u64 ( ) . to_string ( ) ,
136
+ }
94
137
}
95
138
96
139
fn nonce ( & self ) -> & str {
@@ -232,6 +275,11 @@ pub struct GraphQLSetVerificationKey {
232
275
#[ derive( GraphQLObject , Debug , Clone ) ]
233
276
pub struct GraphQLBalance {
234
277
pub total : String ,
278
+ pub block_height : String ,
279
+ pub state_hash : Option < String > ,
280
+ pub liquid : Option < String > ,
281
+ pub locked : Option < String > ,
282
+ pub unknown : String ,
235
283
}
236
284
237
285
// #[derive(GraphQLObject, Debug)]
@@ -310,15 +358,6 @@ impl From<ledger::Timing> for GraphQLTiming {
310
358
}
311
359
}
312
360
313
- // TODO(adonagy)
314
- impl From < ledger:: scan_state:: currency:: Balance > for GraphQLBalance {
315
- fn from ( value : ledger:: scan_state:: currency:: Balance ) -> Self {
316
- Self {
317
- total : value. as_u64 ( ) . to_string ( ) ,
318
- }
319
- }
320
- }
321
-
322
361
impl TryFrom < ledger:: Account > for GraphQLAccount {
323
362
type Error = ConversionError ;
324
363
@@ -340,11 +379,12 @@ impl TryFrom<ledger::Account> for GraphQLAccount {
340
379
. transpose ( ) ?; // Transpose Option<Result<...>> to Result<Option<...>>
341
380
342
381
Ok ( Self {
382
+ inner : value. clone ( ) ,
343
383
public_key : value. public_key . into_address ( ) ,
344
384
token_id : TokenIdKeyHash :: from ( value. token_id . clone ( ) ) . to_string ( ) ,
345
385
token : TokenIdKeyHash :: from ( value. token_id ) . to_string ( ) ,
346
386
token_symbol : TokenSymbol :: from ( & value. token_symbol ) . to_string ( ) ,
347
- balance : GraphQLBalance :: from ( value. balance ) ,
387
+ // balance: GraphQLBalance::from(value.balance),
348
388
nonce : value. nonce . as_u32 ( ) . to_string ( ) ,
349
389
receipt_chain_hash : ReceiptChainHash :: from ( value. receipt_chain_hash ) . to_string ( ) ,
350
390
delegate_key : value. delegate ,
0 commit comments