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
use async_trait::async_trait;
use solana_client::rpc_client::RpcClient;
use solana_program_test::{tokio::sync::Mutex, BanksClient, ProgramTestContext};
use solana_sdk::{
account::Account, hash::Hash, pubkey::Pubkey, signature::Signature, transaction::Transaction,
};
use std::{fmt, future::Future, pin::Pin, sync::Arc};
type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
pub trait SendTransaction {
type Output;
}
pub trait SendTransactionBanksClient: SendTransaction {
fn send<'a>(
&self,
client: &'a mut BanksClient,
transaction: Transaction,
) -> BoxFuture<'a, ProgramClientResult<Self::Output>>;
}
#[derive(Debug, Clone, Copy, Default)]
pub struct ProgramBanksClientProcessTransaction;
impl SendTransaction for ProgramBanksClientProcessTransaction {
type Output = ();
}
impl SendTransactionBanksClient for ProgramBanksClientProcessTransaction {
fn send<'a>(
&self,
client: &'a mut BanksClient,
transaction: Transaction,
) -> BoxFuture<'a, ProgramClientResult<Self::Output>> {
Box::pin(async move {
client
.process_transaction(transaction)
.await
.map_err(Into::into)
})
}
}
pub trait SendTransactionRpc: SendTransaction {
fn send<'a>(
&self,
client: &'a RpcClient,
transaction: &'a Transaction,
) -> BoxFuture<'a, ProgramClientResult<Self::Output>>;
}
#[derive(Debug, Clone, Copy, Default)]
pub struct ProgramRpcClientSendTransaction;
impl SendTransaction for ProgramRpcClientSendTransaction {
type Output = Signature;
}
impl SendTransactionRpc for ProgramRpcClientSendTransaction {
fn send<'a>(
&self,
client: &'a RpcClient,
transaction: &'a Transaction,
) -> BoxFuture<'a, ProgramClientResult<Self::Output>> {
Box::pin(async move { client.send_transaction(transaction).map_err(Into::into) })
}
}
pub type ProgramClientError = Box<dyn std::error::Error + Send + Sync>;
pub type ProgramClientResult<T> = Result<T, ProgramClientError>;
#[async_trait]
pub trait ProgramClient<ST>
where
ST: SendTransaction,
{
async fn get_minimum_balance_for_rent_exemption(
&self,
data_len: usize,
) -> ProgramClientResult<u64>;
async fn get_latest_blockhash(&self) -> ProgramClientResult<Hash>;
async fn send_transaction(&self, transaction: &Transaction) -> ProgramClientResult<ST::Output>;
async fn get_account(&self, address: Pubkey) -> ProgramClientResult<Option<Account>>;
}
enum ProgramBanksClientContext {
Client(Arc<Mutex<BanksClient>>),
Context(Arc<Mutex<ProgramTestContext>>),
}
pub struct ProgramBanksClient<ST> {
context: ProgramBanksClientContext,
send: ST,
}
impl<ST> fmt::Debug for ProgramBanksClient<ST> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ProgramBanksClient").finish()
}
}
impl<ST> ProgramBanksClient<ST> {
fn new(context: ProgramBanksClientContext, send: ST) -> Self {
Self { context, send }
}
pub fn new_from_client(client: Arc<Mutex<BanksClient>>, send: ST) -> Self {
Self::new(ProgramBanksClientContext::Client(client), send)
}
pub fn new_from_context(context: Arc<Mutex<ProgramTestContext>>, send: ST) -> Self {
Self::new(ProgramBanksClientContext::Context(context), send)
}
async fn run_in_lock<F, O>(&self, f: F) -> O
where
for<'a> F: Fn(&'a mut BanksClient) -> BoxFuture<'a, O>,
{
match &self.context {
ProgramBanksClientContext::Client(client) => {
let mut lock = client.lock().await;
f(&mut lock).await
}
ProgramBanksClientContext::Context(context) => {
let mut lock = context.lock().await;
f(&mut lock.banks_client).await
}
}
}
}
#[async_trait]
impl<ST> ProgramClient<ST> for ProgramBanksClient<ST>
where
ST: SendTransactionBanksClient + Send + Sync,
{
async fn get_minimum_balance_for_rent_exemption(
&self,
data_len: usize,
) -> ProgramClientResult<u64> {
self.run_in_lock(|client| {
Box::pin(async move {
let rent = client.get_rent().await?;
Ok(rent.minimum_balance(data_len))
})
})
.await
}
async fn get_latest_blockhash(&self) -> ProgramClientResult<Hash> {
self.run_in_lock(|client| {
Box::pin(async move { client.get_latest_blockhash().await.map_err(Into::into) })
})
.await
}
async fn send_transaction(&self, transaction: &Transaction) -> ProgramClientResult<ST::Output> {
self.run_in_lock(|client| {
let transaction = transaction.clone();
self.send.send(client, transaction)
})
.await
}
async fn get_account(&self, address: Pubkey) -> ProgramClientResult<Option<Account>> {
self.run_in_lock(|client| {
Box::pin(async move { client.get_account(address).await.map_err(Into::into) })
})
.await
}
}
pub struct ProgramRpcClient<'a, ST> {
client: &'a RpcClient,
send: ST,
}
impl<ST> fmt::Debug for ProgramRpcClient<'_, ST> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ProgramRpcClient").finish()
}
}
impl<'a, ST> ProgramRpcClient<'a, ST> {
pub fn new(client: &'a RpcClient, send: ST) -> Self {
Self { client, send }
}
}
#[async_trait]
impl<ST> ProgramClient<ST> for ProgramRpcClient<'_, ST>
where
ST: SendTransactionRpc + Send + Sync,
{
async fn get_minimum_balance_for_rent_exemption(
&self,
data_len: usize,
) -> ProgramClientResult<u64> {
self.client
.get_minimum_balance_for_rent_exemption(data_len)
.map_err(Into::into)
}
async fn get_latest_blockhash(&self) -> ProgramClientResult<Hash> {
self.client.get_latest_blockhash().map_err(Into::into)
}
async fn send_transaction(&self, transaction: &Transaction) -> ProgramClientResult<ST::Output> {
self.send.send(self.client, transaction).await
}
async fn get_account(&self, address: Pubkey) -> ProgramClientResult<Option<Account>> {
Ok(self
.client
.get_account_with_commitment(&address, self.client.commitment())?
.value)
}
}