-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathibc.rs
More file actions
166 lines (149 loc) · 4.61 KB
/
Copy pathibc.rs
File metadata and controls
166 lines (149 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//! IBC-related data types
use std::collections::BTreeMap;
use std::fmt::Display;
use std::str::FromStr;
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use data_encoding::{DecodePartial, HEXLOWER, HEXLOWER_PERMISSIVE};
use ibc::core::host::types::identifiers::{ChannelId, PortId};
pub use ibc::*;
use namada_macros::BorshDeserializer;
#[cfg(feature = "migrations")]
use namada_migrations::*;
use serde::{Deserialize, Serialize};
use super::address::HASH_LEN;
use crate::hash::Hash;
use crate::token;
/// IBC token hash derived from a denomination.
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(
Debug,
Clone,
Serialize,
Deserialize,
BorshSerialize,
BorshDeserialize,
BorshDeserializer,
BorshSchema,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
)]
#[repr(transparent)]
pub struct IbcTokenHash(pub [u8; HASH_LEN]);
impl Display for IbcTokenHash {
#[inline(always)]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", HEXLOWER.encode(&self.0))
}
}
impl FromStr for IbcTokenHash {
type Err = DecodePartial;
fn from_str(h: &str) -> Result<Self, Self::Err> {
let mut output = [0u8; HASH_LEN];
HEXLOWER_PERMISSIVE.decode_mut(h.as_ref(), &mut output)?;
Ok(IbcTokenHash(output))
}
}
/// IBC transaction data section hash
pub type IbcTxDataHash = Hash;
/// IBC transaction data references to retrieve IBC messages
#[derive(Default, Clone, Serialize, Deserialize)]
pub struct IbcTxDataRefs(pub Vec<IbcTxDataHash>);
impl Display for IbcTxDataRefs {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", serde_json::to_string(self).unwrap())
}
}
impl FromStr for IbcTxDataRefs {
type Err = serde_json::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
serde_json::from_str(s)
}
}
/// The target of a PGF payment
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(
Debug,
Clone,
PartialEq,
Serialize,
Deserialize,
Ord,
Eq,
PartialOrd,
BorshDeserializer,
Hash,
)]
pub struct PGFIbcTarget {
/// The target address on the target chain
pub target: String,
/// The amount of token to fund the target address
pub amount: token::Amount,
/// Port ID to fund
pub port_id: PortId,
/// Channel ID to fund
pub channel_id: ChannelId,
}
impl BorshSerialize for PGFIbcTarget {
fn serialize<W: std::io::Write>(
&self,
writer: &mut W,
) -> std::io::Result<()> {
BorshSerialize::serialize(&self.target, writer)?;
BorshSerialize::serialize(&self.amount, writer)?;
BorshSerialize::serialize(&self.port_id.to_string(), writer)?;
BorshSerialize::serialize(&self.channel_id.to_string(), writer)
}
}
impl borsh::BorshDeserialize for PGFIbcTarget {
fn deserialize_reader<R: std::io::Read>(
reader: &mut R,
) -> std::io::Result<Self> {
use std::io::{Error, ErrorKind};
let target: String = BorshDeserialize::deserialize_reader(reader)?;
let amount: token::Amount =
BorshDeserialize::deserialize_reader(reader)?;
let port_id: String = BorshDeserialize::deserialize_reader(reader)?;
let port_id: PortId = port_id.parse().map_err(|err| {
Error::new(
ErrorKind::InvalidData,
format!("Error decoding port ID: {}", err),
)
})?;
let channel_id: String = BorshDeserialize::deserialize_reader(reader)?;
let channel_id: ChannelId = channel_id.parse().map_err(|err| {
Error::new(
ErrorKind::InvalidData,
format!("Error decoding channel ID: {}", err),
)
})?;
Ok(Self {
target,
amount,
port_id,
channel_id,
})
}
}
impl borsh::BorshSchema for PGFIbcTarget {
fn add_definitions_recursively(
definitions: &mut BTreeMap<
borsh::schema::Declaration,
borsh::schema::Definition,
>,
) {
let fields = borsh::schema::Fields::NamedFields(vec![
("target".into(), String::declaration()),
("amount".into(), token::Amount::declaration()),
("port_id".into(), String::declaration()),
("channel_id".into(), String::declaration()),
]);
let definition = borsh::schema::Definition::Struct { fields };
definitions.insert(Self::declaration(), definition);
}
fn declaration() -> borsh::schema::Declaration {
std::any::type_name::<Self>().into()
}
}