1use std::str::FromStr;
7
8use allocative::Allocative;
9use alloy_primitives::U256;
10use async_graphql::scalar;
11use derive_more::Display;
12use linera_witty::{WitLoad, WitStore, WitType};
13use serde::{Deserialize, Serialize};
14use thiserror::Error;
15
16use crate::data_types::Amount;
17
18#[derive(
19 Clone,
20 Copy,
21 Default,
22 Display,
23 Hash,
24 PartialEq,
25 Eq,
26 PartialOrd,
27 Ord,
28 Serialize,
29 Deserialize,
30 WitType,
31 WitStore,
32 WitLoad,
33 Debug,
34 Allocative,
35)]
36#[cfg_attr(with_testing, derive(test_strategy::Arbitrary))]
37pub enum VmRuntime {
39 #[default]
41 Wasm,
42 Evm,
44}
45
46impl FromStr for VmRuntime {
47 type Err = InvalidVmRuntime;
48
49 fn from_str(string: &str) -> Result<Self, Self::Err> {
50 match string {
51 "wasm" => Ok(VmRuntime::Wasm),
52 "evm" => Ok(VmRuntime::Evm),
53 unknown => Err(InvalidVmRuntime(unknown.to_owned())),
54 }
55 }
56}
57
58scalar!(VmRuntime);
59
60#[derive(Clone, Debug, Error)]
62#[error("{0:?} is not a valid virtual machine runtime")]
63pub struct InvalidVmRuntime(String);
64
65#[derive(Clone, Debug, Deserialize, Serialize)]
67pub enum EvmQuery {
68 AccountInfo,
70 Storage(U256),
72 Query(Vec<u8>),
74 Operation(Vec<u8>),
76 Operations(Vec<Vec<u8>>),
78}
79
80#[derive(Debug, Default, Serialize, Deserialize)]
82pub struct EvmOperation {
83 pub value: alloy_primitives::U256,
85 pub argument: Vec<u8>,
87}
88
89impl EvmOperation {
90 pub fn new(amount: Amount, argument: Vec<u8>) -> Self {
92 Self {
93 value: amount.into(),
94 argument,
95 }
96 }
97
98 pub fn to_bytes(&self) -> Result<Vec<u8>, bcs::Error> {
100 bcs::to_bytes(&self)
101 }
102
103 pub fn to_evm_query(&self) -> Result<EvmQuery, bcs::Error> {
105 Ok(EvmQuery::Operation(self.to_bytes()?))
106 }
107}
108
109#[derive(Clone, Default, Serialize, Deserialize)]
112pub struct EvmInstantiation {
113 pub value: alloy_primitives::U256,
115 pub argument: Vec<u8>,
117}