Git Product home page Git Product logo

solana-flux-aggregator's Introduction

solana-flux-aggregator

Solnana Flux Aggregator

Install Dependencies

yarn install

Setup

If you want to setup the whole thing, read: SETUP.md

Submit Prices As Oracle (devnet)

There are price oracles already deployed on the devnet, you can see their deploy configuration and addresses at ./deploy.sandbox.json

Configure .env to use the devnet:

cp .env.sandbox .env

The .env will set the price oracle to be the account:

FosLwbttPgkEDv36VJLU3wwXcBSSoUGkh7dyZPsXNtT4

Then run the oracle:

yarn solink oracle

The oracle should submit updates if the price changes by more than $1. You should see:

info:     Starting a new round {"aggregator":"btc:usd","round":"9"}
info:     Submit value {"aggregator":"btc:usd","round":"9","value":"5748914"}
info:     Submit OK {"aggregator":"btc:usd","withdrawable":"90000","rewardToken":"3oLHHTaRqNsuTMjsTtkVy8bock6Bx8gCmDxku4TurVj1"}
info:     Starting a new round {"aggregator":"btc:usd","round":"10"}
info:     Submit value {"aggregator":"btc:usd","round":"10","value":"5749313"}
info:     Submit OK {"aggregator":"btc:usd","withdrawable":"100000","rewardToken":"3oLHHTaRqNsuTMjsTtkVy8bock6Bx8gCmDxku4TurVj1"}

NOTE: This is a "sandbox" environment on the devnet to make it easy for you to try running the price oracle. Anyone reading this README has access to the private key of the account FosLwbttPgkEDv36VJLU3wwXcBSSoUGkh7dyZPsXNtT4. Do not use this key for production!

NOTE 2: You might get error messages if somebody else is also running the oracle.

Observe The Aggregators

With the oracle running, you can subscribe to price changes. In another terminal, run:

yarn solink observe

You should get prices pushed to you when they update:

info:     update {"description":"btc:usd","decimals":2,"roundID":"21","median":"5744000","updatedAt":"37820525","createdAt":"37820525"}
info:     update {"description":"eth:usd","decimals":2,"roundID":"9","median":"202600","updatedAt":"37820513","createdAt":"37820513"}
info:     update {"description":"btc:usd","decimals":2,"roundID":"22","median":"5743803","updatedAt":"37820552","createdAt":"37820552"}
info:     update {"description":"btc:usd","decimals":2,"roundID":"23","median":"5740350","updatedAt":"37820565","createdAt":"37820565"}

Request New Round

Request a new round for a specific aggregator independently of the current oracle's rounds and price changes.

yarn solink request-round <aggregator-id>

For example to request a new round for btc:usd aggregator we will use:

yarn solink request-round btc:usd

Using Chainlink Node

By default the yarn solink oracle will submit price changes directly.

You can use a chainlink node to delegate the tasks to submit price on the blockchain by running:

yarn solink chainlink-external

For more details on how to set up a chainlink node, you can find a guide in here.

Devnet Oracles

The sandbox environment could be modified by anyone, and is not suitable for development purposes. For a reliable devnet price feed, use ./deploy.dev.json.

Observe the devnet prices by running:

NETWORK=dev DEPLOY_FILE=deploy.dev.json yarn solink observe
  • btc:usd => HBVsLHp8mWGMGfrh1Gf5E8RAxww71mXBgoZa6Zvsk5cK
  • eth:usd => CifT9HWU1W3VyzdWqaKGcarBLdZGKbbP9h65c7vwxF5d

Using Your Own RPC

By default the oracle uses the public RPC host. If you run many price feeds, you may hit the rate limit of the public RPC.

For better stability, you should run use your own private RPC instead.

Configure in .env:

# (Optional) Specify the RPC host you want to use for your network
SOLANA_RPC_HOST=http://localhost:8899

Joining Devnet

If you want to become a testnet oracle, generate a solana devnet wallet (10 SOL will be airdropped):

