1.1.7 • Published 5 years ago

es-ss.js v1.1.7

Weekly downloads
2
License
ISC
Repository
github
Last release
5 years ago

Blockchain data services protocol library

If you would like to use this product outside of a server environment i.e. just use HTML/JS on the client side, please head over to the traditional non node js area. Otherwise, continue below using NodeJS.

Node install

To use this in a Node.js environment please install using npm, as shown below.

npm install es-ss.js

Import

Once installed, please require it inside your software's appropriate application (js) file, as shown below.

let esss = require("es-ss.js"); 
let ESSS = esss.ESSS;

Set provider

You can then create an instantiation of the es-js software by passing in the provider (passing in any single working smart contract search engine provider URL as part of the instantiation). Here are some examples.

// Ethereum (ETH) MainNet 
let searchEngineProvider = new ESSS('https://eth.search.secondstate.io');

// Ethereum Classic (ETC) MainNet
//let searchEngineProvider = new ESSS('https://etc.search.secondstate.io');

// CyberMiles (CMT) MainNet
//let searchEngineProvider = new ESSS('https://cmt.search.secondstate.io');

// CyberMiles (CMT) TestNet
//let searchEngineProvider = new ESSS('https://testnet.cmt.search.secondstate.io');

// SecondState DevChain
//let searchEngineProvider = new ESSS('https://devchain.ss.search.secondstate.io');

You can now call each of the available functions as shown below in the Usage section.

Usage Examples

The following are all using the EthMainNet provider from above. Please ensure to use the correct provider for your application.

Search using native Elasticsearch DSL

var q = { query: { match_all: {} } }

Or perhaps, once you have a contract address from the above you can practice just returning one record (at contract adddress 0x...)

var q = {"query":{"bool":{"must":[{"match":{"contractAddress":"0x6A4eB89b9d0519F6e344D36a70b4450193bd9C78"}}]}}}

Call the function

searchEngineProvider.queryUsingDsl(q).then((theResult) => {
    console.log(theResult);
})

Query TRANSACTIONS using native Elasticsearch syntax

Fetch transactions which were sent to a particular address

var q = {"query":{"bool":{"must":[{"match":{"to":"0xA722A50b3B939aBec992753607B648277f781228"}}]}}}

Fetch transactions which were sent from a particular address

var q = {"query":{"bool":{"must":[{"match":{"from":"0xA722A50b3B939aBec992753607B648277f781228"}}]}}}

You can also query using numerical range. Here we look at all transactions send to an address over the last 365 days.

For example, use the following query to get all transactions sent to address 0xA722A50b3B939aBec992753607B648277f781228 between Fri, 13 Sep 2018 07:51:32 GMT and Fri, 13 Sep 2019 07:51:32 GMT

var now = Math.floor(Date.now() / 1000)
// 1568361092
var lastYear =  Math.floor((Date.now() - (365*24*60*60*1000)) / 1000)
// 1536825092

// You could also use yesterday
// var yesterday =  Math.floor((Date.now() - (1*24*60*60*1000)) / 1000)
var q = {
  "query": {
    "bool": {
      "must": [{
          "match": {
            "to": "0xA722A50b3B939aBec992753607B648277f781228"
          }
        },
        {
          "range": {
            "timestamp": {
              "gte": lastYear,
              "lt": now
            }
          }
        }
      ]
    }
  }
}

Call the function

searchEngineProvider.queryTxUsingDsl(q).then((theResult) => {
    console.log(theResult);
})

The code above will return data like this

[
  {
    "_source": {
      "TxHash": "0x4f05204efaf701a510bc97c60be65ffad21ea2d2ea813bbd75e9ff384fe64e53", 
      "blockNumber": 7716238, 
      "from": "0xDf7D7e053933b5cC24372f878c90E62dADAD5d42", 
      "gasUsed": 21000, 
      "timestamp": 1553357074, 
      "to": "0xA722A50b3B939aBec992753607B648277f781228", 
      "valueEth": 0.10176, 
      "valueWei": "101760456245944314"
    }
  }, 
  {
    "_source": {
      "TxHash": "0x4828f9af2a975b1bb5a87cd48d089ae46c60931a07270d80fcc65523d966d057", 
      "blockNumber": 7541072, 
      "from": "0xDf7D7e053933b5cC24372f878c90E62dADAD5d42", 
      "gasUsed": 21000, 
      "timestamp": 1550878929, 
      "to": "0xA722A50b3B939aBec992753607B648277f781228", 
      "valueEth": 0.109171, 
      "valueWei": "109171346049151794"
    }
  }
]

Once the data is returned, the Ethereum timestamp can be easily converted to human readable data (for browser display) like this.

transactionsDate = new Date(1550878929 * 1000).toGMTString()

The above epoch to date translation will output a value like this

Sat Feb 23 2019 09:42:09 GMT+1000 (Australian Eastern Standard Time)

Express harvest an ABI

var abiHash = '0xcc7a8e503d0020095a55ea78198edf358200c39452b6dbbd9a5d26f425cde6bf'
var blockFloor = '4855734'

Call the function

