1+ use colored:: Colorize ;
12use eyre:: Context ;
23
34use crate :: utils:: { DriaEnv , LAUNCHER_USER_AGENT } ;
45
5- const POINTS_API_BASE_URL : & str =
6- "https://mainnet.dkn.dria.co/dashboard/supply/v0/leaderboard/steps" ;
6+ const POINTS_API_BASE_URL : & str = "https://mainnet.dkn.dria.co/points/v0/total/node/" ;
77
88#[ derive( Debug , serde:: Deserialize ) ]
99pub struct PointsRes {
1010 /// Indicates in which top percentile your points are.
11- ///
12- /// TODO: fix this in new API
13- /// TODO: sometimes returned as `null``
14- pub percentile : Option < String > ,
11+ pub percentile : usize ,
1512 /// The total number of points you have accumulated.
1613 pub score : f64 ,
1714}
@@ -24,9 +21,30 @@ pub async fn show_points() -> eyre::Result<()> {
2421 dria_env. ask_for_key_if_required ( ) ?;
2522 let ( _, _, address) = dria_env. get_account ( ) ?;
2623
27- // the address can have 0x or not, we enforce it ourselves here
24+ let points = get_points ( & address)
25+ . await
26+ . wrap_err ( "could not get points" ) ?;
27+
28+ if points. score == 0.0 {
29+ eprintln ! (
30+ "You have not accumulated any {} yet." ,
31+ "$DRIA points" . purple( )
32+ ) ;
33+ } else {
34+ eprintln ! (
35+ "You have accumulated {} {}, which puts you in the top {}%." ,
36+ points. score,
37+ "$DRIA points" . purple( ) ,
38+ points. percentile
39+ ) ;
40+ }
41+
42+ Ok ( ( ) )
43+ }
44+
45+ async fn get_points ( address : & str ) -> eyre:: Result < PointsRes > {
2846 let url = format ! (
29- "{}?address= 0x{}" ,
47+ "{}/ 0x{}" ,
3048 POINTS_API_BASE_URL ,
3149 address. trim_start_matches( "0x" )
3250 ) ;
@@ -47,15 +65,18 @@ pub async fn show_points() -> eyre::Result<()> {
4765 . await
4866 . wrap_err ( "could not parse body" ) ?;
4967
50- if points. score == 0.0 {
51- eprintln ! ( "You have not accumulated any $DRIA points yet." ) ;
52- } else {
53- eprintln ! (
54- "You have accumulated {} $DRIA points, which puts you in the top {}%." ,
55- points. score,
56- points. percentile. unwrap_or_else( || "100" . to_string( ) )
57- ) ;
58- }
68+ Ok ( points)
69+ }
5970
60- Ok ( ( ) )
71+ #[ cfg( test) ]
72+ mod tests {
73+ use super :: * ;
74+
75+ #[ tokio:: test]
76+ async fn test_get_points ( ) {
77+ let address = "0x1234567890abcdef1234567890abcdef12345678" ;
78+ let points = get_points ( address) . await . unwrap ( ) ;
79+ assert ! ( points. score >= 0.0 ) ;
80+ assert ! ( points. percentile <= 100 ) ;
81+ }
6182}
0 commit comments