Skip to content

Commit

Permalink
Merge pull request #21 from oraichain/feat/add-btc-price-feed
Browse files Browse the repository at this point in the history
Feat/add btc price feed
  • Loading branch information
trung2891 authored Apr 3, 2024
2 parents c9d2718 + 7c59bdb commit 137b50f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/priceFeed/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oraichain/oraimargin-pricefeed",
"version": "1.0.2",
"version": "1.0.3",
"main": "build/cli.js",
"bin": "build/cli.js",
"license": "MIT",
Expand Down
29 changes: 15 additions & 14 deletions packages/priceFeed/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import dotenv from "dotenv";
import { priceFeedHandler, executePriceFeed } from "./index";
import { UserWallet, decrypt, delay, setupWallet } from "@oraichain/oraitrading-common";
import {
UserWallet,
decrypt,
delay,
setupWallet,
} from "@oraichain/oraitrading-common";
import { WebhookClient, time, userMention } from "discord.js";

dotenv.config();
Expand All @@ -15,12 +20,12 @@ async function getSender(rpcUrl: string): Promise<UserWallet | string> {
hdPath: process.env.HD_PATH ?? "m/44'/118'/0'/0/0",
rpcUrl,
prefix: "orai",
gasPrices: "0.001"
gasPrices: "0.001",
}
);
return sender;
} catch (error: any) {
console.log({ error: error.message});
console.log({ error: error.message });
return "Error: " + error.message;
}
}
Expand All @@ -31,26 +36,22 @@ async function handleExecutePriceFeed(
): Promise<string> {
const date = new Date();
let result = "";
const priceFeed = new priceFeedHandler(sender, engine);
try {
const priceFeed = new priceFeedHandler(sender, engine);
try {
const priceMsg = await executePriceFeed(priceFeed);
if (priceMsg.length > 0) {
console.dir(priceMsg, { depth: 4 });
const res = await priceFeed.executeMultiple(priceMsg);
if (res !== undefined) {
console.log(
"appendPrice - txHash:",
res.transactionHash
);
result = `:receipt: BOT: ${sender.address} - appendPrice - txHash: ${res.transactionHash}` + ` at ${time(date)}`;
console.log("appendPrice - txHash:", res.transactionHash);
result =
`:receipt: BOT: ${sender.address} - appendPrice - txHash: ${res.transactionHash}` +
` at ${time(date)}`;
}
}
return result;
} catch (error) {
console.log(
"error in processing appendPrice: ",
{ error }
);
console.log("error in processing appendPrice: ", { error });
console.log("Send discord noti: ", error.message);
return (
`:red_circle: BOT: ${sender.address} - err ` +
Expand Down
59 changes: 55 additions & 4 deletions packages/priceFeed/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,67 @@ export class priceFeedHandler {
},
];
}

async appendPrice(
priceFeed: Addr,
key: string,
url: string
): Promise<ExecuteInstruction[]> {
const decimals = Number((await this.engineClient.config()).decimals);
const oraclePrice = Math.round((await getPriceFeed(key, url)) * decimals);
if (oraclePrice === 0) {
console.log("Oracle price is ZERO!");
return [];
}
console.log({ oraclePrice });
let time = Math.floor(Date.now() / 1000) - 12;
console.log({ time });
const appendPrice = {
append_price: {
key,
price: oraclePrice.toString(),
timestamp: time,
},
} as MarginedPricefeedTypes.ExecuteMsg;

return [
{
contractAddress: priceFeed,
msg: appendPrice,
},
];
}
}

export async function executePriceFeed(
engineHandler: priceFeedHandler
): Promise<ExecuteInstruction[]> {
const priceFeed = process.env.PRICEFEED_CONTRACT;
console.log({ priceFeed });
const appendOraiPrice = await engineHandler.appendOraiprice(priceFeed);
const appendInjPrice = await engineHandler.appendInjprice(priceFeed);
let priceMsg: ExecuteInstruction[] = [];
priceMsg = priceMsg.concat(appendOraiPrice, appendInjPrice);
const [appendOraiPrice, appendInjPrice, appendBTCPrice] = await Promise.all([
engineHandler.appendPrice(
priceFeed,
"ORAI",
"https://pricefeed.oraichainlabs.org/"
),
engineHandler.appendPrice(
priceFeed,
"INJ",
"https://pricefeed-futures.oraichainlabs.org/inj"
),
engineHandler.appendPrice(
priceFeed,
"BTC",
"https://pricefeed-futures.oraichainlabs.org/btc"
),
]);

const priceMsg: ExecuteInstruction[] = [
...appendOraiPrice,
...appendInjPrice,
...appendBTCPrice,
];

console.log(priceMsg);
return priceMsg;
}

0 comments on commit 137b50f

Please sign in to comment.