NETWORK=dev yarn solink new-wallet

info:     address: 8CGZz277PT6yA7nU6HEdpbwQsNYLvyJhP1guoUNxt9mF
info:     mnemonic: ....
info:     airdrop 10 SOL {"address":"8CGZz277PT6yA7nU6HEdpbwQsNYLvyJhP1guoUNxt9mF"}

Save the mnemonic in your .env file, and give the address to me. I'll add you to the oracle list so you can submit.

Using file based price feed

#first, backup you `.env`
cp .env.example .env

# the command find configs from '.env', then 'DEPLOY_FILE' and 'SOLINK_CONFIG' inside '.env'
ts-node src/cli.ts oracle

# then, you can modify price from `config/btc_usd.json` (or other pair)

solana-flux-aggregator's People

Contributors

bartosz-lipinski avatar czl1378 avatar defactojob avatar pokassun avatar sssrot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

solana-flux-aggregator's Issues

Unable to deploy program

Following the readme verbatim, I run into this error:

On the command: yarn solink deploy-program

patrick@iMac: [~/code/solana-chainlink/solana-flux-aggregator -  (master)] $ yarn solink deploy-program
yarn run v1.22.10
$ ts-node src/cli.ts deploy-program
deploying /Users/patrick/code/solana-chainlink/solana-flux-aggregator/build/flux_aggregator.so...
(node:53116) UnhandledPromiseRejectionWarning: Error: Transaction was not confirmed in 10.01 seconds
    at Connection._callee20$ (/Users/patrick/code/solana-chainlink/solana-flux-aggregator/node_modules/solray/node_modules/@solana/web3.js/src/connection.js:2227:13)
    at tryCatch (/Users/patrick/code/solana-chainlink/solana-flux-aggregator/node_modules/regenerator-runtime/runtime.js:63:40)
    at Generator.invoke [as _invoke] (/Users/patrick/code/solana-chainlink/solana-flux-aggregator/node_modules/regenerator-runtime/runtime.js:293:22)
    at Generator.next (/Users/patrick/code/solana-chainlink/solana-flux-aggregator/node_modules/regenerator-runtime/runtime.js:118:21)
    at asyncGeneratorStep (/Users/patrick/code/solana-chainlink/solana-flux-aggregator/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
    at _next (/Users/patrick/code/solana-chainlink/solana-flux-aggregator/node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
(node:53116) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:53116) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
✨  Done in 13.94s.

OS: MacOS BigSur
Node: 12.20.0, 14.15.2
npm: 6.14.9
rustup: 1.23.1
solana-cli: 1.5.2

add-aggregator not working

I'm having issues deploying a new aggregator using the instructions in the README.

dd@dd-p53:~/solana-flux-aggregator$ yarn solink add-aggregator --feedName btc:usd --submitInterval 6 --minSubmissionValue 0 --maxSubmissionValue 18446744073709551615
yarn run v1.22.5
$ ts-node src/cli.ts add-aggregator --feedName btc:usd --submitInterval 6 --minSubmissionValue 0 --maxSubmissionValue 18446744073709551615
Transaction simulation failed: Error processing Instruction 1: invalid instruction data 
    Program 11111111111111111111111111111111 invoke [1]
    Program 11111111111111111111111111111111 success
    Program F3W3N9iZy3ZLZkYFUFRmGPVneBrHoE6gveVyjPPqn825 invoke [1]
    Program log: Error: InvalidInstructionData
    Program F3W3N9iZy3ZLZkYFUFRmGPVneBrHoE6gveVyjPPqn825 consumed 1839 of 200000 compute units
    Program F3W3N9iZy3ZLZkYFUFRmGPVneBrHoE6gveVyjPPqn825 failed: invalid instruction data
(node:128389) UnhandledPromiseRejectionWarning: Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 1: invalid instruction data
    at Connection._callee53$ (/home/dd/solana-flux-aggregator/node_modules/solray/node_modules/@solana/web3.js/src/connection.js:2957:13)
    at tryCatch (/home/dd/solana-flux-aggregator/node_modules/regenerator-runtime/runtime.js:63:40)
    at Generator.invoke [as _invoke] (/home/dd/solana-flux-aggregator/node_modules/regenerator-runtime/runtime.js:293:22)
    at Generator.next (/home/dd/solana-flux-aggregator/node_modules/regenerator-runtime/runtime.js:118:21)
    at asyncGeneratorStep (/home/dd/solana-flux-aggregator/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
    at _next (/home/dd/solana-flux-aggregator/node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:128389) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:128389) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

can't build project locally due to missing type decls

I get this error, after running yarn install in a clean checkout:

$ yarn solink generate-wallet

yarn run v1.22.10
$ ts-node src/cli.ts generate-wallet

/Users/max/Code/solana/solana-flux-aggregator/node_modules/ts-node/src/index.ts:513
    return new TSError(diagnosticText, diagnosticCodes)
           ^
TSError: ⨯ Unable to compile TypeScript:
src/cli.ts:9:8 - error TS2307: Cannot find module 'solray' or its corresponding type declarations.

9 } from "solray"
         ~~~~~~~~

    at createTSError (/Users/max/Code/solana/solana-flux-aggregator/node_modules/ts-node/src/index.ts:513:12)
    at reportTSError (/Users/max/Code/solana/solana-flux-aggregator/node_modules/ts-node/src/index.ts:517:19)
    at getOutput (/Users/max/Code/solana/solana-flux-aggregator/node_modules/ts-node/src/index.ts:752:36)
    at Object.compile (/Users/max/Code/solana/solana-flux-aggregator/node_modules/ts-node/src/index.ts:968:32)
    at Module.m._compile (/Users/max/Code/solana/solana-flux-aggregator/node_modules/ts-node/src/index.ts:1056:42)
    at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/Users/max/Code/solana/solana-flux-aggregator/node_modules/ts-node/src/index.ts:1059:12)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

