Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 731a136

Browse files
author
Tyera Eulberg
authored
Token client improvements (#3139)
* Use-stmt cleanup * Use nonblocking RpcClient * Arc-wrap RpcClient * Confirm tx when using RpcClient * Bump version for releasing
1 parent d087b57 commit 731a136

File tree

6 files changed

+79
-65
lines changed

6 files changed

+79
-65
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

token/client/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2018"
55
license = "Apache-2.0"
66
name = "spl-token-client"
77
repository = "https://github.com/solana-labs/solana-program-library"
8-
version = "0.0.1"
8+
version = "0.1.0"
99

1010
[dependencies]
1111
async-trait = "0.1"

token/client/src/client.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
use async_trait::async_trait;
2-
use solana_client::rpc_client::RpcClient;
3-
use solana_program_test::{tokio::sync::Mutex, BanksClient, ProgramTestContext};
4-
use solana_sdk::{
5-
account::Account, hash::Hash, pubkey::Pubkey, signature::Signature, transaction::Transaction,
1+
use {
2+
async_trait::async_trait,
3+
solana_client::nonblocking::rpc_client::RpcClient,
4+
solana_program_test::{tokio::sync::Mutex, BanksClient, ProgramTestContext},
5+
solana_sdk::{
6+
account::Account, hash::Hash, pubkey::Pubkey, signature::Signature,
7+
transaction::Transaction,
8+
},
9+
std::{fmt, future::Future, pin::Pin, sync::Arc},
610
};
7-
use std::{fmt, future::Future, pin::Pin, sync::Arc};
811

912
type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
1013

@@ -69,7 +72,12 @@ impl SendTransactionRpc for ProgramRpcClientSendTransaction {
6972
client: &'a RpcClient,
7073
transaction: &'a Transaction,
7174
) -> BoxFuture<'a, ProgramClientResult<Self::Output>> {
72-
Box::pin(async move { client.send_transaction(transaction).map_err(Into::into) })
75+
Box::pin(async move {
76+
client
77+
.send_and_confirm_transaction(transaction)
78+
.await
79+
.map_err(Into::into)
80+
})
7381
}
7482
}
7583

@@ -184,25 +192,25 @@ where
184192
}
185193

186194
/// Program client for `RpcClient` from crate `solana-client`.
187-
pub struct ProgramRpcClient<'a, ST> {
188-
client: &'a RpcClient,
195+
pub struct ProgramRpcClient<ST> {
196+
client: Arc<RpcClient>,
189197
send: ST,
190198
}
191199

192-
impl<ST> fmt::Debug for ProgramRpcClient<'_, ST> {
200+
impl<ST> fmt::Debug for ProgramRpcClient<ST> {
193201
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
194202
f.debug_struct("ProgramRpcClient").finish()
195203
}
196204
}
197205

