Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding cli options for deploy contract #166

Merged
merged 9 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rosetta-wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ description = "Command line wallet implemented using the rosetta client."
anyhow = "1.0.69"
async-std = { version = "1.12.0", features = ["attributes"] }
clap = { version = "4.1.8", features = ["derive"] }
ethers-solc = "2.0.1"
futures = "0.3.26"
rosetta-client = { version = "0.4.0", path = "../rosetta-client" }
surf = { version = "2.3.2", default-features = false, features = ["h1-client-rustls"] }
Expand Down
50 changes: 49 additions & 1 deletion rosetta-wallet/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use anyhow::Context;
use anyhow::Result;
use clap::Parser;
use ethers_solc::{artifacts::Source, CompilerInput, Solc};
use futures::stream::StreamExt;
use rosetta_client::types::{AccountIdentifier, BlockTransaction, TransactionIdentifier};
use rosetta_client::EthereumExt;
use std::path::PathBuf;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

#[derive(Parser)]
pub struct Opts {
Expand All @@ -24,6 +27,7 @@ pub enum Command {
Pubkey,
Account,
Balance,
DeployContract(DeployContractOpts),
Faucet(FaucetOpts),
Transfer(TransferOpts),
Transaction(TransactionOpts),
Expand Down Expand Up @@ -57,6 +61,11 @@ pub struct MethodCallOpts {
pub amount: u128,
}

#[derive(Parser)]
pub struct DeployContractOpts {
pub contract_path: String,
}

#[async_std::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
Expand All @@ -79,6 +88,22 @@ async fn main() -> Result<()> {
let balance = wallet.balance().await?;
println!("{}", rosetta_client::amount_to_string(&balance)?);
}
Command::DeployContract(DeployContractOpts { contract_path }) => {
match wallet.config().blockchain {
"astar" | "ethereum" => {
let bytes = compile_file(&contract_path)?;
let response = wallet.eth_deploy_contract(bytes).await?;
let tx_receipt = wallet.eth_transaction_receipt(&response.hash).await?;
let contract_address = tx_receipt.result["contractAddress"]
.as_str()
.ok_or(anyhow::anyhow!("Unable to get contract address"))?;
println!("Deploy contract address {:?}", contract_address);
}
_ => {
Haider-Ali-DS marked this conversation as resolved.
Show resolved Hide resolved
anyhow::bail!("Not implemented");
}
}
}
Command::Transfer(TransferOpts { account, amount }) => {
let amount =
rosetta_client::string_to_amount(&amount, wallet.config().currency_decimals)?;
Expand Down Expand Up @@ -194,3 +219,26 @@ fn print_transaction(tx: &BlockTransaction) -> Result<()> {
}
Ok(())
}

fn compile_file(path: &str) -> Result<Vec<u8>> {
let solc = Solc::default();
let mut sources = BTreeMap::new();
sources.insert(Path::new(path).into(), Source::read(path).unwrap());
let input = &CompilerInput::with_sources(sources)[0];
let output = solc.compile_exact(input)?;
let file = output.contracts.get(path).unwrap();
let (key, _) = file.first_key_value().unwrap();
let contract = file.get(key).unwrap();
let bytecode = contract
.evm
.as_ref()
.context("evm not found")?
.bytecode
.as_ref()
.context("bytecode not found")?
.object
.as_bytes()
.context("could not convert to bytes")?
.to_vec();
Ok(bytecode)
}