raiden-blockchain 0.1.0

Raiden Network implementation in Rust
Documentation
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
use std::sync::Arc;

use raiden_primitives::types::{
	Address,
	BlockHash,
	BlockId,
	ChannelIdentifier,
	GasLimit,
	GasPrice,
	TokenAmount,
};
use raiden_state_machine::types::ChannelStatus;
use tokio::sync::RwLockWriteGuard;
use web3::{
	contract::Options,
	types::BlockNumber,
	Transport,
	Web3,
};

use crate::{
	contracts::GasMetadata,
	proxies::{
		Account,
		ChannelData,
		ParticipantDetails,
		ProxyError,
		TokenNetworkProxy,
		TokenProxy,
	},
	transactions::Transaction,
};

/// On-chain data required to validate SetTotalDeposit.
#[derive(Clone)]
pub struct ChannelSetTotalDepositTransactionData {
	pub(crate) allowance: TokenAmount,
	pub(crate) current_balance: TokenAmount,
	pub(crate) channel_identifier: ChannelIdentifier,
	pub(crate) amount_to_deposit: TokenAmount,
	pub(crate) channel_onchain_details: ChannelData,
	pub(crate) our_details: ParticipantDetails,
	pub(crate) partner_details: ParticipantDetails,
	pub(crate) network_balance: TokenAmount,
	pub(crate) safety_deprecation_switch: bool,
	pub(crate) token_network_deposit_limit: TokenAmount,
	pub(crate) channel_participant_deposit_limit: TokenAmount,
	pub(crate) network_total_deposit: TokenAmount,
}

/// Parameters for setting the channel's total deposit.
#[derive(Clone)]
pub struct ChannelSetTotalDepositTransactionParams {
	pub(crate) channel_identifier: ChannelIdentifier,
	pub(crate) partner: Address,
	pub(crate) total_deposit: TokenAmount,
}

/// Channels set total deposit type.
pub struct ChannelSetTotalDepositTransaction<T: Transport> {
	pub(crate) web3: Web3<T>,
	pub(crate) account: Account<T>,
	pub(crate) token_network: TokenNetworkProxy<T>,
	pub(crate) token: TokenProxy<T>,
	pub(crate) gas_metadata: Arc<GasMetadata>,
}

