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
use crate::{
address::Address,
amino::schema::{Schema, ValueType},
decimal::Decimal,
};
use subtle_encoding::hex;
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Value {
Bytes(Vec<u8>),
SdkAccAddress(Address),
SdkDecimal(Decimal),
SdkValAddress(Address),
String(String),
}
impl Value {
pub fn value_type(&self) -> ValueType {
match self {
Value::Bytes(_) => ValueType::Bytes,
Value::SdkAccAddress(_) => ValueType::SdkAccAddress,
Value::SdkDecimal(_) => ValueType::SdkDecimal,
Value::SdkValAddress(_) => ValueType::SdkValAddress,
Value::String(_) => ValueType::String,
}
}
pub(crate) fn wire_type(&self) -> u64 {
match self {
Value::Bytes(_)
| Value::SdkAccAddress(_)
| Value::SdkDecimal(_)
| Value::SdkValAddress(_)
| Value::String(_) => 2,
}
}
pub(crate) fn to_amino_bytes(&self) -> Vec<u8> {
match self {
Value::Bytes(b) => b.clone(),
Value::SdkAccAddress(addr) | Value::SdkValAddress(addr) => addr.as_ref().to_vec(),
Value::SdkDecimal(decimal) => decimal.to_amino_bytes(),
Value::String(s) => s.clone().into_bytes(),
}
}
pub(crate) fn to_json_value(&self, schema: &Schema) -> serde_json::Value {
serde_json::Value::String(match self {
Value::Bytes(b) => String::from_utf8(hex::encode(b)).unwrap(),
Value::SdkAccAddress(addr) => addr.to_bech32(schema.acc_prefix()),
Value::SdkDecimal(decimal) => decimal.to_string(),
Value::SdkValAddress(addr) => addr.to_bech32(schema.val_prefix()),
Value::String(s) => s.clone(),
})
}
}
impl From<Decimal> for Value {
fn from(dec: Decimal) -> Value {
Value::SdkDecimal(dec)
}
}
impl From<Vec<u8>> for Value {
fn from(b: Vec<u8>) -> Value {
Value::Bytes(b)
}
}
impl From<String> for Value {
fn from(s: String) -> Value {
Value::String(s)
}
}