neo3/neo_contract/
policy_contract.rs

1use async_trait::async_trait;
2use primitive_types::H160;
3use serde::{Deserialize, Serialize};
4
5use crate::{
6	neo_builder::TransactionBuilder,
7	neo_clients::{JsonRpcProvider, RpcClient},
8	neo_contract::{traits::SmartContractTrait, ContractError},
9	neo_types::{
10		serde_with_utils::{deserialize_script_hash, serialize_script_hash},
11		ScriptHash,
12	},
13	ScriptHashExtension,
14};
15
16#[derive(Debug, Clone, Serialize, Deserialize)]
17pub struct PolicyContract<'a, P: JsonRpcProvider> {
18	#[serde(deserialize_with = "deserialize_script_hash")]
19	#[serde(serialize_with = "serialize_script_hash")]
20	script_hash: ScriptHash,
21	#[serde(skip)]
22	provider: Option<&'a RpcClient<P>>,
23}
24
25impl<'a, P: JsonRpcProvider + 'static> PolicyContract<'a, P> {
26	pub const NAME: &'static str = "PolicyContract";
27	// pub const SCRIPT_HASH: H160 = Self::calc_native_contract_hash(Self::NAME).unwrap();
28
29	pub fn new(provider: Option<&'a RpcClient<P>>) -> Self {
30		Self { script_hash: Self::calc_native_contract_hash(Self::NAME).unwrap(), provider }
31	}
32
33	pub async fn get_fee_per_byte(&self) -> Result<i32, ContractError> {
34		self.call_function_returning_int("getFeePerByte", vec![]).await
35	}
36
37	pub async fn get_exec_fee_factor(&self) -> Result<i32, ContractError> {
38		self.call_function_returning_int("getExecFeeFactor", vec![]).await
39	}
40
41	pub async fn get_storage_price(&self) -> Result<i32, ContractError> {
42		self.call_function_returning_int("getStoragePrice", vec![]).await
43	}
44
45	pub async fn is_blocked(&self, script_hash: &H160) -> Result<bool, ContractError> {
46		self.call_function_returning_bool("isBlocked", vec![script_hash.into()]).await
47	}
48
49	// State modifying methods
50
51	pub async fn set_fee_per_byte(&self, fee: i32) -> Result<TransactionBuilder<P>, ContractError> {
52		self.invoke_function("setFeePerByte", vec![fee.into()]).await
53	}
54
55	pub async fn set_exec_fee_factor(
56		&self,
57		fee: i32,
58	) -> Result<TransactionBuilder<P>, ContractError> {
59		self.invoke_function("setExecFeeFactor", vec![fee.into()]).await
60	}
61
62	pub async fn set_storage_price(
63		&self,
64		price: i32,
65	) -> Result<TransactionBuilder<P>, ContractError> {
66		self.invoke_function("setStoragePrice", vec![price.into()]).await
67	}
68
69	pub async fn block_account(
70		&self,
71		account: &H160,
72	) -> Result<TransactionBuilder<P>, ContractError> {
73		self.invoke_function("blockAccount", vec![account.into()]).await
74	}
75
76	pub async fn block_account_address(
77		&self,
78		address: &str,
79	) -> Result<TransactionBuilder<P>, ContractError> {
80		let account = ScriptHash::from_address(address).unwrap();
81		self.block_account(&account).await
82	}
83
84	pub async fn unblock_account(
85		&self,
86		account: &H160,
87	) -> Result<TransactionBuilder<P>, ContractError> {
88		self.invoke_function("unblockAccount", vec![account.into()]).await
89	}
90
91	pub async fn unblock_account_address(
92		&self,
93		address: &str,
94	) -> Result<TransactionBuilder<P>, ContractError> {
95		let account = ScriptHash::from_address(address).unwrap();
96		self.unblock_account(&account).await
97	}
98}
99
100#[async_trait]
101impl<'a, P: JsonRpcProvider> SmartContractTrait<'a> for PolicyContract<'a, P> {
102	type P = P;
103
104	fn script_hash(&self) -> H160 {
105		self.script_hash
106	}
107
108	fn set_script_hash(&mut self, script_hash: H160) {
109		self.script_hash = script_hash;
110	}
111
112	fn provider(&self) -> Option<&RpcClient<P>> {
113		self.provider
114	}
115}