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

Commit 3d42786

Browse files
joncinque2501babe
authored andcommitted
token-cli: Add no-op client
1 parent 008809e commit 3d42786

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

token/cli/src/config.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use crate::{signers_of, Error, MULTISIG_SIGNER_ARG};
22
use clap::ArgMatches;
33
use solana_clap_utils::{
4-
input_parsers::{pubkey_of, pubkey_of_signer},
4+
input_parsers::{pubkey_of, pubkey_of_signer, value_of},
55
input_validators::normalize_to_url_if_moniker,
66
keypair::{signer_from_path, signer_from_path_with_config, SignerFromPathConfig},
77
nonce::{NONCE_ARG, NONCE_AUTHORITY_ARG},
8-
offline::{DUMP_TRANSACTION_MESSAGE, SIGN_ONLY_ARG},
8+
offline::{BLOCKHASH_ARG, DUMP_TRANSACTION_MESSAGE, SIGN_ONLY_ARG},
99
};
1010
use solana_cli_output::OutputFormat;
1111
use solana_client::nonblocking::rpc_client::RpcClient;
@@ -16,7 +16,9 @@ use spl_token_2022::{
1616
extension::StateWithExtensionsOwned,
1717
state::{Account, Mint},
1818
};
19-
use spl_token_client::client::{ProgramClient, ProgramRpcClient, ProgramRpcClientSendTransaction};
19+
use spl_token_client::client::{
20+
ProgramClient, ProgramOfflineClient, ProgramRpcClient, ProgramRpcClientSendTransaction,
21+
};
2022
use std::{process::exit, sync::Arc};
2123

2224
pub(crate) struct MintInfo {
@@ -65,9 +67,19 @@ impl<'a> Config<'a> {
6567
json_rpc_url,
6668
CommitmentConfig::confirmed(),
6769
));
68-
let program_client: Arc<dyn ProgramClient<ProgramRpcClientSendTransaction>> = Arc::new(
69-
ProgramRpcClient::new(rpc_client.clone(), ProgramRpcClientSendTransaction),
70-
);
70+
let sign_only = matches.is_present(SIGN_ONLY_ARG.name);
71+
let program_client: Arc<dyn ProgramClient<ProgramRpcClientSendTransaction>> = if sign_only {
72+
let blockhash = value_of(matches, BLOCKHASH_ARG.name);
73+
Arc::new(ProgramOfflineClient::new(
74+
blockhash,
75+
ProgramRpcClientSendTransaction,
76+
))
77+
} else {
78+
Arc::new(ProgramRpcClient::new(
79+
rpc_client.clone(),
80+
ProgramRpcClientSendTransaction,
81+
))
82+
};
7183
Self::new_with_clients_and_ws_url(
7284
matches,
7385
wallet_manager,

token/client/src/client.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,49 @@ where
240240
.value)
241241
}
242242
}
243+
244+
/// Program client for offline signing.
245+
pub struct ProgramOfflineClient<ST> {
246+
maybe_blockhash: Option<Hash>,
247+
_send: ST,
248+
}
249+
250+
impl<ST> fmt::Debug for ProgramOfflineClient<ST> {
251+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
252+
f.debug_struct("ProgramOfflineClient").finish()
253+
}
254+
}
255+
256+
impl<ST> ProgramOfflineClient<ST> {
257+
pub fn new(maybe_blockhash: Option<Hash>, send: ST) -> Self {
258+
Self {
259+
maybe_blockhash,
260+
_send: send,
261+
}
262+
}
263+
}
264+
265+
#[async_trait]
266+
impl<ST> ProgramClient<ST> for ProgramOfflineClient<ST>
267+
where
268+
ST: SendTransaction<Output = Signature> + Send + Sync,
269+
{
270+
async fn get_minimum_balance_for_rent_exemption(
271+
&self,
272+
_data_len: usize,
273+
) -> ProgramClientResult<u64> {
274+
Err("Unable to fetch minimum blance for rent exemption in offline mode".into())
275+
}
276+
277+
async fn get_latest_blockhash(&self) -> ProgramClientResult<Hash> {
278+
Ok(self.maybe_blockhash.unwrap())
279+
}
280+
281+
async fn send_transaction(&self, transaction: &Transaction) -> ProgramClientResult<ST::Output> {
282+
Ok(*transaction.signatures.first().expect("need a signature"))
283+
}
284+
285+
async fn get_account(&self, _address: Pubkey) -> ProgramClientResult<Option<Account>> {
286+
Err("Unable to fetch account in offline mode".into())
287+
}
288+
}

0 commit comments

Comments
 (0)