Skip to content

Commit

Permalink
Merge pull request #85 from oraichain/feat/FOU-523-display-price
Browse files Browse the repository at this point in the history
feat: get price by usd
  • Loading branch information
haunv3 authored Dec 11, 2023
2 parents 949edeb + 78b8149 commit c06333a
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/oraidex-server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oraichain/oraidex-server",
"version": "1.0.35",
"version": "1.0.36",
"main": "dist/index.js",
"bin": "dist/index.js",
"license": "MIT",
Expand Down
13 changes: 13 additions & 0 deletions packages/oraidex-server/src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ORAI } from "@oraichain/oraidex-sync";
import bech32 from "bech32";
export function parseSymbolsToTickerId([base, quote]: [string, string]) {
return `${base}_${quote}`;
}
Expand Down Expand Up @@ -27,3 +29,14 @@ export function calculateBasePriceFromTickerVolume(baseVolume: string, targetVol
export function pairToString([base, quote]: string[]): string {
return `${base}-${quote}`;
}

export const validateContractAddress = (contractAddress: string) => {

This comment has been minimized.

Copy link
@tubackkhoa

tubackkhoa Dec 11, 2023

Contributor

change name to validateOraiAddress

try {
const { prefix } = bech32.decode(contractAddress);
if (prefix === ORAI) return true;
return false;
} catch (error) {
console.log("error: ", error);
return false;
}
};
41 changes: 39 additions & 2 deletions packages/oraidex-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
GetPoolDetailQuery,
GetPricePairQuery,
GetStakedByUserQuery,
GetPriceAssetByUsdt,
ORAI,
OraiDexSync,
PairInfoDataResponse,
Expand All @@ -35,14 +36,21 @@ import {
oraixCw20Address,
usdcCw20Address,
getPoolLiquidities,
getPoolAmounts
getPoolAmounts,
getPriceAssetByUsdt
} from "@oraichain/oraidex-sync";
import cors from "cors";
import "dotenv/config";
import express, { Request } from "express";
import fs from "fs";
import path from "path";
import { getDate24hBeforeNow, getSpecificDateBeforeNow, pairToString, parseSymbolsToTickerId } from "./helper";
import {
getDate24hBeforeNow,
getSpecificDateBeforeNow,
pairToString,
parseSymbolsToTickerId,
validateContractAddress
} from "./helper";

const app = express();
app.use(cors());
Expand Down Expand Up @@ -451,6 +459,35 @@ app.get("/v1/my-staking", async (req: Request<{}, {}, {}, GetStakedByUserQuery>,
}
});

app.get("/price-by-usdt/", async (req: Request<{}, {}, {}, GetPriceAssetByUsdt>, res) => {
try {
const { contractAddress, denom } = req.query;
let price = 0;
if (contractAddress) {
const checkValidContractAddress = validateContractAddress(contractAddress);
if (!checkValidContractAddress) {
res.status(200).send({ price: 0 });
return;
}
price = await getPriceAssetByUsdt({
token: {
contract_addr: contractAddress
}
});
} else {
price = await getPriceAssetByUsdt({
native_token: {
denom: denom
}
});
}

res.status(200).send({ price });
} catch (error) {
res.status(500).send(error.message);
}
});

app
.listen(port, hostname, async () => {
// sync data for the service to read
Expand Down
14 changes: 13 additions & 1 deletion packages/oraidex-server/tests/helper.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getDate24hBeforeNow } from "../src/helper";
import { getDate24hBeforeNow, validateContractAddress } from "../src/helper";

describe("test-helper", () => {
it("test-getDate24hBeforeNow", () => {
Expand All @@ -9,4 +9,16 @@ describe("test-helper", () => {
// assert
expect(result).toEqual(new Date("2023-07-15T16:07:48.000Z"));
});

it.each([
["orai", false],
["orai1234", false],
["abc", false],
["orai1lus0f0rhx8s03gdllx2n6vhkmf0536dv57wfge", true], // ORAIX
["orai12hzjxfh77wl572gdzct2fxv2arxcwh6gykc7qh", true] // USDT
])("test-validateContractAddress", (contractAddress, expected) => {
const checkContractAddress = validateContractAddress(contractAddress);
// assert
expect(checkContractAddress).toEqual(expected);
});
});
5 changes: 5 additions & 0 deletions packages/oraidex-sync/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ export type GetCandlesQuery = {
endTime: number;
};

export type GetPriceAssetByUsdt = {
denom?: string;
contractAddress?: string;
};

export type GetFeeSwap = {
offerDenom: string;
askDenom: string;
Expand Down

0 comments on commit c06333a

Please sign in to comment.