devnet working aggregators and program_id

It would be useful for developers if you guys published a working program id and the pubkey of a couple of price aggregators in devnet (e.g. btc:usd and eth:usd)

gets stuck in "starting a new round" for a long time

gets stuck here even after price moves a lot

info:     Starting a new round {"aggregator":"btc:usd-test","round":"1205"}
info:     Starting a new round {"aggregator":"btc:usd-test","round":"1205"}
info:     Starting a new round {"aggregator":"eth:usd-test","round":"631"}
info:     Starting a new round {"aggregator":"eth:usd-test","round":"631"}
info:     Starting a new round {"aggregator":"btc:usd-test","round":"1205"}
info:     Starting a new round {"aggregator":"eth:usd-test","round":"631"}
info:     Starting a new round {"aggregator":"btc:usd-test","round":"1205"}
info:     Starting a new round {"aggregator":"eth:usd-test","round":"631"}
info:     Starting a new round {"aggregator":"btc:usd-test","round":"1205"}
info:     Starting a new round {"aggregator":"btc:usd-test","round":"1205"}
info:     Starting a new round {"aggregator":"btc:usd-test","round":"1205"}
info:     Starting a new round {"aggregator":"btc:usd-test","round":"1205"}
info:     Starting a new round {"aggregator":"eth:usd-test","round":"631"}
info:     Starting a new round {"aggregator":"btc:usd-test","round":"1205"}
info:     Starting a new round {"aggregator":"btc:usd-test","round":"1205"}
info:     Starting a new round {"aggregator":"eth:usd-test","round":"631"}
info:     Starting a new round {"aggregator":"eth:usd-test","round":"631"}
info:     Starting a new round {"aggregator":"btc:usd-test","round":"1205"}
info:     Starting a new round {"aggregator":"eth:usd-test","round":"631"}
info:     Starting a new round {"aggregator":"eth:usd-test","round":"631"}
info:     Starting a new round {"aggregator":"btc:usd-test","round":"1205"}

