1use crate::{
9 serde_helper::vec_bytes,
10 transaction::transaction_argument::TransactionArgument,
11};
12use move_core_types::{
13 identifier::{IdentStr, Identifier},
14 language_storage::{ModuleId, TypeTag},
15};
16use serde::{Deserialize, Serialize};
17use std::fmt;
18
19#[derive(Clone, Hash, Eq, PartialEq, Serialize, Deserialize)]
21pub struct Script {
22 #[serde(with = "serde_bytes")]
23 code: Vec<u8>,
24 ty_args: Vec<TypeTag>,
25 args: Vec<TransactionArgument>,
26}
27
28impl Script {
29 pub fn new(
30 code: Vec<u8>, ty_args: Vec<TypeTag>, args: Vec<TransactionArgument>,
31 ) -> Self {
32 Script {
33 code,
34 ty_args,
35 args,
36 }
37 }
38
39 pub fn code(&self) -> &[u8] { &self.code }
40
41 pub fn ty_args(&self) -> &[TypeTag] { &self.ty_args }
42
43 pub fn args(&self) -> &[TransactionArgument] { &self.args }
44
45 pub fn into_inner(self) -> (Vec<u8>, Vec<TransactionArgument>) {
46 (self.code, self.args)
47 }
48}
49
50impl fmt::Debug for Script {
51 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52 f.debug_struct("Script")
53 .field("code", &hex::encode(&self.code))
54 .field("ty_args", &self.ty_args)
55 .field("args", &self.args)
56 .finish()
57 }
58}
59
60#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
62pub enum ScriptABI {
63 TransactionScript(TransactionScriptABI),
64 ScriptFunction(ScriptFunctionABI),
65}
66
67#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
68pub struct ScriptFunctionABI {
69 name: String,
71 module_name: ModuleId,
73 doc: String,
75 ty_args: Vec<TypeArgumentABI>,
77 args: Vec<ArgumentABI>,
79}
80
81#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
82pub struct TransactionScriptABI {
83 name: String,
85 doc: String,
87 #[serde(with = "serde_bytes")]
89 code: Vec<u8>,
90 ty_args: Vec<TypeArgumentABI>,
92 args: Vec<ArgumentABI>,
94}
95
96#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
98pub struct ArgumentABI {
99 name: String,
101 type_tag: TypeTag,
104}
105
106#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
108pub struct TypeArgumentABI {
109 name: String,
111}
112
113impl TransactionScriptABI {
114 pub fn new(
115 name: String, doc: String, code: Vec<u8>,
116 ty_args: Vec<TypeArgumentABI>, args: Vec<ArgumentABI>,
117 ) -> Self {
118 Self {
119 name,
120 doc,
121 code,
122 ty_args,
123 args,
124 }
125 }
126
127 pub fn name(&self) -> &str { &self.name }
128
129 pub fn doc(&self) -> &str { &self.doc }
130
131 pub fn code(&self) -> &[u8] { &self.code }
132
133 pub fn ty_args(&self) -> &[TypeArgumentABI] { &self.ty_args }
134
135 pub fn args(&self) -> &[ArgumentABI] { &self.args }
136}
137
138impl ScriptFunctionABI {
139 pub fn new(
140 name: String, module_name: ModuleId, doc: String,
141 ty_args: Vec<TypeArgumentABI>, args: Vec<ArgumentABI>,
142 ) -> Self {
143 Self {
144 name,
145 module_name,
146 doc,
147 ty_args,
148 args,
149 }
150 }
151
152 pub fn name(&self) -> &str { &self.name }
153
154 pub fn module_name(&self) -> &ModuleId { &self.module_name }
155
156 pub fn doc(&self) -> &str { &self.doc }
157
158 pub fn ty_args(&self) -> &[TypeArgumentABI] { &self.ty_args }
159
160 pub fn args(&self) -> &[ArgumentABI] { &self.args }
161}
162
163impl ScriptABI {
164 pub fn is_script_fun_abi(&self) -> bool {
165 matches!(self, Self::ScriptFunction(_))
166 }
167
168 pub fn is_transaction_script_abi(&self) -> bool {
169 matches!(self, Self::TransactionScript(_))
170 }
171
172 pub fn name(&self) -> &str {
173 match self {
174 Self::TransactionScript(abi) => abi.name(),
175 Self::ScriptFunction(abi) => abi.name(),
176 }
177 }
178
179 pub fn doc(&self) -> &str {
180 match self {
181 Self::TransactionScript(abi) => abi.doc(),
182 Self::ScriptFunction(abi) => abi.doc(),
183 }
184 }
185
186 pub fn ty_args(&self) -> &[TypeArgumentABI] {
187 match self {
188 Self::TransactionScript(abi) => abi.ty_args(),
189 Self::ScriptFunction(abi) => abi.ty_args(),
190 }
191 }
192
193 pub fn args(&self) -> &[ArgumentABI] {
194 match self {
195 Self::TransactionScript(abi) => abi.args(),
196 Self::ScriptFunction(abi) => abi.args(),
197 }
198 }
199}
200
201impl ArgumentABI {
202 pub fn new(name: String, type_tag: TypeTag) -> Self {
203 Self { name, type_tag }
204 }
205
206 pub fn name(&self) -> &str { &self.name }
207
208 pub fn type_tag(&self) -> &TypeTag { &self.type_tag }
209}
210
211impl TypeArgumentABI {
212 pub fn new(name: String) -> Self { Self { name } }
213
214 pub fn name(&self) -> &str { &self.name }
215}
216
217#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
219pub struct ScriptFunction {
220 module: ModuleId,
221 function: Identifier,
222 ty_args: Vec<TypeTag>,
223 #[serde(with = "vec_bytes")]
224 args: Vec<Vec<u8>>,
225}
226
227impl ScriptFunction {
228 pub fn new(
229 module: ModuleId, function: Identifier, ty_args: Vec<TypeTag>,
230 args: Vec<Vec<u8>>,
231 ) -> Self {
232 ScriptFunction {
233 module,
234 function,
235 ty_args,
236 args,
237 }
238 }
239
240 pub fn module(&self) -> &ModuleId { &self.module }
241
242 pub fn function(&self) -> &IdentStr { &self.function }
243
244 pub fn ty_args(&self) -> &[TypeTag] { &self.ty_args }
245
246 pub fn args(&self) -> &[Vec<u8>] { &self.args }
247}