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
use std::sync::Arc;

use ethabi::ethereum_types::U256;
use raiden_primitives::types::{
	Address,
	BlockHash,
	BlockId,
	SecretRegistryAddress,
	SettleTimeout,
	TokenAddress,
	TokenAmount,
	TokenNetworkAddress,
	TransactionHash,
};
use web3::{
	contract::{
		Contract,
		Options,
	},
	Transport,
	Web3,
};

use super::{
	Account,
	ProxyError,
	TokenProxy,
};
use crate::{
	contracts::GasMetadata,
	transactions::{
		RegisterTokenTransaction,
		RegisterTokenTransactionParams,
		Transaction,
	},
};

/// Token network registry error type.
type Result<T> = std::result::Result<T, ProxyError>;

/// Token network registry proxy to interact with the on-chain contract.
#[derive(Clone)]
pub struct TokenNetworkRegistryProxy<T: Transport> {
	web3: Web3<T>,
	contract: Contract<T>,
	gas_metadata: Arc<GasMetadata>,
}

impl<T> TokenNetworkRegistryProxy<T>
where
	T: Transport + Send + Sync,
	T::Out: Send,
{
	/// Returns a new instance of `TokenNetworkRegistryProxy`.
	pub fn new(web3: Web3<T>, gas_metadata: Arc<GasMetadata>, contract: Contract<T>) -> Self {
		Self { web3, gas_metadata, contract }
	}

	/// Register a new token network.
	pub async fn add_token(
		&self,
		account: Account<T>,
		token_proxy: TokenProxy<T>,
		token_address: TokenAddress,
		block: BlockHash,
	) -> Result<(TransactionHash, TokenNetworkAddress)> {
		let add_token_transaction = RegisterTokenTransaction {
			web3: self.web3.clone(),
			account: account.clone(),
			token_network_registry: self.clone(),
			token: token_proxy,
			gas_metadata: self.gas_metadata.clone(),
		};

		add_token_transaction
			.execute(
				RegisterTokenTransactionParams {
					token_address,
					channel_participant_deposit_limit: TokenAmount::MAX,
					token_network_deposit_limit: TokenAmount::MAX,
				},
				block,
			)
			.await
	}

	/// Get the registry's controller account.
	pub async fn get_controller(&self, block: BlockHash) -> Result<Address> {
		self.contract
			.query("controller", (), None, Options::default(), Some(BlockId::Hash(block)))
			.await
			.map_err(Into::into)
	}

	/// Get address of a token network by token address.
	pub async fn get_token_network(
		&self,
		token_address: TokenAddress,
		block: BlockHash,
	) -> Result<Address> {
		self.contract
			.query(
				"token_to_token_networks",
				(token_address,),
				None,
				Options::default(),
				Some(BlockId::Hash(block)),
			)
			.await
			.map_err(Into::into)
	}

	/// Get minimum settlement timeout.
	pub async fn settlement_timeout_min(&self, block: BlockHash) -> Result<SettleTimeout> {
		self.contract
			.query(
				"settlement_timeout_min",
				(),
				None,
				Options::default(),
				Some(BlockId::Hash(block)),
			)
			.await
			.map(|b: U256| b.as_u64().into())
			.map_err(Into::into)
	}

	/// Get maximum settlement timeout.
	pub async fn settlement_timeout_max(&self, block: BlockHash) -> Result<SettleTimeout> {
		self.contract
			.query(
				"settlement_timeout_max",
				(),
				None,
				Options::default(),
				Some(BlockId::Hash(block)),
			)
			.await
			.map(|b: U256| b.as_u64().into())
			.map_err(Into::into)
	}

	/// Returns address of secret registry
	pub async fn get_secret_registry_address(
		&self,
		block: BlockHash,
	) -> Result<SecretRegistryAddress> {
		self.contract
			.query(
				"secret_registry_address",
				(),
				None,
				Options::default(),
				Some(BlockId::Hash(block)),
			)
			.await
			.map_err(Into::into)
	}

	/// Get the maximum number of allowed token networks.
	pub async fn get_max_token_networks(&self, block: BlockHash) -> Result<U256> {
		self.contract
			.query("max_token_networks", (), None, Options::default(), Some(BlockId::Hash(block)))
			.await
			.map_err(Into::into)
	}

	/// Get the current count of token networks.
	pub async fn get_token_networks_created(&self, block: BlockHash) -> Result<U256> {
		self.contract
			.query(
				"token_network_created",
				(),
				None,
				Options::default(),
				Some(BlockId::Hash(block)),
			)
			.await
			.map_err(Into::into)
	}
}