# @smartweave/modules

> ## Overview The goal of the project is to provide standardized modules which can be used by anyone. ## Installation ``` yarn add @smartweave/modules ``` ## Usage ### Setting up the contract In your project, create an `index.ts`. The modules allows you to

Latest version **0.0.0-alpha** (published 2021-04-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install @smartweave/modules
pnpm add @smartweave/modules
yarn add @smartweave/modules
bun add @smartweave/modules
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.0-alpha |
| Published | 2021-04-13 |
| First published | 2021-04-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 5.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Fabian Riewe |
| Maintainers | fabianriewe |

## Links

- npm: https://www.npmjs.com/package/@smartweave/modules
- Repository: https://github.com/SmartWeave/modules
- Homepage: https://github.com/SmartWeave/modules#readme
- Issues: https://github.com/SmartWeave/modules/issues
- npm.io page: https://npm.io/package/@smartweave/modules

## Recent versions

- 0.0.0-alpha (latest) — 2021-04-13

## README

# SmartWeave modules

## Overview
The goal of the project is to provide standardized modules which can be used 
by anyone.
## Installation
```
yarn add @smartweave/modules
```
## Usage
### Setting up the contract
In your project, create an `index.ts`. The modules allows you to write 
SmartWeave Contracts in Typescript. Use can use this skeleton to get started:
```typescript
import {ActionInterface, StateInterface} from "@smartweave/modules/faces";

declare const SmartWeave: any;

export async function handle(state: StateInterface, action: ActionInterface) {
  switch (action.input.function) {
   
    default:
      throw new ContractError(`${input.function} not implemented`);
  }
}
```
### Using a module
To use a module, simply import it and register it in `handle`:
```typescript
import {ActionInterface, StateInterface} from "@smartweave/modules/faces";
import {Transfer} from "@smartweave/modules/token/transfer";

export async function handle(state: StateInterface, action: ActionInterface) {
  switch (action.input.function) {
    case "transfer":
      return { state: Transfer(state, action) };
    default:
      throw new ContractError(`${input.function} not implemented`);
  }
}
```
### Building the contract
Building the contract requires an extra script. Create a `build.js` script:
```javascript
const { build } = require("esbuild");
const fs = require("fs");

(async () => {
  await build({
    entryPoints: ["./src/index.ts"],
    outfile: "./dist/index.js",
    format: "esm",
    bundle: true,
  });

  let src = fs.readFileSync("./dist/index.js").toString();
  src = src.replace("async function handle", "export async function handle");
  src = src.replace("export {\n  handle\n};\n", "");
  fs.writeFileSync("./dist/index.js", src);
})();
```
To run the script execute:
```
node build.js
```
### Deploying the contract
Deploying the contract requires an extra script. Copy your arweave-keyfile, create a `deploy.js` script and your initial `state.json`:
```javascript
const Arweave = require("arweave");
const { createContract } = require("smartweave");
const fs = require("fs");

const client = new Arweave({
  host: "arweave.net",
  port: 443,
  protocol: "https",
});

const wallet = JSON.parse(fs.readFileSync("./arweave.json"));
const src = fs.readFileSync("./dist/index.js");
const state = fs.readFileSync("./scripts/state.json");

(async () => {
  const id = await createContract(client, wallet, src, state);
  console.log(id);
})();

```
To run the script execute:
```
node deploy.js
```

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