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.
18
use precompile_utils::{
19
	prelude::*,
20
	solidity::codec::{Reader, Writer},
21
};
22

            
23
pub enum RequestStatus {
24
	DoesNotExist,
25
	Pending,
26
	Ready,
27
	Expired,
28
}
29

            
30
pub enum RandomnessSource {
31
	LocalVRF,
32
	RelayBabeEpoch,
33
}
34

            
35
impl 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
5
	fn write(writer: &mut Writer, value: Self) {
47
5
		let encoded: u8 = match value {
48
1
			RequestStatus::DoesNotExist => 0u8,
49
1
			RequestStatus::Pending => 1u8,
50
1
			RequestStatus::Ready => 2u8,
51
2
			RequestStatus::Expired => 3u8,
52
		};
53
5
		solidity::Codec::write(writer, encoded);
54
5
	}
55

            
56
2
	fn has_static_size() -> bool {
57
2
		true
58
2
	}
59

            
60
	fn signature() -> String {
61
		u8::signature()
62
	}
63
}
64

            
65
impl 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
1
	fn write(writer: &mut Writer, value: Self) {
75
1
		let encoded: u8 = match value {
76
1
			RandomnessSource::LocalVRF => 0u8,
77
			RandomnessSource::RelayBabeEpoch => 1u8,
78
		};
79
1
		solidity::Codec::write(writer, encoded);
80
1
	}
81

            
82
2
	fn has_static_size() -> bool {
83
2
		true
84
2
	}
85

            
86
	fn signature() -> String {
87
		u8::signature()
88
	}
89
}