forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
59 lines (49 loc) · 1.8 KB
/
Copy pathmod.rs
File metadata and controls
59 lines (49 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//! Fe to EVM compiler.
use crate::errors::CompileError;
use crate::types::{Bytecode, NamedBytecodeContracts, NamedYulContracts, YulIr};
/// Compile a map of Yul contracts to a map of bytecode contracts.
pub fn compile(
mut contracts: NamedYulContracts,
optimize: bool,
) -> Result<NamedBytecodeContracts, CompileError> {
contracts
.drain()
.map(|(name, yul_src)| {
compile_single_contract(&name, yul_src, optimize).map(|bytecode| (name, bytecode))
})
.collect::<Result<NamedBytecodeContracts, _>>()
}
/// Compiles a single Yul contract to bytecode.
pub fn compile_single_contract(
name: &str,
yul_src: YulIr,
optimize: bool,
) -> Result<Bytecode, CompileError> {
let solc_temp = include_str!("solc_temp.json");
let input = solc_temp
.replace("{optimizer_enabled}", &optimize.to_string())
.replace("{src}", &yul_src);
let raw_output = solc::compile(&input);
let output: serde_json::Value = serde_json::from_str(&raw_output)?;
let bytecode = output["contracts"]["input.yul"][name]["evm"]["bytecode"]["object"]
.to_string()
.replace("\"", "");
if bytecode == "null" {
return Err(CompileError::str(&output.to_string()));
}
Ok(bytecode)
}
#[test]
fn test_solc_sanity() {
let yul_src = "{ sstore(0,0) }";
let solc_temp = include_str!("solc_temp.json");
let input = solc_temp
.replace("{optimizer_enabled}", "false")
.replace("{src}", &yul_src);
let raw_output = solc::compile(&input);
let output: serde_json::Value = serde_json::from_str(&raw_output).unwrap();
let bytecode = output["contracts"]["input.yul"]["object"]["evm"]["bytecode"]["object"]
.to_string()
.replace("\"", "");
assert_eq!(bytecode, "6000600055", "incorrect bytecode",);
}