Skip to content

NEAR indexer backed by ThreadDB distributed database from Textile

Notifications You must be signed in to change notification settings

hdriqi/near-textile-indexer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

Developers at NEAR create their own indexer (Flux Protocol, Mintbase, Paras). They need to setup a centralized database for the indexing layer.

What if we also have a decentralized database for the indexing layer? This is where Textile's ThreadDB shine. A decentralized indexing layer.

Ethereum developers has Graph Protocol and they expect NEAR to have something similar. This repo is the proof of concept for NEAR indexing layer using decentralized database by Textile.

TL;DR

Read the implementation via example.

or

Watch the demo & tutorial

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

const events = new PersistentVector<Event>('events')

getEvent

export function getEvent(index: i32): Event {
	return events[index]
}

getEventHeight

export function getEventHeight(): i32 {
	return events.length
}

Event type

Event {
  collection: string
  action: string
  params: string[]
}

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 = require('near-textile-indexer')

const config = {
  ...
}

indexer(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: process.env.TEXTILE_API_KEY
  },
  // admin private key generated via https://textileio.github.io/js-hub/docs/hub.privatekey
  privateKey: process.env.ADMIN_PRIVATE_KEY,
  // see https://textileio.github.io/js-hub/docs/hub.collectionconfig
	collections: CollectionConfig[],
	// callback called when new event found
	async processEvent(ctx, event, textileClient) {
		// 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 } = require('@textile/hub')

const keyInfo = {
	key: 'xxx',
}
const threadID = 'yyy'

const client = await Client.withKeyInfo(keyInfo)
const identity = PrivateKey.fromRandom()
await client.getToken(identity)
const tID = ThreadID.fromString(threadID)
const x = await client.find(tID, 'person', {})

License

MIT License

About

NEAR indexer backed by ThreadDB distributed database from Textile

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published