2.0.0 • Published 3 days ago

@bnb-chain/greenfield-js-sdk v2.0.0

Weekly downloads
-
License
GPLv3
Repository
github
Last release
3 days ago

Greenfield Chain JS SDK

Install

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

Create Client

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

Tx

1. Tx construction

transfer tx for example:

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

2. Simulate Tx

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

3. Boradcast Tx

// 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:

// 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:

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

Query

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

Examples:

Storage Provider Client

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

SDK support two 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:

// 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',
  },
);
// 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.

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

axios.put(...)
2.0.0-alpha.9

9 days ago

2.0.0-alpha.10

3 days ago

2.0.0

3 days ago

1.2.4

11 days ago

2.0.0-alpha.8

11 days ago

2.0.0-alpha.7

14 days ago

2.0.0-alpha.6

24 days ago

2.0.0-alpha.5

25 days ago

2.0.0-alpha.4

30 days ago

1.2.3

1 month ago

1.3.0-alpha.3

1 month ago

1.3.0-alpha.2

2 months ago

1.3.0-alpha.0

2 months ago

1.3.0-alpha.1

2 months ago

1.2.2

2 months ago

1.2.0

4 months ago

1.2.1

2 months ago

1.1.3

4 months ago

1.2.0-alpha.2

4 months ago

1.2.0-alpha.1

4 months ago

1.1.1

4 months ago

1.1.2

4 months ago

1.1.0

5 months ago

1.1.0-alpha.2

5 months ago

1.1.0-alpha.3

5 months ago

1.1.0-alpha.1

5 months ago

1.1.0-alpha.0

5 months ago

1.0.6

5 months ago

1.0.5

5 months ago

1.0.4

5 months ago

1.0.3

6 months ago

1.0.2

6 months ago

1.0.1

7 months ago

1.0.1-alpha.0

7 months ago

1.0.0

7 months ago

1.0.0-alpha.3

7 months ago

0.2.7-alpha.2

7 months ago

0.2.7-alpha.1

7 months ago

0.2.7-alpha.0

7 months ago

0.2.6

7 months ago

0.2.6-alpha.0

8 months ago

0.2.5

8 months ago

0.2.5-alpha.9

8 months ago

0.2.5-alpha.8

8 months ago

0.2.5-alpha.7

8 months ago

0.2.5-alpha.6

8 months ago

0.2.5-alpha.5

8 months ago

0.2.5-alpha.4

8 months ago

0.2.5-alpha.3

8 months ago

0.2.5-alpha.2

8 months ago

0.2.5-alpha.1

8 months ago

0.2.4

8 months ago

0.2.4-alpha.28

8 months ago

0.2.4-alpha.27

8 months ago

0.2.4-alpha.26

8 months ago

0.2.4-alpha.25

8 months ago

0.2.4-alpha.24

8 months ago

0.2.4-alpha.23

8 months ago

0.2.4-alpha.22

8 months ago

0.2.4-alpha.21

8 months ago

0.2.4-alpha.20

8 months ago

0.2.4-alpha.19

8 months ago

0.2.4-alpha.18

8 months ago

0.2.4-alpha.17

8 months ago

0.2.4-alpha.16

8 months ago

0.2.4-alpha.15

8 months ago

0.2.4-alpha.14

8 months ago

0.2.4-alpha.13

8 months ago

0.2.4-alpha.12

8 months ago

0.2.4-alpha.11

8 months ago

0.2.4-alpha.10

8 months ago

0.2.4-alpha.9

8 months ago

0.2.4-alpha.8

8 months ago

0.2.4-alpha.7

9 months ago

0.2.4-alpha.6

9 months ago

0.2.4-alpha.5

9 months ago

0.2.4-beta.0

9 months ago

0.2.4-alpha.4

9 months ago

0.2.4-alpha.3

9 months ago

0.2.4-alpha.2

9 months ago

0.2.4-alpha.1

9 months ago

0.2.4-alpha.0

9 months ago

0.2.3

9 months ago