Added cli command oracle-file for submitting prices by updating a local file (#3)

This commit is contained in:
Nicholas Clarke 2021-05-05 16:02:45 -07:00 committed by GitHub
parent a689faec7e
commit db7c27b737
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 96 additions and 1 deletions

View File

@ -54,6 +54,23 @@ not use this key for production!
NOTE 2: You might get error messages if somebody else is also running the
oracle.
### Manual price feed
Alternatively, you can submit prices to the oracle manually by instructing the oracle to watch for changes to a local file.
This can be useful for testing specific behaviour (such as triggering liquidations).
Run the oracle:
```
yarn solink oracle-file {pair} {filepath}
```
{pair} must be one of the aggregator pairs in deploy.sandbox.json
Then you can update prices by running
```
echo {price} > {filepath}
```
# Observe The Aggregators
With the oracle running, you can subscribe to price changes. In another

View File

@ -10,6 +10,8 @@ import { PriceFeeder } from "./PriceFeeder"
import { sleep, walletFromEnv } from "./utils"
import { PublicKey, Wallet } from "solray"
import { log } from "./log"
import { Submitter } from "./Submitter"
import { file } from "./feeds"
const cli = new Command()
@ -83,4 +85,51 @@ cli.command("observe").action(async (name?: string) => {
}
})
cli.command("oracle-file <pair> <path>").action(async (pair, path) => {
console.log(pair);
console.log(path);
let deployInfo = loadJSONFile<AggregatorDeployFile>(process.env.DEPLOY_FILE!)
// validate pair arg here
if (!(pair in deployInfo.aggregators)) {
throw 'Invalid pair ' + pair;
}
let slot = await conn.getSlot()
conn.onSlotChange((slotInfo) => {
slot = slotInfo.slot
})
const wallet = await walletFromEnv("ORACLE_MNEMONIC", conn)
// await maybeRequestAirdrop(wallet.pubkey)
let aggregatorInfo = deployInfo.aggregators[pair];
const oracleInfo = Object.values(aggregatorInfo.oracles).find(
(oracleInfo) => {
return oracleInfo.owner.equals(wallet.pubkey)
}
)
let minValueChangeForNewRound = 0
const submitter = new Submitter(
deployInfo.programID,
aggregatorInfo.pubkey,
oracleInfo!.pubkey,
wallet,
file(pair, path),
{
minValueChangeForNewRound,
},
() => slot
)
submitter.start();
})
cli.parse(process.argv)

View File

@ -5,7 +5,7 @@ import { eventsIter, median, notify } from "./utils"
import { log } from "./log"
import winston from "winston"
import fs from "fs";
const SECONDS = 1000
export const UPDATE = "UPDATE"
@ -444,3 +444,32 @@ export function coinbase(pair: string): IPriceFeed {
return eventsIter(emitter, UPDATE)
}
export function file(pair: string, filepath: string): IPriceFeed {
const emitter = new EventEmitter()
try {
fs.accessSync(filepath);
} catch {
fs.writeFileSync(filepath, '');
console.log('feed file created at ' + filepath)
}
console.log('started file feed for ' + pair)
fs.watch(filepath, (event) => {
if (event === 'change') {
const data = fs.readFileSync(filepath, 'utf8')
const price: IPrice = {
source: "file",
pair,
decimals: 2,
value: parseFloat(data)
}
console.log('price update: ', price)
emitter.emit(UPDATE, price)
}
});
return eventsIter(emitter, UPDATE)
}