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

@generalprotocols/price-oracle

v3.0.1

Published

Library for creating, parsing, signing, verifying and transferring PriceOracle messages.

Downloads

816

Readme

PriceOracle Libraries

Library for creating, parsing, signing, verifying and transferring oracle price messages according to the PriceOracle Specification.

Best Practices For Price Oracle Consumers

Smart contracts can involve amounts of money ranging from tiny test amounts to large fortunes, and lock times from minutes to years. Below are some minimal considerations before you make a contract that depends on a price oracle.

  • As a starting point, never make large or long contracts with a price oracle that is in beta or that you have not done due diligence on.
  • Does the price oracle commit to strictly following the specification, and do you trust the commitment?
  • Does the price oracle commit to operation for the time period of my contract, and do you trust the commitment?
  • Do you trust the price oracle to maintain its security and not expose its signing key?

Price oracle metadata answers most due diligence questions in a technical sense, but you must also consider the larger picture of how much trust you place in a price oracle's commitments.

Installation

Install the library via NPM:

# npm install @generalprotocols/price-oracle

Usage

Setup

Before you can use the Price Oracle functionality in your project, you need to import it to your project:

// Import the Price Oracle library.
import { OracleData, OracleNetwork, OracleProtocol } from '@generalprotocols/price-oracle';

Oracle Data

The OracleData class provides utility functions for managing price messages and makes it easy to create, parse, sign and verify signed prices messages.

// Create an oracle price message from parts.
const message = await OracleData.createPriceMessage(messageTimestamp, messageSequence, priceSequence, priceValue);

// Parse an oracle price message into parts.
const messageParts = await OracleData.parsePriceMessage(message);

// Sign an oracle price message.
const signature = await OracleData.signMessage(message, privateKeyWIF);

// Verify an oracle price message signature.
const validity = await OracleData.verifyMessageSignature(message, signature, publicKey);

Oracle Network

The PriceOracle Specification contains directives for how to distribute price messages in different ways. The OracleNetwork class provides utility functions for interfacing with other oracles.

Note: the oracles can form a network and relay data, but does not have automatic peer discovery, so you will need to manually choose what oracles to connect to.

Broadcast / Push

To broadcast to all connected peers, you first need to set up a broadcast connection after which you can broadcast messages to any currently connected peers.

Note: broadcast default port is TCP 7084.

// Set up the broadcast network connection.
await OracleNetwork.setupBroadcasting();

// Wait for peers to connect

// Distribute a price message to connected peers.
const broadcastResult = await OracleNetwork.broadcastPriceMessage(message, signature, publicKey);

To connect to an oracle to receive broadcasted messages:

// Set up a callback handler for broadcasted messages.
const handleBroadcastedMessages = function(topic, content)
{
	// Check for the types you care about..
	if(topic === OracleProtocol.TOPIC_PING)
	{
		// Do something interesting with the message here.
	}
}

// Listen for broadcasted messages and handle them with the callback handler.
await OracleNetwork.listenToBroadcasts(handleBroadcastedMessages, oracleAddress);

Request - Reply / Pull

To send a generic request:

Note: request-reply default port is TCP 7083.

// Send a generic request from the external oracle.
const response = await OracleNetwork.request('ping!', oracleAddress);

To respond to a request:

// Set up a request handler.
const handleRequest = function(content)
{
	// Send the reply.
	OracleNetwork.reply('pong!');
}

// Listen for requests and handle them with the callback handler.
await OracleNetwork.listenToRequests(handleRequest);

Relay

Relay allows you to send data directly to specific peers by connecting to them individually. It functions similar to broadcasting and is intended to be used to setup fixed private connections.

To relay a message to another peer:

Note: relay default port is TCP 7085.

// Set up a connection with a relay peer.
await OracleNetwork.setupRelay(oracleAddress);

// Forward a message to all connected relay peers.
await OracleNetwork.relay(topic, content);

To receive relayed message from peers:

// Set up a callback handler for relayed messages.
const handleRelayedMessages = function(topic, content)
{
	// Do something interesting with the message here.
}

// Listen for relayed messages and handle them with the callback handler.
await OracleNetwork.listenToRelays(handleRelayedMessages);