Skip to content

Commit 0d155b0

Browse files
refactor and lint
1 parent 50a3b10 commit 0d155b0

1 file changed

Lines changed: 15 additions & 11 deletions

File tree

packages/financial-templates-lib/src/price-feed/DefiPulseTotalPriceFeed.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const { PriceFeedInterface } = require("./PriceFeedInterface");
2-
const { parseFixed } = require("@ethersproject/bignumber");
32
const assert = require("assert");
43

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

31-
this.toBN = this.web3.utils.toBN;
30+
this.toWei = this.web3.utils.toWei;
3231

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

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

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

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

6059
if (closestTime.timestamp === 0) {
6160
return undefined;
6261
} else {
63-
return this.toBN(historicalPrice);
62+
return historicalPrice;
6463
}
6564
}
6665

@@ -124,16 +123,21 @@ class DefiPulseTotalPriceFeed extends PriceFeedInterface {
124123
}
125124
}
126125

127-
// Based on the UMIP 24
128-
// newPrice = Sum of TVL for all projects on DeFi Pulse divided by 1,000,000,000 with 3 decimal points of precision
129-
130-
const newPrice = this.web3.utils.toWei((mostRecent.tvlUSD / 1000000000).toFixed(3), "ether");
126+
const newPrice = this.scaleResult(mostRecent.tvlUSD);
131127

132128
// 5. Store results.
133129
this.lastUpdateTime = currentTime;
134-
this.currentPrice = this.toBN(newPrice);
130+
this.currentPrice = newPrice;
135131
this.historicalPrices = response;
136132
}
133+
134+
scaleResult(_tvlUSD) {
135+
// As described in UMIP 24
136+
// In an effort to make the token price affordable, the value of the token is the tvlUSD divided by 1 billion.
137+
// We also cut off precision after 3 decimals to match the specified price step of .001
138+
139+
return this.toWei((_tvlUSD / 1000000000).toFixed(3), "ether");
140+
}
137141
}
138142

139143
module.exports = {

0 commit comments

Comments
 (0)