198-
impl<'a, ST> ProgramRpcClient<'a, ST> {
199-
pub fn new(client: &'a RpcClient, send: ST) -> Self {
206+
impl<ST> ProgramRpcClient<ST> {
207+
pub fn new(client: Arc<RpcClient>, send: ST) -> Self {
200208
Self { client, send }
201209
}
202210
}
203211

204212
#[async_trait]
205-
impl<ST> ProgramClient<ST> for ProgramRpcClient<'_, ST>
213+
impl<ST> ProgramClient<ST> for ProgramRpcClient<ST>
206214
where
207215
ST: SendTransactionRpc + Send + Sync,
208216
{
@@ -212,21 +220,23 @@ where
212220
) -> ProgramClientResult<u64> {
213221
self.client
214222
.get_minimum_balance_for_rent_exemption(data_len)
223+
.await
215224
.map_err(Into::into)
216225
}
217226

218227
async fn get_latest_blockhash(&self) -> ProgramClientResult<Hash> {
219-
self.client.get_latest_blockhash().map_err(Into::into)
228+
self.client.get_latest_blockhash().await.map_err(Into::into)
220229
}
221230

222231
async fn send_transaction(&self, transaction: &Transaction) -> ProgramClientResult<ST::Output> {
223-
self.send.send(self.client, transaction).await
232+
self.send.send(&self.client, transaction).await
224233
}
225234

226235
async fn get_account(&self, address: Pubkey) -> ProgramClientResult<Option<Account>> {
227236
Ok(self
228237
.client
229-
.get_account_with_commitment(&address, self.client.commitment())?
238+
.get_account_with_commitment(&address, self.client.commitment())
239+
.await?
230240
.value)
231241
}
232242
}

token/client/src/token.rs

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,41 @@
1-
use crate::client::{ProgramClient, ProgramClientError, SendTransaction};
2-
use solana_program_test::tokio::time;
3-
use solana_sdk::{
4-
account::Account as BaseAccount,
5-
epoch_info::EpochInfo,
6-
hash::Hash,
7-
instruction::Instruction,
8-
program_error::ProgramError,
9-
pubkey::Pubkey,
10-
signer::{signers::Signers, Signer},
11-
system_instruction,
12-
transaction::Transaction,
13-
};
14-
use spl_associated_token_account::{
15-
get_associated_token_address_with_program_id, instruction::create_associated_token_account,
16-
};
17-
use spl_token_2022::{
18-
extension::{
19-
confidential_transfer, default_account_state, memo_transfer, transfer_fee, ExtensionType,
20-
StateWithExtensionsOwned,
1+
use {
2+
crate::client::{ProgramClient, ProgramClientError, SendTransaction},
3+
solana_program_test::tokio::time,
4+
solana_sdk::{
5+
account::Account as BaseAccount,
6+
epoch_info::EpochInfo,
7+
hash::Hash,
8+
instruction::Instruction,
9+
program_error::ProgramError,
10+
pubkey::Pubkey,
11+
signer::{signers::Signers, Signer},
12+
system_instruction,
13+
transaction::Transaction,
2114
},
22-
instruction, native_mint,
23-
solana_zk_token_sdk::{
24-
encryption::{auth_encryption::*, elgamal::*},
25-
errors::ProofError,
26-
instruction::transfer_with_fee::FeeParameters,
15+
spl_associated_token_account::{
16+
get_associated_token_address_with_program_id, instruction::create_associated_token_account,
2717
},
28-
state::{Account, AccountState, Mint},
29-
};
30-
use std::{
31-
convert::TryInto,
32-
fmt, io,
33-
sync::{Arc, RwLock},
34-
time::{Duration, Instant},
18+
spl_token_2022::{
19+
extension::{
20+
confidential_transfer, default_account_state, memo_transfer, transfer_fee,
21+
ExtensionType, StateWithExtensionsOwned,
22+
},
23+
instruction, native_mint,
24+
solana_zk_token_sdk::{
25+
encryption::{auth_encryption::*, elgamal::*},
26+
errors::ProofError,
27+
instruction::transfer_with_fee::FeeParameters,
28+
},
29+
state::{Account, AccountState, Mint},
30+
},
31+
std::{
32+
convert::TryInto,
33+
fmt, io,
34+
sync::{Arc, RwLock},
35+
time::{Duration, Instant},
36+
},
37+
thiserror::Error,
3538
};
36-
use thiserror::Error;
3739

3840
#[derive(Error, Debug)]
3941
pub enum TokenError {

token/client/tests/program-test.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
use solana_program_test::{
2-
tokio::{self, sync::Mutex},
3-
ProgramTest,
1+
use {
2+
solana_program_test::{
3+
tokio::{self, sync::Mutex},
4+
ProgramTest,
5+
},
6+
solana_sdk::{
7+
program_option::COption,
8+
signer::{keypair::Keypair, Signer},
9+
},
10+
spl_token_2022::{instruction, state},
11+
spl_token_client::{
12+
client::{ProgramBanksClient, ProgramBanksClientProcessTransaction, ProgramClient},
13+
token::Token,
14+
},
15+
std::sync::Arc,
416
};
5-
use solana_sdk::{
6-
program_option::COption,
7-
signer::{keypair::Keypair, Signer},
8-
};
9-
use spl_token_2022::{instruction, state};
10-
use spl_token_client::{
11-
client::{ProgramBanksClient, ProgramBanksClientProcessTransaction, ProgramClient},
12-
token::Token,
13-
};
14-
use std::sync::Arc;
1517

1618
struct TestContext {
1719
pub decimals: u8,

token/program-2022-test/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ solana-sdk = "=1.10.10"
2121
spl-associated-token-account = { version = "1.0.5", path = "../../associated-token-account/program" }
2222
spl-memo = { version = "3.0.1", path = "../../memo/program", features = ["no-entrypoint"] }
2323
spl-token-2022 = { version = "0.3", path="../program-2022", features = ["no-entrypoint"] }
24-
spl-token-client = { version = "0.0.1", path = "../client" }
24+
spl-token-client = { version = "0.1.0", path = "../client" }

0 commit comments

Comments
 (0)