pallet_evm_precompile_randomness/
solidity_types.rs

1// Copyright 2019-2025 PureStake Inc.
2// This file is part of Moonbeam.
3
4// Moonbeam is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Moonbeam is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Moonbeam.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Solidity types for randomness precompile.
18use precompile_utils::{
19	prelude::*,
20	solidity::codec::{Reader, Writer},
21};
22
23pub enum RequestStatus {
24	DoesNotExist,
25	Pending,
26	Ready,
27	Expired,
28}
29
30pub enum RandomnessSource {
31	LocalVRF,
32	RelayBabeEpoch,
33}
34
35impl solidity::Codec for RequestStatus {
36	fn read(reader: &mut Reader) -> MayRevert<Self> {
37		match reader.read().in_field("variant")? {
38			0u8 => Ok(RequestStatus::DoesNotExist),
39			1u8 => Ok(RequestStatus::Pending),
40			2u8 => Ok(RequestStatus::Ready),
41			3u8 => Ok(RequestStatus::Expired),
42			_ => Err(RevertReason::custom("Unknown RequestStatus variant").into()),
43		}
44	}
45
46	fn write(writer: &mut Writer, value: Self) {
47		let encoded: u8 = match value {
48			RequestStatus::DoesNotExist => 0u8,
49			RequestStatus::Pending => 1u8,
50			RequestStatus::Ready => 2u8,
51			RequestStatus::Expired => 3u8,
52		};
53		solidity::Codec::write(writer, encoded);
54	}
55
56	fn has_static_size() -> bool {
57		true
58	}
59
60	fn signature() -> String {
61		u8::signature()
62	}
63}
64
65impl solidity::Codec for RandomnessSource {
66	fn read(reader: &mut Reader) -> MayRevert<Self> {
67		match reader.read().in_field("variant")? {
68			0u8 => Ok(RandomnessSource::LocalVRF),
69			1u8 => Ok(RandomnessSource::RelayBabeEpoch),
70			_ => Err(RevertReason::custom("Unknown RandomnessSource variant").into()),
71		}
72	}
73
74	fn write(writer: &mut Writer, value: Self) {
75		let encoded: u8 = match value {
76			RandomnessSource::LocalVRF => 0u8,
77			RandomnessSource::RelayBabeEpoch => 1u8,
78		};
79		solidity::Codec::write(writer, encoded);
80	}
81
82	fn has_static_size() -> bool {
83		true
84	}
85
86	fn signature() -> String {
87		u8::signature()
88	}
89}