searchEngineProvider.expressHarvestAnAbi(abiHash, blockFloor)
.then(function(result) {
    console.log(result);
  })
  .catch(function() {
    console.log("Error");
  });

The above code will return a response object like this

{ "response": "true" }

or this

{ "response": "false" }

You can use this response object to change how the DApp frontend responds to the end user. For example.

searchEngineProvider.expressHarvestAnAbi(abiHash, blockFloor).then((theStatus) => {
    console.log(JSON.parse(theStatus).response);
    if (JSON.parse(theStatus).response == "true") {
        // Do something 
    } else {
        console.log(JSON.parse(theStatus).response);
        // Do something else
    }
});

Update the indexed state of a contract at a particular address

To update the indexed state of a contract in relation to all of its associated ABIs please set the abi var to the string keyword of 'all', as shown below.

var abi = 'all'
var contractAddress = '0x1234...56789

If you would only like to update the indexed state of a contract in relation to a specific ABI the you can add one ABI explicitly as a string, as shown below.

var abi = '[{"valid": "abi", "goes": "here"}]'
var contractAddress = '0x1234...56789

Call the function

var indexingResult = searchEngineProvider.updateStateOfContractAddress(abi, contractAddress);
indexingResult.then(function(result) {
    console.log("Result is " + result);
  })
  .catch(function() {
    console.log("Error");
  });

The above code will return a response object like this

{ "response": "true" }

or this

{ "response": "false" }

You can use this response object to change how the DApp frontend responds to the end user. For example.

searchEngineProvider.updateStateOfContractAddress(JSON.stringify(abi), instance.address).then((theStatus) => {
    console.log(JSON.parse(theStatus).response);
    if (JSON.parse(theStatus).response == "true") {
        // Do something 
    } else {
        console.log(JSON.parse(theStatus).response);
        // Do something else
    }
});

Get the block interval, as per the smart contract search engine config

var number = searchEngineProvider.getBlockInterval();
number.then(function(result) {
    console.log("Most recent block number is " + result);
  })
  .catch(function() {
    console.log("Error");
});

Call the function using different syntax

searchEngineProvider.getBlockInterval().then((theResult) => {
    console.log(theResult);
})

Returns single int

1

Get the most recent indexed block number

var number = searchEngineProvider.getMostRecentIndexedBlockNumber();
number.then(function(result) {
    console.log("Most recent block number is " + result);
  })
  .catch(function() {
    console.log("Error");
});

Check to see if a contract has been deployed using the respective transaction hash

Create the variable to be passed in to the confirmDeployment function

var txHash = 'hash of transaction which is perhaps not an already deployed contract' //0x1234

Call the function

var description = searchEngineProvider.confirmDeployment(txHash);
description.then(function(result) {
    console.log("Result is " + result);
  })
  .catch(function() {
    console.log("Error");
  });

Call the function using different syntax

searchEngineProvider.confirmDeployment(txHash).then((theResult) => {
    var r = JSON.parse(theResult);
    console.log(r);
})

Returns data like this

{ response: '0x69962D233c454b3f958fA5bc08f61FD252A01E9a' }

Describe an item using its transaction hash

Create variables to be passed into the describeUsingTx function.

var txHash = 'hash of transaction which deployed contract' //0x1234

Call the function

var description = searchEngineProvider.describeUsingTx(txHash);
description.then(function(result) {
    console.log("Result is " + result);
  })
  .catch(function() {
    console.log("Error");
  });

Submit ABI and hash for indexing

Create variables to be passed into the submitAbi function.

var abi = '[valid abi goes here]'
var txHash = 'hash of transaction which deployed contract' //0x1234

Call the function

var abiSubmission = searchEngineProvider.submitAbi(abi, txHash);
abiSubmission.then(function(result) {
    console.log("Result is " + result);
  })
  .catch(function() {
    console.log("Error");
  });

Submit many ABIs (in conjuntion with a single Tx hash) for indexing

Create variables to be passed into the submitManyAbis function.

var abis = {'abis': {0: {'abi': [valid abi string goes here]}, 1: {'abi': [valid abi string goes here]}}}
var txHash = 'hash of transaction which deployed contract' //0x1234

Call the function

var abiSubmission = searchEngineProvider.submitManyAbis(abis, txHash);
abiSubmission.then(function(result) {
    console.log("Result is " + result);
  })
  .catch(function() {
    console.log("Error");
  });

Sha an ABI

This produces a canonical deterministic Sha3 hash of an ABI. The hash (which is returned from this function) can be used to filter searches as well as customise frontend displays. The next item in this documentation searchUsingAbi is a good example of searching for items which adhere to a specific ABI. As you will see, we are about to use this newly created abiSha3 for search.

var abi = '[valid abi goes here]'

Call the function

var abiSha = searchEngineProvider.shaAbi(abi);
abiSha.then(function(result) {
    console.log("Result is " + result);
  })
  .catch(function() {
    console.log("Error");
  });

Returns hash like this

0x4722ca26325a45bfad1538b8a73d341548cfa007765f81071e3b0f966adcedff

Search using an Address

This returns an indexed item based on its contract address

