Skip to content

Commit

Permalink
streamline
Browse files Browse the repository at this point in the history
  • Loading branch information
fl0rek committed Nov 21, 2024
1 parent 963450f commit 9bb8299
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 26 deletions.
29 changes: 8 additions & 21 deletions grpc/src/types/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ use k256::ecdsa::{signature::Signer, Signature};
use pbjson_types::Any;
use prost::{Message, Name};

use celestia_proto::cosmos::base::v1beta1::Coin;
use celestia_proto::cosmos::crypto::secp256k1;
use celestia_proto::cosmos::tx::v1beta1::mode_info::{Single, Sum};
use celestia_proto::cosmos::tx::v1beta1::{
BroadcastMode, BroadcastTxRequest, BroadcastTxResponse, Fee, GetTxRequest as RawGetTxRequest,
GetTxResponse as RawGetTxResponse, ModeInfo, SignDoc, SignerInfo, TxBody as RawTxBody,
BroadcastMode, BroadcastTxRequest, BroadcastTxResponse, GetTxRequest as RawGetTxRequest,
GetTxResponse as RawGetTxResponse, SignDoc,
};
use celestia_tendermint::public_key::Secp256k1 as VerifyingKey;
use celestia_tendermint_proto::Protobuf;
use celestia_types::auth::BaseAccount;
use celestia_types::blob::{Blob, RawBlob, RawBlobTx};
use celestia_types::state::{RawTx, TxResponse};
use celestia_types::tx::{AuthInfo, Tx};
use celestia_types::state::{
AuthInfo, Fee, ModeInfo, RawTx, RawTxBody, SignerInfo, Sum, Tx, TxResponse,
};

use crate::types::{FromGrpcResponse, IntoGrpcParam};
use crate::Error;
Expand Down Expand Up @@ -104,26 +103,14 @@ pub fn sign_tx(
gas_limit: u64,
fee: u64,
) -> RawTx {
// From https://github.com/celestiaorg/celestia-app/blob/v2.3.1/pkg/appconsts/global_consts.go#L77
const FEE_DENOM: &str = "utia";
// From https://github.com/celestiaorg/cosmos-sdk/blob/v1.25.0-sdk-v0.46.16/proto/cosmos/tx/signing/v1beta1/signing.proto#L24
const SIGNING_MODE_INFO: Option<ModeInfo> = Some(ModeInfo {
sum: Some(Sum::Single(Single { mode: 1 })),
});

let fee = Fee {
amount: vec![Coin {
denom: FEE_DENOM.to_string(),
amount: fee.to_string(),
}],
gas_limit,
..Fee::default()
const SIGNING_MODE_INFO: ModeInfo = ModeInfo {
sum: Sum::Single { mode: 1 },
};

let public_key = secp256k1::PubKey {
key: verifying_key.to_encoded_point(true).as_bytes().to_vec(),
};

let public_key_as_any = Any {
type_url: secp256k1::PubKey::type_url(),
value: public_key.encode_to_vec().into(),
Expand All @@ -135,7 +122,7 @@ pub fn sign_tx(
mode_info: SIGNING_MODE_INFO,
sequence: base_account.sequence,
}],
fee,
fee: Fee::new(fee, gas_limit),
};
let auth_info_bytes: Result<_, Infallible> = auth_info.encode_vec();

Expand Down
4 changes: 4 additions & 0 deletions types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ pub enum Error {
#[error("Invalid balance amount: {0}")]
InvalidBalanceAmount(String),

/// Invalid coin amount.
#[error("Invalid coin amount: {0}")]
InvalidCoinAmount(String),

/// Invalid Public Key
#[error("Invalid Public Key")]
InvalidPublicKeyType(String),
Expand Down
5 changes: 4 additions & 1 deletion types/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ pub use self::balance::Balance;
pub use self::query_delegation::{
QueryDelegationResponse, QueryRedelegationsResponse, QueryUnbondingDelegationResponse,
};
pub use self::tx::{RawTx, RawTxBody, RawTxResponse, Tx, TxBody, TxResponse};
pub use self::tx::{
AuthInfo, Coin, Fee, ModeInfo, RawTx, RawTxBody, RawTxResponse, SignerInfo, Sum, Tx, TxBody,
TxResponse, BOND_DENOM,
};

/// A 256-bit unsigned integer.
pub type Uint = ruint::aliases::U256;
27 changes: 23 additions & 4 deletions types/src/state/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ pub use celestia_proto::cosmos::tx::v1beta1::TxBody as RawTxBody;

pub type Signature = Vec<u8>;

// [`BOND_DENOM`] defines the native staking denomination
pub const BOND_DENOM: &str = "utia";

/// [`Tx`] is the standard type used for broadcasting transactions.
#[derive(Debug, Clone)]
pub struct Tx {
Expand Down Expand Up @@ -188,7 +191,7 @@ pub enum Sum {
/// Fee includes the amount of coins paid in fees and the maximum
/// gas to be used by the transaction. The ratio yields an effective "gasprice",
/// which must be above some miminum to be accepted into the mempool.
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Fee {
/// amount is the amount of coins to be paid as a fee
pub amount: Vec<Coin>,
Expand All @@ -211,7 +214,20 @@ pub struct Coin {
/// Coin denomination
pub denom: String,
/// Coin amount
pub amount: String, // TODO: should be int?
pub amount: u64,
}

impl Fee {
pub fn new(utia_fee: u64, gas_limit: u64) -> Self {
Fee {
amount: vec![Coin {
denom: BOND_DENOM.to_string(),
amount: utia_fee,
}],
gas_limit,
..Default::default()
}
}
}

impl TryFrom<RawTxBody> for TxBody {
Expand Down Expand Up @@ -407,7 +423,7 @@ impl From<Coin> for RawCoin {
fn from(value: Coin) -> Self {
RawCoin {
denom: value.denom,
amount: value.amount,
amount: value.amount.to_string(),
}
}
}
Expand All @@ -418,7 +434,10 @@ impl TryFrom<RawCoin> for Coin {
fn try_from(value: RawCoin) -> Result<Self, Self::Error> {
Ok(Coin {
denom: value.denom,
amount: value.amount,
amount: value
.amount
.parse()
.map_err(|_| Error::InvalidCoinAmount(value.amount))?,
})
}
}
Expand Down

0 comments on commit 9bb8299

Please sign in to comment.