# Using the client

The crate can be used via crates.io:
```toml
fabric-sdk = "0.4.0"
```

Here is an simple code example how to use the library:

```rust
use std::error::Error;

use fabric_sdk::{client::ClientBuilder, identity::IdentityBuilder, signer::Signer};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let pem_bytes = include_bytes!("/home/user/fabric/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/signcerts/User1@org1.example.com-cert.pem");
    let tlsca_bytes = include_bytes!("/home/user/fabric/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/tlsca/tlsca.org1.example.com-cert.pem");
    let msp_key_bytes = include_bytes!("/home/user/fabric/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/keystore/priv_sk");

    let identity = IdentityBuilder::from_pem(pem_bytes)
        .with_msp("Org1MSP")?
        .with_private_key(msp_key_bytes)?
        .build()?;

    let mut client = ClientBuilder::new()
        .with_identity(identity)?
        .with_tls(tlsca_bytes)?
        .with_scheme("https")?
        .with_authority("localhost:7051")?
        .build()?;
    client.connect().await?;

    let mut tx_builder = client
        .get_transaction_builder()
        .with_channel_name("mychannel")?
        .with_chaincode_id("basic")?
        .with_function_name("CreateAsset")?
        .with_function_args(["assetCustom", "orange", "10", "Frank", "600"])?;
    match tx_builder.build() {
        Ok(prepared_transaction) => client.submit_transaction(prepared_transaction).await {
            Ok(result) => {
                println!("{}", String::from_utf8_lossy(result.as_slice()));
            }
            Err(err) => println!("{}", err),
        },
        Err(err) => println!("{}", err),
    }

    tx_builder.with_function_args(["assetCustom", "orange", "0", "AnotherFrank", "100"])?;
    match tx_builder.build() {
        Ok(prepared_transaction) => match client.submit_transaction(prepared_transaction).await {
            Ok(result) => {
                println!("{}", String::from_utf8_lossy(result.as_slice()));
            }
            Err(err) => println!("{}", err),
        },
        Err(err) => println!("{}", err),
    }
    Ok(())
}
```

To run this example you need to have a test network running with fabric samples and the basic assets chaincode deployed.

Executing the example twice will result the first one sending an error, that the asset already exists, demonstrating the behavior of an error.


# Developing locally

The tests written in this project are based on the basic chaincode written in the [docs](https://ethan-li-fabric.readthedocs.io/en/latest/test_network.html).
A test network with the basic asset chaincode deployed is recommended to test functionallity via tests. A test project using the library locally on your machine is also possible.

The tests are using the env variables defined in .env.

Clone the project
```bash
git clone https://github.com/LF-Decentralized-Trust-labs/fabric-sdk-rust && cd fabric-sdk-rust
```

Copy the env_default to .env and edit it according to your needings (if needed)
```bash
cp env_default .env
```
This project uses the fabric-protos git as submodule for the protobuf files. To initialize the submodule, run:

```bash
git submodule update --init --recursive
```

And now you are ready to go!

If you have set up the fabric test network with the basic asset chaincode, the tests should succeed:

```bash
cargo test
```
