Skip to content

Commit

Permalink
Add WIP macros
Browse files Browse the repository at this point in the history
  • Loading branch information
prasannavl committed Sep 6, 2023
1 parent 9417e0a commit ba02ca1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
15 changes: 11 additions & 4 deletions lib/ain-contracts/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ use anyhow::{bail, Context, Result};
use ethers_solc::{Project, ProjectPathsConfig, Solc};

fn main() -> Result<()> {
let out_dir: PathBuf = PathBuf::from(env::var("OUT_DIR")?);
let manifest_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);
let solc_path_str = env::var("SOLC_PATH")?;

// If TARGET_DIR is set, which we do from Makefile, uses that instead of OUT_DIR.
// Otherwise, use the path for OUT_DIR that cargo sets, as usual.
// Reason: Currently setting --out-dir is nightly only, so there's no way to get OUT_DIR
// out of cargo reliably for pointing deps determinisitcally.
let target_dir: PathBuf = PathBuf::from(env::var("CARGO_TARGET_DIR").or(env::var("OUT_DIR"))?);
let solc_artifact_dir = target_dir.join("solc");

// Solidity project root and contract names relative to our project
let contracts = vec![
("dfi_intrinsics", "DFIIntrinsics"),
Expand All @@ -18,7 +25,7 @@ fn main() -> Result<()> {
for (sol_project_name, contract_name) in contracts {
let solc = Solc::new(&solc_path_str);

let sol_project_root = PathBuf::from(sol_project_name);
let sol_project_root = manifest_path.join(sol_project_name);
if !sol_project_root.exists() {
bail!("Solidity project missing: {sol_project_root:?}");
}
Expand All @@ -37,7 +44,7 @@ fn main() -> Result<()> {

let output = project.compile()?;
let artifacts = output.into_artifacts();
let sol_project_outdir = out_dir.join(sol_project_name);
let sol_project_outdir = solc_artifact_dir.join(sol_project_name);

for (id, artifact) in artifacts {
if id.name != contract_name {
Expand All @@ -53,7 +60,7 @@ fn main() -> Result<()> {
let items = [
("abi.json", serde_json::to_string(&abi)?),
("bytecode.json", serde_json::to_string(&bytecode)?),
("bytecode_deployed.json", serde_json::to_string(&deployed_bytecode)?),
("deployed_bytecode.json", serde_json::to_string(&deployed_bytecode)?),
];

fs::create_dir_all(&sol_project_outdir)?;
Expand Down
22 changes: 14 additions & 8 deletions lib/ain-contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ use sp_core::{Blake2Hasher, Hasher};

pub type Result<T> = std::result::Result<T, anyhow::Error>;

macro_rules! get_solc_artifact_path {
($project_name:expr, $artifact:expr) => {
concat!(env!("OUT_DIR"), "/solc/", $project_name, "/", $artifact, ".json")
};
}

macro_rules! get_solc_artifact_string {
($project_name:expr, $artifact:expr) => {
include_str!(get_solc_artifact_path!($project_name, $artifact))
};
}

fn get_bytecode(input: &str) -> Result<Vec<u8>> {
let bytecode_json: serde_json::Value = serde_json::from_str(input)?;
let bytecode_raw = bytecode_json["object"]
Expand Down Expand Up @@ -46,10 +58,7 @@ pub struct FixedContract {

lazy_static::lazy_static! {
pub static ref INTRINSIC_CONTRACT: FixedContract = {
let bytecode = get_bytecode(include_str!(concat!(
env!("OUT_DIR"),
"/dfi_intrinsics/bytecode_deployed.json"
))).unwrap();
let bytecode = get_bytecode(get_solc_artifact_string!("dfi_intrinsics", "deployed_bytecode")).unwrap();

FixedContract {
contract: Contract {
Expand All @@ -68,10 +77,7 @@ lazy_static::lazy_static! {
// Note that input, bytecode, and deployed bytecode is used in confusing ways since
// deployedBytecode was exposed as bytecode earlier in build script.
// TODO: Refactor terminology to align with the source of truth.
let bytecode = get_bytecode(include_str!(concat!(
env!("OUT_DIR"),
"/transfer_domain/bytecode_deployed.json"
))).unwrap();
let bytecode = get_bytecode(get_solc_artifact_string!("transfer_domain", "deployed_bytecode")).unwrap();
let input = get_bytecode(include_str!(concat!(
env!("OUT_DIR"),
"/transfer_domain/bytecode.json"
Expand Down

0 comments on commit ba02ca1

Please sign in to comment.