Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal Audit #1

Open
wants to merge 197 commits into
base: empty
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
197 commits
Select commit Hold shift + click to select a range
e861edb
First commit containing code for monitoring & middleware for testing
jacquesvcritien Nov 17, 2022
12d7d84
Fixed accept oracle script + added grafana dashboard
jacquesvcritien Nov 17, 2022
3b85427
Added oracle name to monitoring
jacquesvcritien Nov 17, 2022
f5d6c2f
Added more docs regarding oracles.json file
jacquesvcritien Nov 18, 2022
8553f74
Fixed typo
jacquesvcritien Nov 18, 2022
83c5e79
Forced price submission for timer requests
jacquesvcritien Nov 18, 2022
028daf5
Merge branch 'master' of https://github.com/jacquesvcritien/agoric-cl…
jacquesvcritien Nov 18, 2022
5029f9d
Fixed monitoring script
jacquesvcritien Nov 22, 2022
0b530db
Cleaned code and added documentation
jacquesvcritien Dec 5, 2022
8d487c0
Cleaned code & added documentation
jacquesvcritien Dec 5, 2022
22b5d3f
Fixed small issues with new clean code
jacquesvcritien Dec 6, 2022
03fc00f
Added the following:
jacquesvcritien Jan 5, 2023
60fa5df
Updated variable names
jacquesvcritien Jan 5, 2023
9d93831
Fixed more variables
jacquesvcritien Jan 5, 2023
5b7f94d
Added some changes to the monitorinf script which reflect the latest …
jacquesvcritien Jan 5, 2023
5076931
changes to use numWantsSatisfied
jacquesvcritien Jan 12, 2023
7132e34
Added last job submission time to state and changed to check errors
jacquesvcritien Jan 12, 2023
18c55c0
Fixed monitoring & updated dashboard
jacquesvcritien Jan 12, 2023
61b3f74
Added script to provision wallet
jacquesvcritien Jan 16, 2023
981cce9
Optimising middleware and monitoring script
jacquesvcritien Jan 19, 2023
ce1b7e4
Reducing gas consumption
jacquesvcritien Jan 19, 2023
1b71181
Avoiding old round submissions
jacquesvcritien Jan 20, 2023
dcde3d2
Added reply to the CL node without waiting for the price push
jacquesvcritien Jan 20, 2023
b94e758
Making price pushes more efficient
jacquesvcritien Jan 20, 2023
36ee050
Increasing pushing interval to 10m
jacquesvcritien Jan 21, 2023
8daa778
Fixing middleware with issue for startedAt
jacquesvcritien Jan 21, 2023
b4d69e9
Fixing issue with middleware
jacquesvcritien Jan 21, 2023
3edd26e
Removed unwanted files
jacquesvcritien Jan 22, 2023
02c4e5f
Readd monitor scrpi
jacquesvcritien Jan 24, 2023
7f006bf
Change folder name
jacquesvcritien Jan 24, 2023
353b3a9
Update documentation
jacquesvcritien Jan 24, 2023
f90f087
Updated documentation
jacquesvcritien Jan 24, 2023
3a956f9
Removed unwanted line
jacquesvcritien Jan 24, 2023
4e1f7ec
Removed unused function
jacquesvcritien Jan 24, 2023
f4e3f2d
Updated documentation
jacquesvcritien Jan 24, 2023
a1930f0
Documentation update
jacquesvcritien Jan 24, 2023
0c2c891
Updated documentation
jacquesvcritien Jan 25, 2023
5d1c4cc
Cleaning code
jacquesvcritien Jan 25, 2023
5776fb4
Small update to pointer
jacquesvcritien Jan 26, 2023
d4e1c20
Getting rid of exiters
jacquesvcritien Jan 26, 2023
8a3670c
Updated documentation to reflect last commit's changes
jacquesvcritien Jan 26, 2023
2e2d3e0
Removing global env vars for poll, push intervals, decimal places and…
jacquesvcritien Jan 26, 2023
3ca7280
Fixing entry script for monitoring
jacquesvcritien Jan 26, 2023
1983889
Fixed path for feeds file
jacquesvcritien Jan 26, 2023
aa3e56b
Updated documentation
jacquesvcritien Jan 26, 2023
80bc727
Updating accept oracle invitation script
jacquesvcritien Jan 27, 2023
9d1a956
Updating middleware to avoid using offers file
jacquesvcritien Jan 27, 2023
7ba566f
Updated documentation to remove OFFERS_FILE
jacquesvcritien Jan 27, 2023
805c217
Updated toc
jacquesvcritien Jan 27, 2023
f635a39
Increasing poll interval to 3m
jacquesvcritien Jan 28, 2023
d18f7ee
Fixing startup issue
jacquesvcritien Feb 6, 2023
6a7d5a0
Added price update condition
jacquesvcritien Feb 6, 2023
ac87a0f
Trying to fix middlewre offers issue
jacquesvcritien Feb 6, 2023
6f9fc0f
Added change for offers to monitor
jacquesvcritien Feb 6, 2023
627388b
Fixing issue with monitoring script
jacquesvcritien Feb 6, 2023
fa27d53
Fixing issue with monitoring script
jacquesvcritien Feb 6, 2023
9e3c0d6
Fixed some typos
jacquesvcritien Feb 14, 2023
57e20c2
Merge branch 'master' into full_review
Feb 15, 2023
ca6a76d
Added tests, fixed small issue with getOffers, updated docs and added…
jacquesvcritien Feb 16, 2023
c4427c2
Updated modules for tests
jacquesvcritien Feb 16, 2023
91b72a3
Merge pull request #2 from jacquesvcritien/master
jacquesvcritien Feb 16, 2023
47346bd
Moved to sqllite DB from JSON state file + Updated docs, tests.
jacquesvcritien Feb 21, 2023
e594b39
Fixed typos and added line width
jacquesvcritien Feb 21, 2023
195cc08
Added builds to docker-compose file
jacquesvcritien Feb 21, 2023
786f629
Updated read me for docker-compose change
jacquesvcritien Feb 22, 2023
276d72d
Merge pull request #3 from jacquesvcritien/master
jacquesvcritien Feb 22, 2023
e7d6c89
Adding 80 character line limit
jacquesvcritien Feb 22, 2023
707949b
Removed .DS_Store and added it to gitignore
jacquesvcritien Feb 22, 2023
75621d3
Refactored code and split functions into helper functions
jacquesvcritien Feb 22, 2023
a6326b8
Removed useless print
jacquesvcritien Feb 22, 2023
cd76729
Fixed documentation typos and grammatical errors
jacquesvcritien Feb 22, 2023
eedaed1
Changed to docker-compose commands
jacquesvcritien Feb 22, 2023
b0e6e4b
Updated docs for consistency
jacquesvcritien Feb 22, 2023
d618962
Rewording in docs
jacquesvcritien Feb 22, 2023
2220ec8
Moved feeds.json to config folder
jacquesvcritien Feb 22, 2023
dcfe378
Moved feeds file to config folder
jacquesvcritien Feb 22, 2023
978a034
Fixed small typo
jacquesvcritien Feb 22, 2023
22a2462
Moved imports before mocking
jacquesvcritien Feb 22, 2023
2783150
Merge pull request #4 from jacquesvcritien/master
jacquesvcritien Feb 22, 2023
ef14bde
Added all env vars to docker-compose and rename feeds.json to feeds-c…
jacquesvcritien Feb 22, 2023
c6b21b3
Updated description in package.json
jacquesvcritien Feb 22, 2023
0a0d06b
Lowered job request timeout to 5s
jacquesvcritien Feb 22, 2023
aa7bbc4
Changed failover price to -1
jacquesvcritien Feb 22, 2023
d342e64
Removed extra comment
jacquesvcritien Feb 22, 2023
131718b
Removed .vscode from git
jacquesvcritien Feb 22, 2023
670ac02
Fixed for line width
jacquesvcritien Feb 22, 2023
2a7bc45
Formatted tests for 80 line width
jacquesvcritien Feb 22, 2023
d85360c
Documentation updates
jacquesvcritien Feb 22, 2023
ad4d3cb
Changed != to !== and == to ===
jacquesvcritien Feb 22, 2023
5811039
Cleaned util code
jacquesvcritien Feb 22, 2023
d2ad409
Changed all var to let
jacquesvcritien Feb 22, 2023
e6e00fe
Changing lastVisited to -1 to avoid possible issue with id 0
jacquesvcritien Feb 22, 2023
8db3abf
Cleaned code for line width
jacquesvcritien Feb 22, 2023
c97d578
Updated comments to include space and starting letter as capital
jacquesvcritien Feb 22, 2023
4d59ffa
Merge pull request #5 from jacquesvcritien/master
jacquesvcritien Feb 22, 2023
87abd4f
Removed extra code
jacquesvcritien Feb 22, 2023
1ba5446
Added a fix to the monitoring script
jacquesvcritien Feb 23, 2023
f9432cb
Fixed some indentation
jacquesvcritien Feb 23, 2023
b428c13
Fixed docker service for monitor
jacquesvcritien Feb 23, 2023
138c431
Removing counter and using history.length
jacquesvcritien Feb 23, 2023
f060346
Fixed typos in docs
jacquesvcritien Feb 23, 2023
07cae21
Fixed else if indentation
jacquesvcritien Feb 23, 2023
e3ae942
Changed wording in docs
jacquesvcritien Feb 23, 2023
8a77991
Fixed example in docs
jacquesvcritien Feb 23, 2023
e4c3b49
Combined condition
jacquesvcritien Feb 23, 2023
71bb622
Updated comment
jacquesvcritien Feb 23, 2023
e9a33da
Separated function which checks whether a price push update should be…
jacquesvcritien Feb 23, 2023
1881ba6
Fixing issue with new utils function
jacquesvcritien Feb 23, 2023
e16864f
Split condition
jacquesvcritien Feb 23, 2023
6248090
Broke down condition
jacquesvcritien Feb 23, 2023
a18a2ee
Removing extra line setting the id for the offer
jacquesvcritien Feb 23, 2023
a534c54
Removed extra line in monitoring
jacquesvcritien Feb 23, 2023
eaa7011
Removed any ts comments
jacquesvcritien Feb 23, 2023
e9bf66d
Removed ts comments part 2
jacquesvcritien Feb 23, 2023
3772c9e
Changed to foreach
jacquesvcritien Feb 23, 2023
de8807b
Fixed function comment
jacquesvcritien Feb 23, 2023
e91055d
Fixed docs
jacquesvcritien Feb 23, 2023
cf641ec
Fixed comment
jacquesvcritien Feb 23, 2023
4fc5833
Calling getOraclesInvitations() on every interval in monitor
jacquesvcritien Feb 23, 2023
327efae
Removed extra print
jacquesvcritien Feb 23, 2023
9c07061
Fixed some formatting in monitor
jacquesvcritien Feb 23, 2023
488fb2f
Changed to oracleLastEpoch for consistency
jacquesvcritien Feb 23, 2023
07a9328
Fixed test comment to match standard
jacquesvcritien Feb 23, 2023
7b9b88c
Reverted offers foreach
jacquesvcritien Feb 23, 2023
6bd4755
Refactored controller and bridge
jacquesvcritien Feb 23, 2023
9972147
Fixed package name
jacquesvcritien Feb 23, 2023
9d49fe1
Fixed condition for /adapter
jacquesvcritien Feb 23, 2023
2a70a72
Added some more comments
jacquesvcritien Feb 23, 2023
5408f18
Updated spacing
jacquesvcritien Feb 23, 2023
00e2e01
Updated more spacing
jacquesvcritien Feb 23, 2023
3c8d4af
Fixed formatting
jacquesvcritien Feb 23, 2023
9f027c8
Fixed comments indentation
jacquesvcritien Feb 23, 2023
dd01cbd
Added more spacing
jacquesvcritien Feb 23, 2023
1068142
Fixed line limit for comment
jacquesvcritien Feb 23, 2023
bab17ac
Added space in monitor
jacquesvcritien Feb 23, 2023
54132c2
Cleaned line in monitor
jacquesvcritien Feb 23, 2023
66a78fb
Updated docs
jacquesvcritien Feb 23, 2023
4c5dd9c
Removed two unused scripts
jacquesvcritien Feb 23, 2023
707addb
Added link to setup-docs
jacquesvcritien Feb 23, 2023
70bc917
Updated docs
jacquesvcritien Feb 23, 2023
822e961
Fixing comment
jacquesvcritien Feb 23, 2023
42dc406
Added examples for feed and jobName
jacquesvcritien Feb 23, 2023
cd710e1
Added vstorage explanation
jacquesvcritien Feb 23, 2023
653447f
Fixed console print
jacquesvcritien Feb 23, 2023
78b48d4
Creating a class for the middleware env vars
jacquesvcritien Feb 23, 2023
82aea45
Fixing middlewareEnv
jacquesvcritien Feb 23, 2023
47af31f
Fixing env var classes
jacquesvcritien Feb 23, 2023
6e5ed8b
Added block interval to middleware env vars
jacquesvcritien Feb 23, 2023
fdffab7
Added checks for numbers
jacquesvcritien Feb 23, 2023
3efa2ed
Changed != to !== in env var classes
jacquesvcritien Feb 23, 2023
670217e
Fixed comment in monitor
jacquesvcritien Feb 23, 2023
4ac2d76
Removing FEEDS_FILE from an env var
jacquesvcritien Feb 23, 2023
2c22ec9
Wrapped endpoints in try catches
jacquesvcritien Feb 24, 2023
6903cb6
Moved to winston logger
jacquesvcritien Feb 24, 2023
5e00308
Changed log commas to +
jacquesvcritien Feb 24, 2023
e4354ea
Removed extra JSON parsing
jacquesvcritien Feb 24, 2023
2bc26d5
Adding validation for feed invitation
jacquesvcritien Feb 24, 2023
a3baaa8
Added try catches for capdata parsing
jacquesvcritien Feb 24, 2023
2c23f76
Wrapped db functions in try catches
jacquesvcritien Feb 24, 2023
0634fe7
Added check for jobname
jacquesvcritien Feb 24, 2023
049494e
Removed catches from updateTable
jacquesvcritien Feb 24, 2023
ad863b5
Removed extra catches
jacquesvcritien Feb 24, 2023
0e14b95
Changed name for uppercase to temp
jacquesvcritien Feb 24, 2023
0d858dd
Renamed Env class files
jacquesvcritien Feb 24, 2023
5467000
Fixed small thing in test
jacquesvcritien Feb 24, 2023
a9027ca
Added oracle parameter to getOraclesInvitations
jacquesvcritien Feb 24, 2023
df3e68e
Removed unused import
jacquesvcritien Feb 24, 2023
ec0c342
Fixed issue related to getOraclesInvitations
jacquesvcritien Feb 24, 2023
9899126
Added some formatting changes to chain.js
jacquesvcritien Feb 24, 2023
bafa574
Changes to modularise the monitoring script
jacquesvcritien Feb 24, 2023
3a916f2
Created config classes
jacquesvcritien Feb 24, 2023
56be0c2
Fixed small import
jacquesvcritien Feb 24, 2023
a292d52
Imported feeds config
jacquesvcritien Feb 24, 2023
bc2d6ee
Fixing issue with feedsconfig
jacquesvcritien Feb 24, 2023
f622fd6
Fixed another issue with feeds
jacquesvcritien Feb 24, 2023
c24989f
Added db migrations
jacquesvcritien Feb 24, 2023
00e4382
Fixed second migration
jacquesvcritien Feb 24, 2023
c683c7a
Fixed db functions to match new package
jacquesvcritien Feb 24, 2023
40f68e5
Updated second migration
jacquesvcritien Feb 24, 2023
de65db1
Changed round details to a class
jacquesvcritien Feb 24, 2023
5166612
Merge pull request #6 from jacquesvcritien/master
jacquesvcritien Feb 25, 2023
0f2d4ec
Removed unused imports
jacquesvcritien Feb 27, 2023
a64393c
Changed env classes to singletons + removed some unused imports
jacquesvcritien Feb 27, 2023
dfbb9c0
Fixing import
jacquesvcritien Feb 27, 2023
b95e541
Importing monitorEnvInstance for monitor.js
jacquesvcritien Feb 27, 2023
ec2df2c
Updated .env sample
jacquesvcritien Feb 27, 2023
317321c
Changed filenames
jacquesvcritien Feb 27, 2023
5875788
Updated credentials filename
jacquesvcritien Feb 27, 2023
e2df618
Reverting env sample changes
jacquesvcritien Feb 27, 2023
268caad
Cosmetic changes
jacquesvcritien Feb 27, 2023
9379c56
More cosmetic changes
jacquesvcritien Feb 27, 2023
29244ce
More minor fixes
jacquesvcritien Feb 27, 2023
672f0f1
Added poll_interval to docker-compose, added check for invitations va…
jacquesvcritien Feb 27, 2023
c332c3e
Merge pull request #8 from jacquesvcritien/master
jacquesvcritien Feb 27, 2023
eeb7e3d
Switched to template strings
jacquesvcritien Feb 27, 2023
0cff0c4
Fixing typo
jacquesvcritien Feb 27, 2023
d67b4f1
Merge pull request #9 from jacquesvcritien/master
jacquesvcritien Feb 28, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
8 changes: 8 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
THIS_VM_IP=
WALLET_ADDR=
AGORIC_NET=agoriclocal
AGORIC_RPC=http://0.0.0.0:26657
EI_CHAINLINKURL=http://0.0.0.0:6691
FROM=$WALLET_ADDR
SEND_CHECK_INTERVAL=45
SUBMIT_RETRIES=3
CREDENTIALS_FILE=config/ei_credentials.json
DB_FILE=data/database.db
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules/*
.env
.DS_Store
.vscode
26 changes: 26 additions & 0 deletions Dockerfile.middleware
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM node:16.17.1

RUN apt install g++
RUN apt install bash gcc openssl
RUN apt update && apt install ca-certificates wget
RUN wget -O go.tgz https://golang.org/dl/go1.19.2.linux-amd64.tar.gz
RUN tar -C /usr/local -xzf go.tgz
ENV PATH "/usr/local/go/bin:$PATH"
ENV GOPATH "/opt/go/"
ENV PATH "$PATH:$GOPATH/bin"

WORKDIR /agoric
RUN git clone https://github.com/agoric/agoric-sdk
WORKDIR /agoric/agoric-sdk
RUN yarn install
RUN yarn build
RUN cd packages/cosmic-swingset && make
ENV PATH "$PATH:$HOME/bin"

WORKDIR /oracle-cl-middleware
COPY package.json package.json
RUN yarn install
COPY . .
WORKDIR /oracle-cl-middleware/src

CMD [ "./bin-middleware.js" ]
13 changes: 13 additions & 0 deletions Dockerfile.monitor
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM node:16.17.1

WORKDIR /oracle-cl-middleware

COPY package.json package.json

RUN yarn install

COPY . .

WORKDIR /oracle-cl-middleware/src

CMD [ "./bin-monitor.js" ]
231 changes: 231 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
# Agoric Oracle Middleware

- [Oracle Network Structure](#oracle-network-structure)
- [Smart Contract Details](#smart-contract-details)
- [Aims/Objectives](#aims/objectives)
- [Middleware](#middleware)
- [Monitoring](#monitoring)
- [File structure](#file-structure)
- [Technical Documentation](#technical-documentation)
- [How to run](#htr)

## Oracle Network Structure

<img src="docs/images/struct.png"></img>
<br>

Each node operator has to run:
- A Chainlink node hosted locally in Docker that queries <a href="https://docs.chain.link/chainlink-nodes/external-adapters/external-adapters/">Chainlink adapters</a>
- Chainlink adapters hosted beside the Chainlink node that act as middleware between the node as various 3rd party APIs
- Middleware which initiates jobs on the Chainlink node through its API if a round hasn't been created in 1 minute on the on chain aggregator contract. Rounds are also created if the oracle operator's current price is deviated past X% from the latest on-chain median price. Oracle operators are not allowed to create 2 rounds subsequently to prevent spamming.
- An Agoric node hosted on their own infrastructure to allow the middleware / oracle proxy to communicate and broadcast transactions reliably to the network.

Before making use of this middleware, oracles should prepare their setups using this <a href="https://github.com/jacquesvcritien/agoric-chainlink-setup-docs">setup guide</a>.

## Smart Contract Details

Smart Contract - https://github.com/Agoric/agoric-sdk/tree/8720d22ddf25a005aee25786bfa8ee4bccaf19c9/packages/inter-protocol/src/price

- The smart contract resembles a native Chainlink Solidity Flux-Monitor smart contract on Ethereum (Example - https://etherscan.io/address/0x79febf6b9f76853edbcbc913e6aae8232cfb9de9).
- For each feed, there will be a smart contract on chain and each node operator receives invitations to their smart wallet to be part of the oracle set. Then, oracles can use that invitation to push prices on chain.
- There is a minimum number of submissions which has to be reached in order for the on-chain price to be updated
- An oracle cannot initialise multiple consecutive rounds to avoid network congestion
- The updated price is the median from the submitted prices for a particular round
- Submissions for old rounds cannot be submitted and will be rejected

## Aims/Objectives

#### Middleware

The middleware is needed to:
1. Create CL jobs. CL jobs are created every minute. A cron job is not used in order to be able to pass in particular inputs to the job run so that the progress of the request can be stored and monitored.
2. Get price responses back from the CL node after Job requests are made from the middleware.
3. Query smart contract to see if submission period for a new round is open.
4. Submit a transaction to update prices on-chain.

The middleware should contain the following functionalities:

1. An SQLite database that allows the middleware to resume from its last execution whenever it is restarted.
2. An endpoint that listens for new and removed jobs from the CL node, allowing the middleware to maintain a list of jobs for which requests need to be sent.
3. Keep a list of active jobs in its state
4. Query the price on chain and the latest round every X seconds so that a new job request is created if a new round is found
5. Send CL job requests to the CL node with parameters including a request ID and the reason for the request every Y seconds. The reason can be one of the following 3 reasons:
1. Type 1: Time expired and a new request have to be made
2. Type 2: A price deviation from the on-chain price was found
3. Type 3: A new round was found
6. An endpoint to listen for results from a CL job for submitted job requests. This endpoint should decide whether or not to push the price on chain. A price should be pushed on chain if one of the following cases is satisfied:
1. It is a new round and the oracle has not yet submitted to this round
2. An interval of Z minutes/hours expired since the last price pushed on chain. **NOTE**: This is only done if the oracle has not started the previous round, as oracles are not allowed to start consecutive rounds.
3. There is a deviation of more than W% from the current price on-chain. Once again, only done if the oracle has not started the previous round.
7. Keep the following information in the DB for each job in the jobs table

| Table Name | Field Name | Description | Type |
|------------ |-------------------------- |------------------------------------------------------------- |-------- |
| jobs | id | The CL job's external id for that feed | String |
| jobs | name | The name of the feed | String |
| jobs | request_id | The last request id | Number |
| jobs | last_reported_round | The last reported round on chain | Number |
| jobs | last_request_sent | The timestamp of the last price submission made on chain | Number |
| jobs | last_submission_time | The timestamp of the last request made to the CL node | Number |
| jobs | last_result | The last aggregated price on chain | Number |
| jobs | last_received_request_id | The request id of the last result received from the CL node | Number |

8. Keep the following information in the DB for each job in the jobs table:

| Table Name | Field Name | Description | Type |
|------------ |----------------- |---------------------------------------------------- |--------- |
| rounds | feed | The name of the feed | String |
| rounds | roundId | Latest round id | Number |
| rounds | startedAt | The timestamp when the round was started | Number |
| rounds | startedBy | The address who started the latest round | String |
| rounds | submissionMade | Whether a submission was made for the latest round | Boolean |

#### Monitoring

The monitoring script is needed to:
1. Monitor the actual price and that it is being updated
2. Monitor node operators' submissions to ensure they are submitting values within acceptable thresholds and that they are submitting to rounds and not missing them
3. Monitor node operators' balances to ensure they have enough balance for transaction fees to push prices on chain
4. Monitor the rate at which rounds are being created

The monitoring script should contain the following functionalities:

1. A state file which is updated so that whenever the monitoring script is restarted it is able to continue from where it was stopped.
2. Monitor multiple oracles at once
3. An endpoint to expose prometheus metrics which are a set of time-series data which can be graphed and used to monitor the whole oracle network.
4. Expose the following metrics
1. The latest submitted value on chain by an oracle for a feed
2. The timestamp in which an oracle made an on-chain submission for a feed
3. The last round for which an oracle made an on-chain submission for a feed
4. The deviation of an oracle's submitted price from the latest aggregated value on-chain for a feed
5. The oracle balance
6. The actual price on-chain
4. See all invitations IDs to be part of the oracle set from wallets
5. Query the latest prices and round submissions of oracles every X seconds and update the metrics
6. Have an efficient way of polling only the latest price pushes so be able to monitor oracles efficiently when the number of offers used start to increase


## File structure

In this section, one is able to find a description of the file structure and the contents of files

#### migrations

The migrations directory contains SQL files used to migrate the DB and update it smoothly without any user intervention.

#### scripts

The scripts directory contains scripts which are used to for deployments.

The following are the different scripts which can be found in this directory

1. accept-oracle-invitation.sh - This script can be used to accept an oracle invitation. This script takes in 3 parameters, the wallet name, the brand in and brand out. An example of a command to run this is ```./accept-oracle-invitation.sh $WALLET_NAME ATOM USD```. This requires the oracles to have provisioned a smart wallet as per the <a href="https://github.com/jacquesvcritien/agoric-chainlink-setup-docs">setup docs</a>.
2. provision-wallet.sh - This script can be used to provision a smart wallet. This script takes in one parameter, the wallet name. An example of a command to run this is ```./provision-wallet.sh $WALLET_NAME```. This requires oracles to have created a wallet as per the <a href="https://github.com/jacquesvcritien/agoric-chainlink-setup-docs">setup docs</a>.

#### config

This directory contains the following file:

1. <b>feeds-config.json</b> - This file contains the configuration for each feed in the oracle network. Each feed will have the following fields:
- <u>pollInterval</u> - The interval in seconds which needs to pass between each CL job
- <u>pushInterval</u> - The interval in seconds which needs to pass between each round creation on-chain
- <u>decimalPlaces</u> - The number of decimal places allowed for the price
- <u>priceDeviationPerc</u> - The price deviation percentage threshold on when to create a new round on-chain
The file should have the following structure
```json
{
"ATOM-USD" : {
"decimalPlaces": 6,
"pollInterval": 60,
"pushInterval": 600,
"priceDeviationPerc": 1
},
"OSMO-USD" : {
"decimalPlaces": 6,
"pollInterval": 60,
"pushInterval": 600,
"priceDeviationPerc": 1
}
}
```

#### src

This directory includes all the source code. It is split into three other directories, <b>helpers</b>, <b>oracle</b> and <b>lib</b>.

Furthermore, it contains the following two files which serve as an entry point to the middleware and monitoring script:

* <b>bin-middleware.js</b> - This serves as an entry point to the middleware by calling the middleware() function
* <b>bin-monitor.js</b> - This serves as an entry point to the monitoring script by calling the getOraclesInvitations() and monitor() functions to first get the oracle invitation IDs and then starting the monitoring.

##### helpers

This directory contains the following files:

1. <b>chain.js</b> - This file contains helper functions which are needed to interact with the Agoric chain
2. <b>chainlink.js</b> - This file contains helper functions to send job requests to the CL node
3. <b>db.js</b> - This file contains helper functions related to the database
4. <b>utils.js</b> - This file contains basic helper functions
5. <b>MiddlewareEnv.js</b> - This file contains a class to represent the middleware's environment variables
6. <b>MonitorEnv.js</b> - This file contains a class to represent the monitoring script's environment variables
7. <b>logger.js</b> - This file exports a logger
8. <b>MonitoringState.js</b> - This file contains a class to represent the monitoring script's state and to interact with it
9. <b>MonitorMetrics.js</b> - This file contains a class to represent the monitoring script's metrics
10. <b>OracleMonitorConfig.js</b> - This file contains a class to represent the monitoring script's config
11. <b>Credentials.js</b> - This file contains a class to represent the EI credentials
12. <b>FeedsConfig.js</b> - This file contains a class to represent the feeds configuration
12. <b>RoundDetails.js</b> - This file contains a class to represent round details


##### oracle

This directory contains the following files

1. <b>bridge.js</b> - This file contains the NodeJS server which will listen to requests from the CL node
2. <b>controller.js</b> - This file contains the controller which will query the chain for rounds and prices
3. <b>middleware.js</b> - This file contains all the necessary code and functions for the middleware
4. <b>monitor.js</b> - This file contains all the necessary code and functions for monitoring the oracle network

##### lib

This directory contains files which I cloned from the <b>agoric-sdk</b> repository and these contain functions which are used in the middleware and monitoring script.

##### docker-compose.yml

This is a docker-compose file to spin up the middleware and monitoring script

##### Dockerfile.middleware

This is a docker file to build the middleware

##### Dockerfile.monitor

This is a docker file to build the monitoring script

##### monitoring-grafana-dashboard.json

This is a Grafana template to monitor an oracle node or the whole oracle network

## Technical Documentation

Technical documentation can be found [here](docs/otherfile.md)

## How to run
#### Create oracles.json file

In order to monitor oracles, you need to create a file at <b>~/config/oracles.json</b>.
As can be seen below, it takes an array of oracles with their addresses and names.

```json
{
"agoric12345aaaaaaaaaa" : { "oracleName": "Oracle 1" },
"agoric678910bbbbbbbbb" : { "oracleName": "Oracle 2" }
}
```

#### To run both the middleware and monitoring script

```bash
docker-compose build
docker-compose up -d
```
dillu24 marked this conversation as resolved.
Show resolved Hide resolved
Loading