Unable to add Oracle

Following the README verbatim to deploy an oracle, I run into the following.

On adding an oracle:

yarn solink add-oracle \
  --index 0 \
  --feedAddress 8ox6rTwuVEeKRgkVTr1tdzyjpGNLAt2W6NT63zubcWiM \
  --oracleName solink-test \
  --oracleOwner Ea36cguU9E1p6FBYfikzhMTfG4ekY7WDK8HwUBvV2P3A

I get:

add oracle...
Transaction simulation failed: Error processing Instruction 1: invalid account data for instruction 
    Program 11111111111111111111111111111111 invoke [1]
    Program 11111111111111111111111111111111 success
    Program 3wEiZwdEygZisYbaDCe3bMztMVBbo3rK4X3bHs52Xc5F invoke [1]
    Program log: Instruction: AddOracle
    Program log: Error: InvalidAccountData
    Program 3wEiZwdEygZisYbaDCe3bMztMVBbo3rK4X3bHs52Xc5F consumed 12260 of 200000 compute units
    Program 3wEiZwdEygZisYbaDCe3bMztMVBbo3rK4X3bHs52Xc5F failed: invalid account data for instruction
(node:43280) UnhandledPromiseRejectionWarning: Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 1: invalid account data for instruction
    at Connection._callee53$ (/Users/patrick/code/solana-chainlink/solana-flux-aggregator/node_modules/solray/node_modules/@solana/web3.js/src/connection.js:2957:13)
    at tryCatch (/Users/patrick/code/solana-chainlink/solana-flux-aggregator/node_modules/regenerator-runtime/runtime.js:63:40)
    at Generator.invoke [as _invoke] (/Users/patrick/code/solana-chainlink/solana-flux-aggregator/node_modules/regenerator-runtime/runtime.js:293:22)
    at Generator.next (/Users/patrick/code/solana-chainlink/solana-flux-aggregator/node_modules/regenerator-runtime/runtime.js:118:21)
    at asyncGeneratorStep (/Users/patrick/code/solana-chainlink/solana-flux-aggregator/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
    at _next (/Users/patrick/code/solana-chainlink/solana-flux-aggregator/node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:43280) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:43280) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
✨  Done in 7.99s.

OS: MacOS BigSur
Node: 12.20.0, 14.15.2
npm: 6.14.9
rustup: 1.23.1
solana-cli: 1.5.2

extend flux aggregator with exponential moving average

This is how Uniswap's price oracle works:

They provides exponential price average, which allows users to calculate an average price for any arbitrary elapsed time (1h, 1 day, 1week) to track the exponential moving average, uniswap records price * seconds:

price0CumulativeLast += uint(UQ112x112.encode(_reserve1).uqdiv(_reserve0)) * timeElapsed; 

and to calculate moving average for a period, a smart contract can record the difference between the price cumulatives, and divide by the time period, like this:

price0Average = FixedPoint.uq112x112(
            uint224((price0Cumulative - price0CumulativeLast) / timeElapsed)
        );

Failover on RPC node maintenance

We noticed an issue while running the aggregator, that keeps causing maintenance on our end. Basically the websocket might get locked in a weird state, when the RPC endpoint goes down for maintenance. The error looked like this:

ws error: connect ECONNREFUSED xxx:443
ws error: connect ECONNREFUSED xxx:443
ws error: connect ECONNREFUSED xxx:443

At some point the socket stopped trying to reconnect and I had to manually restart the oracle to make it work again.

Ideally the code would just allow us to specify a preference of rpc nodes and switch to a different one, in case the primary one becomes unavailable. Also it should switch back, when the primary one becomes available and is ready to accept transactions Could you look into that?

Error with answerSubmissions buffer length

Hi, I'm getting an error when observing any oracles Error: Expected buffer length 32 isn't within bounds: answerSubmissions - do you have any suggestions?

OS: Mac OS X
Cargo: 1.51.0
Rustup: 1.23.1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.