Skip to content

Commit

Permalink
compile foo
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmovses committed Mar 5, 2024
1 parent 2b85bbc commit 6645ca8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ move-core-types = { path = "protocol-units/zkfp/move-zk/vendors/move/language/mo
move-compiler = { path = "protocol-units/zkfp/move-zk/vendors/move/language/move-compiler" }
move-stdlib = { path = "protocol-units/zkfp/move-zk/vendors/move/language/move-stdlib" }

#Eth dependencies
ethers = "2.0.13"
foundry-compilers = { git = "https://github.com/foundry-rs/compilers" }

# Always optimize; building and running the guest takes much longer without optimization.
[profile.dev]
Expand Down
3 changes: 3 additions & 0 deletions protocol-units/zkfp/eth-zk/host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ tracing-subscriber = { workspace = true }
tempfile = { workspace = true }
serde = { workspace = true }

ethers = { workspace = true }
foundry-compilers = { workspace = true }

methods = { path = "../methods" }
9 changes: 9 additions & 0 deletions protocol-units/zkfp/eth-zk/host/contracts/Foo.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Foo {
// Function `bar` that returns a constant uint64 value
function bar() public pure returns (uint64) {
return 42;
}
}
38 changes: 27 additions & 11 deletions protocol-units/zkfp/eth-zk/host/src/compiler_examples.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
use anyhow::Result;
use serde::Serialize;
use std::path::PathBuf;

use foundry_compilers::{info::ContractInfo, Artifact, Project, ProjectCompileOutput, ProjectPathsConfig};

/// Makes a foo function that is compatible with the expectations of the guest code.
pub fn make_foo(
structs: &[&str],
fun_sig: &str,
fun_body: &str,
) -> Vec<u8> {
vec![0]
}
pub fn compile_foo(structs: &[&str], fun_sig: &str, fun_body: &str) -> Vec<u8> {
let structs = structs.to_vec().join("\n");
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let paths = ProjectPathsConfig::builder()
.sources(root.join("contracts"))
.build()
.unwrap();
let project = Project::builder()
.paths(paths)
.no_artifacts()
.build()
.unwrap();

/// Uses make_foo to make a very simple function that returns a u64 (42)
pub fn return_u64() -> Vec<u8> {
make_foo(&[], "(): u64", "16")
}
let compiled = project.compile().unwrap();
let info = ContractInfo::new("contracts/Foo.sol:Foo");
let bytes = compiled
.find_contract(info)
.expect("Could not find contract")
.get_bytecode_bytes()
.unwrap()
.to_vec();
bytes
}

0 comments on commit 6645ca8

Please sign in to comment.