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

use raiden_primitives::{
	constants::LOCKSROOT_OF_NO_LOCKS,
	types::{
		Address,
		BlockHash,
		BlockId,
		ChannelIdentifier,
		GasLimit,
		GasPrice,
		TokenAmount,
		TransactionHash,
	},
};
use raiden_state_machine::{
	machine::channel::utils::compute_locksroot,
	types::{
		ChannelStatus,
		PendingLocksState,
	},
};
use web3::{
	contract::Options,
	types::BlockNumber,
	Transport,
	Web3,
};

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

/// On-chain data to validate unlocking a channel
#[derive(Clone)]
pub struct ChannelUnlockTransactionData {
	channel_onchain_details: ChannelData,
	sender_details: ParticipantDetails,
}

/// Parameters required for unlocking a channel.
#[derive(Clone)]
pub struct ChannelUnlockTransactionParams {
	pub(crate) channel_identifier: ChannelIdentifier,
	pub(crate) sender: Address,
	pub(crate) receiver: Address,
	pub(crate) pending_locks: PendingLocksState,
}

/// Channel unlock transaction type.
pub struct ChannelUnlockTransaction<T: Transport> {
	pub(crate) web3: Web3<T>,
	pub(crate) account: Account<T>,
	pub(crate) token_network: TokenNetworkProxy<T>,
	pub(crate) gas_metadata: Arc<GasMetadata>,
}

#[async_trait::async_trait]
impl<T> Transaction for ChannelUnlockTransaction<T>
where
	T: Transport + Send + Sync,
	T::Out: Send,
{
	type Output = TransactionHash;
	type Params = ChannelUnlockTransactionParams;
	type Data = ChannelUnlockTransactionData;

	async fn onchain_data(
		&self,
		params: Self::Params,
		at_block_hash: BlockHash,
	) -> Result<Self::Data, ProxyError> {
		let channel_onchain_details = self
			.token_network
			.channel_details(
				Some(params.channel_identifier),
				params.sender,
				params.receiver,
				at_block_hash,
			)
			.await?;

		let sender_details = self
			.token_network
			.participant_details(
				params.channel_identifier,
				params.sender,
				params.receiver,
				Some(at_block_hash),
			)
			.await?;

		Ok(ChannelUnlockTransactionData { channel_onchain_details, sender_details })
	}

	async fn validate_preconditions(
		&self,
		params: Self::Params,
		data: Self::Data,
		_block: BlockHash,
	) -> Result<(), ProxyError> {
		if data.channel_onchain_details.status != ChannelStatus::Settled {
			return Err(ProxyError::BrokenPrecondition(
				"The channel was not settled at the provided block".to_owned(),
			))
		}

		let local_locksroot = compute_locksroot(&params.pending_locks);
		if data.sender_details.locksroot != local_locksroot {
			return Err(ProxyError::BrokenPrecondition(
				"The provided locksroot does not correspond to the on-chain locksroot".to_owned(),
			))
		}
		if data.sender_details.locked_amount == TokenAmount::zero() {
			return Err(ProxyError::BrokenPrecondition(
				"The provided locked amount on-chain is 0".to_owned(),
			))
		}

		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;

		let leaves_packed = params.pending_locks.locks.iter().fold(vec![], |mut current, lock| {
			current.extend_from_slice(&lock.0);
			current
		});
		let receipt = self
			.token_network
			.contract
			.signed_call_with_confirmations(
				"unlock",
				(
					params.channel_identifier,
					self.account.address(),
					params.sender,
					params.receiver,
					leaves_packed,
				),
				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(receipt.transaction_hash)
	}

	async fn validate_postconditions(
		&self,
		params: Self::Params,
		_block: 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.unlock").into(),
				failed_at_blocknumber,
			)
			.await?;

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

		let channel_status = data.channel_onchain_details.status;
		if channel_status == ChannelStatus::Opened ||
			channel_status == ChannelStatus::Closed ||
			channel_status == ChannelStatus::Closing
		{
			return Err(ProxyError::Recoverable(
				"Cannot call close on a channel that has not been settled already".to_owned(),
			))
		}

		if data.sender_details.locksroot == *LOCKSROOT_OF_NO_LOCKS {
			return Err(ProxyError::Recoverable("The locks are already unlocked".to_owned()))
		}

		Err(ProxyError::Recoverable(format!(
			"Unlock channel failed. Gas estimation failed for
            unknown reason. Reference block {} - {}",
			failed_at_blockhash, failed_at_blocknumber,
		)))
	}

	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)?;

		let leaves_packed = params.pending_locks.locks.iter().fold(vec![], |mut current, lock| {
			current.extend_from_slice(&lock.0);
			current
		});
		self.token_network
			.contract
			.estimate_gas(
				"unlock",
				(
					params.channel_identifier,
					self.account.address(),
					params.sender,
					params.receiver,
					leaves_packed,
				),
				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)
	}
}