Skip to main content

outmove_common/types/
transaction_argument.rs

1// Copyright (c) The Diem Core Contributors
2// SPDX-License-Identifier: Apache-2.0
3
4use super::{account_address::AccountAddress, value::MoveValue};
5use serde::{Deserialize, Serialize};
6use std::fmt;
7
8#[derive(Clone, Hash, Eq, PartialEq, Serialize, Deserialize)]
9pub enum TransactionArgument {
10    U8(u8),
11    U64(u64),
12    U128(u128),
13    Address(AccountAddress),
14    U8Vector(#[serde(with = "serde_bytes")] Vec<u8>),
15    Bool(bool),
16}
17
18impl fmt::Debug for TransactionArgument {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        match self {
21            TransactionArgument::U8(value) => write!(f, "{{U8: {}}}", value),
22            TransactionArgument::U64(value) => write!(f, "{{U64: {}}}", value),
23            TransactionArgument::U128(value) => write!(f, "{{U128: {}}}", value),
24            TransactionArgument::Bool(boolean) => write!(f, "{{BOOL: {}}}", boolean),
25            TransactionArgument::Address(address) => write!(f, "{{ADDRESS: {:?}}}", address),
26            TransactionArgument::U8Vector(vector) => {
27                write!(f, "{{U8Vector: 0x{}}}", hex::encode(vector))
28            }
29        }
30    }
31}
32
33/// Convert the transaction arguments into Move values.
34pub fn convert_txn_args(args: &[TransactionArgument]) -> Vec<Vec<u8>> {
35    args.iter()
36        .map(|arg| {
37            let mv = match arg {
38                TransactionArgument::U8(i) => MoveValue::U8(*i),
39                TransactionArgument::U64(i) => MoveValue::U64(*i),
40                TransactionArgument::U128(i) => MoveValue::U128(*i),
41                TransactionArgument::Address(a) => MoveValue::Address(*a),
42                TransactionArgument::Bool(b) => MoveValue::Bool(*b),
43                TransactionArgument::U8Vector(v) => MoveValue::vector_u8(v.clone()),
44            };
45            mv.simple_serialize()
46                .expect("transaction arguments must serialize")
47        })
48        .collect()
49}