Skip to content

Commit

Permalink
Merge pull request #317 from ainft-team/release/2.1.0
Browse files Browse the repository at this point in the history
Release/2.1.0
  • Loading branch information
jiyoung-an authored Sep 13, 2024
2 parents aa6d696 + fd3c3ab commit 9c5db01
Show file tree
Hide file tree
Showing 72 changed files with 3,414 additions and 2,688 deletions.
99 changes: 80 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,81 @@
The ainft-js is typescript SDK to interact with AIN blockchain and create and manage AINFT.

## AINFT Factory
The AINFT Factory is a component consisting of AINFT Factory server and ainft-js. AINFT Factory supports the following two features:

The AINFT Factory is a component consisting of AINFT Factory server and ainft-js. AINFT Factory supports the following two features:

- AINFT: Supports creating and managing AINFT, the NFT of the Ain blockchain.
- Tokenomics: Supports functions for activating tokenomics in NFT communities.

You can see reference about AINFT Factory: https://docs.ainetwork.ai/ainfts/ainft.

## Getting start
## Installation

```bash
npm install @ainft-team/ainft-js
yarn add @ainft-team/ainft-js
```

After installing the app, you can then import and use the SDK
```javascript
const AinftJs = require('@ainft-team/ainft-js').default;
## Usage

First, install the application and import the SDK to get started:

// Enter the private key for the account you want to use to create and manage AINFT.
// If you don't have an account, you can create it through ain wallet,
// or you can leverage tools/create_account.js.
const ainftJs = new AinftJs(<YOUR_PRIVATE_KEY>);
```js
import AinftJs from '@ainft-team/ainft-js';
```

If you want to connect to the testnet of the Ain blockchain, you can set the Ain blockchain endpoint.
```javascript
const config = {
ainftServerEndpoint: 'https://ainft-api-dev.ainetwork.ai',
ainBlockchainEndpoint: 'https://testnet-api.ainetwork.ai'
}
const ainftJs = new AinftJs(<YOUR_PRIVATE_KEY>, config);
### Configuration

To initialize the SDK with a private key, create an instance of **\`AinftJs\`** as follows. If you don't have an account, create one through the AIN wallet or by using script at **\`examples/wallet/createAccount.js\`**;

```js
const ainft = new AinftJs({
privateKey: '<YOUR_PRIVATE_KEY>',
});
```

Alternatively, you can use the AIN wallet for authentication and transaction signing in the Chrome browser.

```js
const ainft = new AinftJs({
signer: new AinWalletSigner(),
});
```

### Connecting to the Testnet

To connect to the AIN blockchain testnet, configure the SDK with the testnet endpoints:

```js
const ainft = new AinftJs({
privateKey: '<YOUR_PRIVATE_KEY>',
baseUrl: 'https://ainft-api-dev.ainetwork.ai',
blockchainUrl: 'https://testnet-api.ainetwork.ai',
chainId: 0,
});
```

## Features

### AINFT

You can create AINFT object and mint AINFT though AINFT object. Below modules support it.

- `nft`: Creates AINFT object and Searches AINFTs and AINFT objects.
- `ainft721Object`: It is AINFT object class. Mints AINFTs and Transfers it to other accounts.
- `ainftToken`: It is AINFT class. Updates metadata.

You can learn how to make AINFT in [tutorials](https://docs.ainetwork.ai/ainfts/developer-reference/ainft-tutorial).

### Tokenomics

Features for activating tokenomics in NFT communities.

- `credit`: Create and manage community-specific credits.
- `event`: Create and manage events where user can take action and receive rewards. This is a function for credit mining.
- `store`: You can create items, register them in the store, and sell them. This is a function for consuming credit.


## NFT API

Introducing the main API functions that can be used in the `nft` module.

- `create(name, symbol)`: Creates AINFT object.
Expand All @@ -67,30 +94,64 @@ Introducing the main API functions that can be used in the `nft` module.
- `searchAinfts(searchParams)`: Search for AINFT. You can use ainft object id, name, symbol, token id, user address for searching.

## AINFT721 Object API

Introducing the main API functions that can be used in the `ainftObject` module.

- `getToken(tokenId)`: Gets AINFT that was minted by AINFT object.
- `transfer(from, to, tokenId)`: Transfers AINFT to other account.
- `mint(to, tokenId)`: Mints AINFT.

## AINFT Token API

Introducing the main API functions that can be used in the `ainftToken` module.

- `setMetadata(metadata)`: Sets metadata of AINFT.

## AI API
We provide AI API functions, including assistant, thread, and message.
To use these functions follow streamlined steps:
1. **Initialization**: Before using any AI functions, initialize the event channel with the `connect()` method.
2. **Using AI functions**: After opening the event channel, you can use AI function.
3. **Closure**: Ensure to close the event channel with the `disconnect()` method when the functions are no longer needed.

```js
import AinftJs from '@ainft-team/ainft-js';

