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

Price oracles update #583

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
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
51 changes: 16 additions & 35 deletions packages/helium-admin-cli/src/submit-price.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
import { init } from "@helium/price-oracle-sdk";
import { sendInstructionsWithPriorityFee } from "@helium/spl-utils";
import * as anchor from "@coral-xyz/anchor";
import { PublicKey } from "@solana/web3.js";
import os from "os";
import yargs from "yargs/yargs";
import { BN } from "bn.js";
import axios from "axios";

async function findBinancePrice(symbol: string): Promise<number> {
try {
const response = await axios.get(
`https://api.binance.com/api/v3/ticker/price?symbol=${symbol}`
);
const data = response.data;
return parseFloat(data.price);
} catch (error: any) {
console.error(
`Error fetching ticker price for ${symbol}: ${error.message}`
);
throw error;
}
}

export async function findCoingeckoPrice(token: string): Promise<number> {
try {
const response = await axios.get(
Expand Down Expand Up @@ -59,13 +45,6 @@ export async function run(args: any = process.argv) {
describe:
"Name or Address of the price oracle to submit a price to. Example: hnt, iot, mobile, horUtvuHQFWxPFrZ35YZUmXUZ2TSQdSXhcD4kkCVNKi",
},
useBinancePrice: {
type: "string",
required: false,
describe:
"If supplied, will try and find the binance price for this pair. E.g. 'HNTUSDT' will submit the binance HNT-USDT price",
default: null,
},
useCoingeckoPrice: {
type: "string",
required: false,
Expand All @@ -83,16 +62,14 @@ export async function run(args: any = process.argv) {
const provider = anchor.getProvider() as anchor.AnchorProvider;
const program = await init(provider);

if (!argv.useBinancePrice && !argv.price && !argv.useCoingeckoPrice) {
if (!argv.price && !argv.useCoingeckoPrice) {
throw new Error("Need to supply a price");
}

const price = argv.price
? argv.price
: argv.useCoingeckoPrice
? await findCoingeckoPrice(argv.useCoingeckoPrice)
: argv.useBinancePrice
? await findBinancePrice(argv.useBinancePrice)
: null;

switch (argv.priceOracle) {
Expand Down Expand Up @@ -120,16 +97,20 @@ export async function run(args: any = process.argv) {

const decimalShiftedPrice = new BN(price! * 10 ** priceOracleAcc.decimals);

await program.methods
.submitPriceV0({
oracleIndex,
price: decimalShiftedPrice,
})
.accounts({
priceOracle,
oracle: provider.wallet.publicKey,
})
.rpc({ skipPreflight: true });
await sendInstructionsWithPriorityFee(provider, [
await program.methods
.submitPriceV0({
oracleIndex,
price: decimalShiftedPrice,
})
.accounts({
priceOracle,
oracle: provider.wallet.publicKey,
})
.instruction(),
], {
computeUnitLimit: 1000000
});

console.log(`Submitted price: ${price}`);
}
Loading