cosm_tome_wasm_deploy_fork/chain/
request.rs

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