Ain-Js

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.