# Rust Bot SDK


Bot traders interact with the CrypDefi backend via API calls. To simplify
authentication and transaction signing, the Bot SDK can be used instead
of manually managing individual API requests.

## Create keys for the Bot login.

### Generate Bot's private key

# Unix
```bash
openssl ecparam -genkey -name prime256v1 -out bot_private_key.key
```
 
# Windows
 ```bash
ssh-keygen.exe -t ecdsa -b 256 -m pkcs8 -f .\\bot_key
```


### Generate Bot's public key

# Unix
```bash
openssl ec -in bot_private_key.key -pubout -out bot_public_key.pem
openssl pkcs8 -in bot_private_key.key -inform pem -topk8 -nocrypt -out bot_private_key.pkcs8
```
 
# Windows
```bash
    ssh-keygen.exe -f .\\bot_key.pub -e -m pkcs8
```

## Examples

### Instantiating a Bot

```rust
use crypdefi_bot_sdk::user::{Bot, UserId};
use std::fs;

// --------------------------- CONFIG ---------------------------
// Replace with your bot's user_id.
let user_id_str = String::from("us-0000000000-94518ea57547afd340c3");
// This check that the format of the user id is at least correct.
let user_id = UserId::new(user_id_str)?;

// Replace with the wallet_id of the wallet you want to trade with.
let wallet_id = "wa-0000000000-9f3542a65690ff697b85"; 
// This is the bot's private key used to authenticate with CrypDefi (not the wallet's private key).
let private_key_pem = fs::read_to_string("private_key_pkcs8.pem").expect("Failed to read private_key_pkcs8.pem");

// [OPTIONAL] customize the main API endpoint for your deployment
let base_url = String::from("https://api.release.crypdefi.eu");

// [OPTIONAL] customize the signing API endpoint for your deployment
let signing_url = String::from("https://sign.release.crypdefi.eu");

let bot = Bot::new(String::from(private_key_pem), user_id, Some(base_url), Some(signing_url)).unwrap();
println!("Bot: {:?}", bot);

// --------------------------- LOGIN WITH BOT ---------------------------
// if you turn off auto_refresh the bot will not automatically re-authenticate itself when its login-token is about to expire
bot.login(
    false
).await.unwrap();

```

### Getting values from the Bot

```rust
// **Note:**  Bot should already be instantiated and logged in. 

// --------------------------- Check for expiration time of AUTH ---------------------------
let expiration = bot.auth_expiration_unix_time().await;
println!("Expiration: {:?}\n", expiration);

// --------------------------- FETCH ASSOCIATED WALLETS ---------------------------
let wallets = bot.get_wallets().await.unwrap();
println!("Wallets: {:?}\n", wallets);

```

### Bot Actions

```rust
// **Note:**  Bot should already be instantiated and logged in. 

// --------------------------- CREATE & SIGN TRANSACTION ---------------------------
println!("Preparing transaction to sign...\n");
// Include steps to create transaction here. Replace hex below with the actual hex string of the transaction to sign.
let example_evm_hex = "02f8af01018390f560850461933067828cb394a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4880b844095ea7b300000000000000000000000097802f38a37e1d789eba194513e3eb7e918d34df000000000000000000000000000000000000000000000000000000001dcd6500c001a0ad0b4a87309ef94b96d38f145d676d971ca1f1e4702c9cace99fdec8df4a8814a008651a171f31629bcf3a686ca26b9d3cece44c6dfec39fb2c1848e3b290ba121";

println!("Signing transaction...");
// Sign the transaction using the wallet that was configured. Make sure the bot has access to this wallet in the Management UI.
let signature = bot.sign_transaction(
    wallet_id.to_string(),
    SignatureRequestKind::Transaction,
    example_evm_hex.to_string()
).await.unwrap();
println!("Signature: {:?}\n", signature);
 
// --------------------------- REFRESH SESSION ---------------------------
// Pass `true` to enable auto-refresh (recommended for long-running bots)
println!("Refreshing Bot session...");
bot.refresh(true).await.unwrap();
 
// --------------------------- LOGOUT ---------------------------
let logout_result = bot.logout().await.unwrap();
println!("Logout result: {:?}", logout_result);


```
## Contributors

These are commands and tools for developers of the SDK.

You will need to have the static library somewhere in the file system.

### Building library locally
Run: 
```bash
cargo build
```

### run example bot

This runs a very simple main function with the basic functionality of the SDK
```bash
cargo run --bin crypdefi-bot
```
