Skip to content

Commit

Permalink
Add Boba gas oracle tests (#150)
Browse files Browse the repository at this point in the history
* Add Boba gas oracle tests

* Update Github Action

Co-authored-by: CAPtheorem <79423264+CAPtheorem@users.noreply.github.com>
  • Loading branch information
boyuan-chen and CAPtheorem authored May 8, 2022
1 parent 0fd71d8 commit e2c378e
Show file tree
Hide file tree
Showing 6 changed files with 746 additions and 88 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/boba-gas-oracle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: boba-gas-oracle unit tests

on:
push:
paths:
- 'packages/boba/gas-price-oracle/**'
branches:
- 'master'
- 'develop'
- '*rc'
- 'regenesis/*'
pull_request:
paths:
- 'packages/boba/gas-price-oracle/**'
branches:
- 'master'
- 'develop'
- '*rc'
- 'regenesis/*'
workflow_dispatch:

jobs:
tests:
runs-on: ubuntu-latest
steps:
# Monorepo tests
- uses: actions/checkout@v2

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"

- uses: actions/cache@v2
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Build
working-directory: ./
run: yarn && yarn install && yarn build

- name: Run Boba gas oracle unit tests
working-directory: ./packages/boba/gas-price-oracle
run: yarn test
3 changes: 2 additions & 1 deletion packages/boba/gas-price-oracle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"clean": "rimraf dist/ ./tsconfig.build.tsbuildinfo",
"lint": "yarn lint:fix && yarn lint:check",
"lint:fix": "prettier --config .prettierrc.json --write \"{src,exec,test}/**/*.ts\"",
"lint:check": "tslint --format stylish --project ."
"lint:check": "tslint --format stylish --project .",
"test": "hardhat test --show-stack-traces"
},
"keywords": [
"optimism",
Expand Down
7 changes: 7 additions & 0 deletions packages/boba/gas-price-oracle/src/exec/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ const main = async () => {
parseFloat(env.BOBA_FEE_RATIO_MIN_PERCENT_CHANGE) || 0.05
)

// disable gasPrice=0 for local testing
const BOBA_LOCAL_TESTNET_CHAINID = config.uint(
'boba-local-testnet-chainid',
parseInt(env.BOBA_LOCAL_TESTNET_CHAINID, 10) || 31338
)

if (!GAS_PRICE_ORACLE_ADDRESS) {
throw new Error('Must pass GAS_PRICE_ORACLE_ADDRESS')
}
Expand Down Expand Up @@ -157,6 +163,7 @@ const main = async () => {
maxL1BaseFee: MAX_L1_BASE_FEE,
bobaFeeRatio100X: BOBA_FEE_RATIO_100X,
bobaFeeRatioMinPercentChange: BOBA_FEE_RATIO_MIN_PERCENT_CHANGE,
bobaLocalTestnetChainId: BOBA_LOCAL_TESTNET_CHAINID,
})

await service.start()
Expand Down
102 changes: 15 additions & 87 deletions packages/boba/gas-price-oracle/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,14 @@ import { Contract, Wallet, BigNumber, providers, utils } from 'ethers'
import fs, { promises as fsPromise } from 'fs'
import path from 'path'
import { orderBy } from 'lodash'
import fetch from 'node-fetch'

/* Imports: Internal */
import { sleep } from '@eth-optimism/core-utils'
import { BaseService } from '@eth-optimism/common-ts'
import { loadContract } from '@eth-optimism/contracts'

