Go to file
Hernán Di Pietro 20a92e53c9 vaa-processor pyteal wip 2021-10-22 17:10:29 -03:00
backend TSC, Settings, some refactoring. 2021-10-12 10:37:11 -03:00
lib Store exp as 2-comp signed (Pyth emits NEGATIVE exponent!) 2021-10-08 15:45:46 -03:00
settings TSC, Settings, some refactoring. 2021-10-12 10:37:11 -03:00
teal vaa-processor pyteal wip 2021-10-22 17:10:29 -03:00
test Service booting and feeding :happy: 2021-10-08 16:03:37 -03:00
tools Added Deployment tool. 2021-10-07 15:47:14 -03:00
.eslintrc.js Missing files. 2021-10-01 11:01:43 -03:00
.gitignore TSC, Settings, some refactoring. 2021-10-12 10:37:11 -03:00
LICENSE Initial commit 2021-09-30 15:34:21 -03:00
README.md Updated README with global state and input message 2021-10-13 12:50:12 -03:00
package-lock.json TSC, Settings, some refactoring. 2021-10-12 10:37:11 -03:00
package.json README and alternate coins 2021-10-12 10:59:59 -03:00
tsconfig.json TSC, Settings, some refactoring. 2021-10-12 10:37:11 -03:00

README.md

Pricecaster Service

This service consumes prices from "price fetchers" and feeds blockchain publishers. In case of Algorand publisher class, a TEAL program with messages containing signed price data. The program code validates signature and message validity, and if successful, subsequently stores the price information in the global application information for other contracts to retrieve.

All gathered price information is stored in a buffer by the Fetcher component -with a maximum size determined by settings-. The price to get from that buffer is selected by the IStrategy class implementation; the default implementation being to get the most recent price and clear the buffer for new items to arrive.

Alternative strategies for different purposes, such as getting averages and forecasting, can be implemented easily.

System Overview

The Pricecaster backend can be configured with any class implementing IPriceFetcher and IPublisher interfaces. The following diagram shows the service operating with a fetcher from Pyth Network, feeding the Algorand chain through the StdAlgoPublisher class.

PRICECASTER

Data Format

Input Message

The TEAL contract expects a fixed-length message consisting of:

  Field size
  9           header      Literal "PRICEDATA"
  1           version     int8 (Must be 1)
  8           dest        This appId 
  16          symbol      String padded with spaces e.g ("ALGO/USD        ")
  8           price       Price. 64bit integer.
  8           priceexp    Price exponent. Interpret as two-compliment, Big-Endian 64bit
  8           conf        Confidence (stdev). 64bit integer. 
  8           slot        Valid-slot of this aggregate price.
  8           ts          timestamp of this price submitted by PriceFetcher service
  32          s           Signature s-component
  32          r           Signature r-component 

  Size: 138 bytes. 

Global state

The global state that is mantained by the contract consists of the following fields:

sym      : byte[] Symbol to keep price for   
vaddr    : byte[] Validator account          
price    : uint64 current price 
stdev    : uint64 current confidence (standard deviation)
slot     : uint64 slot of this onchain publication
exp      : byte[] exponent. Interpret as two-compliment, Big-Endian 64bit
ts       : uint64 last timestamp

Price parsing

The exponent is stored as a byte array containing a signed, two-complement 64-bit Big-Endian integer, as some networks like Pyth publish negative values here. For example, to parse the byte array from JS:

    const stExp = await tools.readAppGlobalStateByKey(algodClient, appId, VALIDATOR_ADDR, 'exp')
    const bufExp = Buffer.from(stExp, 'base64')
    const val = bufExp.readBigInt64BE()

Backend Configuration

The backend will read configuration from a settings.ts file pointed by the PRICECASTER_SETTINGS environment variable.

Tests

At this time, there is a TEAL contract test that can be run with

npm run test

Backend tests will come shortly.