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
use diem_types::transaction::{
ArgumentABI, ScriptABI, ScriptFunctionABI, TransactionScriptABI, TypeArgumentABI,
};
use heck::CamelCase;
use move_core_types::language_storage::TypeTag;
use serde_reflection::{ContainerFormat, Format, Named, VariantFormat};
use std::collections::{BTreeMap, BTreeSet};
pub(crate) fn type_not_allowed(type_tag: &TypeTag) -> ! {
panic!(
"Transaction scripts cannot take arguments of type {}.",
type_tag
);
}
pub(crate) fn prepare_doc_string(doc: &str) -> String {
doc.replace("\n ", "\n").trim().to_string()
}
fn quote_type_as_format(type_tag: &TypeTag) -> Format {
use TypeTag::*;
match type_tag {
Bool => Format::Bool,
U8 => Format::U8,
U64 => Format::U64,
U128 => Format::U128,
Address => Format::TypeName("AccountAddress".into()),
Vector(type_tag) => match type_tag.as_ref() {
U8 => Format::Bytes,
Vector(type_tag) => {
if type_tag.as_ref() == &U8 {
Format::TypeName("VecBytes".into())
} else {
type_not_allowed(type_tag)
}
}
_ => type_not_allowed(type_tag),
},
Struct(_) | Signer => type_not_allowed(type_tag),
}
}
fn quote_type_parameter_as_field(ty_arg: &TypeArgumentABI) -> Named<Format> {
Named {
name: ty_arg.name().to_string(),
value: Format::TypeName("TypeTag".into()),
}
}
fn quote_parameter_as_field(arg: &ArgumentABI) -> Named<Format> {
Named {
name: arg.name().to_string(),
value: quote_type_as_format(arg.type_tag()),
}
}
pub(crate) fn make_abi_enum_container(abis: &[ScriptABI]) -> ContainerFormat {
let mut variants = BTreeMap::new();
for (index, abi) in abis.iter().enumerate() {
let mut fields = Vec::new();
for ty_arg in abi.ty_args() {
fields.push(quote_type_parameter_as_field(ty_arg));
}
for arg in abi.args() {
fields.push(quote_parameter_as_field(arg));
}
let format = VariantFormat::Struct(fields);
variants.insert(
index as u32,
Named {
name: abi.name().to_camel_case(),
value: format,
},
);
}
ContainerFormat::Enum(variants)
}
pub(crate) fn mangle_type(type_tag: &TypeTag) -> String {
use TypeTag::*;
match type_tag {
Bool => "bool".into(),
U8 => "u8".into(),
U64 => "u64".into(),
U128 => "u128".into(),
Address => "address".into(),
Vector(type_tag) => match type_tag.as_ref() {
U8 => "u8vector".into(),
Vector(type_tag) => {
if type_tag.as_ref() == &U8 {
"vecbytes".into()
} else {
type_not_allowed(type_tag)
}
}
_ => type_not_allowed(type_tag),
},
Struct(_) | Signer => type_not_allowed(type_tag),
}
}
pub(crate) fn get_external_definitions(diem_types: &str) -> serde_generate::ExternalDefinitions {
let definitions = vec![(
diem_types,
vec![
"AccountAddress",
"TypeTag",
"Script",
"TransactionArgument",
"VecBytes",
],
)];
definitions
.into_iter()
.map(|(module, defs)| {
(
module.to_string(),
defs.into_iter().map(String::from).collect(),
)
})
.collect()
}
pub(crate) fn get_required_helper_types(abis: &[ScriptABI]) -> BTreeSet<&TypeTag> {
let mut required_types = BTreeSet::new();
for abi in abis {
for arg in abi.args() {
let type_tag = arg.type_tag();
required_types.insert(type_tag);
}
}
required_types
}
pub(crate) fn filter_transaction_scripts(abis: &[ScriptABI]) -> Vec<ScriptABI> {
abis.iter()
.cloned()
.filter(|abi| abi.is_transaction_script_abi())
.collect()
}
pub(crate) fn transaction_script_abis(abis: &[ScriptABI]) -> Vec<TransactionScriptABI> {
abis.iter()
.cloned()
.filter_map(|abi| match abi {
ScriptABI::TransactionScript(abi) => Some(abi),
ScriptABI::ScriptFunction(_) => None,
})
.collect::<Vec<_>>()
}
pub(crate) fn script_function_abis(abis: &[ScriptABI]) -> Vec<ScriptFunctionABI> {
abis.iter()
.cloned()
.filter_map(|abi| match abi {
ScriptABI::ScriptFunction(abi) => Some(abi),
ScriptABI::TransactionScript(_) => None,
})
.collect::<Vec<_>>()
}