|
| 1 | +import type { Logger } from '@aztec/aztec.js/log'; |
| 2 | +import { EthCheatCodes } from '@aztec/aztec/testing'; |
| 3 | +import { createExtendedL1Client } from '@aztec/ethereum/client'; |
| 4 | +import { RollupContract, STATE_VIEW_ADDRESS } from '@aztec/ethereum/contracts'; |
| 5 | +import { retryUntil } from '@aztec/foundation/retry'; |
| 6 | +import { DateProvider } from '@aztec/foundation/timer'; |
| 7 | + |
| 8 | +import { jest } from '@jest/globals'; |
| 9 | +import type { Anvil } from '@viem/anvil'; |
| 10 | +import { mnemonicToAccount } from 'viem/accounts'; |
| 11 | +import { foundry } from 'viem/chains'; |
| 12 | + |
| 13 | +import { MNEMONIC } from './fixtures/fixtures.js'; |
| 14 | +import { getLogger, setup, startAnvil } from './fixtures/utils.js'; |
| 15 | +import { MockStateView, diffInBps } from './shared/mock_state_view.js'; |
| 16 | + |
| 17 | +describe('FeeAssetPriceOracle E2E', () => { |
| 18 | + jest.setTimeout(300_000); |
| 19 | + |
| 20 | + let logger: Logger; |
| 21 | + let teardown: () => Promise<void>; |
| 22 | + let anvil: Anvil; |
| 23 | + let rollup: RollupContract; |
| 24 | + let mockStateView: MockStateView; |
| 25 | + let ethCheatCodes: EthCheatCodes; |
| 26 | + |
| 27 | + // Beware, if you use "mainnet" here it will be completely broken due to blobs... |
| 28 | + const chain = foundry; |
| 29 | + |
| 30 | + beforeAll(async () => { |
| 31 | + logger = getLogger(); |
| 32 | + |
| 33 | + const anvilResult = await startAnvil({ chainId: chain.id }); |
| 34 | + anvil = anvilResult.anvil; |
| 35 | + const rpcUrl = anvilResult.rpcUrl; |
| 36 | + |
| 37 | + // Set ETHEREUM_HOSTS so setup() uses our pre-started Anvil |
| 38 | + process.env.ETHEREUM_HOSTS = rpcUrl; |
| 39 | + |
| 40 | + // Deploy mock StateView BEFORE the full setup, so the oracle can read from it |
| 41 | + ethCheatCodes = new EthCheatCodes([rpcUrl], new DateProvider()); |
| 42 | + const account = mnemonicToAccount(MNEMONIC, { addressIndex: 999 }); |
| 43 | + const walletClient = createExtendedL1Client([rpcUrl], account, chain); |
| 44 | + |
| 45 | + await ethCheatCodes.setBalance(account.address, 100n * 10n ** 18n); |
| 46 | + |
| 47 | + mockStateView = await MockStateView.deploy(ethCheatCodes, walletClient, STATE_VIEW_ADDRESS); |
| 48 | + logger.info(`Deployed mock StateView at ${STATE_VIEW_ADDRESS}`); |
| 49 | + |
| 50 | + // The initial oracle price (default value) is 1e7 |
| 51 | + await mockStateView.setEthPerFeeAsset(10n ** 7n); |
| 52 | + |
| 53 | + await ethCheatCodes.mineEmptyBlock(); |
| 54 | + await ethCheatCodes.mine(10); |
| 55 | + await ethCheatCodes.mineEmptyBlock(); |
| 56 | + |
| 57 | + const context = await setup(0, { l1ChainId: chain.id, minTxsPerBlock: 0 }, {}, chain); |
| 58 | + teardown = context.teardown; |
| 59 | + |
| 60 | + const l1Client = context.deployL1ContractsValues.l1Client; |
| 61 | + rollup = new RollupContract(l1Client, context.deployL1ContractsValues.l1ContractAddresses.rollupAddress); |
| 62 | + }); |
| 63 | + |
| 64 | + afterAll(async () => { |
| 65 | + await teardown?.(); |
| 66 | + await anvil?.stop().catch(err => logger.error('Failed to stop anvil', err)); |
| 67 | + delete process.env.ETHEREUM_HOSTS; |
| 68 | + }); |
| 69 | + |
| 70 | + it('on-chain price converges toward oracle price over multiple checkpoints', async () => { |
| 71 | + // Move the price up 2.5% (2 moves of 1% and another smaller) |
| 72 | + // Wait until we are within 1 bps or the price |
| 73 | + // Then move the price down 0.5% |
| 74 | + // Wait until 1 bps of the price |
| 75 | + // Profit |
| 76 | + |
| 77 | + const targetOraclePrice = (BigInt(10n ** 7n) * 1025n) / 1000n; |
| 78 | + await mockStateView.setEthPerFeeAsset(targetOraclePrice); |
| 79 | + logger.info(`Set uniswap price to ${targetOraclePrice}`); |
| 80 | + |
| 81 | + // Get initial on-chain price |
| 82 | + const initialOnChainPrice = await rollup.getEthPerFeeAsset(); |
| 83 | + logger.info(`Initial on-chain price: ${initialOnChainPrice}, target oracle price: ${targetOraclePrice}`); |
| 84 | + |
| 85 | + await retryUntil( |
| 86 | + async () => { |
| 87 | + const currentPrice = await rollup.getEthPerFeeAsset(); |
| 88 | + logger.info(`Current on-chain price: ${currentPrice}, waiting for: ${targetOraclePrice}`); |
| 89 | + return diffInBps(currentPrice, targetOraclePrice) == 0n; |
| 90 | + }, |
| 91 | + 'price convergence toward oracle', |
| 92 | + 120, // timeout in seconds |
| 93 | + 5, // check interval in seconds |
| 94 | + ); |
| 95 | + |
| 96 | + const priceAfterFirstAlignment = await rollup.getEthPerFeeAsset(); |
| 97 | + const targetOraclePrice2 = (BigInt(priceAfterFirstAlignment) * 995n) / 1000n; |
| 98 | + await mockStateView.setEthPerFeeAsset(targetOraclePrice2); |
| 99 | + logger.info(`Set uniswap price to ${targetOraclePrice}`); |
| 100 | + |
| 101 | + await retryUntil( |
| 102 | + async () => { |
| 103 | + const currentPrice = await rollup.getEthPerFeeAsset(); |
| 104 | + logger.info(`Current on-chain price: ${currentPrice}, waiting for: ${targetOraclePrice2}`); |
| 105 | + return diffInBps(currentPrice, targetOraclePrice2) == 0n; |
| 106 | + }, |
| 107 | + 'price convergence toward oracle', |
| 108 | + 120, // timeout in seconds |
| 109 | + 5, // check interval in seconds |
| 110 | + ); |
| 111 | + |
| 112 | + const finalPrice = await rollup.getEthPerFeeAsset(); |
| 113 | + logger.info(`Final on-chain price: ${finalPrice}`); |
| 114 | + |
| 115 | + // Verify the price moved toward the oracle price |
| 116 | + expect(finalPrice).toBeGreaterThan(initialOnChainPrice); |
| 117 | + expect(diffInBps(finalPrice, targetOraclePrice2)).toBe(0n); |
| 118 | + }); |
| 119 | +}); |
0 commit comments