Skip to content

Commit

Permalink
Add titles to CLI Docs (#27)
Browse files Browse the repository at this point in the history
* Add titles to CLI Docs

---------

Co-authored-by: Sushants-Git <[email protected]>
  • Loading branch information
sushantCat and Sushants-Git authored Jun 27, 2024
1 parent 6773cea commit bd302ef
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 47 deletions.
67 changes: 21 additions & 46 deletions docs/docs/cookbook/CliToolWithGardenSdk.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
id: cli-tool-with-garden-sdk
id: swapper-cli
---

# CLI tool with Garden SDK
# Swapper CLI

:::note
This guide is provided as an example to help you get accustomed to using the Garden SDK. It is not intended to serve as a standard for creating CLI tools with the Garden SDK. A proper tool will take into consideration many best practices and optimizations. In the example below, we have cut a lot of corners for simplicity. Full code is available here [Swapper CLI](https://github.com/gardenfi/swapper-cli).
Expand Down Expand Up @@ -81,7 +81,7 @@ bun add @catalogfi/wallets @gardenfi/orderbook @gardenfi/core
# Installs Yargs an npm package used for building cli tools
bun add yargs

# Intalling the types for Yargs
# Installing the types for Yargs
bun add -D @types/yargs

# Installs ethers 6.8.0 as other versions may not be compatible with the SDK
Expand All @@ -93,9 +93,7 @@ bun add [email protected]
- **Initiating yargs**
Yargs in an npm package widely used for building CLI's with nodejs.

```ts
// File: src/command.ts

```ts title="/src/command.ts"
import yargs from "yargs";
import { hideBin } from "yargs/helpers";

Expand All @@ -107,17 +105,14 @@ let ccreator = yargs(hideBin(process.argv));
export { ivar, ccreator };
```

```ts
// File : src/types.ts
```ts title="/src/types.ts"
export type Argv = {
privatekey?: string;
amount?: number;
};
```

```ts
// File: src/index.ts

```ts title="/src/index.ts"
#! /usr/bin/env bun
import { ivar, ccreator } from "./command.ts";

Expand All @@ -132,9 +127,7 @@ console.log(ivar);

- Creating an **evm** wallet

```ts
// File: src/index.ts

```ts title="/src/index.ts"
#! /usr/bin/env bun

import { EVMWallet } from "@catalogfi/wallets";
Expand Down Expand Up @@ -165,9 +158,9 @@ ccreator.command("createevmwallet", "creates an evm wallet", async () => {
});

ccreator.parse(); // Will always come at the end of `src/index.ts` file
```

// File: src/utility.ts

```ts title="/src/utility.ts"
function logAddressAndBalance(address: string, balance: number | bigint) {
console.info("Fetching Address and Balance...");
console.info(`Address : ${address}`);
Expand All @@ -177,9 +170,7 @@ function logAddressAndBalance(address: string, balance: number | bigint) {
export { logAddressAndBalance };
```

```ts
// File: src/errors.ts

```ts title="/src/errors.ts"
class KeyError extends Error {
constructor(message = "Private key is undefined") {
super(message);
Expand All @@ -194,9 +185,7 @@ export { KeyError };

- **Creating a bitcoin wallet**

```ts
// File: src/index.ts

```ts title="/src/index.ts"
#! /usr/bin/env bun

import {
Expand Down Expand Up @@ -246,9 +235,7 @@ As we move on we will need to reuse the **privatekeys** a bunch of times, so it'

- Read ( or Create ) `.swapper_config.json`

```ts
// File: src/index.ts

```ts title="/src/index.ts"
#! /usr/bin/env bun

/* Previous Imports */
Expand All @@ -263,9 +250,9 @@ const DOT_CONFIG_PATH = join(homedir(), ".swapper_config.json");

// Read config
let dotConfig = readJsonFileSync(DOT_CONFIG_PATH);
```

// File: src/types.ts

```ts title="/src/types.ts"
export type DotConfig = {
evmPrivateKey?: string;
bitcoinPrivateKey?: string;
Expand All @@ -277,9 +264,7 @@ export type Argv = {
};
```

```ts
// File: src/utility.ts

```ts title="/src/utility.ts"
import { writeFileSync, existsSync, readFileSync } from "fs";
import { type DotConfig } from "./types.ts";

Expand All @@ -304,9 +289,7 @@ Above code will read the **.swapper_config.json** file present in your home dire

- **Saving private keys** in `.swapper_config.json`

```ts
// File: src/index.ts

```ts title="/src/index.ts"
/* Previous Imports */

import { writeFileSync } from "fs";
Expand Down Expand Up @@ -340,9 +323,7 @@ that's it! now whenever someone creates a wallet with their **private keys**, it

- Create **getdetails**

```ts
// File: src/index.ts

```ts title="/src/index.ts"
ccreator.command(
"getdetails",
"gets the contents of $HOME/.swapper_config.json",
Expand All @@ -360,9 +341,7 @@ ccreator.parse(); // <-- Don't forget that this should be at the end of `src/ind

- Create **swapwbtctobtc**

```ts
// File: src/index.ts

```ts title="/src/index.ts"
/* Previous Imports */

import { sleep } from "bun";
Expand Down Expand Up @@ -436,9 +415,7 @@ ccreator.command("swapwbtctobtc", "Swaps from WBTC to BTC", async () => {
});
```

```ts
// File: src/utility.ts

```ts title="/src/utility.ts"
/* Previous Imports */

import { BitcoinWallet, BitcoinProvider, EVMWallet } from "@catalogfi/wallets";
Expand Down Expand Up @@ -490,13 +467,11 @@ export {
};
```

```ts
// File: src/errors.ts

```ts title="/src/errors.ts"
/* KeyError class */

class WalletError extends Error {
constructor(message = "Wallets have not been initialised") {
constructor(message = "Wallets have not been initialized") {
super(message);
this.name = "WalletError";
}
Expand Down
2 changes: 1 addition & 1 deletion docs/sidebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const sidebars: SidebarsConfig = {

cookbook: [
"cookbook/cookbook",
"cookbook/cli-tool-with-garden-sdk",
"cookbook/swapper-cli",
"cookbook/swapper-frontend"
],

Expand Down

0 comments on commit bd302ef

Please sign in to comment.