const ainft = new AinftJs({
privateKey: '<YOUR_PRIVATE_KEY>',
baseUrl: 'https://ainft-api-dev.ainetwork.ai',
blockchainUrl: 'https://testnet-api.ainetwork.ai',
chainId: 0,
});

async function main() {
await ainft.connect(); // connect to the blockchain endpoint

// your ai function usage here
// ...

await ainft.disconnect(); // disconnect from the blockchain endpoint
}

main();
```

## AINFT tutorial

You can view the [tutorial document](https://docs.ainetwork.ai/ainfts/developer-reference/ainft-tutorial) at the following link. and You can also look at scripts created for tutorials in the [tutorial directory](https://github.com/ainft-team/ainft-js/tree/main/examples).

Tutorial scripts

- [createAinftObject](https://github.com/ainft-team/ainft-js/blob/master/examples/nft/createAinftObject.js)
- [mintAinft](https://github.com/ainft-team/ainft-js/blob/master/examples/nft/mintNft.js)
- [transferAinft](https://github.com/ainft-team/ainft-js/blob/master/examples/nft/transferNft.js)
- [retrieveAinft](https://github.com/ainft-team/ainft-js/blob/master/examples/nft/retrieve.js)
- [searchAinft](https://github.com/ainft-team/ainft-js/blob/master/examples/nft/search.js)

## API Documentation

API documentation is available at https://ainft-team.github.io/ainft-js.

## License
MIT License

MIT License
40 changes: 40 additions & 0 deletions examples/ai/assistant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// To run this example, you must own an ainft object and token; create one if you don't.
// https://docs.ainetwork.ai/ainfts/developer-reference/ainft-tutorial/create-ainft-object-and-mint

const AinftJs = require('@ainft-team/ainft-js').default;
const { privateKey, objectId, appId, tokenId } = require('../config.json'); // TODO(user): set these in config.json

const ainft = new AinftJs({
privateKey,
baseUrl: 'https://ainft-api-dev.ainetwork.ai',
blockchainUrl: 'https://testnet-api.ainetwork.ai',
chainId: 0,
});

async function main() {
try {
console.log('Creating assistant...\n');

await ainft.connect();

const { assistant, tx_hash } = await ainft.assistant.create(objectId, tokenId, {
model: 'gpt-4', // TODO(user): update this
name: 'QuickSupport', // TODO(user): update this
instructions: 'Answer tech support questions.', // TODO(user): update this
description: 'A chatbot for quick tech-related queries.', // TODO(user): update this
metadata: { topic: 'Tech', language: 'en' }, // TODO(user): update this
});

await ainft.disconnect();

console.log(`\nSuccessfully created assistant with ID: ${assistant.id}`);
console.log(`assistant: ${JSON.stringify(assistant, null, 4)}`);
console.log(`txHash: ${tx_hash}`);
console.log(`\nView more details at: https://testnet-insight.ainetwork.ai/database/values/apps/${appId}/tokens/${tokenId}/ai`);
} catch (error) {
console.error('Failed to create assistant: ', error.message);
process.exit(1);
}
}

main();
19 changes: 6 additions & 13 deletions examples/chat/config.js → examples/ai/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,12 @@
// https://docs.ainetwork.ai/ainfts/developer-reference/ainft-tutorial/create-ainft-object-and-mint

const AinftJs = require('@ainft-team/ainft-js').default;
const config = require('../config.json');
const { privateKey, objectId, appId } = require('../config.json'); // TODO(user): set these in config.json

['privateKey', 'objectId', 'appId'].forEach((key) => {
if (!config[key]?.trim()) {
throw new Error(`${key} is missing or empty in config.json`);
}
});

const { privateKey, objectId, appId } = config; // TODO(user): set these in config.json

