1
1
use dkn_p2p:: libp2p:: { multiaddr:: Protocol , Multiaddr , PeerId } ;
2
- use dkn_p2p :: DriaNetworkType ;
2
+ use dkn_utils :: { DriaNetwork , SemanticVersion } ;
3
3
use eyre:: { Context , OptionExt , Result } ;
4
4
use std:: fmt:: Debug ;
5
5
@@ -8,12 +8,12 @@ use std::fmt::Debug;
8
8
pub struct DriaRPC {
9
9
pub addr : Multiaddr ,
10
10
pub peer_id : PeerId ,
11
- pub network : DriaNetworkType ,
11
+ pub network : DriaNetwork ,
12
12
}
13
13
14
14
impl DriaRPC {
15
15
/// Creates a new RPC target at the given type, along with a network type for refreshing the RPC address.
16
- pub fn new ( addr : Multiaddr , network : DriaNetworkType ) -> Result < Self > {
16
+ pub fn new ( addr : Multiaddr , network : DriaNetwork ) -> Result < Self > {
17
17
let peer_id = addr
18
18
. iter ( )
19
19
. find_map ( |p| match p {
@@ -29,28 +29,31 @@ impl DriaRPC {
29
29
} )
30
30
}
31
31
32
- /// Creates a new RPC target for the given network type.
33
- pub async fn new_for_network ( network : DriaNetworkType ) -> Result < Self > {
34
- let addr = get_rpc_for_network ( & network) . await ?;
32
+ /// Creates a new RPC target for the given network type and version .
33
+ pub async fn new_for_network ( network : DriaNetwork , version : & SemanticVersion ) -> Result < Self > {
34
+ let addr = get_rpc_for_network ( & network, version ) . await ?;
35
35
Self :: new ( addr, network)
36
36
}
37
37
}
38
38
39
39
/// Calls the DKN API to get an RPC address for the given network type.
40
40
///
41
41
/// The peer id is expected to be within the multi-address.
42
- async fn get_rpc_for_network ( network : & DriaNetworkType ) -> Result < Multiaddr > {
42
+ async fn get_rpc_for_network (
43
+ network : & DriaNetwork ,
44
+ version : & SemanticVersion ,
45
+ ) -> Result < Multiaddr > {
43
46
#[ derive( serde:: Deserialize , Debug ) ]
44
47
struct DriaNodesApiResponse {
45
48
pub rpc : Multiaddr ,
46
49
}
47
50
48
51
// url to be used is determined by the network type
49
- let url = match network {
50
- DriaNetworkType :: Community => "https://dkn.dria.co/v5/available-nodes" ,
51
- DriaNetworkType :: Pro => "https://dkn.dria.co/v5/sdk/available-nodes" ,
52
- DriaNetworkType :: Test => "https://dkn.dria.co/v5/test/available-nodes" ,
52
+ let base_url = match network {
53
+ DriaNetwork :: Mainnet => "https://dkn.dria.co/available-nodes" ,
54
+ DriaNetwork :: Testnet => "https://dkn.dria.co/available-nodes" ,
53
55
} ;
56
+ let url = format ! ( "{}/{}" , base_url, version. as_major_minor( ) ) ;
54
57
55
58
// make the request
56
59
let response = reqwest:: get ( url) . await ?;
@@ -69,7 +72,9 @@ mod tests {
69
72
70
73
#[ tokio:: test]
71
74
async fn test_dria_nodes ( ) {
72
- let node = DriaRPC :: new_for_network ( DriaNetworkType :: Community ) . await ;
75
+ let node =
76
+ DriaRPC :: new_for_network ( DriaNetwork :: Mainnet , & SemanticVersion :: from_crate_version ( ) )
77
+ . await ;
73
78
assert ! ( node. is_ok( ) ) ;
74
79
}
75
80
}
0 commit comments