solana-flux-aggregator/src/cli.ts

87 lines
2.6 KiB
TypeScript
Raw Normal View History

2021-02-20 01:05:05 -08:00
import dotenv from "dotenv"
dotenv.config()
import { Command, option } from "commander"
import { jsonReplacer, loadJSONFile } from "./json"
2021-02-20 03:28:39 -08:00
import { AggregatorDeployFile, Deployer } from "./Deployer"
2021-02-20 01:05:05 -08:00
import { conn, network } from "./context"
import { AggregatorObserver } from "./AggregatorObserver"
import { Aggregator, Answer } from "./schema"
import { PriceFeeder } from "./PriceFeeder"
2021-02-20 03:28:39 -08:00
import { sleep, walletFromEnv } from "./utils"
import { PublicKey, Wallet } from "solray"
import { log } from "./log"
2021-02-20 01:05:05 -08:00
const cli = new Command()
2021-02-20 03:28:39 -08:00
async function maybeRequestAirdrop(pubkey: PublicKey) {
if (network != "mainnet") {
log.info("airdrop 10 SOL", { address: pubkey.toBase58() })
await conn.requestAirdrop(pubkey, 10 * 1e9)
await sleep(500)
}
}
function deployFile(): AggregatorDeployFile {
return loadJSONFile<AggregatorDeployFile>(process.env.DEPLOY_FILE!)
}
cli.command("new-wallet").action(async (name) => {
const mnemonic = Wallet.generateMnemonic()
const wallet = await Wallet.fromMnemonic(mnemonic, conn)
log.info(`address: ${wallet.address}`)
log.info(`mnemonic: ${mnemonic}`)
await maybeRequestAirdrop(wallet.pubkey)
})
cli.command("setup <setup-file>").action(async (setupFile) => {
const wallet = await walletFromEnv("ADMIN_MNEMONIC", conn)
await maybeRequestAirdrop(wallet.pubkey)
const deployer = new Deployer(process.env.DEPLOY_FILE!, setupFile, wallet)
await deployer.runAll()
})
cli.command("oracle").action(async (name) => {
const wallet = await walletFromEnv("ORACLE_MNEMONIC", conn)
// await maybeRequestAirdrop(wallet.pubkey)
2021-02-20 03:28:39 -08:00
let deploy = loadJSONFile<AggregatorDeployFile>(process.env.DEPLOY_FILE!)
const feeder = new PriceFeeder(deploy, wallet)
feeder.start()
})
2021-02-20 03:42:19 -08:00
cli.command("observe").action(async (name?: string) => {
2021-02-20 01:05:05 -08:00
let deploy = loadJSONFile<AggregatorDeployFile>(process.env.DEPLOY_FILE!)
2021-02-20 03:42:19 -08:00
for (let [name, aggregatorInfo] of Object.entries(deploy.aggregators)) {
const observer = new AggregatorObserver(aggregatorInfo.pubkey, conn)
let agg = await Aggregator.load(aggregatorInfo.pubkey)
log.debug("observe aggregator", { name })
function printAnswer(answer: Answer) {
log.info("update", {
description: aggregatorInfo.config.description,
decimals: aggregatorInfo.config.decimals,
roundID: answer.roundID.toString(),
median: answer.median.toString(),
updatedAt: answer.updatedAt.toString(),
createdAt: answer.createdAt.toString(),
})
}
async function go() {
printAnswer(agg.answer)
for await (let answer of observer.answers()) {
printAnswer(answer)
}
}
2021-02-20 01:05:05 -08:00
2021-02-20 03:42:19 -08:00
go()
2021-02-20 01:05:05 -08:00
}
})
cli.parse(process.argv)