Skip to content

Commit

Permalink
Added tx index and block number to rust sdk txs (#646)
Browse files Browse the repository at this point in the history
  • Loading branch information
markopoloparadox authored Aug 27, 2024
1 parent 2d53922 commit a6600ea
Show file tree
Hide file tree
Showing 6 changed files with 301 additions and 32 deletions.
1 change: 1 addition & 0 deletions avail-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ pub use primitives::block::{
pub use primitives::kate::{Cell, GDataProof, GRow};
pub use subxt;
pub use subxt::config::polkadot::U256;
pub use subxt_signer;
pub use utils::utils_raw;
pub use utils::FetchTransactionError;
51 changes: 48 additions & 3 deletions avail-rust/src/primitives/block/extrinsics.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use super::extrinsics_params::OnlyCodecExtra;
use crate::{avail::runtime_types::da_runtime::RuntimeCall, Address, Signature};

use codec::{Compact, Decode, Error, Input};
use serde::Deserialize;
use codec::{Compact, Decode, Encode, EncodeLike, Error, Input};
use serde::{Deserialize, Serialize};
use std::mem::size_of;
use subxt::backend::legacy::rpc_methods::Bytes;

pub type SignaturePayload = (Address, Signature, OnlyCodecExtra);
Expand All @@ -14,7 +15,7 @@ pub type SignaturePayload = (Address, Signature, OnlyCodecExtra);
/// the decoding fails.
const EXTRINSIC_FORMAT_VERSION: u8 = 4;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AppUncheckedExtrinsic {
/// The signature, address, number of extrinsics have come before from
/// the same signer and an era describing the longevity of this transaction,
Expand All @@ -25,6 +26,18 @@ pub struct AppUncheckedExtrinsic {
}

impl AppUncheckedExtrinsic {
fn encode_vec_compatible(inner: &[u8]) -> Vec<u8> {
let compact_len = codec::Compact::<u32>(inner.len() as u32);

// Allocate the output buffer with the correct length
let mut output = Vec::with_capacity(compact_len.size_hint() + inner.len());

compact_len.encode_to(&mut output);
output.extend(inner);

output
}

pub fn app_id(&self) -> crate::AppId {
self.signature
.as_ref()
Expand All @@ -34,6 +47,38 @@ impl AppUncheckedExtrinsic {
}
}

impl Encode for AppUncheckedExtrinsic {
fn encode(&self) -> Vec<u8> {
let mut tmp = Vec::with_capacity(size_of::<Self>());

// 1 byte version id.
match self.signature.as_ref() {
Some(s) => {
tmp.push(EXTRINSIC_FORMAT_VERSION | 0b1000_0000);
s.encode_to(&mut tmp);
},
None => {
tmp.push(EXTRINSIC_FORMAT_VERSION & 0b0111_1111);
},
}
self.function.encode_to(&mut tmp);

Self::encode_vec_compatible(&tmp)
}
}

impl EncodeLike for AppUncheckedExtrinsic {}

impl Serialize for AppUncheckedExtrinsic {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: ::serde::Serializer,
{
let encoded = self.encode();
sp_core::bytes::serialize(&encoded, s)
}
}

impl Decode for AppUncheckedExtrinsic {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
// This is a little more complicated than usual since the binary format must be compatible
Expand Down
6 changes: 6 additions & 0 deletions avail-rust/src/rpcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use subxt::rpc_params;
/// Arbitrary properties defined in chain spec as a JSON object
pub type Properties = serde_json::map::Map<String, serde_json::Value>;

#[derive(Clone)]
pub struct Rpc {
pub client: RpcClient,
pub legacy_methods: LegacyRpcMethods<AvailConfig>,
Expand Down Expand Up @@ -45,6 +46,7 @@ impl Rpc {
}
}

#[derive(Clone)]
pub struct Payment {
client: RpcClient,
}
Expand Down Expand Up @@ -111,6 +113,7 @@ mod tests {
}
}

#[derive(Clone)]
pub struct System {
client: RpcClient,
}
Expand Down Expand Up @@ -202,6 +205,7 @@ impl System {
}
}

#[derive(Clone)]
pub struct Chain {
client: RpcClient,
}
Expand Down Expand Up @@ -250,6 +254,7 @@ impl Chain {
}
}

#[derive(Clone)]
pub struct Author {
client: RpcClient,
}
Expand All @@ -268,6 +273,7 @@ impl Author {
}
}

#[derive(Clone)]
pub struct Kate {
client: RpcClient,
}
Expand Down
1 change: 1 addition & 0 deletions avail-rust/src/sdk.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{rpcs::Rpc, transactions::Transactions, utils::Util, Api};

#[derive(Clone)]
pub struct SDK {
pub api: Api,
pub tx: Transactions,
Expand Down
Loading

0 comments on commit a6600ea

Please sign in to comment.