File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11use anyhow:: { Context , Result } ;
2- use ethers:: abi:: Abi ;
2+ use ethers:: { abi:: Abi , types :: Bytes , utils :: hex :: decode } ;
33use std:: fs;
44
55fn 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}
Original file line number Diff line number Diff line change 11use anyhow:: Result ;
2- use ethers:: signers:: { LocalWallet , Wallet } ;
2+ use ethers:: signers:: { LocalWallet , Signer , Wallet } ;
33use std:: path:: Path ;
44
55/// Load a wallet from a keystore file.
66/// `cast wallet new .`
77pub 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}
Original file line number Diff line number Diff line change 1- use std:: {
2- path:: { Path , PathBuf } ,
3- process:: Output ,
4- } ;
1+ use std:: path:: { Path , PathBuf } ;
52
3+ use anyhow:: Error ;
64use foundry_compilers:: { Project , ProjectPathsConfig } ;
75
86pub 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 ( ( ) )
Original file line number Diff line number Diff line change 11use crate :: artifacts;
22use anyhow:: Result ;
3- use ethers:: prelude:: * ;
3+ use ethers:: { abi :: Token , prelude:: * } ;
44use std:: sync:: Arc ;
55
66pub 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 {:?}\n Bytecode {:?}" , 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}
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments