1.13.3 • Published 10 months ago

@ainblockchain/ain-js v1.13.3

Weekly downloads
32
License
ISC
Repository
github
Last release
10 months ago

AIN Blockchain SDK

npm version npm-downloads license

A simple library for JavaScript and TypeScript to interact with AI Network via JSON RPC API.

Installation

$ npm install @ainblockchain/ain-js

Usage

The full API of this library can be found in API document, along with code examples. The following code shows how to create a wallet account using the wallet API.

Create Wallet

const Ain = require('@ainblockchain/ain-js').default;

const ain = new Ain('https://testnet-api.ainetwork.ai', 'wss://testnet-event.ainetwork.ai', 0);

function main() {
  const accounts = ain.wallet.create(1);

  console.log(accounts[0]);
}

main();

// output example:
// {
//   address: '0xb2585543Cfcfb79CF73a1a14b2DfBC411913940F',
//   private_key: '...',
//   public_key: '...'
// }

Read and Write Data

const Ain = require('@ainblockchain/ain-js').default;

const ain = new Ain('https://testnet-api.ainetwork.ai', 'wss://testnet-event.ainetwork.ai', 0);

async function main() {
  const address = ain.wallet.addAndSetDefaultAccount('YOUR_PRIVATE_KEY');

  // write value to db
  const result = await ain.db.ref('YOUR_DATA_PATH').setValue({
    value: 'hello',
    gas_price: 500,
    timestamp: Date.now(),
    nonce: -1,
  });

  // read value from db
  const data = await ain.db.ref('YOUR_DATA_PATH').getValue();
  console.log(data);
}

main();

Rules and Owners

Rule configs validate transactions and control write permissions, while owner configs manage write access to both rules and themselves.

The following code shows how to configure a rule to allow write access for all users:

const Ain = require('@ainblockchain/ain-js').default;

const ain = new Ain('https://testnet-api.ainetwork.ai', 'wss://testnet-event.ainetwork.ai', 0);

async function main() {
  const address = ain.wallet.addAndSetDefaultAccount('YOUR_PRIVATE_KEY');

  // set the rule to allow write access for all users
  const result = await ain.db.ref(appPath).setRule({
    value: {
      '.rule': {
        write: true,
      },
    },
    gas_price: 500,
    timestamp: Date.now(),
    nonce: -1,
  });
}

main();

Function Call

const Ain = require('@ainblockchain/ain-js').default;

const ain = new Ain('https://testnet-api.ainetwork.ai', 'wss://testnet-event.ainetwork.ai', 0);

async function main() {
  const address = ain.wallet.addAndSetDefaultAccount('YOUR_PRIVATE_KEY');

  // trigger a function when a value is written to the data path
  const result = await ain.db.ref('YOUR_DATA_PATH').setFunction({
    value: {
      '.function': {
        YOUR_FUNCTION_ID: {
          function_type: 'REST',
          function_url: 'YOUR_FUNCTION_URL',
          function_id: 'YOUR_FUNCTION_ID',
        },
      },
    },
    gas_price: 500,
    timestamp: Date.now(),
    nonce: -1,
  });
}

main();

Documentation

Browse the documentation online:

Testing

To run tests, a local blockchain node must be running.

  1. Clone and install the AIN Blockchain:
$ git clone https://github.com/ainblockchain/ain-blockchain.git
$ cd ain-blockchain
$ npm install
  1. Start the local blockchain:
$ bash start_local_blockchain.sh
  • For event manager test cases, ensure Node 2 is started with the ENABLE_EVENT_HANDLER environment variable set to true.
  1. Run the tests:
$ npm run test
  • To update test snapshot files:
$ npm run test_snapshot

License

MPL-2.0 License.

1.13.2

11 months ago

1.13.1

11 months ago

1.13.3

10 months ago

1.13.0

1 year ago

1.12.0

1 year ago

1.10.2

1 year ago

1.11.0

1 year ago

1.10.1

1 year ago

1.10.0

1 year ago

1.9.0

1 year ago

1.8.0

2 years ago

1.7.1

2 years ago

1.7.0

2 years ago

1.6.3

2 years ago

1.6.2

2 years ago

1.6.1

2 years ago

1.6.0

2 years ago

1.5.0

2 years ago

1.4.0

2 years ago

1.3.7

3 years ago

1.3.6

3 years ago

1.3.5

3 years ago

1.3.4

3 years ago

1.3.3

3 years ago

1.3.2

4 years ago

1.2.0

4 years ago

1.1.9

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.1

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago