-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
42 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |