Skip to content

Commit f1dbb13

Browse files
committed
add simple contract for testing purposes
1 parent fb5c07f commit f1dbb13

5 files changed

Lines changed: 62 additions & 20 deletions

File tree

core/src/artifacts.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
use anyhow::{Context, Result};
2-
use ethers::abi::Abi;
2+
use ethers::{abi::Abi, types::Bytes, utils::hex::decode};
33
use std::fs;
44

55
fn contract_path(contract_name: &str) -> String {
6+
let artifacts_dir =
7+
std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src/solidity/artifacts");
8+
69
format!(
7-
"contracts/{}/out/{}.sol/{}.json",
8-
contract_name, contract_name, contract_name
10+
"{}/{}.sol/{}.json",
11+
artifacts_dir.display(),
12+
contract_name,
13+
contract_name
914
)
1015
}
1116

@@ -19,14 +24,14 @@ pub fn load_abi(contract_name: &str) -> Result<Abi> {
1924
Ok(abi)
2025
}
2126

22-
pub fn load_bytecode(contract_name: &str) -> Result<String> {
27+
pub fn load_bytecode(contract_name: &str) -> Result<Bytes> {
2328
let path = contract_path(contract_name);
2429
let json = fs::read_to_string(&path)
2530
.with_context(|| format!("Failed to read bytecode from {}", path))?;
2631
let v: serde_json::Value = serde_json::from_str(&json)?;
27-
let bytecode = v["bytecode"]["object"]
32+
let bytecode_str = v["bytecode"]["object"]
2833
.as_str()
29-
.context("Missing bytecode object")?
30-
.to_string();
31-
Ok(bytecode)
34+
.context("Missing bytecode object")?;
35+
let bytecode = decode(bytecode_str.strip_prefix("0x").unwrap_or(bytecode_str))?;
36+
Ok(Bytes::from(bytecode))
3237
}

core/src/client.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
use anyhow::Result;
2-
use ethers::signers::{LocalWallet, Wallet};
2+
use ethers::signers::{LocalWallet, Signer, Wallet};
33
use std::path::Path;
44

55
/// Load a wallet from a keystore file.
66
/// `cast wallet new .`
77
pub async fn wallet_from_keystore(path: &str, password: &str) -> Result<LocalWallet> {
88
let path = Path::new(path);
9-
let wallet = Wallet::decrypt_keystore(&path, password)?;
9+
let mut wallet = Wallet::decrypt_keystore(&path, password)?;
10+
11+
// TODO Replace with chain id from provider
12+
let chain_id: u64 = 84532;
13+
wallet = wallet.with_chain_id(chain_id);
14+
1015
println!("Wallet loaded successfully.");
1116
Ok(wallet)
1217
}

core/src/compiler.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
use std::{
2-
path::{Path, PathBuf},
3-
process::Output,
4-
};
1+
use std::path::{Path, PathBuf};
52

3+
use anyhow::Error;
64
use foundry_compilers::{Project, ProjectPathsConfig};
75

86
pub fn compile_contract() -> Result<(), anyhow::Error> {
@@ -14,10 +12,12 @@ pub fn compile_contract() -> Result<(), anyhow::Error> {
1412
.paths(ProjectPathsConfig::hardhat(&contracts_path).unwrap())
1513
.set_no_artifacts(false)
1614
.build(Default::default())
17-
.unwrap();
15+
.map_err(|e| Error::new(e).context("Failed to build project"))?;
1816

1917
println!("Project:\n {:?}\n", project);
20-
let output = project.compile().unwrap();
18+
let output = project
19+
.compile()
20+
.map_err(|e| Error::new(e).context("Failed to compile contracts"))?;
2121

2222
println!("Output:\n {:?}\n", output);
2323
Ok(())

core/src/deployer.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
use crate::artifacts;
22
use anyhow::Result;
3-
use ethers::prelude::*;
3+
use ethers::{abi::Token, prelude::*};
44
use std::sync::Arc;
55

66
pub async fn deploy_contract(
77
contract_name: &str,
88
provider: Provider<Http>,
99
wallet: LocalWallet,
1010
) -> Result<Address> {
11+
let endpoint_address = "0x0000000000000000000000000000000000000001".parse::<Address>()?;
12+
let constructor_args = vec![
13+
abi::Token::Address(endpoint_address),
14+
abi::Token::Address(endpoint_address),
15+
];
16+
1117
let abi = artifacts::load_abi(contract_name)?;
1218
let bytecode = artifacts::load_bytecode(contract_name)?;
19+
println!("ABI {:?}\nBytecode {:?}", abi, bytecode);
1320
let client = Arc::new(SignerMiddleware::new(provider, wallet));
1421

15-
let factory = ContractFactory::new(abi, Bytes::from(bytecode.as_bytes().to_vec()), client);
16-
let deployer = factory.deploy(())?.send().await?;
22+
let factory = ContractFactory::new(abi, bytecode, Arc::new(client.clone()));
23+
24+
println!("Factory {:?}", factory);
25+
26+
let deployed = factory.deploy(constructor_args)?.send().await?;
1727

18-
Ok(deployer.address())
28+
Ok(deployed.address())
1929
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
contract SimpleIncrement {
5+
uint256 private _value;
6+
7+
event ValueIncremented(address indexed by, uint256 newValue);
8+
9+
constructor() {
10+
_value = 0;
11+
}
12+
13+
function increment() public returns (uint256) {
14+
_value += 1;
15+
emit ValueIncremented(msg.sender, _value);
16+
return _value;
17+
}
18+
19+
function getValue() public view returns (uint256) {
20+
return _value;
21+
}
22+
}

0 commit comments

Comments
 (0)