use crate::{
serde_helper::vec_bytes,
transaction::transaction_argument::TransactionArgument,
};
use move_core_types::{
identifier::{IdentStr, Identifier},
language_storage::{ModuleId, TypeTag},
};
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub struct Script {
#[serde(with = "serde_bytes")]
code: Vec<u8>,
ty_args: Vec<TypeTag>,
args: Vec<TransactionArgument>,
}
impl Script {
pub fn new(
code: Vec<u8>, ty_args: Vec<TypeTag>, args: Vec<TransactionArgument>,
) -> Self {
Script {
code,
ty_args,
args,
}
}
pub fn code(&self) -> &[u8] { &self.code }
pub fn ty_args(&self) -> &[TypeTag] { &self.ty_args }
pub fn args(&self) -> &[TransactionArgument] { &self.args }
pub fn into_inner(self) -> (Vec<u8>, Vec<TransactionArgument>) {
(self.code, self.args)
}
}
impl fmt::Debug for Script {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Script")
.field("code", &hex::encode(&self.code))
.field("ty_args", &self.ty_args)
.field("args", &self.args)
.finish()
}
}
#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub enum ScriptABI {
TransactionScript(TransactionScriptABI),
ScriptFunction(ScriptFunctionABI),
}
#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub struct ScriptFunctionABI {
name: String,
module_name: ModuleId,
doc: String,
ty_args: Vec<TypeArgumentABI>,
args: Vec<ArgumentABI>,
}
#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub struct TransactionScriptABI {
name: String,
doc: String,
#[serde(with = "serde_bytes")]
code: Vec<u8>,
ty_args: Vec<TypeArgumentABI>,
args: Vec<ArgumentABI>,
}
#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub struct ArgumentABI {
name: String,
type_tag: TypeTag,
}
#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub struct TypeArgumentABI {
name: String,
}
impl TransactionScriptABI {
pub fn new(
name: String, doc: String, code: Vec<u8>,
ty_args: Vec<TypeArgumentABI>, args: Vec<ArgumentABI>,
) -> Self {
Self {
name,
doc,
code,
ty_args,
args,
}
}
pub fn name(&self) -> &str { &self.name }
pub fn doc(&self) -> &str { &self.doc }
pub fn code(&self) -> &[u8] { &self.code }
pub fn ty_args(&self) -> &[TypeArgumentABI] { &self.ty_args }
pub fn args(&self) -> &[ArgumentABI] { &self.args }
}
impl ScriptFunctionABI {
pub fn new(
name: String, module_name: ModuleId, doc: String,
ty_args: Vec<TypeArgumentABI>, args: Vec<ArgumentABI>,
) -> Self {
Self {
name,
module_name,
doc,
ty_args,
args,
}
}
pub fn name(&self) -> &str { &self.name }
pub fn module_name(&self) -> &ModuleId { &self.module_name }
pub fn doc(&self) -> &str { &self.doc }
pub fn ty_args(&self) -> &[TypeArgumentABI] { &self.ty_args }
pub fn args(&self) -> &[ArgumentABI] { &self.args }
}
impl ScriptABI {
pub fn is_script_fun_abi(&self) -> bool {
matches!(self, Self::ScriptFunction(_))
}
pub fn is_transaction_script_abi(&self) -> bool {
matches!(self, Self::TransactionScript(_))
}
pub fn name(&self) -> &str {
match self {
Self::TransactionScript(abi) => abi.name(),
Self::ScriptFunction(abi) => abi.name(),
}
}
pub fn doc(&self) -> &str {
match self {
Self::TransactionScript(abi) => abi.doc(),
Self::ScriptFunction(abi) => abi.doc(),
}
}
pub fn ty_args(&self) -> &[TypeArgumentABI] {
match self {
Self::TransactionScript(abi) => abi.ty_args(),
Self::ScriptFunction(abi) => abi.ty_args(),
}
}
pub fn args(&self) -> &[ArgumentABI] {
match self {
Self::TransactionScript(abi) => abi.args(),
Self::ScriptFunction(abi) => abi.args(),
}
}
}
impl ArgumentABI {
pub fn new(name: String, type_tag: TypeTag) -> Self {
Self { name, type_tag }
}
pub fn name(&self) -> &str { &self.name }
pub fn type_tag(&self) -> &TypeTag { &self.type_tag }
}
impl TypeArgumentABI {
pub fn new(name: String) -> Self { Self { name } }
pub fn name(&self) -> &str { &self.name }
}
#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub struct ScriptFunction {
module: ModuleId,
function: Identifier,
ty_args: Vec<TypeTag>,
#[serde(with = "vec_bytes")]
args: Vec<Vec<u8>>,
}
impl ScriptFunction {
pub fn new(
module: ModuleId, function: Identifier, ty_args: Vec<TypeTag>,
args: Vec<Vec<u8>>,
) -> Self {
ScriptFunction {
module,
function,
ty_args,
args,
}
}
pub fn module(&self) -> &ModuleId { &self.module }
pub fn function(&self) -> &IdentStr { &self.function }
pub fn ty_args(&self) -> &[TypeTag] { &self.ty_args }
pub fn args(&self) -> &[Vec<u8>] { &self.args }
}