npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@thorchain/asgardex-midgard

v1.1.0

Published

ASGARDEX Midgard is a Midgard verification module needed by ASGARDEX clients.

Downloads

33

Readme

ASGARDEX-MIDGARD

The ASGARDEX Midgard package is a Midgard verification module used by ASGARDEX clients. It provides an easy way to get a valid, but random base url of Midgards API by proofing active THORNodes.

It will do all the hard work behind the scenes by challenging active THORNodes. If Midgard cannot get consensus on data, it will blacklist bad nodes and try again. This is because of THORChain's trust model - it does not trust any Node, instead it verifies them by comparing against each other. It assumes that no more than 1/3rd of THORNodes are malicious and colluding.

Midgard gets the list of active nodes by querying the seed service: testnet-seed.thorchain.info (testnet), chaosnet-seed.thorchain.info (chaosnet) or seed.thorchain.info (mainnet), which crawls THORChain for active node IPs.

Note: The Seed Service is now the weak link and must be semi-trusted (however it can be independently verified by auditing the nodes manually). In future the Seed Service will move to an Ethereum Smart Contract and Midgard will use Web3 to retrieve seed nodes IPs. This will allow the service to be fully trustless.

Installation

  • Install @thorchain/asgardex-midgard from npm
yarn add @thorchain/asgardex-midgard

Usage

Basic usage

Whenever your application needs to ask Midgard API for data, call midgard() before. It will return a valid, but random base url for Midgard API.

Please note: That's the only way to send a transaction to a proofed THORNode at any time.

import midgard from '@thorchain/asgardex-midgard'

// baseUrl on testnet
const baseUrl = await midgard()
// or
const baseUrl = await midgard('testnet')
//
// baseUrl on chaosnet
const baseUrl = await midgard('chaosnet')
//
// baseUrl on mainnet
const baseUrl = await midgard('mainnet')

// fetch data from an endpoint, for example to get data of `/v1/pools`
const data = await fetch(`${baseUrl}/v1/pools`)

Side note: Behind the scenes Midgard is memorizing a proofed list of baseUrls to avoid increasing requests. Midgard will recycle its cache every hour. If you want to get a "fresh", not cached baseUrl, set the second parameter to true.

// Testnet example:
const baseUrl = await midgard('testnet', true)
// Chaosnet example:
const baseUrl = await midgard('chaosnet', true)
// Mainnet example:
const baseUrl = await midgard('mainnet', true)

Examples

Testnet

ts-node examples/testnet.ts
# output
testnet: http://136.243.227.164:8080

Chaosnet

ts-node examples/chaosnet.ts
# output
chaosnet: http://54.255.107.211:8080

Mainnet

ts-node examples/mainnet.ts
# output an error (as long as mainnet is not supported)
Error: Empty IP list: [object Object]

That might be helpful for error handling, see next chapter "Error handling".

Error handling

In same cases a cached baseUrl can be invalid if a node goes offline for any reason. In this case you can force Midgard to return a non-cached baseUrl to retry this request, but using another baseUrl.

// Get cached `baseUrl` as usual
// Testnet example:
const baseUrl = await midgard('testnet')

// Catch errors to retry another request using a "fresh" proofed `baseUrl`
try {
  const data = await fetch(`${baseUrl}/v1/pools`)
  ...
} catch (error) {
  retry()
}

const retry = () => {
  // Get another, "fresh" proofed `baseUrl`
  const anotherBaseUrl = await midgard('testnet', true)
  const data = await fetch(`${baseUrl}/v1/pools`)
}

Please note: Use a non-cached baseUrl as few as possible to avoid to increase requests made by Midgard.

Development

Build

yarn build

Tests

yarn test