# @smartweaver/slick-contract

> Slick builder APIs for Arweave smart contract creation

Latest version **0.0.10** (published 2023-12-19) · GPL-3.0 license · 0 weekly downloads

## Install

```sh
npm install @smartweaver/slick-contract
pnpm add @smartweaver/slick-contract
yarn add @smartweaver/slick-contract
bun add @smartweaver/slick-contract
```

Provides the command `slick-contract`.

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.10 |
| Published | 2023-12-19 |
| First published | 2023-11-08 |
| Weekly downloads | 0 |
| License | GPL-3.0 |
| TypeScript types | none |
| Module format | ESM |
| Dependencies | 3 |
| Unpacked size | 113.1 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Eric Crooks |
| Maintainers | crookse |
| Keywords | arweave, blockchain, blockweave, contracts, crypto, dapps, drashland, framework, http, permanence, smart-contracts, smartweave, web3 |

## Links

- npm: https://www.npmjs.com/package/@smartweaver/slick-contract
- Repository: https://github.com/smartweaver/slick-contract
- npm.io page: https://npm.io/package/@smartweaver/slick-contract

## Dependencies (3)

- [esbuild](https://npm.io/package/esbuild.md) ~0.19.10
- [prettier](https://npm.io/package/prettier.md) ~3.1.1
- [@smartweaver/core](https://npm.io/package/@smartweaver/core.md) ~0.0.2

## Alternatives

- [@sveltejs/kit](https://npm.io/package/@sveltejs/kit.md) — 2.2M weekly downloads
- [@atlaskit/theme](https://npm.io/package/@atlaskit/theme.md) — 402.0K weekly downloads
- [@tangle-network/brand](https://npm.io/package/@tangle-network/brand.md) — 10.0K weekly downloads
- [seneca](https://npm.io/package/seneca.md) — 7.4K weekly downloads
- [@bsb/base](https://npm.io/package/@bsb/base.md) — 7.2K weekly downloads

## Recent versions

- 0.0.10 (latest) — 2023-12-19
- 0.0.9 — 2023-12-19
- 0.0.8 — 2023-12-19
- 0.0.7 — 2023-11-08
- 0.0.6 — 2023-11-08
- 0.0.5 — 2023-11-08
- 0.0.4 — 2023-11-08
- 0.0.3 — 2023-11-08
- 0.0.2 — 2023-11-08
- 0.0.1 — 2023-11-08

## README

# Slick Contract

Slick builder APIs for Arweave smart contract creation

## Installation

```text
npm install @smartweaver/slick-contract
```

## Usage - Creating a Contract and State File

This is a simple example showing how to:

- define an initial state;
- define a `contract` object using the initial state;
- add actions to the `contract`; and
- providing the `contract` to the exported `handle()` function.

### Steps

1. Create your `state.ts` file.

    ```ts
    // File: state.ts

    export const state = {
      users: {},
      posts: {},
    };
    ```

1. Create your `contract.ts` file.

    ```ts
    // File: contract.ts

    import { Contract } from "@smartweaver/slick-contract/mod.js";
    import { state } from "./state.ts";

    declare const ContractError;

    //
    // Create your contract
    //

    const contract = Contract
      .builder()                             // Access the `Contract` class' builder

      .initialState<typeof state>()          // Call `initialState()` and (optionally) pass your state's typing

      .action("add_user", (context) => {     // Now you can add actions to modify the state

        const { input } = context.action;    // The `input` object is in the `context.action` field

        const { id, name } = input.payload;  // If we have a user in `input.payload` ...
        context.state.users[id] = { name };  // ... then we can add the user to the state

        return context;                      // Return the `context` object to "end" the action
      })

      .build();                              // Build your contract when you are done (this returns a `.handle()` method)

    //
    // Export the required handle function
    //

    export async function handle(state, action) {

      const context = { state, action };     // Create a `context`` object. This becomes the `context` param
                                             // in the `.action("some_name", (context) => { ... })` methods.

      try {
        const result = await contract        // Pass the `context` object to your contract to get a `result`
          .handle(context);

        return { state: result.state };      // The `result` will contain the `state` object that you return.
                                             // Returning the `state` is required. See the following:
                                             // https://github.com/ArweaveTeam/SmartWeave/blob/master/CONTRACT-GUIDE.md#contract-format-and-interface
      } catch (e) {
        const message = e.message            // Slick Contract's internals only throw `Error` objects. They
          ? e.message                        // not throw `ContractError` objects. You have to throw the
          : "We hit an error. Sorry!";       // `ContractError` object yourself like how it is shown here.
        throw new ContractError(message);
      }
    }
    ```

1. Build your `contract.ts` file (and optionally your `state.ts` file).

    ```bash
    npx @smartweaver/slick-contract build contract.ts state.ts
    ```

    The above command will create:

   - a `contract.ts.build.js` file in the same directory as your `contract.ts` file; and
   - a `state.ts.build.json` file in the same directory as your `state.ts` file.
   
   These are the files you deploy to the Arweave network.

    _Note: This script tries to be smart about ensuring your built contract file has a valid `export async function handle(...)`, but please verify this manually. Otherwise your contract might not work in the Arweave network._

## Usage - CLI

The CLI only exists to help you build your contract. It only:

- bundles your contract file into a single `.js` file; and
- converts your state file from `.ts|.js` to `.json`.

You can run the CLI using:

```bash
npx @smartweaver/slick-contract <COMMAND> <CONTRACT_FILE> [STATE_FILE]
```

The full CLI usage is below:

```text
USAGE
  
    npx @smartweaver/slick-contract <COMMAND> <CONTRACT_FILE> [STATE_FILE]


EXAMPLE USAGE

    npx @smartweaver/slick-contract build src/contract.ts src/state.ts


COMMANDS

    The commands for this script are as follows:

    build        Build your contract's source and state files
```

## Data Types

Slick Contract has a strict syntax. It requires the `action` and `context` objects to match the data types below. Validation is performed on these objects to ensure they are in the correct data type format. If not, an error will be thrown with a message showing how to correct the objects.

### Action Objects

The `action` argument in the `handle` function should have the following `Action` data type shown below:

```ts
type Action = {
  input: {
    function: string;
    payload?: any;
  }
};

export function handle(state: any, action: Action) {
  //
  // ... code shortened for brevity
  //
}
```

### Context Objects

The `context` argument in the `.action("some_name", (context) => { ... })` method should have the following `Context` data type shown below:

```ts
//
// ... code shortened for brevity
//

type Action = {
  input: {
    function: string;
    payload?: any;
  }
};

type Context = {
  state: any;
  action: Action;
};

export function handle(state: any, action: Action) {

  try {

    const context: Context = { state, action };
    const result = contract.handle(context);
    return { state: result.state };

  } catch (error) {

    throw new ContractError(error.message);

  }
}
```

## Resources/Links

- npm: https://www.npmjs.com/package/@smartweaver/slick-contract

---
_Source: https://npm.io/package/@smartweaver/slick-contract · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
