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

Update price from CoinGecko #1367

Merged
merged 3 commits into from
Mar 15, 2024
Merged
Changes from 1 commit
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
120 changes: 92 additions & 28 deletions packages/boba/gas-price-oracle/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,12 @@ export class GasPriceOracleService extends BaseService<GasPriceOracleOptions> {
/* eslint-disable */
this.state.L1ETHBalance = BigNumber.from(historyJSON.L1ETHBalance)
this.state.L1ETHCostFee = BigNumber.from(historyJSON.L1ETHCostFee)
this.state.L1RelayerBalance = BigNumber.from(historyJSON.L1RelayerBalance)
this.state.L1RelayerCostFee = BigNumber.from(historyJSON.L1RelayerCostFee)
this.state.L1RelayerBalance = BigNumber.from(
historyJSON.L1RelayerBalance
)
this.state.L1RelayerCostFee = BigNumber.from(
historyJSON.L1RelayerCostFee
)
/* eslint-enable */
} else {
this.logger.warn('Invalid L1 cost history!')
Expand Down Expand Up @@ -299,16 +303,32 @@ export class GasPriceOracleService extends BaseService<GasPriceOracleOptions> {
/* eslint-disable */
this._readL2FeeCost(historyJSON, ETHVaultBalance, 'L2ETHCollectFee')
this._readL2FeeCost(historyJSON, BOBAVaultBalance, 'L2BOBACollectFee')
this._readL2FeeCost(historyJSON, BOBABillingBalance, 'L2BOBABillingCollectFee')
this._readL2FeeCost(
historyJSON,
BOBABillingBalance,
'L2BOBABillingCollectFee'
)
} else {
this.logger.warn('No L2 cost history Found!')
this.state.L2ETHCollectFee = ETHVaultBalance
this.state.L2BOBACollectFee = BOBAVaultBalance
this.state.L2BOBABillingCollectFee = BOBABillingBalance
}
this._adjustL2FeeCost(ETHVaultBalance, this.state.L2ETHCollectFee, 'L2ETHCollectFee')
this._adjustL2FeeCost(BOBAVaultBalance, this.state.L2BOBACollectFee, 'L2BOBACollectFee')
this._adjustL2FeeCost(BOBABillingBalance, this.state.L2BOBABillingCollectFee, 'L2BOBABillingCollectFee')
this._adjustL2FeeCost(
ETHVaultBalance,
this.state.L2ETHCollectFee,
'L2ETHCollectFee'
)
this._adjustL2FeeCost(
BOBAVaultBalance,
this.state.L2BOBACollectFee,
'L2BOBACollectFee'
)
this._adjustL2FeeCost(
BOBABillingBalance,
this.state.L2BOBABillingCollectFee,
'L2BOBABillingCollectFee'
)
/* eslint-enable */
this.state.L2ETHVaultBalance = ETHVaultBalance
this.state.L2BOBAVaultBalance = BOBAVaultBalance
Expand Down Expand Up @@ -415,9 +435,19 @@ export class GasPriceOracleService extends BaseService<GasPriceOracleOptions> {
/* eslint-disable */
L1ETHBalance: this._formatBigNumberToEther(this.state.L1ETHBalance),
L1ETHCostFee: this._formatBigNumberToEther(this.state.L1ETHCostFee),
L1ETHCostFeeUSD: this._formatBigNumberToEtherUSD(this.state.L1ETHCostFee, this.state.ETHUSDPrice, 2),
L1RelayerCostFee: this._formatBigNumberToEther(this.state.L1RelayerCostFee),
L1RelayerCostFeeUSD: this._formatBigNumberToEtherUSD(this.state.L1RelayerCostFee, this.state.ETHUSDPrice, 2),
L1ETHCostFeeUSD: this._formatBigNumberToEtherUSD(
this.state.L1ETHCostFee,
this.state.ETHUSDPrice,
2
),
L1RelayerCostFee: this._formatBigNumberToEther(
this.state.L1RelayerCostFee
),
L1RelayerCostFeeUSD: this._formatBigNumberToEtherUSD(
this.state.L1RelayerCostFee,
this.state.ETHUSDPrice,
2
),
/* eslint-enable */
},
})
Expand Down Expand Up @@ -468,12 +498,30 @@ export class GasPriceOracleService extends BaseService<GasPriceOracleOptions> {
network: 'L2',
data: {
/* eslint-disable */
boyuan-chen marked this conversation as resolved.
Show resolved Hide resolved
L2ETHCollectFee: this._formatBigNumberToEther(this.state.L2ETHCollectFee),
L2BOBACollectFee: this._formatBigNumberToEther(this.state.L2BOBACollectFee),
L2BOBABillingCollectFee: this._formatBigNumberToEther(this.state.L2BOBABillingCollectFee),
L2ETHCollectFeeUSD: this._formatBigNumberToEtherUSD(this.state.L2ETHCollectFee, this.state.ETHUSDPrice, 2),
L2BOBACollectFeeUSD: this._formatBigNumberToEtherUSD(this.state.L2BOBACollectFee, this.state.BOBAUSDPrice, 2),
L2BOBABillingCollectFeeUSD: this._formatBigNumberToEtherUSD(this.state.L2BOBABillingCollectFee, this.state.BOBAUSDPrice, 2),
L2ETHCollectFee: this._formatBigNumberToEther(
this.state.L2ETHCollectFee
),
L2BOBACollectFee: this._formatBigNumberToEther(
this.state.L2BOBACollectFee
),
L2BOBABillingCollectFee: this._formatBigNumberToEther(
this.state.L2BOBABillingCollectFee
),
L2ETHCollectFeeUSD: this._formatBigNumberToEtherUSD(
this.state.L2ETHCollectFee,
this.state.ETHUSDPrice,
2
),
L2BOBACollectFeeUSD: this._formatBigNumberToEtherUSD(
this.state.L2BOBACollectFee,
this.state.BOBAUSDPrice,
2
),
L2BOBABillingCollectFeeUSD: this._formatBigNumberToEtherUSD(
this.state.L2BOBABillingCollectFee,
this.state.BOBAUSDPrice,
2
),
BOBAUSDPrice: Number(this.state.BOBAUSDPrice.toFixed(2)),
ETHUSDPrice: Number(this.state.ETHUSDPrice.toFixed(2)),
/* eslint-enable */
Expand Down Expand Up @@ -571,8 +619,10 @@ export class GasPriceOracleService extends BaseService<GasPriceOracleOptions> {

if (
/* eslint-disable */
targetOverheadGas > overheadProduction * (1 + this.options.overheadMinPercentChange) &&
targetOverheadGas < overheadProduction * (1 - this.options.overheadMinPercentChange) &&
targetOverheadGas >
overheadProduction * (1 + this.options.overheadMinPercentChange) &&
targetOverheadGas <
overheadProduction * (1 - this.options.overheadMinPercentChange) &&
targetOverheadGas > this.options.minOverhead
/* eslint-enable */
) {
Expand Down Expand Up @@ -621,16 +671,13 @@ export class GasPriceOracleService extends BaseService<GasPriceOracleOptions> {

private async _queryTokenPrice(tokenPair: string): Promise<void> {
if (tokenPair === 'ETH/USD') {
const latestAnswer = await this.state.BobaStraw_ETHUSD.latestAnswer()
const decimals = await this.state.BobaStraw_ETHUSD.decimals()
this.state.ETHUSDPrice = this._calculateTokenPrice(latestAnswer, decimals)
this.state.ETHUSDPrice = Number(
await this._getTokenPriceFromCoinGecko('ethereum')
)
}
if (tokenPair === 'BOBA/USD') {
const latestAnswer = await this.state.BobaStraw_BOBAUSD.latestAnswer()
const decimals = await this.state.BobaStraw_BOBAUSD.decimals()
this.state.BOBAUSDPrice = this._calculateTokenPrice(
latestAnswer,
decimals
this.state.BOBAUSDPrice = Number(
await this._getTokenPriceFromCoinGecko('boba-network')
)
}
}
Expand Down Expand Up @@ -688,8 +735,10 @@ export class GasPriceOracleService extends BaseService<GasPriceOracleOptions> {
prefixCollectFee: string = null
) {
/* eslint-disable */
const vaultBalanceName = prefixVaultBalance === null ? `${prefix}VaultBalance`: prefixVaultBalance
const collectFeeName = prefixCollectFee === null ? `${prefix}CollectFee`: prefixCollectFee
const vaultBalanceName =
prefixVaultBalance === null ? `${prefix}VaultBalance` : prefixVaultBalance
const collectFeeName =
prefixCollectFee === null ? `${prefix}CollectFee` : prefixCollectFee
/* eslint-enable */
// If vault balance is lower than the cache, it means that the vault has been drained
if (latestCollectFee.lt(vaultBalanceHistory)) {
Expand All @@ -710,13 +759,28 @@ export class GasPriceOracleService extends BaseService<GasPriceOracleOptions> {
): number {
if (decimals >= minDecimals) {
/* eslint-disable */
const tokenPriceX = tokenPrice.div(BigNumber.from(10).pow(decimals - minDecimals))
const tokenPriceX = tokenPrice.div(
BigNumber.from(10).pow(decimals - minDecimals)
)
return tokenPriceX.toNumber() / 100
/* eslint-enable */
}
return tokenPrice.toNumber() / 10 ** decimals
}

// Data provided by CoinGecko
private async _getTokenPriceFromCoinGecko(id: string): Promise<number> {
try {
const URL = `https:///api.coingecko.com/api/v3/coins/${id}?localization=false&tickers=false&community_date=false&developer_data=false&sparkline=false`
const payload = await fetch(URL)
const payloadParsed = await payload.json()
return Number(payloadParsed.market_data.current_price.usd)
} catch (err) {
this.logger.warn(`CAN\'T QUERY TOKEN PRICE ${err} - ${id} FROM CoinGecko`)
return 0
boyuan-chen marked this conversation as resolved.
Show resolved Hide resolved
}
}

private _formatBigNumberToEther(
number: BigNumber | string,
decimal = 6
Expand Down
Loading