-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathread_contract.rs
99 lines (83 loc) · 3.26 KB
/
read_contract.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use std::{num::NonZeroUsize, str::FromStr};
use clap::Parser;
use dash_sdk::{mock::provider::GrpcContextProvider, platform::Fetch, Sdk, SdkBuilder};
use dpp::prelude::{DataContract, Identifier};
use rs_dapi_client::{Address, AddressList};
use zeroize::Zeroizing;
#[derive(clap::Parser, Debug)]
#[command(version)]
pub struct Config {
/// Dash Platform server hostname or IPv4 address
#[arg(short = 'i', long = "address")]
pub server_address: String,
/// Dash Core IP port
#[arg(short = 'c', long)]
pub core_port: u16,
// Dash Core RPC user
#[arg(short = 'u', long)]
pub core_user: String,
// Dash Core RPC password
#[arg(short = 'p', long)]
pub core_password: Zeroizing<String>,
/// Dash Platform DAPI port
#[arg(short = 'd', long)]
pub platform_port: u16,
}
/// Read data contract.
///
/// This example demonstrates how to connect to running platform and try to read a data contract.
/// TODO: add wallet, context provider, etc.
#[tokio::main(flavor = "multi_thread", worker_threads = 1)]
async fn main() {
// Replace const below with data contract identifier of data contract, 32 bytes
const DATA_CONTRACT_ID_BYTES: [u8; 32] = [1; 32];
// Read configuration
let config = Config::parse();
// Configure the Sdk
let sdk = setup_sdk(&config);
// read data contract
// Convert bytes to identifier object that can be used as a Query
let id = Identifier::from_bytes(&DATA_CONTRACT_ID_BYTES).expect("parse data contract id");
// Fetch identity from Platform
let contract: Option<DataContract> =
DataContract::fetch(&sdk, id).await.expect("fetch identity");
// Check the result; note that in our case, we expect to not find the data contract, as the
// identifier is not valid.
assert!(contract.is_none(), "result: {:?}", contract);
}
/// Setup Rust SDK
fn setup_sdk(config: &Config) -> Sdk {
// We need to implement a ContextProvider.
// Here, we will just use a mock implementation.
// Tricky thing here is that this implementation requires SDK, so we have a
// circular dependency between SDK and ContextProvider.
// We'll first provide `None` Sdk, and then update it later.
//
// To modify context provider, we need locks and Arc to overcome ownership rules.
let context_provider = GrpcContextProvider::new(
None,
&config.server_address,
config.core_port,
&config.core_user,
&config.core_password,
NonZeroUsize::new(100).expect("data contracts cache size"),
NonZeroUsize::new(100).expect("quorum public keys cache size"),
)
.expect("context provider");
// Let's build the Sdk.
// First, we need an URI of some Dash Platform DAPI host to connect to and use as seed.
let address = Address::from_str(&format!(
"https://{}:{}",
config.server_address, config.platform_port
))
.expect("parse uri");
// Now, we create the Sdk with the wallet and context provider.
let sdk = SdkBuilder::new(AddressList::from_iter([address]))
.build()
.expect("cannot build sdk");
// Reconfigure context provider with Sdk
context_provider.set_sdk(Some(sdk.clone()));
sdk.set_context_provider(context_provider);
// Return the SDK we created
sdk
}