Skip to content

Commit

Permalink
Update subxt (#673)
Browse files Browse the repository at this point in the history
* wip

* Wip: update subxt and also drop other runtimes

Note, this can be cleaned up more

* rename devnet to current
  • Loading branch information
muhamadazmy authored Apr 13, 2023
1 parent 31f4a35 commit 31d2888
Show file tree
Hide file tree
Showing 14 changed files with 939 additions and 2,532 deletions.
1,237 changes: 830 additions & 407 deletions clients/tfchain_client_rs/Cargo.lock

Large diffs are not rendered by default.

8 changes: 2 additions & 6 deletions clients/tfchain_client_rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ edition = "2021"
path = "src/lib.rs"
name = "tfchain_client"

[[bin]]
path = "src/main.rs"
name = "tfchain_cli"

[dependencies]
subxt = "0.25.0"
subxt-codegen = "0.25.0"
subxt = "0.28.0"
subxt-codegen = "0.28.0"
syn = "1.0.99"
tokio = { version = "1.8", features = ["rt-multi-thread", "macros", "time"] }
futures-util = "0.3.23"
Expand Down
Binary file removed clients/tfchain_client_rs/artifacts/local.scale
Binary file not shown.
Binary file removed clients/tfchain_client_rs/artifacts/mainnet.scale
Binary file not shown.
Binary file removed clients/tfchain_client_rs/artifacts/testnet.scale
Binary file not shown.
139 changes: 52 additions & 87 deletions clients/tfchain_client_rs/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,12 @@
use crate::runtimes::{devnet, local, mainnet, testnet, types};
use crate::runtimes::current;
pub use current::{BlockNumber, Contract, Farm, Node, SystemAccountInfo, Twin, H256 as Hash};
use std::str::FromStr;
use subxt::utils::AccountId32;
use subxt::{
ext::{
sp_core::{crypto::SecretStringError, ed25519, sr25519, Pair},
sp_runtime::AccountId32,
},
ext::sp_core::{crypto::SecretStringError, ed25519, sr25519, Pair},
tx::{PairSigner, Signer},
Error, OnlineClient, PolkadotConfig,
};
pub use types::{BlockNumber, Contract, Hash, SystemAccountInfo, TfgridFarm, TfgridNode, Twin};

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Runtime {
Local,
Devnet,
Testnet,
Mainnet,
}

impl FromStr for Runtime {
type Err = &'static str;

fn from_str(v: &str) -> Result<Self, Self::Err> {
match v {
"local" => Ok(Self::Local),
"devnet" => Ok(Self::Devnet),
"mainnet" => Ok(Self::Mainnet),
"testnet" => Ok(Self::Testnet),
_ => Err("unknown runtime"),
}
}
}

#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize)]
pub enum KeyType {
Expand Down Expand Up @@ -78,10 +54,38 @@ impl KeyPair {
Ok(pair)
}

pub fn signer(&self) -> Box<dyn Signer<PolkadotConfig> + Send + Sync> {
pub fn signer(&self) -> KeySigner {
match self {
Self::Ed25519(pair) => KeySigner::Ed25519(PairSigner::new(pair.clone())),
Self::Sr25519(pair) => KeySigner::Sr25519(PairSigner::new(pair.clone())),
}
}
}

pub enum KeySigner {
Sr25519(PairSigner<PolkadotConfig, sr25519::Pair>),
Ed25519(PairSigner<PolkadotConfig, ed25519::Pair>),
}

impl Signer<PolkadotConfig> for KeySigner {
fn account_id(&self) -> &<PolkadotConfig as subxt::Config>::AccountId {
match self {
Self::Sr25519(signer) => signer.account_id(),
Self::Ed25519(signer) => signer.account_id(),
}
}

fn address(&self) -> <PolkadotConfig as subxt::Config>::Address {
match self {
Self::Sr25519(signer) => signer.address(),
Self::Ed25519(signer) => signer.address(),
}
}

fn sign(&self, signer_payload: &[u8]) -> <PolkadotConfig as subxt::Config>::Signature {
match self {
Self::Ed25519(pair) => Box::new(PairSigner::new(pair.clone())),
Self::Sr25519(pair) => Box::new(PairSigner::new(pair.clone())),
Self::Sr25519(signer) => signer.sign(signer_payload),
Self::Ed25519(signer) => signer.sign(signer_payload),
}
}
}
Expand All @@ -100,26 +104,14 @@ impl From<ed25519::Pair> for KeyPair {

#[derive(Clone)]
pub struct Client {
pub runtime: Runtime,
pub api: OnlineClient<PolkadotConfig>,
}

macro_rules! call {
($self:ident, $name:ident, $($arg:expr),+) => (
match $self.runtime {
Runtime::Local => local::$name($self, $($arg),+).await,
Runtime::Devnet => devnet::$name($self, $($arg),+).await,
Runtime::Testnet => testnet::$name($self, $($arg),+).await,
Runtime::Mainnet => mainnet::$name($self, $($arg),+).await,
}
)
}

impl Client {
pub async fn new<U: AsRef<str>>(url: U, runtime: Runtime) -> Result<Client, Error> {
pub async fn new<U: AsRef<str>>(url: U) -> Result<Client, Error> {
let api = OnlineClient::<PolkadotConfig>::from_url(url).await?;

Ok(Client { api, runtime })
Ok(Client { api })
}

// Creates a twin and checks for success, twin ID is returned on success
Expand All @@ -129,7 +121,7 @@ impl Client {
relay: Option<String>,
pk: Option<String>,
) -> Result<u32, Error> {
call!(self, create_twin, kp, relay, pk)
current::create_twin(self, kp, relay, pk).await
}

// Updates a twin and checks for success, blockhash is returned on success
Expand All @@ -139,7 +131,7 @@ impl Client {
relay: Option<String>,
pk: Option<&[u8]>,
) -> Result<Hash, Error> {
call!(self, update_twin, kp, relay, pk)
current::update_twin(self, kp, relay, pk).await
}

// Signs terms and condition and checks for success, blockhash is returned on success
Expand All @@ -149,67 +141,40 @@ impl Client {
document_link: String,
document_hash: String,
) -> Result<Hash, Error> {
call!(
self,
sign_terms_and_conditions,
kp,
document_link,
document_hash
)
current::sign_terms_and_conditions(self, kp, document_link, document_hash).await
}

pub async fn get_twin_by_id(
&self,
id: u32,
at_block: Option<Hash>,
) -> Result<Option<Twin>, Error> {
call!(self, get_twin_by_id, id, at_block)
pub async fn get_twin_by_id(&self, id: u32) -> Result<Option<Twin>, Error> {
current::get_twin_by_id(self, id).await
}

pub async fn get_twin_id_by_account(
&self,
account: AccountId32,
at_block: Option<types::Hash>,
) -> Result<Option<u32>, Error> {
call!(self, get_twin_id_by_account, account, at_block)
pub async fn get_twin_id_by_account(&self, account: AccountId32) -> Result<Option<u32>, Error> {
current::get_twin_id_by_account(self, account).await
}

pub async fn get_farm_by_id(
&self,
id: u32,
at_block: Option<Hash>,
) -> Result<Option<TfgridFarm>, Error> {
call!(self, get_farm_by_id, id, at_block)
pub async fn get_farm_by_id(&self, id: u32) -> Result<Option<Farm>, Error> {
current::get_farm_by_id(self, id).await
}

pub async fn get_node_by_id(
&self,
id: u32,
at_block: Option<Hash>,
) -> Result<Option<TfgridNode>, Error> {
call!(self, get_node_by_id, id, at_block)
pub async fn get_node_by_id(&self, id: u32) -> Result<Option<Node>, Error> {
current::get_node_by_id(self, id).await
}

pub async fn get_balance(
&self,
account: &AccountId32,
at_block: Option<Hash>,
) -> Result<Option<SystemAccountInfo>, Error> {
call!(self, get_balance, account, at_block)
current::get_balance(self, account).await
}

pub async fn get_block_hash(
&self,
block_number: Option<BlockNumber>,
) -> Result<Option<Hash>, Error> {
call!(self, get_block_hash, block_number)
current::get_block_hash(self, block_number).await
}

pub async fn get_contract_by_id(
&self,
id: u64,
at_block: Option<Hash>,
) -> Result<Option<Contract>, Error> {
call!(self, get_contract_by_id, id, at_block)
pub async fn get_contract_by_id(&self, id: u64) -> Result<Option<Contract>, Error> {
current::get_contract_by_id(self, id).await
}
}
142 changes: 0 additions & 142 deletions clients/tfchain_client_rs/src/main.rs

This file was deleted.

Loading

0 comments on commit 31d2888

Please sign in to comment.