const ainft = new AinftJs(privateKey, {
ainftServerEndpoint: 'https://ainft-api-dev.ainetwork.ai',
ainBlockchainEndpoint: 'https://testnet-api.ainetwork.ai',
const ainft = new AinftJs({
privateKey,
baseUrl: 'https://ainft-api-dev.ainetwork.ai',
blockchainUrl: 'https://testnet-api.ainetwork.ai',
chainId: 0,
});

Expand All @@ -25,7 +18,7 @@ async function main() {
const { config, tx_hash } = await ainft.chat.configure(objectId, 'openai');

console.log(`Successfully configured chat for ainft object!`);
console.log(`config: ${JSON.stringify(config, null, 2)}`);
console.log(`config: ${JSON.stringify(config, null, 4)}`);
console.log(`txHash: ${tx_hash}`);
console.log(`View more details at: https://testnet-insight.ainetwork.ai/database/values/apps/${appId}`);
} catch (error) {
Expand Down
22 changes: 10 additions & 12 deletions examples/chat/deposit.js → examples/ai/deposit.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
const AinftJs = require('@ainft-team/ainft-js').default;
const config = require('../config.json');
const { privateKey } = require('../config.json'); // TODO(user): set this in config.json

if (!config.privateKey?.trim()) {
throw new Error('privateKey is missing or empty in config.json');
}

const { privateKey } = config;

const ainft = new AinftJs(privateKey, {
ainftServerEndpoint: 'https://ainft-api-dev.ainetwork.ai',
ainBlockchainEndpoint: 'https://testnet-api.ainetwork.ai',
const ainft = new AinftJs({
privateKey,
baseUrl: 'https://ainft-api-dev.ainetwork.ai',
blockchainUrl: 'https://testnet-api.ainetwork.ai',
chainId: 0,
});

async function main() {
try {
console.log('Depositing credit...\n');

// TODO(user): get testnet AIN from [faucet](https://faucet.ainetwork.ai)
await ainft.connect();

const { tx_hash, address, balance } = await ainft.chat.depositCredit('openai', 10);

await ainft.disconnect();

console.log(`\nSuccessfully deposited credit for chatting!`);
console.log(`address: ${address}`);
console.log(`balance: ${balance}`);
console.log(`txHash: ${tx_hash}`);
console.log(`View more details at: https://testnet-insight.ainetwork.ai/transactions/${tx_hash}`);
console.log(`\nView more details at: https://testnet-insight.ainetwork.ai/transactions/${tx_hash}`);
} catch (error) {
console.error('Failed to deposit credit: ', error.message);
process.exit(1);
Expand Down
38 changes: 38 additions & 0 deletions examples/ai/message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// To run this example you must create a thread (see examples/chat/thread.js)

const AinftJs = require('@ainft-team/ainft-js').default;
const { privateKey, objectId, appId, tokenId } = require('../config.json'); // TODO(user): set these in config.json

const ainft = new AinftJs({
privateKey,
baseUrl: 'https://ainft-api-dev.ainetwork.ai',
blockchainUrl: 'https://testnet-api.ainetwork.ai',
chainId: 0,
});

async function main() {
try {
console.log('Creating message...\n');

await ainft.connect();

const threadId = '<YOUR_THREAD_ID>'; // TODO(user): update this
const { messages, tx_hash } = await ainft.message.create(objectId, tokenId, threadId, {
role: 'user',
content: 'What is git?', // TODO(user): update this
metadata: { language: 'en' }, // TODO(user): update this
});

await ainft.disconnect();

console.log(`\nSuccessfully created new message with reply:`);
console.log(`messages: ${JSON.stringify(messages, null, 4)}`);
console.log(`txHash: ${tx_hash}`);
console.log(`\nView more details at: https://testnet-insight.ainetwork.ai/database/values/apps/${appId}/tokens/${tokenId}/ai/history`);
} catch (error) {
console.error('Failed to create message: ', error.message);
process.exit(1);
}
}

main();
31 changes: 31 additions & 0 deletions examples/ai/thread.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const AinftJs = require('@ainft-team/ainft-js').default;
const { privateKey, objectId, appId, tokenId } = require('../config.json'); // TODO(user): set these in config.json

const ainft = new AinftJs({
privateKey,
baseUrl: 'https://ainft-api-dev.ainetwork.ai',
blockchainUrl: 'https://testnet-api.ainetwork.ai',
chainId: 0,
});

async function main() {
try {
console.log('Creating thread...\n');

await ainft.connect();

const { thread, tx_hash } = await ainft.thread.create(objectId, tokenId, {});

await ainft.disconnect();

console.log(`\nSuccessfully created thread with ID: ${thread.id}`);
console.log(`thread: ${JSON.stringify(thread, null, 2)}`);
console.log(`txHash: ${tx_hash}`);
console.log(`\nView more details at: https://testnet-insight.ainetwork.ai/database/values/apps/${appId}/tokens/${tokenId}/ai/history`);
} catch (error) {
console.error('Failed to create thread: ', error.message);
process.exit(1);
}
}

main();
44 changes: 0 additions & 44 deletions examples/chat/assistant.js

This file was deleted.

Loading

0 comments on commit 9c5db01

Please sign in to comment.