near-textile-indexer
Event indexer for smart contract on NEAR using Textile as storage and query engine.
Problem
Doing intensive computation (sorting, etc) for get request is not possible on blockchain due to it's nature for decentralization.
One solution is to create an indexing layer that are decentralized. Textile provide the tools to create a decentralized database via its ThreadDB to help scale the get method for NEAR smart contract.
Installation
npm install near-textile-indexer
or
yarn add near-textile-indexer
How to use
Unlike Ethereum, NEAR does not have built-in Event. This indexer requires your smart contract to have the Event store.
Smart Contract
You can find the smart contract on code on example here.
You must create a PersistentVector
that holds all the Event
on smart contract with getEvent
and getEventHeight
public methods for querying the event.
PersistentVector
getEvent
getEventHeight
Event type
Event
Indexer
If your smart contract is ready with all the requirement above, you can setup the indexer.
The indexer will generate setup.json
that contains threadID
and eventHeight
.
const indexer = const config = ...
Config
const config = // NEAR network ID networkId: string // NEAR node url nodeUrl: string // NEAR contract name contractName: string // key generated via https://docs.textile.io/hub/apis/ keyInfo: key: processenvTEXTILE_API_KEY // admin private key generated via https://textileio.github.io/js-hub/docs/hub.privatekey privateKey: processenvADMIN_PRIVATE_KEY // see https://textileio.github.io/js-hub/docs/hub.collectionconfig collections: CollectionConfig // callback called when new event found async { // see example for more information }
Query
You can easily query the data using @textile/hub
on client or server side.
Here's the example code
const Client ThreadID PrivateKey = const keyInfo = key: 'xxx'const threadID = 'yyy' const client = await Clientconst identity = PrivateKeyawait clientconst tID = ThreadIDconst x = await client
License
MIT License