npm package: @ocap/sdk
Description: Forge javascript SDK packages all-in-one
Author: wangshijun
Latest version: 1.28.9

![forge-sdk](https://www.arcblock.io/.netlify/functions/badge/?text=forge-sdk)

[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![docs](https://img.shields.io/badge/powered%20by-arcblock-green.svg)](https://docs.arcblock.io)
[![Gitter](https://badges.gitter.im/ArcBlock/community.svg)](https://gitter.im/ArcBlock/community?utm_source=badge\&utm_medium=badge\&utm_campaign=pr-badge)

> Forge Javascript SDK packages all in one


## Table of Contents

* [Install](#install)
* [Usage](#usage)
  * [ES5(commonjs)](#es5commonjs)
  * [ES6](#es6)
  * [Util](#util)
  * [Wallet](#wallet)
  * [Message](#message)
* [Packages Included](#packages-included)


## Install

```sh
npm i @ocap/sdk
// OR
bun install @ocap/sdk
```


## Usage

### ES5(commonjs)

> Support Node.js

```js
const OcapSDK = require('@ocap/sdk');

// Connect to multi endpoints
OcapSDK.connect('https://test.abtnetwork.io/api', { name: 'test' });
OcapSDK.connect('https://beta.abtnetwork.io/api', { name: 'beta' });

const sender = OcapSDK.Wallet.fromRandom();
const receiver = OcapSDK.Wallet.fromRandom();

// Declare on test chain
await OcapSDK.declare({ moniker: 'sender', wallet: sender }, { conn: 'test' }).then(console.log);
await OcapSDK.declare({ moniker: 'receiver', wallet: receiver }, { conn: 'test' }).then(console.log);

// Get beta chain info
OcapSDK.getChainInfo({ conn: 'beta' }).then(console.log);

// Get local chain info
OcapSDK.getChainInfo({ conn: 'local' }).then(console.log);

// Poke and transfer on test chain
await OcapSDK.transfer({ to: receiver.address, token: 1, wallet: sender }, { conn: 'test' }).then(console.log);
```

### ES6

> If you want to support both node.js and browser, please use lite version
> And the lite version only supports http connections

```js
import OcapSDK from '@ocap/sdk';

OcapSDK.connect('https://test.abtnetwork.io/api', { name: 'test' });

OcapSDK.getChainInfo().then(console.log);
OcapSDK.declare({
  moniker: 'abcd',
  wallet: OcapSDK.Wallet.fromRandom(),
}).then(console.log);
```

### Util

```javascript
const OcapSDK = require('@ocap/sdk');

const bn = OcapSDK.Util.fromTokenToUnit(10, 16);
console.log(bn);
```

### Wallet

```javascript
const OcapSDK = require('@ocap/sdk');

const wallet = OcapSDK.Wallet.fromRandom();
console.log(wallet.toJSON());
```

### Message

```javascript
const OcapSDK = require('@ocap/sdk');

const message = OcapSDK.Message.createMessage('Transaction', {
  from: 'xxx',
  nonce: 1234,
  itx: {},
});
console.log(message);
```


## Packages Included

* [graphql-client](https://www.npmjs.com/package/@ocap/client), available after `OcapSDK.connect`
* [grpc-client](https://www.npmjs.com/package/@ocap/grpc-client), available after `OcapSDK.connect`
* [forge-util](https://www.npmjs.com/package/@ocap/util), available on `OcapSDK.Util`
* [did-util](https://www.npmjs.com/package/@arcblock/did-util), available on `OcapSDK.Util`
* [forge-wallet](https://www.npmjs.com/package/@ocap/wallet), available on `OcapSDK.Wallet`
* [forge-message](https://www.npmjs.com/package/@ocap/message), available on `OcapSDK.Message`