1pub mod batch;
6
7use anyhow::Result;
8use subxt::backend::legacy::LegacyRpcMethods;
9use subxt::backend::rpc::RpcClient;
10use subxt::{OnlineClient, PolkadotConfig};
11use subxt_signer::sr25519::Keypair;
12
13pub type GlinConfig = PolkadotConfig;
14pub type GlinClient = OnlineClient<GlinConfig>;
15
16pub use batch::BatchRpc;
18
19pub async fn create_client(rpc_url: &str) -> Result<GlinClient> {
32 let client = OnlineClient::<GlinConfig>::from_url(rpc_url).await?;
33 Ok(client)
34}
35
36pub async fn create_rpc_client(rpc_url: &str) -> Result<LegacyRpcMethods<GlinConfig>> {
38 let rpc_client = RpcClient::from_url(rpc_url).await?;
39 Ok(LegacyRpcMethods::<GlinConfig>::new(rpc_client))
40}
41
42pub fn get_dev_account(name: &str) -> Result<Keypair> {
46 use subxt_signer::sr25519::dev;
47
48 let keypair = match name.to_lowercase().as_str() {
49 "alice" => dev::alice(),
50 "bob" => dev::bob(),
51 "charlie" => dev::charlie(),
52 "dave" => dev::dave(),
53 "eve" => dev::eve(),
54 "ferdie" => dev::ferdie(),
55 _ => anyhow::bail!(
56 "Unknown dev account: {}. Use alice, bob, charlie, dave, eve, or ferdie",
57 name
58 ),
59 };
60
61 Ok(keypair)
62}
63
64pub fn account_from_seed(seed: &str) -> Result<Keypair> {
70 use std::str::FromStr;
71 use subxt_signer::SecretUri;
72
73 if let Ok(uri) = SecretUri::from_str(seed) {
75 return Keypair::from_uri(&uri)
76 .map_err(|e| anyhow::anyhow!("Failed to create keypair from URI: {:?}", e));
77 }
78
79 use subxt_signer::bip39::Mnemonic;
81 if let Ok(mnemonic) = Mnemonic::parse(seed) {
82 return Keypair::from_phrase(&mnemonic, None)
83 .map_err(|e| anyhow::anyhow!("Failed to create keypair from phrase: {:?}", e));
84 }
85
86 anyhow::bail!("Invalid seed format. Use a secret URI (e.g., //Alice) or mnemonic phrase")
87}
88
89pub fn get_address(keypair: &Keypair) -> String {
93 use subxt::utils::AccountId32;
94
95 let account_id: AccountId32 = keypair.public_key().into();
96 format!("{:?}", account_id)
97}
98
99#[cfg(test)]
100mod tests {
101 use super::*;
102
103 #[test]
104 fn test_dev_accounts() {
105 let alice = get_dev_account("alice");
106 assert!(alice.is_ok());
107
108 let invalid = get_dev_account("invalid");
109 assert!(invalid.is_err());
110 }
111
112 #[test]
113 fn test_secret_uri() {
114 let keypair = account_from_seed("//Alice");
115 assert!(keypair.is_ok());
116 }
117}