import L1StandardBridgeJson from '@eth-optimism/contracts/artifacts/contracts/L1/messaging/L1StandardBridge.sol/L1StandardBridge.json'
import L2GovernanceERC20Json from '@eth-optimism/contracts/artifacts/contracts/standards/L2GovernanceERC20.sol/L2GovernanceERC20.json'
import Boba_GasPriceOracleJson from '@eth-optimism/contracts/artifacts/contracts/L2/predeploys/Boba_GasPriceOracle.sol/Boba_GasPriceOracle.json'
import DiscretionaryExitBurnJson from '@boba/contracts/artifacts/contracts/DiscretionaryExitBurn.sol/DiscretionaryExitBurn.json'
import L1LiquidityPoolJson from '@boba/contracts/artifacts/contracts/LP/L1LiquidityPool.sol/L1LiquidityPool.json'
import L2LiquidityPoolJson from '@boba/contracts/artifacts/contracts/LP/L2LiquidityPool.sol/L2LiquidityPool.json'
import L1NFTBridgeJson from '@boba/contracts/artifacts/contracts/bridges/L1NFTBridge.sol/L1NFTBridge.json'
import L2NFTBridgeJson from '@boba/contracts/artifacts/contracts/bridges/L2NFTBridge.sol/L2NFTBridge.json'
import FluxAggregatorJson from '@boba/contracts/artifacts/contracts/oracle/FluxAggregator.sol/FluxAggregator.json'

