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

Add idena price fallback #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
50 changes: 37 additions & 13 deletions bsc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,21 +190,45 @@ exports.getContractAddress = function () {
}

async function getIdenaPrice() {
let resp = await axios.get("https://pro-api.coinmarketcap.com/v2/tools/price-conversion?id=5836&convert_id=1839&amount=1", {
headers: {
'X-CMC_PRO_API_KEY': process.env.PRICE_API_KEY,
},
});
if (resp.status !== 200) {
logger.error(`Failed to get idena price, status: ${resp.status}`);
return 0
const coinmarketcapPrice = async function() {
let resp = await axios.get("https://pro-api.coinmarketcap.com/v2/tools/price-conversion?id=5836&convert_id=1839&amount=1", {
headers: {
'X-CMC_PRO_API_KEY': process.env.PRICE_API_KEY,
},
});
if (resp.status !== 200) {
logger.error(`Failed to get idena price, status: ${resp.status}`);
return 0
}
const price = resp.data.data.quote['1839'].price
if (!price) {
logger.error(`Failed to get idena price, res: ${resp.data}`);
return 0
}
return ethers.utils.parseEther(price.toFixed(18).toString());
}
const price = resp.data.data.quote['1839'].price
if (!price) {
logger.error(`Failed to get idena price, res: ${resp.data}`);
return 0

const coingeckoPrice = async function() {
let resp = await axios.get("https://api.coingecko.com/api/v3/simple/price?ids=idena&vs_currencies=bnb");
if (resp.status !== 200) {
logger.error(`Failed to get idena price, status: ${resp.status}`);
return 0
}
if (!resp.data.idena.bnb) {
logger.error(`Failed to get idena price, res: ${resp.data.idena.bnb}`);
return 0
}
return ethers.utils.parseEther(resp.data.idena.bnb.toString());
}
return ethers.utils.parseEther(price.toFixed(18).toString());

let res
try {
res = await coinmarketcapPrice()
} catch (e) {
res = await coingeckoPrice()
}

return res
}

exports.isNewTx = async function (tx) {
Expand Down
Loading