Skip to content

Commit

Permalink
refactor and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanjcampbell1 committed Jan 26, 2021
1 parent 50a3b10 commit 0d155b0
Showing 1 changed file with 15 additions and 11 deletions.
@@ -1,5 +1,4 @@
const { PriceFeedInterface } = require("./PriceFeedInterface");
const { parseFixed } = require("@ethersproject/bignumber");
const assert = require("assert");

// An implementation of PriceFeedInterface that uses DefiPulse Data api to retrieve prices.
Expand All @@ -15,7 +14,7 @@ class DefiPulseTotalPriceFeed extends PriceFeedInterface {
* @param {Integer} minTimeBetweenUpdates Min number of seconds between updates. If update() is called again before
* this number of seconds has passed, it will be a no-op.
*/
constructor(logger, web3, apiKey, lookback, networker, getTime, minTimeBetweenUpdates, decimals = 18) {
constructor(logger, web3, apiKey, lookback, networker, getTime, minTimeBetweenUpdates) {
super();
this.logger = logger;
this.web3 = web3;
Expand All @@ -28,7 +27,7 @@ class DefiPulseTotalPriceFeed extends PriceFeedInterface {
this.getTime = getTime;
this.minTimeBetweenUpdates = minTimeBetweenUpdates;

this.toBN = this.web3.utils.toBN;
this.toWei = this.web3.utils.toWei;

this.historicalPrices = [];
}
Expand All @@ -44,7 +43,7 @@ class DefiPulseTotalPriceFeed extends PriceFeedInterface {

let closestTime = { timestamp: 0, tvlUSD: 0 };

// go through all values and find time that that is the largest and still less than 'time'
// Go through all values and find time that that is the largest and still less than 'time'
for (let i = 0; i < this.historicalPrices.length; i++) {
let past = this.historicalPrices[i].timestamp;
let val = this.historicalPrices[i].tvlUSD;
Expand All @@ -55,12 +54,12 @@ class DefiPulseTotalPriceFeed extends PriceFeedInterface {
}
}

const historicalPrice = this.web3.utils.toWei((closestTime.tvlUSD / 1000000000).toFixed(3), "ether");
const historicalPrice = this.scaleResult(closestTime.tvlUSD);

if (closestTime.timestamp === 0) {
return undefined;
} else {
return this.toBN(historicalPrice);
return historicalPrice;
}
}

Expand Down Expand Up @@ -124,16 +123,21 @@ class DefiPulseTotalPriceFeed extends PriceFeedInterface {
}
}

// Based on the UMIP 24
// newPrice = Sum of TVL for all projects on DeFi Pulse divided by 1,000,000,000 with 3 decimal points of precision

const newPrice = this.web3.utils.toWei((mostRecent.tvlUSD / 1000000000).toFixed(3), "ether");
const newPrice = this.scaleResult(mostRecent.tvlUSD);

// 5. Store results.
this.lastUpdateTime = currentTime;
this.currentPrice = this.toBN(newPrice);
this.currentPrice = newPrice;
this.historicalPrices = response;
}

scaleResult(_tvlUSD) {
// As described in UMIP 24
// In an effort to make the token price affordable, the value of the token is the tvlUSD divided by 1 billion.
// We also cut off precision after 3 decimals to match the specified price step of .001

return this.toWei((_tvlUSD / 1000000000).toFixed(3), "ether");
}
}

module.exports = {
Expand Down

0 comments on commit 0d155b0

Please sign in to comment.