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
use std::{fmt::Debug, sync::Arc};

use jsonrpsee::{core::RpcResult, proc_macros::rpc};
use pallet_pool_system::{
	tranches::{TrancheIndex, TrancheLoc, TrancheSolution},
	EpochSolution,
};
use parity_scale_codec::Codec;
use runtime_common::apis::PoolsApi as PoolsRuntimeApi;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_runtime::traits::{Block as BlockT, Get};

use crate::rpc::{invalid_params_error, runtime_error};

#[rpc(client, server)]
pub trait PoolsApi<PoolId, TrancheId, Balance, Currency, BalanceRatio, BlockHash, MaxTranches>
where
	MaxTranches: Get<u32>,
{
	#[method(name = "pools_currency")]
	fn currency(&self, poold_id: PoolId, at: Option<BlockHash>) -> RpcResult<Currency>;

	#[method(name = "pools_inspectEpochSolution")]
	fn inspect_epoch_solution(
		&self,
		pool_id: PoolId,
		solution: Vec<TrancheSolution>,
		at: Option<BlockHash>,
	) -> RpcResult<EpochSolution<Balance, MaxTranches>>;

	#[method(name = "pools_trancheTokenPrice")]
	fn tranche_token_price(
		&self,
		pool_id: PoolId,
		tranche: TrancheId,
		at: Option<BlockHash>,
	) -> RpcResult<BalanceRatio>;

	#[method(name = "pools_trancheTokenPrices")]
	fn tranche_token_prices(
		&self,
		pool_id: PoolId,
		at: Option<BlockHash>,
	) -> RpcResult<Vec<BalanceRatio>>;

	#[method(name = "pools_trancheIds")]
	fn tranche_ids(&self, pool_id: PoolId, at: Option<BlockHash>) -> RpcResult<Vec<TrancheId>>;

	#[method(name = "pools_trancheId")]
	fn tranche_id(
		&self,
		pool_id: PoolId,
		tranche_index: TrancheIndex,
		at: Option<BlockHash>,
	) -> RpcResult<TrancheId>;

	#[method(name = "pools_trancheCurrency")]
	fn tranche_currency(
		&self,
		pool_id: PoolId,
		tranche_id: TrancheId,
		at: Option<BlockHash>,
	) -> RpcResult<Currency>;
}

pub struct Pools<C, P> {
	client: Arc<C>,
	_marker: std::marker::PhantomData<P>,
}

impl<C, P> Pools<C, P> {
	pub fn new(client: Arc<C>) -> Self {
		Pools {
			client,
			_marker: Default::default(),
		}
	}
}

impl<C, Block, PoolId, TrancheId, Balance, Currency, BalanceRatio, MaxTranches>
	PoolsApiServer<PoolId, TrancheId, Balance, Currency, BalanceRatio, Block::Hash, MaxTranches>
	for Pools<C, Block>
where
	Block: BlockT,
	C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
	C::Api: PoolsRuntimeApi<Block, PoolId, TrancheId, Balance, Currency, BalanceRatio, MaxTranches>,
	Balance: Codec + Copy,
	PoolId: Codec + Copy + Debug,
	TrancheId: Codec + Clone + Debug,
	Currency: Codec,
	BalanceRatio: Codec,
	MaxTranches: Codec + Get<u32>,
{
	fn currency(&self, pool_id: PoolId, at: Option<Block::Hash>) -> RpcResult<Currency> {
		let api = self.client.runtime_api();
		let at = match at {
			Some(hash) => hash,
			None => self.client.info().best_hash,
		};

		api.currency(at, pool_id)
			.map_err(|e| runtime_error("Unable to query pool currency", e))?
			.ok_or_else(|| invalid_params_error("Pool not found"))
	}

	fn inspect_epoch_solution(
		&self,
		pool_id: PoolId,
		solution: Vec<TrancheSolution>,
		at: Option<Block::Hash>,
	) -> RpcResult<EpochSolution<Balance, MaxTranches>> {
		let api = self.client.runtime_api();
		let at = match at {
			Some(hash) => hash,
			None => self.client.info().best_hash,
		};

		api.inspect_epoch_solution(at, pool_id, solution)
			.map_err(|e| runtime_error("Unable to query inspection for epoch solution", e))?
			.ok_or_else(|| invalid_params_error("Pool not found or invalid solution"))
	}

	fn tranche_token_price(
		&self,
		pool_id: PoolId,
		tranche_id: TrancheId,
		at: Option<Block::Hash>,
	) -> RpcResult<BalanceRatio> {
		let api = self.client.runtime_api();
		let at = match at {
			Some(hash) => hash,
			None => self.client.info().best_hash,
		};

		api.tranche_token_price(at, pool_id, TrancheLoc::Id(tranche_id))
			.map_err(|e| runtime_error("Unable to query tranche token price", e))?
			.ok_or_else(|| invalid_params_error("Pool or tranche not found"))
	}

	fn tranche_token_prices(
		&self,
		pool_id: PoolId,
		at: Option<Block::Hash>,
	) -> RpcResult<Vec<BalanceRatio>> {
		let api = self.client.runtime_api();
		let at = match at {
			Some(hash) => hash,
			None => self.client.info().best_hash,
		};

		api.tranche_token_prices(at, pool_id)
			.map_err(|e| runtime_error("Unable to query tranche token prices.", e))?
			.ok_or_else(|| invalid_params_error("Pool not found."))
	}

	fn tranche_ids(&self, pool_id: PoolId, at: Option<Block::Hash>) -> RpcResult<Vec<TrancheId>> {
		let api = self.client.runtime_api();
		let at = match at {
			Some(hash) => hash,
			None => self.client.info().best_hash,
		};

		api.tranche_ids(at, pool_id)
			.map_err(|e| runtime_error("Unable to query tranche ids.", e))?
			.ok_or_else(|| invalid_params_error("Pool not found"))
	}

	fn tranche_id(
		&self,
		pool_id: PoolId,
		tranche_index: TrancheIndex,
		at: Option<Block::Hash>,
	) -> RpcResult<TrancheId> {
		let api = self.client.runtime_api();
		let at = match at {
			Some(hash) => hash,
			None => self.client.info().best_hash,
		};

		api.tranche_id(at, pool_id, tranche_index)
			.map_err(|e| runtime_error("Unable to query tranche ids.", e))?
			.ok_or_else(|| invalid_params_error("Pool or tranche not found."))
	}

	fn tranche_currency(
		&self,
		pool_id: PoolId,
		tranche_id: TrancheId,
		at: Option<Block::Hash>,
	) -> RpcResult<Currency> {
		let api = self.client.runtime_api();
		let at = match at {
			Some(hash) => hash,
			None => self.client.info().best_hash,
		};

		api.tranche_currency(at, pool_id, TrancheLoc::Id(tranche_id))
			.map_err(|e| runtime_error("Unable to query tranche currency.", e))?
			.ok_or_else(|| invalid_params_error("Pool or tranche not found."))
	}
}