interface GasPriceOracleOptions {
Expand Down Expand Up @@ -64,6 +57,9 @@ interface GasPriceOracleOptions {

// minimum percentage change for boba fee / eth fee
bobaFeeRatioMinPercentChange: number

// local testnet chain ID
bobaLocalTestnetChainId: number
}

const optionSettings = {}
Expand All @@ -76,14 +72,8 @@ export class GasPriceOracleService extends BaseService<GasPriceOracleOptions> {
private state: {
Lib_AddressManager: Contract
OVM_GasPriceOracle: Contract
Proxy__L1StandardBridge: Contract
DiscretionaryExitBurn: Contract
Proxy__L1LiquidityPool: Contract
Proxy__L2LiquidityPool: Contract
CanonicalTransactionChain: Contract
StateCommitmentChain: Contract
Proxy__L1NFTBridge: Contract
Proxy__L2NFTBridge: Contract
Boba_GasPriceOracle: Contract
BobaBillingContractAddress: string
L2BOBA: Contract
Expand All @@ -101,6 +91,7 @@ export class GasPriceOracleService extends BaseService<GasPriceOracleOptions> {
L2BOBABillingCollectFee: BigNumber
BOBAUSDPrice: number
ETHUSDPrice: number
chainID: number
}

protected async _init(): Promise<void> {
Expand All @@ -119,6 +110,7 @@ export class GasPriceOracleService extends BaseService<GasPriceOracleOptions> {
minL1BaseFee: this.options.minL1BaseFee,
bobaFeeRatio100X: this.options.bobaFeeRatio100X,
bobaFeeRatioMinPercentChange: this.options.bobaFeeRatioMinPercentChange,
bobaLocalTestnetChainId: this.options.bobaLocalTestnetChainId,
})

this.state = {} as any
Expand All @@ -133,77 +125,6 @@ export class GasPriceOracleService extends BaseService<GasPriceOracleOptions> {
address: this.state.Lib_AddressManager.address,
})

this.logger.info('Connecting to Proxy__L1StandardBridge...')
const Proxy__L1StandardBridgeAddress =
await this.state.Lib_AddressManager.getAddress('Proxy__L1StandardBridge')
this.state.Proxy__L1StandardBridge = new Contract(
Proxy__L1StandardBridgeAddress,
L1StandardBridgeJson.abi,
this.options.l1RpcProvider
)
this.logger.info('Connected to Proxy__L1StandardBridge', {
address: this.state.Proxy__L1StandardBridge.address,
})

this.logger.info('Connecting to DiscretionaryExitBurn...')
const DiscretionaryExitBurnAddress =
await this.state.Lib_AddressManager.getAddress('DiscretionaryExitBurn')
this.state.DiscretionaryExitBurn = new Contract(
DiscretionaryExitBurnAddress,
DiscretionaryExitBurnJson.abi,
this.options.gasPriceOracleOwnerWallet
)
this.logger.info('Connected to DiscretionaryExitBurn', {
address: this.state.DiscretionaryExitBurn.address,
})

this.logger.info('Connecting to Proxy__L1LiquidityPool...')
const Proxy__L1LiquidityPoolAddress =
await this.state.Lib_AddressManager.getAddress('Proxy__L1LiquidityPool')
this.state.Proxy__L1LiquidityPool = new Contract(
Proxy__L1LiquidityPoolAddress,
L1LiquidityPoolJson.abi,
this.options.l1RpcProvider
)
this.logger.info('Connected to Proxy__L1LiquidityPool', {
address: this.state.Proxy__L1LiquidityPool.address,
})

this.logger.info('Connecting to Proxy__L2LiquidityPool...')
const Proxy__L2LiquidityPoolAddress =
await this.state.Lib_AddressManager.getAddress('Proxy__L2LiquidityPool')
this.state.Proxy__L2LiquidityPool = new Contract(
Proxy__L2LiquidityPoolAddress,
L2LiquidityPoolJson.abi,
this.options.gasPriceOracleOwnerWallet
)
this.logger.info('Connected to Proxy__L2LiquidityPool', {
address: this.state.Proxy__L2LiquidityPool.address,
})

this.logger.info('Connecting to Proxy__L1NFTBridge...')
const Proxy__L1NFTBridgeAddress =
await this.state.Lib_AddressManager.getAddress('Proxy__L1NFTBridge')
this.state.Proxy__L1NFTBridge = new Contract(
Proxy__L1NFTBridgeAddress,
L1NFTBridgeJson.abi,
this.options.gasPriceOracleOwnerWallet
)
this.logger.info('Connected to Proxy__L1NFTBridge', {
address: this.state.Proxy__L1NFTBridge.address,
})

const Proxy__L2NFTBridgeAddress =
await this.state.Lib_AddressManager.getAddress('Proxy__L2NFTBridge')
this.state.Proxy__L2NFTBridge = new Contract(
Proxy__L2NFTBridgeAddress,
L2NFTBridgeJson.abi,
this.options.gasPriceOracleOwnerWallet
)
this.logger.info('Connected to Proxy__L2NFTBridge', {
address: this.state.Proxy__L2NFTBridge.address,
})

this.logger.info('Connecting to CanonicalTransactionChain...')
const CanonicalTransactionChainAddress =
await this.state.Lib_AddressManager.getAddress(
Expand Down Expand Up @@ -314,6 +235,9 @@ export class GasPriceOracleService extends BaseService<GasPriceOracleOptions> {
// Load history
await this._loadL1ETHFee()
await this._loadL2FeeCost()

// Get chain ID
this.state.chainID = (await this.options.l2RpcProvider.getNetwork()).chainId
}

protected async _start(): Promise<void> {
Expand Down Expand Up @@ -753,7 +677,9 @@ export class GasPriceOracleService extends BaseService<GasPriceOracleOptions> {
await this.state.Boba_GasPriceOracle.updatePriceRatio(
targetUpdatedPriceRatio,
targetMarketPriceRatio,
{ gasPrice: 0 }
this.state.chainID === this.options.bobaLocalTestnetChainId
? {}
: { gasPrice: 0 }
)
await gasPriceTx.wait()
this.logger.info('Updated price ratio', {
Expand Down Expand Up @@ -876,7 +802,9 @@ export class GasPriceOracleService extends BaseService<GasPriceOracleOptions> {
) {
const tx = await this.state.OVM_GasPriceOracle.setL1BaseFee(
l1GasPrice,
{ gasPrice: 0 }
this.state.chainID === this.options.bobaLocalTestnetChainId
? {}
: { gasPrice: 0 }
)
await tx.wait()
this.logger.info('Updated l1BaseFee', {
Expand All @@ -896,7 +824,7 @@ export class GasPriceOracleService extends BaseService<GasPriceOracleOptions> {
}
}

private async _queryTokenPrice(tokenPair): Promise<void> {
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()
Expand Down
Loading

0 comments on commit e2c378e

Please sign in to comment.