Call the function by passing in the address of the smart contract

address = '0xfA390F18C916EF2aC3C060920e7DD509baf94EEa';
var addressSearch = searchEngineProvider.searchUsingAddress(address);
addressSearch.then(function(result) {
    console.log("Result is " + result);
  })
  .catch(function() {
    console.log("Error");
  });

Search using ABI

This returns any and all items in the index which have the canonical deterministic Sha3 hash which we calculated in the previous step.

Call the function by passing in the Sha3 of the ABI.

abiHash = '0x4722ca26325a45bfad1538b8a73d341548cfa007765f81071e3b0f966adcedff';
var abiSearch = searchEngineProvider.searchUsingAbi(abiHash);
abiSearch.then(function(result) {
    console.log("Result is " + result);
  })
  .catch(function() {
    console.log("Error");
  });

Returns (many of) the following data structures in a list format i.e. [{"TxHash":...}, {{"TxHash":...}}, {{"TxHash":...}}]

You can iterate through the results or access each list index explicitly as follows. result[0].functionData.name //CyberMiles Token

 {
    "TxHash": "0x4e950082ac6360c6f8152331a30cbad0c7d08525c4c3914d5236d6fc15f684e8", 
    "abiShaList": [
      "0xeebaaf546eec4bda1a771e7246e58ce83290ce0d24723c3ed62e21506994fc64", 
      "0x2b5710e2cf7eb7c9bd50bfac8e89070bdfed6eb58f0c26915f034595e5443286", 
      "0x7f63f9caca226af6ac1e87fee18b638da04cfbb980f202e8f17855a6d4617a69"
    ], 
    "blockNumber": 4654938, 
    "contractAddress": "0xf85fEea2FdD81d51177F6b8F35F0e6734Ce45F5F", 
    "creator": "0xe5a5d9775ef541b9640bf252d1e725a9b6ccd135", 
    "functionData": {
      "INITIAL_SUPPLY": "1000000000000000000000000000", 
      "decimals": "18", 
      "name": "CyberMiles Token", 
      "owner": "0xE5a5d9775EF541B9640bF252D1E725a9B6CCD135", 
      "paused": "True", 
      "symbol": "CMT", 
      "totalSupply": "1000000000000000000000000000"
    }, 
    "functionDataId": "0xeed7572566a5dc329cc25d8d279987897d8bfe92ddfbfc1840e50851798a12ef", 
    "indexInProgress": "false", 
    "quality": "50", 
    "requiresUpdating": "yes", 
    "uniqueAbiAndAddressHash": "0xeb1e434155dba8a43848ac724b5567bcc6a07b756377e842438229d199ab5e77"
}

Search using Keywords

This returns any and all items in the index which contain one or more instances of any of the words in the list.

Prepare the list as JSON

data = {}
data["keywords"] = ["cmt", "CyberMiles", "token"]

Call the function by passing in the JSON

var keywordSearch = searchEngineProvider.searchUsingKeywords(data);
keywordSearch.then(function(result) {
    console.log("Result is " + result);
  })
  .catch(function() {
    console.log("Error");
  });

Returns data in the same format as shown above.

Search using Keywords and ABI

This returns any and all items in the index which contain the ABI hash or any of the words in the list of keywords.

Prepare the list as JSON

data = {}
data["keywords"] = ["cmt", "CyberMiles", "token"]

Prepare the abiHash as a string

abiHash = '0x4722ca26325a45bfad1538b8a73d341548cfa007765f81071e3b0f966adcedff';

Call the function by passing in the Sha3 of the ABI and the list of keywords.

var keywordAbiSearch = searchEngineProvider.searchUsingKeywordsAndAbi(abiHash, data);
keywordAbiSearch.then(function(result) {
    console.log("Result is " + result);
  })
  .catch(function() {
    console.log("Error");
  });

Returns data in the same format as shown above.

Get ABI count

This returns the number of indexed ABIs

var abiCount = searchEngineProvider.getAbiCount();
abiCount.then(function(result) {
    console.log("Result is " + result);
  })
  .catch(function() {
    console.log("Error");
  });

Returns a single integer

105

Get all count

This returns the number of contracts which are known (regardless of whether the smart contract search engine has an ABI which is associated with that contract)

var allCount = searchEngineProvider.getAllCount();
allCount.then(function(result) {
    console.log("Result is " + result);
  })
  .catch(function() {
    console.log("Error");
  });

Returns a single integer

953457

Get contract count

This returns the number of contracts which have at least one ABI associated with them

var contractCount = searchEngineProvider.getContractCount();
contractCount.then(function(result) {
    console.log("Result is " + result);
  })
  .catch(function() {
    console.log("Error");
  });

Returns a single integer

24501

Update the quality score field

var contractAddress = '0x12345...678'
var qualityScore = '50' //Must be a number between 0 and 100

Call the function

var qualityScoreResult = searchEngineProvider.updateQualityScore(contractAddress, qualityScore);
qualityScoreResult.then(function(result) {
    console.log("Result is " + result);
  })
  .catch(function() {
    console.log("Error");
  });
1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 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.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

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

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago