Skip to content

Commit

Permalink
Merge pull request #836 from AhaLabs/feat/bindings_from_network
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal authored Aug 4, 2023
2 parents 5f71b96 + 7ac5d48 commit f25effe
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 15 deletions.
4 changes: 2 additions & 2 deletions cmd/soroban-cli/src/commands/contract/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ pub enum Error {
}

impl Cmd {
pub fn run(&self) -> Result<(), Error> {
pub async fn run(&self) -> Result<(), Error> {
match &self {
Cmd::Json(json) => json.run()?,
Cmd::Rust(rust) => rust.run()?,
Cmd::Typescript(ts) => ts.run()?,
Cmd::Typescript(ts) => ts.run().await?,
}
Ok(())
}
Expand Down
40 changes: 32 additions & 8 deletions cmd/soroban-cli/src/commands/contract/bindings/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@ use std::{fmt::Debug, path::PathBuf};
use clap::{command, Parser};
use soroban_spec_typescript::{self as typescript, boilerplate::Project};

use crate::commands::config::{
locator,
network::{self, Network},
};
use crate::wasm;
use crate::{
commands::{
config::{
ledger_file, locator,
network::{self, Network},
},
contract::{self, fetch},
},
utils::contract_spec::{self, ContractSpec},
};

#[derive(Parser, Debug, Clone)]
#[group(skip)]
pub struct Cmd {
#[command(flatten)]
wasm: wasm::Args,
/// Path to optional wasm binary
#[arg(long)]
pub wasm: Option<std::path::PathBuf>,

/// where to place generated project
#[arg(long)]
Expand Down Expand Up @@ -47,11 +54,28 @@ pub enum Error {

#[error(transparent)]
Locator(#[from] locator::Error),
#[error(transparent)]
Fetch(#[from] fetch::Error),
#[error(transparent)]
Spec(#[from] contract_spec::Error),
}

impl Cmd {
pub fn run(&self) -> Result<(), Error> {
let spec = self.wasm.parse().unwrap().spec;
pub async fn run(&self) -> Result<(), Error> {
let spec = if let Some(wasm) = &self.wasm {
let wasm: wasm::Args = wasm.into();
wasm.parse().unwrap().spec
} else {
let fetch = contract::fetch::Cmd {
contract_id: self.contract_id.clone(),
out_file: None,
locator: self.locator.clone(),
network: self.network.clone(),
ledger_file: ledger_file::Args::default(),
};
let bytes = fetch.get_bytes().await?;
ContractSpec::new(&bytes)?.spec
};
if self.output_dir.is_file() {
return Err(Error::IsFile(self.output_dir.clone()));
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-cli/src/commands/contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub enum Error {
impl Cmd {
pub async fn run(&self) -> Result<(), Error> {
match &self {
Cmd::Bindings(bindings) => bindings.run()?,
Cmd::Bindings(bindings) => bindings.run().await?,
Cmd::Build(build) => build.run()?,
Cmd::Bump(bump) => bump.run().await?,
Cmd::Deploy(deploy) => deploy.run().await?,
Expand Down
13 changes: 11 additions & 2 deletions cmd/soroban-cli/src/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use clap::arg;
use soroban_env_host::xdr::{self, ContractEntryBodyType, LedgerKey, LedgerKeyContractCode};
use std::{fs, io, path::Path};
use std::{
fs, io,
path::{Path, PathBuf},
};

use crate::utils::{self, contract_spec::ContractSpec};

Expand Down Expand Up @@ -30,7 +33,7 @@ pub enum Error {
pub struct Args {
/// Path to wasm binary
#[arg(long)]
pub wasm: std::path::PathBuf,
pub wasm: PathBuf,
}

impl Args {
Expand Down Expand Up @@ -63,6 +66,12 @@ impl Args {
}
}

impl From<&PathBuf> for Args {
fn from(wasm: &PathBuf) -> Self {
Self { wasm: wasm.clone() }
}
}

impl TryInto<LedgerKey> for Args {
type Error = Error;
fn try_into(self) -> Result<LedgerKey, Self::Error> {
Expand Down
4 changes: 2 additions & 2 deletions docs/soroban-cli-full-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ Generate Rust bindings

Generate a TypeScript / JavaScript package

**Usage:** `soroban contract bindings typescript [OPTIONS] --wasm <WASM> --output-dir <OUTPUT_DIR> --contract-name <CONTRACT_NAME> --contract-id <CONTRACT_ID>`
**Usage:** `soroban contract bindings typescript [OPTIONS] --output-dir <OUTPUT_DIR> --contract-name <CONTRACT_NAME> --contract-id <CONTRACT_ID>`

###### **Options:**

* `--wasm <WASM>` — Path to wasm binary
* `--wasm <WASM>` — Path to optional wasm binary
* `--output-dir <OUTPUT_DIR>` — where to place generated project
* `--contract-name <CONTRACT_NAME>`
* `--contract-id <CONTRACT_ID>`
Expand Down

0 comments on commit f25effe

Please sign in to comment.