# @bnb-chain/greenfield-js-sdk

> greenfield js chain and sp sdk

Latest version **2.2.2** (published 2025-05-07) · GPLv3 license · 0 weekly downloads

## Install

```sh
npm install @bnb-chain/greenfield-js-sdk
pnpm add @bnb-chain/greenfield-js-sdk
yarn add @bnb-chain/greenfield-js-sdk
bun add @bnb-chain/greenfield-js-sdk
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

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

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.2.2 |
| Published | 2025-05-07 |
| First published | 2023-08-03 |
| Weekly downloads | 0 |
| License | GPLv3 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 26 |
| Unpacked size | 2.2 MB |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 42 |
| Maintainers | robot-ux |
| Keywords | greenfiled, js-sdk |

## Links

- npm: https://www.npmjs.com/package/@bnb-chain/greenfield-js-sdk
- Repository: https://github.com/bnb-chain/greenfield-js-sdk
- Homepage: https://github.com/bnb-chain/greenfield-js-sdk/tree/alpha/packages/js-sdk#readme
- Issues: https://github.com/bnb-chain/greenfield-js-sdk/issues
- npm.io page: https://npm.io/package/@bnb-chain/greenfield-js-sdk

## Dependencies (26)

- [long](https://npm.io/package/long.md) ^5.2.1
- [dayjs](https://npm.io/package/dayjs.md) ^1.11.7
- [tsyringe](https://npm.io/package/tsyringe.md) ^4.8.0
- [lodash.get](https://npm.io/package/lodash.get.md) ^4.4.2
- [lodash.set](https://npm.io/package/lodash.set.md) ^4.3.2
- [mime-types](https://npm.io/package/mime-types.md) ^2.1.35
- [superagent](https://npm.io/package/superagent.md) ^9.0.2
- [cross-fetch](https://npm.io/package/cross-fetch.md) ^4.0.0
- [@noble/curves](https://npm.io/package/@noble/curves.md) ^1.3.0
- [lodash.sortby](https://npm.io/package/lodash.sortby.md) ^4.7.0
- [browser-or-node](https://npm.io/package/browser-or-node.md) ^3.0.0
- [fast-xml-parser](https://npm.io/package/fast-xml-parser.md) ^4.2.7
- [@cosmjs/stargate](https://npm.io/package/@cosmjs/stargate.md) ^0.32.0
- [lodash.clonedeep](https://npm.io/package/lodash.clonedeep.md) ^4.5.0
- [lodash.mapvalues](https://npm.io/package/lodash.mapvalues.md) ^4.6.0
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13
- [@ethersproject/bytes](https://npm.io/package/@ethersproject/bytes.md) 5.7.0
- [@ethersproject/units](https://npm.io/package/@ethersproject/units.md) ^5.7.0
- [@cosmjs/proto-signing](https://npm.io/package/@cosmjs/proto-signing.md) ^0.32.0
- [@ethersproject/base64](https://npm.io/package/@ethersproject/base64.md) ^5.7.0
- [ethereum-cryptography](https://npm.io/package/ethereum-cryptography.md) ^2.0.0
- [@cosmjs/tendermint-rpc](https://npm.io/package/@cosmjs/tendermint-rpc.md) ^0.32.0
- [@ethersproject/strings](https://npm.io/package/@ethersproject/strings.md) 5.7.0
- [@metamask/eth-sig-util](https://npm.io/package/@metamask/eth-sig-util.md) ^5.0.2
- [@ethersproject/signing-key](https://npm.io/package/@ethersproject/signing-key.md) ^5.8.0
- [@bnb-chain/greenfield-cosmos-types](https://npm.io/package/@bnb-chain/greenfield-cosmos-types.md) 0.4.0-alpha.32

## Recent versions

- 2.2.2 (latest) — 2025-05-07
- 2.2.2-alpha.0 (alpha) — 2025-05-07
- 0.2.4-beta.0 (beta) — 2023-08-17
- 2.2.1 — 2025-03-06
- 2.2.1-alpha.0 — 2025-03-05
- 2.2.0 — 2024-09-23
- 2.2.0-alpha.0 — 2024-09-19
- 2.1.1 — 2024-07-01
- 2.1.0 — 2024-07-01
- 2.0.1 — 2024-05-31
- 2.1.0-alpha.2 — 2024-05-28
- 2.1.0-alpha.1 — 2024-05-21
- 2.1.0-alpha.0 — 2024-05-07
- 2.0.0 — 2024-04-30
- 2.0.0-alpha.10 — 2024-04-30
- … 80 more at https://npm.io/package/@bnb-chain/greenfield-js-sdk/versions

## README

# Greenfield Chain JS SDK

## Install

```bash
npm install @bnb-chain/greenfield-js-sdk
```

## Create Client

```js
import {Client} from '@bnb-chain/greenfield-js-sdk'