#[async_trait::async_trait]
impl<T> Transaction for ChannelSetTotalDepositTransaction<T>
where
	T: Transport + Send + Sync,
	T::Out: Send,
{
	type Output = ();
	type Params = ChannelSetTotalDepositTransactionParams;
	type Data = ChannelSetTotalDepositTransactionData;

	async fn onchain_data(
		&self,
		params: Self::Params,
		at_blockhash: BlockHash,
	) -> Result<Self::Data, ProxyError> {
		let current_balance =
			self.token.balance_of(self.account.address(), Some(at_blockhash)).await?;

		let channel_identifier = self
			.token_network
			.get_channel_identifier(self.account.address(), params.partner, Some(at_blockhash))
			.await?
			.ok_or(ProxyError::BrokenPrecondition("Block not found".to_string()))?;

		let channel_onchain_details = self
			.token_network
			.channel_details(
				Some(channel_identifier),
				self.account.address(),
				params.partner,
				at_blockhash,
			)
			.await?;

		let our_details = match self
			.token_network
			.participant_details(
				channel_identifier,
				self.account.address(),
				params.partner,
				Some(at_blockhash),
			)
			.await
		{
			Ok(our_details) => our_details,
			Err(_) =>
				self.token_network
					.participant_details(
						channel_identifier,
						self.account.address(),
						params.partner,
						None,
					)
					.await?,
		};

		let partner_details = self
			.token_network
			.participant_details(
				channel_identifier,
				params.partner,
				self.account.address(),
				Some(at_blockhash),
			)
			.await?;

		let allowance = self
			.token
			.allowance(
				self.token_network.contract.address(),
				self.account.address(),
				Some(at_blockhash),
			)
			.await?;

		let network_balance =
			self.token.balance_of(self.account.address(), Some(at_blockhash)).await?;

		let safety_deprecation_switch =
			self.token_network.safety_deprecation_switch(at_blockhash).await?;

		let token_network_deposit_limit =
			self.token_network.token_network_deposit_limit(at_blockhash).await?;

		let channel_participant_deposit_limit =
			self.token_network.channel_participant_deposit_limit(at_blockhash).await?;

		let network_total_deposit =
			self.token.balance_of(self.account.address(), Some(at_blockhash)).await?;

		let amount_to_deposit = params.total_deposit - our_details.deposit;

		Ok(ChannelSetTotalDepositTransactionData {
			allowance,
			current_balance,
			channel_identifier,
			amount_to_deposit,
			channel_onchain_details,
			our_details,
			partner_details,
			network_balance,
			safety_deprecation_switch,
			token_network_deposit_limit,
			channel_participant_deposit_limit,
			network_total_deposit,
		})
	}

	async fn validate_preconditions(
		&self,
		params: Self::Params,
		data: Self::Data,
		at_block_hash: BlockHash,
	) -> Result<(), ProxyError> {
		if data.current_balance < data.amount_to_deposit {
			return Err(ProxyError::BrokenPrecondition(format!(
				"new_total_deposit - previous_total_deposit = {} \
                 cannot be larger than the available balance {}.",
				data.amount_to_deposit, data.current_balance,
			)))
		}

		if data.channel_identifier != params.channel_identifier {
			return Err(ProxyError::BrokenPrecondition(format!(
				"There is a channel open between \
                {} and {}. However the channel id \
                on-chain {} and the provided \
                id {} do not match.",
				self.account.address(),
				params.partner,
				params.channel_identifier,
				data.channel_identifier,
			)))
		}

		if data.safety_deprecation_switch {
			return Err(ProxyError::BrokenPrecondition(format!(
				"This token network has been deprecated."
			)))
		}

		if data.channel_onchain_details.status != ChannelStatus::Opened {
			return Err(ProxyError::BrokenPrecondition(format!(
				"The channel was not opened at the provided block \
                ({}). This call should never have been attempted.",
				at_block_hash
			)))
		}

		if params.total_deposit <= data.our_details.deposit {
			return Err(ProxyError::BrokenPrecondition(format!(
				"Current total deposit ({}) is already larger \
                than the requested total deposit amount ({})",
				data.our_details.deposit, params.total_deposit,
			)))
		}

		let (_, total_channel_deposit_overflow) =
			params.total_deposit.overflowing_add(data.partner_details.deposit);
		if total_channel_deposit_overflow {
			return Err(ProxyError::BrokenPrecondition(format!("Deposit overflow")))
		}

		if params.total_deposit > data.channel_participant_deposit_limit {
			return Err(ProxyError::BrokenPrecondition(format!(
				"Deposit of {} is larger than the \
                channel participant deposit limit",
				params.total_deposit,
			)))
		}

		if data.network_total_deposit + data.amount_to_deposit > data.token_network_deposit_limit {
			return Err(ProxyError::BrokenPrecondition(format!(
				"Deposit of {} will have \
                exceeded the token network deposit limit.",
				data.amount_to_deposit,
			)))
		}

		if data.network_balance < data.amount_to_deposit {
			return Err(ProxyError::BrokenPrecondition(format!(
				"new_total_deposit - previous_total_deposit =  {} can \
                not be larger than the available balance {}, \
                for token at address {}",
				data.amount_to_deposit,
				data.network_balance,
				self.account.address(),
			)))
		}

		Ok(())
	}

	async fn execute_prerequisite(
		&self,
		_params: Self::Params,
		data: Self::Data,
	) -> Result<(), ProxyError> {
		self.token
			.approve(
				self.account.clone(),
				self.token_network.contract.address(),
				data.amount_to_deposit,
			)
			.await?;
		Ok(())
	}

	async fn submit(
		&self,
		params: Self::Params,
		_data: Self::Data,
		gas_estimate: GasLimit,
		gas_price: GasPrice,
	) -> Result<Self::Output, ProxyError> {
		let nonce = self.account.peek_next_nonce().await;
		self.account.next_nonce().await;

		self.token_network
			.contract
			.signed_call_with_confirmations(
				"setTotalDeposit",
				(
					params.channel_identifier,
					self.account.address(),
					params.total_deposit,
					params.partner,
				),
				Options::with(|opt| {
					opt.value = Some(GasLimit::from(0));
					opt.gas = Some(gas_estimate);
					opt.nonce = Some(nonce);
					opt.gas_price = Some(gas_price);
				}),
				1,
				self.account.private_key(),
			)
			.await?;

		Ok(())
	}

	async fn validate_postconditions(
		&self,
		params: Self::Params,
		_at_block_hash: BlockHash,
	) -> Result<Self::Output, ProxyError> {
		let failed_at = self
			.web3
			.eth()
			.block(BlockId::Number(BlockNumber::Latest))
			.await
			.map_err(ProxyError::Web3)?
			.ok_or(ProxyError::Recoverable("Block not found".to_string()))?;

		let failed_at_blocknumber = failed_at.number.unwrap();
		let failed_at_blockhash = failed_at.hash.unwrap();

		self.account
			.check_for_insufficient_eth(
				self.gas_metadata.get("TokenNetwork.setTotalDeposit").into(),
				failed_at_blocknumber,
			)
			.await?;

		let data = self.onchain_data(params.clone(), failed_at_blockhash).await?;

		if data.channel_onchain_details.status == ChannelStatus::Closed {
			return Err(ProxyError::Recoverable(format!(
				"Deposit failed because the channel was closed meanwhile",
			)))
		}

		if data.channel_onchain_details.status == ChannelStatus::Settled {
			return Err(ProxyError::Recoverable(format!(
				"Deposit failed because the channel was settled meanwhile",
			)))
		}

		if data.channel_onchain_details.status == ChannelStatus::Removed {
			return Err(ProxyError::Recoverable(format!(
				"Deposit failed because the channel was settled and unlocked meanwhile",
			)))
		}

		let (_, total_channel_deposit_overflow) =
			params.total_deposit.overflowing_add(data.partner_details.deposit);
		if total_channel_deposit_overflow {
			return Err(ProxyError::Recoverable(format!("Deposit overflow")))
		}

		if data.our_details.deposit >= params.total_deposit {
			return Err(ProxyError::Recoverable(format!(
				"Requested total deposit was already performed"
			)))
		}

		if data.network_total_deposit + data.amount_to_deposit > data.token_network_deposit_limit {
			return Err(ProxyError::Recoverable(format!(
				"Deposit of {} will have \
                exceeded the token network deposit limit.",
				data.amount_to_deposit,
			)))
		}

		if params.total_deposit > data.channel_participant_deposit_limit {
			return Err(ProxyError::Recoverable(format!(
				"Deposit of {} is larger than the \
                channel participant deposit limit",
				params.total_deposit,
			)))
		}

		if data.network_balance < data.amount_to_deposit {
			return Err(ProxyError::Recoverable(format!(
				"new_total_deposit - previous_total_deposit =  {} can \
                not be larger than the available balance {}, \
                for token at address {}",
				data.amount_to_deposit,
				data.network_balance,
				self.account.address(),
			)))
		}

		let has_sufficient_balance = self
			.token
			.balance_of(self.token_network.contract.address(), Some(failed_at_blockhash))
			.await? >= data.amount_to_deposit;
		if !has_sufficient_balance {
			return Err(ProxyError::Recoverable(format!(
				"The account does not have enough balance to complete the deposit"
			)))
		}

		if data.allowance < data.amount_to_deposit {
			return Err(ProxyError::Recoverable(format!(
				"The allowance of the {} deposit changed, current: {}. \
                Check concurrent deposits \
                for the same token network but different proxies.",
				data.amount_to_deposit, data.allowance,
			)))
		}

		let latest_deposit = self
			.token_network
			.participant_details(
				params.channel_identifier,
				self.account.address(),
				params.partner,
				Some(failed_at_blockhash),
			)
			.await?
			.deposit;
		if latest_deposit < params.total_deposit {
			return Err(ProxyError::Recoverable(format!("The tokens were not transferred")))
		}

		return Err(ProxyError::Recoverable(format!("deposit failed for an unknown reason")))
	}

	async fn estimate_gas(
		&self,
		params: Self::Params,
		_data: Self::Data,
	) -> Result<(GasLimit, GasPrice), ProxyError> {
		let nonce = self.account.peek_next_nonce().await;
		let gas_price = self.web3.eth().gas_price().await.map_err(ProxyError::Web3)?;

		self.token_network
			.contract
			.estimate_gas(
				"setTotalDeposit",
				(
					params.channel_identifier,
					self.account.address(),
					params.total_deposit,
					params.partner,
				),
				self.account.address(),
				Options::with(|opt| {
					opt.value = Some(GasLimit::from(0));
					opt.nonce = Some(nonce);
					opt.gas_price = Some(gas_price);
				}),
			)
			.await
			.map(|estimate| (estimate, gas_price))
			.map_err(ProxyError::ChainError)
	}

	async fn acquire_lock(&self) -> Option<RwLockWriteGuard<bool>> {
		Some(self.token.lock.write().await)
	}
}