-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprime_network_contract.rs
183 lines (166 loc) · 4.8 KB
/
prime_network_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use crate::web3::contracts::constants::addresses::PRIME_NETWORK_ADDRESS;
use crate::web3::contracts::core::contract::Contract;
use crate::web3::wallet::Wallet;
use alloy::dyn_abi::DynSolValue;
use alloy::primitives::{Address, FixedBytes, U256};
use alloy::providers::Provider;
#[derive(Clone)]
pub struct PrimeNetworkContract {
pub instance: Contract,
}
impl PrimeNetworkContract {
pub fn new(wallet: &Wallet, abi_file_path: &str) -> Self {
let instance = Contract::new(PRIME_NETWORK_ADDRESS, wallet, abi_file_path);
Self { instance }
}
pub async fn register_provider(
&self,
stake: U256,
) -> Result<FixedBytes<32>, Box<dyn std::error::Error>> {
let register_tx = self
.instance
.instance()
.function("registerProvider", &[stake.into()])?
.send()
.await?
.watch()
.await?;
Ok(register_tx)
}
pub async fn add_compute_node(
&self,
node_address: Address,
compute_units: U256,
signature: Vec<u8>,
) -> Result<FixedBytes<32>, Box<dyn std::error::Error>> {
let add_node_tx = self
.instance
.instance()
.function(
"addComputeNode",
&[
node_address.into(),
"ipfs://nodekey/".to_string().into(),
compute_units.into(),
DynSolValue::Bytes(signature.to_vec()),
],
)?
.send()
.await?
.watch()
.await?;
Ok(add_node_tx)
}
pub async fn validate_node(
&self,
provider_address: Address,
node_address: Address,
) -> Result<FixedBytes<32>, Box<dyn std::error::Error>> {
let validate_node_tx = self
.instance
.instance()
.function(
"validateNode",
&[provider_address.into(), node_address.into()],
)?
.send()
.await?
.watch()
.await?;
Ok(validate_node_tx)
}
pub async fn create_domain(
&self,
domain_name: String,
validation_logic: Address,
domain_uri: String,
) -> Result<FixedBytes<32>, Box<dyn std::error::Error>> {
let create_domain_tx = self
.instance
.instance()
.function(
"createDomain",
&[
domain_name.into(),
validation_logic.into(),
domain_uri.into(),
],
)?
.send()
.await?
.watch()
.await?;
Ok(create_domain_tx)
}
pub async fn update_validation_logic(
&self,
domain_id: U256,
validation_logic: Address,
) -> Result<FixedBytes<32>, Box<dyn std::error::Error>> {
let update_validation_logic_tx = self
.instance
.instance()
.function(
"updateValidationLogic",
&[domain_id.into(), validation_logic.into()],
)?
.send()
.await?
.watch()
.await?;
Ok(update_validation_logic_tx)
}
pub async fn set_stake_minimum(
&self,
min_stake_amount: U256,
) -> Result<FixedBytes<32>, Box<dyn std::error::Error>> {
let set_stake_minimum_tx = self
.instance
.instance()
.function("setStakeMinimum", &[min_stake_amount.into()])?
.send()
.await?
.watch()
.await?;
Ok(set_stake_minimum_tx)
}
pub async fn whitelist_provider(
&self,
provider_address: Address,
) -> Result<FixedBytes<32>, Box<dyn std::error::Error>> {
let whitelist_provider_tx = self
.instance
.instance()
.function("whitelistProvider", &[provider_address.into()])?
.send()
.await?
.watch()
.await?;
let receipt = self
.instance
.provider()
.get_transaction_receipt(whitelist_provider_tx)
.await?;
println!("Receipt: {:?}", receipt);
Ok(whitelist_provider_tx)
}
pub async fn invalidate_work(
&self,
pool_id: U256,
penalty: U256,
data: Vec<u8>,
) -> Result<FixedBytes<32>, Box<dyn std::error::Error>> {
let invalidate_work_tx = self
.instance
.instance()
.function(
"invalidateWork",
&[pool_id.into(), penalty.into(), data.into()],
)?
.send()
.await?
.watch()
.await?;
Ok(invalidate_work_tx)
}
}