const client = Client.create(GRPC_URL, GREEN_CHAIN_ID);
```

## Usage

The SDK consists of two parts:

* Chain: https://docs.bnbchain.org/greenfield-docs/docs/api/blockchain-rest
* Storage Provider: https://docs.bnbchain.org/greenfield-docs/docs/api/storage-provider-rest

## Chain

### Tx

#### 1. Tx construction

`transfer` tx for example:

```js
const transferTx = await client.account.transfer({
  fromAddress: address,
  toAddress: transferInfo.to,
  amount: [
    {
      denom: 'BNB',
      amount: ethers.utils.parseEther(transferInfo.amount).toString(),
    },
  ],
});
```

#### 2. Simulate Tx

```js
// simulate tx
const simulateInfo = await transferTx.simulate({
  denom: 'BNB',
});
```

#### 3. Boradcast Tx

```js
// broadcast tx
const broadcastRes = await transferTx.broadcast({
  denom: 'BNB',
  gasLimit: Number(simulateInfo.gasLimit),
  gasPrice: simulateInfo.gasPrice,
  payer: address,
  granter: '',
});
```

#### NOTICE: Signature mode for `Broadcast`  

`broadcast` use `window.ethereum` as signature provider by default.

If you want to use others, you can set `signTypedDataCallback`:

```js
// trustwallet:
const broadcastRes = await transferTx.broadcast({
  // ...
  signTypedDataCallback: async (addr: string, message: string) => {
    return await window.trustwallet.request({
      method: 'eth_signTypedData_v4',
      params: [addr, message],
    });
  }
});
```

If you broadcast in Nodejs, you can broadcast a tx by `privateKey`:

```js
const broadcastRes = await transferTx.broadcast({
  // ...
  privateKey: '0x.......'
});
```

### Query

```js
// get account info
await client.account.getAccount(address);
```

Examples:

* [Next.js](../../examples/nextjs/README.md)
* [Node.js](../../examples/nodejs/README.md)

### Storage Provider Client

> https://docs.bnbchain.org/greenfield-docs/docs/api/storgae-provider-rest

SDK support two [authentication type](https://docs.bnbchain.org/greenfield-docs/docs/api/storage-provider-rest#authentication-type):

* ECDSA: It is usually used on Node.js(Because it need to use a private key)
* EDDSA: It is usually used in a browser

`getBucketReadQuota` as example:

```js
// browser:

// generate seed:
const allSps = await getAllSps();
const offchainAuthRes = await client.offchainauth.genOffChainAuthKeyPairAndUpload(
  {
    sps: allSps,
    chainId: GREEN_CHAIN_ID,
    expirationMs: 5 * 24 * 60 * 60 * 1000,
    domain: window.location.origin,
    address: 'your address',
  },
  provider: 'wallet provider',
);

// request sp api
const bucketQuota = await client.bucket.getBucketReadQuota(
  {
    bucketName,
  },
  {
    type: 'EDDSA',
    seed: offchainAuthRes.seedString,
    domain: window.location.origin,
    address: 'your address',
  },
);
```

```js
// Node.js:
// request sp api
const bucketQuota = await client.bucket.getBucketReadQuota(
  {
    bucketName,
  },
  {
    type: 'ECDSA',
    privateKey: '0x....'
  },
);
```

#### Support Custom Http Request

It's actually an HTTP request, we use `fetch` by default, and if you want to use another http library like `axios`, we'll construct it for you as well.

```js
// custom upload object
const { PUT_OBJECT: getPutObjectMetaInfo } = client.spClient.getMetaInfo(endpoint, payload);
const {reqMeta, url} = await getPutObjectMetaInfo(endpoint, params);

axios.put(...)
```

---
_Source: https://npm.io/package/@bnb-chain/greenfield-js-sdk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
