cosm_utils/chain/
request.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4use cosmrs::proto::cosmos::base::query::v1beta1::{PageRequest, PageResponse};
5
6use crate::modules::auth::model::Account;
7
8use super::fee::Fee;
9
10#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, Eq, PartialEq, Hash)]
11pub struct PaginationRequest {
12    pub page: PageID,
13    pub limit: u64,
14    pub reverse: bool,
15}
16
17impl From<PaginationRequest> for PageRequest {
18    fn from(p: PaginationRequest) -> PageRequest {
19        let (key, offset) = match p.page {
20            PageID::Key(key) => (key, OffsetParams::default()),
21            PageID::Offset(offset) => (vec![], offset),
22        };
23
24        PageRequest {
25            key,
26            offset: offset.offset,
27            count_total: offset.count_total,
28            limit: p.limit,
29            reverse: p.reverse,
30        }
31    }
32}
33
34impl From<PageRequest> for PaginationRequest {
35    fn from(p: PageRequest) -> PaginationRequest {
36        let page = if p.key.is_empty() {
37            PageID::Offset(OffsetParams {
38                offset: p.offset,
39                count_total: p.count_total,
40            })
41        } else {
42            PageID::Key(p.key)
43        };
44
45        PaginationRequest {
46            page,
47            limit: p.limit,
48            reverse: p.reverse,
49        }
50    }
51}
52
53#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, Eq, PartialEq, Hash)]
54pub enum PageID {
55    /// key is the value in PaginationResponse.next_key used to query the next page.
56    Key(Vec<u8>),
57
58    /// offset is a numeric offset that can be used when key is unavailable.
59    /// It is less efficient than using key.
60    Offset(OffsetParams),
61}
62
63#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, Eq, PartialEq, Hash, Default)]
64pub struct OffsetParams {
65    pub offset: u64,
66    pub count_total: bool,
67}
68
69#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, Eq, PartialEq, Hash, Default)]
70pub struct PaginationResponse {
71    pub next_key: Vec<u8>,
72    pub total: u64,
73}
74
75impl From<PageResponse> for PaginationResponse {
76    fn from(p: PageResponse) -> PaginationResponse {
77        PaginationResponse {
78            next_key: p.next_key,
79            total: p.total,
80        }
81    }
82}
83
84impl From<PaginationResponse> for PageResponse {
85    fn from(p: PaginationResponse) -> PageResponse {
86        PageResponse {
87            next_key: p.next_key,
88            total: p.total,
89        }
90    }
91}
92
93/// Options the user can set when executing txs on chain
94#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
95pub struct TxOptions {
96    /// The block height after which this transaction will not be processed by the chain
97    pub timeout_height: Option<u64>,
98
99    /// If set, this fee will be used, instead of simulating the fee
100    pub fee: Option<Fee>,
101
102    /// If set, this fee will be used, instead of querying the account
103    pub account: Option<Account>,
104
105    /// An arbitrary memo to be added to the transaction
106    pub memo: String,
107}
108
109impl Default for TxOptions {
110    fn default() -> Self {
111        Self {
112            fee: None,
113            account: None,
114            timeout_height: Some(0),
115            memo: "Made with cosm-tome client".to_string(),
116        }
117    }
118}