Skip to content

Commit 9e8c2bf

Browse files
authored
Update price from CoinGecko (#1367)
* Update price from CoinGecko * Fix price 0 logic * Remove eslint comment
1 parent f16c305 commit 9e8c2bf

File tree

1 file changed

+98
-58
lines changed

1 file changed

+98
-58
lines changed

packages/boba/gas-price-oracle/src/service.ts

Lines changed: 98 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,14 @@ export class GasPriceOracleService extends BaseService<GasPriceOracleOptions> {
266266
const historyJsonRaw = await fsPromise.readFile(dumpsPath)
267267
const historyJSON = JSON.parse(historyJsonRaw.toString())
268268
if (historyJSON.L1ETHCostFee) {
269-
/* eslint-disable */
270269
this.state.L1ETHBalance = BigNumber.from(historyJSON.L1ETHBalance)
271270
this.state.L1ETHCostFee = BigNumber.from(historyJSON.L1ETHCostFee)
272-
this.state.L1RelayerBalance = BigNumber.from(historyJSON.L1RelayerBalance)
273-
this.state.L1RelayerCostFee = BigNumber.from(historyJSON.L1RelayerCostFee)
274-
/* eslint-enable */
271+
this.state.L1RelayerBalance = BigNumber.from(
272+
historyJSON.L1RelayerBalance
273+
)
274+
this.state.L1RelayerCostFee = BigNumber.from(
275+
historyJSON.L1RelayerCostFee
276+
)
275277
} else {
276278
this.logger.warn('Invalid L1 cost history!')
277279
}
@@ -296,20 +298,34 @@ export class GasPriceOracleService extends BaseService<GasPriceOracleOptions> {
296298
this.logger.warn('Loading L2 cost history...')
297299
const historyJsonRaw = await fsPromise.readFile(dumpsPath)
298300
const historyJSON = JSON.parse(historyJsonRaw.toString())
299-
/* eslint-disable */
300301
this._readL2FeeCost(historyJSON, ETHVaultBalance, 'L2ETHCollectFee')
301302
this._readL2FeeCost(historyJSON, BOBAVaultBalance, 'L2BOBACollectFee')
302-
this._readL2FeeCost(historyJSON, BOBABillingBalance, 'L2BOBABillingCollectFee')
303+
this._readL2FeeCost(
304+
historyJSON,
305+
BOBABillingBalance,
306+
'L2BOBABillingCollectFee'
307+
)
303308
} else {
304309
this.logger.warn('No L2 cost history Found!')
305310
this.state.L2ETHCollectFee = ETHVaultBalance
306311
this.state.L2BOBACollectFee = BOBAVaultBalance
307312
this.state.L2BOBABillingCollectFee = BOBABillingBalance
308313
}
309-
this._adjustL2FeeCost(ETHVaultBalance, this.state.L2ETHCollectFee, 'L2ETHCollectFee')
310-
this._adjustL2FeeCost(BOBAVaultBalance, this.state.L2BOBACollectFee, 'L2BOBACollectFee')
311-
this._adjustL2FeeCost(BOBABillingBalance, this.state.L2BOBABillingCollectFee, 'L2BOBABillingCollectFee')
312-
/* eslint-enable */
314+
this._adjustL2FeeCost(
315+
ETHVaultBalance,
316+
this.state.L2ETHCollectFee,
317+
'L2ETHCollectFee'
318+
)
319+
this._adjustL2FeeCost(
320+
BOBAVaultBalance,
321+
this.state.L2BOBACollectFee,
322+
'L2BOBACollectFee'
323+
)
324+
this._adjustL2FeeCost(
325+
BOBABillingBalance,
326+
this.state.L2BOBABillingCollectFee,
327+
'L2BOBABillingCollectFee'
328+
)
313329
this.state.L2ETHVaultBalance = ETHVaultBalance
314330
this.state.L2BOBAVaultBalance = BOBAVaultBalance
315331
this.logger.info('Loaded L2 Cost Data', {
@@ -412,13 +428,21 @@ export class GasPriceOracleService extends BaseService<GasPriceOracleOptions> {
412428
this.logger.info('Got L1 ETH balances', {
413429
network: 'L1',
414430
data: {
415-
/* eslint-disable */
416431
L1ETHBalance: this._formatBigNumberToEther(this.state.L1ETHBalance),
417432
L1ETHCostFee: this._formatBigNumberToEther(this.state.L1ETHCostFee),
418-
L1ETHCostFeeUSD: this._formatBigNumberToEtherUSD(this.state.L1ETHCostFee, this.state.ETHUSDPrice, 2),
419-
L1RelayerCostFee: this._formatBigNumberToEther(this.state.L1RelayerCostFee),
420-
L1RelayerCostFeeUSD: this._formatBigNumberToEtherUSD(this.state.L1RelayerCostFee, this.state.ETHUSDPrice, 2),
421-
/* eslint-enable */
433+
L1ETHCostFeeUSD: this._formatBigNumberToEtherUSD(
434+
this.state.L1ETHCostFee,
435+
this.state.ETHUSDPrice,
436+
2
437+
),
438+
L1RelayerCostFee: this._formatBigNumberToEther(
439+
this.state.L1RelayerCostFee
440+
),
441+
L1RelayerCostFeeUSD: this._formatBigNumberToEtherUSD(
442+
this.state.L1RelayerCostFee,
443+
this.state.ETHUSDPrice,
444+
2
445+
),
422446
},
423447
})
424448
} catch (error) {
@@ -467,16 +491,32 @@ export class GasPriceOracleService extends BaseService<GasPriceOracleOptions> {
467491
this.logger.info('Got L2 Gas Collect', {
468492
network: 'L2',
469493
data: {
470-
/* eslint-disable */
471-
L2ETHCollectFee: this._formatBigNumberToEther(this.state.L2ETHCollectFee),
472-
L2BOBACollectFee: this._formatBigNumberToEther(this.state.L2BOBACollectFee),
473-
L2BOBABillingCollectFee: this._formatBigNumberToEther(this.state.L2BOBABillingCollectFee),
474-
L2ETHCollectFeeUSD: this._formatBigNumberToEtherUSD(this.state.L2ETHCollectFee, this.state.ETHUSDPrice, 2),
475-
L2BOBACollectFeeUSD: this._formatBigNumberToEtherUSD(this.state.L2BOBACollectFee, this.state.BOBAUSDPrice, 2),
476-
L2BOBABillingCollectFeeUSD: this._formatBigNumberToEtherUSD(this.state.L2BOBABillingCollectFee, this.state.BOBAUSDPrice, 2),
494+
L2ETHCollectFee: this._formatBigNumberToEther(
495+
this.state.L2ETHCollectFee
496+
),
497+
L2BOBACollectFee: this._formatBigNumberToEther(
498+
this.state.L2BOBACollectFee
499+
),
500+
L2BOBABillingCollectFee: this._formatBigNumberToEther(
501+
this.state.L2BOBABillingCollectFee
502+
),
503+
L2ETHCollectFeeUSD: this._formatBigNumberToEtherUSD(
504+
this.state.L2ETHCollectFee,
505+
this.state.ETHUSDPrice,
506+
2
507+
),
508+
L2BOBACollectFeeUSD: this._formatBigNumberToEtherUSD(
509+
this.state.L2BOBACollectFee,
510+
this.state.BOBAUSDPrice,
511+
2
512+
),
513+
L2BOBABillingCollectFeeUSD: this._formatBigNumberToEtherUSD(
514+
this.state.L2BOBABillingCollectFee,
515+
this.state.BOBAUSDPrice,
516+
2
517+
),
477518
BOBAUSDPrice: Number(this.state.BOBAUSDPrice.toFixed(2)),
478519
ETHUSDPrice: Number(this.state.ETHUSDPrice.toFixed(2)),
479-
/* eslint-enable */
480520
},
481521
})
482522
} catch (error) {
@@ -570,11 +610,11 @@ export class GasPriceOracleService extends BaseService<GasPriceOracleOptions> {
570610
).toNumber()
571611

572612
if (
573-
/* eslint-disable */
574-
targetOverheadGas > overheadProduction * (1 + this.options.overheadMinPercentChange) &&
575-
targetOverheadGas < overheadProduction * (1 - this.options.overheadMinPercentChange) &&
613+
targetOverheadGas >
614+
overheadProduction * (1 + this.options.overheadMinPercentChange) &&
615+
targetOverheadGas <
616+
overheadProduction * (1 - this.options.overheadMinPercentChange) &&
576617
targetOverheadGas > this.options.minOverhead
577-
/* eslint-enable */
578618
) {
579619
this.logger.debug('Updating overhead gas...')
580620
const tx = await this.state.OVM_GasPriceOracle.setOverhead(
@@ -621,17 +661,20 @@ export class GasPriceOracleService extends BaseService<GasPriceOracleOptions> {
621661

622662
private async _queryTokenPrice(tokenPair: string): Promise<void> {
623663
if (tokenPair === 'ETH/USD') {
624-
const latestAnswer = await this.state.BobaStraw_ETHUSD.latestAnswer()
625-
const decimals = await this.state.BobaStraw_ETHUSD.decimals()
626-
this.state.ETHUSDPrice = this._calculateTokenPrice(latestAnswer, decimals)
664+
const ETHUSDPrice = Number(
665+
await this._getTokenPriceFromCoinGecko('ethereum')
666+
)
667+
if (ETHUSDPrice > 0) {
668+
this.state.ETHUSDPrice = ETHUSDPrice
669+
}
627670
}
628671
if (tokenPair === 'BOBA/USD') {
629-
const latestAnswer = await this.state.BobaStraw_BOBAUSD.latestAnswer()
630-
const decimals = await this.state.BobaStraw_BOBAUSD.decimals()
631-
this.state.BOBAUSDPrice = this._calculateTokenPrice(
632-
latestAnswer,
633-
decimals
672+
const BOBAUSDPrice = Number(
673+
await this._getTokenPriceFromCoinGecko('boba-network')
634674
)
675+
if (BOBAUSDPrice > 0) {
676+
this.state.BOBAUSDPrice = BOBAUSDPrice
677+
}
635678
}
636679
}
637680

@@ -687,10 +730,10 @@ export class GasPriceOracleService extends BaseService<GasPriceOracleOptions> {
687730
prefixVaultBalance: string = null,
688731
prefixCollectFee: string = null
689732
) {
690-
/* eslint-disable */
691-
const vaultBalanceName = prefixVaultBalance === null ? `${prefix}VaultBalance`: prefixVaultBalance
692-
const collectFeeName = prefixCollectFee === null ? `${prefix}CollectFee`: prefixCollectFee
693-
/* eslint-enable */
733+
const vaultBalanceName =
734+
prefixVaultBalance === null ? `${prefix}VaultBalance` : prefixVaultBalance
735+
const collectFeeName =
736+
prefixCollectFee === null ? `${prefix}CollectFee` : prefixCollectFee
694737
// If vault balance is lower than the cache, it means that the vault has been drained
695738
if (latestCollectFee.lt(vaultBalanceHistory)) {
696739
this.state[vaultBalanceName] = latestCollectFee
@@ -709,14 +752,27 @@ export class GasPriceOracleService extends BaseService<GasPriceOracleOptions> {
709752
minDecimals: number = 2
710753
): number {
711754
if (decimals >= minDecimals) {
712-
/* eslint-disable */
713-
const tokenPriceX = tokenPrice.div(BigNumber.from(10).pow(decimals - minDecimals))
755+
const tokenPriceX = tokenPrice.div(
756+
BigNumber.from(10).pow(decimals - minDecimals)
757+
)
714758
return tokenPriceX.toNumber() / 100
715-
/* eslint-enable */
716759
}
717760
return tokenPrice.toNumber() / 10 ** decimals
718761
}
719762

763+
// Data provided by CoinGecko
764+
private async _getTokenPriceFromCoinGecko(id: string): Promise<number> {
765+
try {
766+
const URL = `https:///api.coingecko.com/api/v3/coins/${id}?localization=false&tickers=false&community_date=false&developer_data=false&sparkline=false`
767+
const payload = await fetch(URL)
768+
const payloadParsed = await payload.json()
769+
return Number(payloadParsed.market_data.current_price.usd)
770+
} catch (err) {
771+
this.logger.warn(`CAN\'T QUERY TOKEN PRICE ${err} - ${id} FROM CoinGecko`)
772+
return 0
773+
}
774+
}
775+
720776
private _formatBigNumberToEther(
721777
number: BigNumber | string,
722778
decimal = 6
@@ -923,7 +979,6 @@ export class GasPriceOracleAltL1Service extends BaseService<GasPriceOracleOption
923979
const historyJsonRaw = await fsPromise.readFile(dumpsPath)
924980
const historyJSON = JSON.parse(historyJsonRaw.toString())
925981
if (historyJSON.L1SecondaryFeeTokenCostFee) {
926-
/* eslint-disable */
927982
this.state.L1SecondaryFeeTokenBalance = BigNumber.from(
928983
historyJSON.L1SecondaryFeeTokenBalance
929984
)
@@ -936,7 +991,6 @@ export class GasPriceOracleAltL1Service extends BaseService<GasPriceOracleOption
936991
this.state.L1RelayerCostFee = BigNumber.from(
937992
historyJSON.L1RelayerCostFee
938993
)
939-
/* eslint-enable */
940994
} else {
941995
this.logger.warn('Invalid L1 cost history!')
942996
}
@@ -963,7 +1017,6 @@ export class GasPriceOracleAltL1Service extends BaseService<GasPriceOracleOption
9631017
this.logger.warn('Loading L2 cost history...')
9641018
const historyJsonRaw = await fsPromise.readFile(dumpsPath)
9651019
const historyJSON = JSON.parse(historyJsonRaw.toString())
966-
/* eslint-disable */
9671020
this._readL2FeeCost(historyJSON, ETHVaultBalance, 'L2BOBACollectFee')
9681021
this._readL2FeeCost(
9691022
historyJSON,
@@ -996,7 +1049,6 @@ export class GasPriceOracleAltL1Service extends BaseService<GasPriceOracleOption
9961049
this.state.L2SecondaryFeeTokenCollectFee,
9971050
'L2SecondaryFeeTokenCollectFee'
9981051
)
999-
/* eslint-enable */
10001052
this.state.L2BOBAVaultBalance = ETHVaultBalance
10011053
this.logger.info('Loaded L2 Cost Data', {
10021054
L2BOBAVaultBalance: this.state.L2BOBAVaultBalance.toString(),
@@ -1100,7 +1152,6 @@ export class GasPriceOracleAltL1Service extends BaseService<GasPriceOracleOption
11001152
this.logger.info('Got L1 ETH balances', {
11011153
network: 'L1',
11021154
data: {
1103-
/* eslint-disable */
11041155
L1SecondaryFeeTokenBalance: this._formatBigNumberToEther(
11051156
this.state.L1SecondaryFeeTokenBalance
11061157
),
@@ -1125,7 +1176,6 @@ export class GasPriceOracleAltL1Service extends BaseService<GasPriceOracleOption
11251176
this.state.L1NativeTokenPrice,
11261177
2
11271178
),
1128-
/* eslint-enable */
11291179
},
11301180
})
11311181
} catch (error) {
@@ -1175,7 +1225,6 @@ export class GasPriceOracleAltL1Service extends BaseService<GasPriceOracleOption
11751225
this.logger.info('Got L2 Gas Collect', {
11761226
network: 'L2',
11771227
data: {
1178-
/* eslint-disable */
11791228
L2BOBACollectFee: this._formatBigNumberToEther(
11801229
this.state.L2BOBACollectFee
11811230
),
@@ -1197,7 +1246,6 @@ export class GasPriceOracleAltL1Service extends BaseService<GasPriceOracleOption
11971246
this.state.L2SecondaryFeeTokenCollectFee,
11981247
this.state.L1NativeTokenPrice
11991248
),
1200-
/* eslint-enable */
12011249
},
12021250
})
12031251
} catch (error) {
@@ -1260,13 +1308,11 @@ export class GasPriceOracleAltL1Service extends BaseService<GasPriceOracleOption
12601308
).toNumber()
12611309

12621310
if (
1263-
/* eslint-disable */
12641311
targetOverheadGas >
12651312
overheadProduction * (1 + this.options.overheadMinPercentChange) &&
12661313
targetOverheadGas <
12671314
overheadProduction * (1 - this.options.overheadMinPercentChange) &&
12681315
targetOverheadGas > this.options.minOverhead
1269-
/* eslint-enable */
12701316
) {
12711317
this.logger.debug('Updating overhead gas...')
12721318
const tx = await this.state.OVM_GasPriceOracle.setOverhead(
@@ -1313,7 +1359,6 @@ export class GasPriceOracleAltL1Service extends BaseService<GasPriceOracleOption
13131359

13141360
private async _updatePriceRatio(): Promise<void> {
13151361
try {
1316-
/* eslint-disable */
13171362
const BobaPriceFromCoinGecko = await this._getTokenPriceFromCoinGecko(
13181363
'boba-network'
13191364
)
@@ -1350,7 +1395,6 @@ export class GasPriceOracleAltL1Service extends BaseService<GasPriceOracleOption
13501395
const l1NativeTokenPrice = calculateAverage(
13511396
this.filterOutliers(l1NativeTokenMarketPricesFromCoinMarketCap)
13521397
)
1353-
/* eslint-enable */
13541398

13551399
// store prices
13561400
this.state.BobaPrice = BobaPrice
@@ -1369,14 +1413,12 @@ export class GasPriceOracleAltL1Service extends BaseService<GasPriceOracleOption
13691413
(marketPriceRatio * this.options.bobaFeeRatio100X) / 100
13701414
)
13711415

1372-
/* eslint-disable */
13731416
const originalPriceRatio = (
13741417
await this.state.Boba_GasPriceOracle.priceRatio()
13751418
).toNumber()
13761419
const originalMarketPriceRatio = (
13771420
await this.state.Boba_GasPriceOracle.marketPriceRatio()
13781421
).toNumber()
1379-
/* eslint-enable */
13801422

13811423
if (
13821424
priceRatio !== originalPriceRatio ||
@@ -1541,12 +1583,10 @@ export class GasPriceOracleAltL1Service extends BaseService<GasPriceOracleOption
15411583
prefixVaultBalance: string = null,
15421584
prefixCollectFee: string = null
15431585
) {
1544-
/* eslint-disable */
15451586
const vaultBalanceName =
15461587
prefixVaultBalance === null ? `${prefix}VaultBalance` : prefixVaultBalance
15471588
const collectFeeName =
15481589
prefixCollectFee === null ? `${prefix}CollectFee` : prefixCollectFee
1549-
/* eslint-enable */
15501590
// If vault balance is lower than the cache, it means that the vault has been drained
15511591
if (latestCollectFee.lt(vaultBalanceHistory)) {
15521592
this.state[vaultBalanceName] = latestCollectFee

0 commit comments

Comments
 (0)