1- import { smock } from '@defi-wonderland/smock'
2- import { deploy , DeployType , GraphNetworkContracts , helpers , toBN , toGRT } from '@graphprotocol/sdk'
3- import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'
1+ import { FakeContract , smock } from '@defi-wonderland/smock'
42import { expect , use } from 'chai'
53import { constants , ContractTransaction , Signer , utils , Wallet } from 'ethers'
64import hre from 'hardhat'
75
8- // Define types for the contracts we'll use in the tests
9- type L2GraphToken = any
10- type L2GraphTokenGateway = any
11- type CallhookReceiverMock = any
12- type GraphToken = any
13- type L1GraphTokenGateway = any
14- type RewardsManager = any
15-
6+ import { CallhookReceiverMock } from '../../../build/types/CallhookReceiverMock'
7+ import { L2GraphToken } from '../../../build/types/L2GraphToken'
8+ import { L2GraphTokenGateway } from '../../../build/types/L2GraphTokenGateway'
169import { NetworkFixture } from '../lib/fixtures'
17- import { L2ArbitrumMessengerMock } from './l2ArbitrumMessengerMock'
1810
19- // Initialize smock before using it
20- smock . fake // This ensures smock is initialized
2111use ( smock . matchers )
2212
23- // Mock the L2ArbitrumMessenger.sendTxToL1 function
24- // This is done by monkey-patching the L2ArbitrumMessenger contract
25- // We'll use this to verify that the function was called with the correct parameters
26- L2ArbitrumMessengerMock . reset ( )
13+ import { deploy , DeployType , GraphNetworkContracts , helpers , toBN , toGRT } from '@graphprotocol/sdk'
14+ import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'
15+
16+ import { GraphToken , L1GraphTokenGateway } from '../../../build/types'
17+ import { RewardsManager } from '../../../build/types/RewardsManager'
2718
2819const { AddressZero } = constants
2920
3021describe ( 'L2GraphTokenGateway' , ( ) => {
31- // Remove the graph function call as it's not available in this environment
22+ const graph = hre . graph ( )
3223 let me : SignerWithAddress
3324 let governor : SignerWithAddress
3425 let tokenSender : SignerWithAddress
3526 let l1Receiver : SignerWithAddress
3627 let l2Receiver : SignerWithAddress
3728 let pauseGuardian : SignerWithAddress
3829 let fixture : NetworkFixture
30+ let arbSysMock : FakeContract
3931
4032 let fixtureContracts : GraphNetworkContracts
4133 let l1MockContracts : GraphNetworkContracts
@@ -56,10 +48,9 @@ describe('L2GraphTokenGateway', () => {
5648 )
5749
5850 before ( async function ( ) {
59- // Get test accounts directly from ethers
60- ; [ me , governor , tokenSender , l1Receiver , l2Receiver , pauseGuardian ] = await hre . ethers . getSigners ( )
51+ ; [ me , governor , tokenSender , l1Receiver , l2Receiver , pauseGuardian ] = await graph . getTestAccounts ( )
6152
62- fixture = new NetworkFixture ( hre . ethers . provider )
53+ fixture = new NetworkFixture ( graph . provider )
6354
6455 // Deploy L2
6556 fixtureContracts = await fixture . load ( governor , true )
@@ -84,24 +75,23 @@ describe('L2GraphTokenGateway', () => {
8475 // Give some funds to the token sender and router mock
8576 await grt . connect ( governor ) . mint ( tokenSender . address , senderTokens )
8677 await helpers . setBalance ( routerMock . address , utils . parseEther ( '1' ) )
87-
88- // Deploy the ArbSys mock at the correct address
89- const ArbSysMockFactory = await hre . ethers . getContractFactory ( 'ArbSysMock' )
90- const arbSysMock = await ArbSysMockFactory . deploy ( )
91- await arbSysMock . deployed ( )
92-
93- // Get the bytecode of the deployed contract
94- const code = await hre . ethers . provider . getCode ( arbSysMock . address )
95-
96- // Set the code at the ArbSys precompile address
97- const ARB_SYS_ADDRESS = '0x0000000000000000000000000000000000000064'
98- await hre . ethers . provider . send ( 'hardhat_setCode' , [ ARB_SYS_ADDRESS , code ] )
9978 } )
10079
10180 beforeEach ( async function ( ) {
10281 await fixture . setUp ( )
103- // Reset the L2ArbitrumMessengerMock for each test
104- L2ArbitrumMessengerMock . reset ( )
82+ // Thanks to Livepeer: https://github.com/livepeer/arbitrum-lpt-bridge/blob/main/test/unit/L2/l2LPTGateway.test.ts#L86
83+ // Skip smock setup when running under coverage due to provider compatibility issues
84+ const isRunningUnderCoverage =
85+ hre . network . name === 'coverage' ||
86+ process . env . SOLIDITY_COVERAGE === 'true' ||
87+ process . env . npm_lifecycle_event === 'test:coverage'
88+
89+ if ( ! isRunningUnderCoverage ) {
90+ arbSysMock = await smock . fake ( 'ArbSys' , {
91+ address : '0x0000000000000000000000000000000000000064' ,
92+ } )
93+ arbSysMock . sendTxToL1 . returns ( 1 )
94+ }
10595 } )
10696
10797 afterEach ( async function ( ) {
@@ -242,10 +232,20 @@ describe('L2GraphTokenGateway', () => {
242232 . emit ( l2GraphTokenGateway , 'TxToL1' )
243233 . withArgs ( tokenSender . address , l1GRTGatewayMock . address , 1 , calldata )
244234
245- // We verify the TxToL1 event was emitted with the correct parameters
246- // This is sufficient to verify that sendTxToL1 was called correctly
247- // The event is emitted in the sendTxToL1 function of L2ArbitrumMessenger
235+ // For some reason the call count doesn't work properly,
236+ // and each function call is counted 12 times.
237+ // Possibly related to https://github.com/defi-wonderland/smock/issues/85 ?
238+ // expect(arbSysMock.sendTxToL1).to.have.been.calledOnce
239+
240+ // Only check smock expectations when not running under coverage
241+ const isRunningUnderCoverage =
242+ hre . network . name === 'coverage' ||
243+ process . env . SOLIDITY_COVERAGE === 'true' ||
244+ process . env . npm_lifecycle_event === 'test:coverage'
248245
246+ if ( ! isRunningUnderCoverage && arbSysMock ) {
247+ expect ( arbSysMock . sendTxToL1 ) . to . have . been . calledWith ( l1GRTGatewayMock . address , calldata )
248+ }
249249 const senderBalance = await grt . balanceOf ( tokenSender . address )
250250 expect ( senderBalance ) . eq ( toGRT ( '990' ) )
251251 }
@@ -273,26 +273,12 @@ describe('L2GraphTokenGateway', () => {
273273 } )
274274 it ( 'burns tokens and triggers an L1 call' , async function ( ) {
275275 await grt . connect ( tokenSender ) . approve ( l2GraphTokenGateway . address , toGRT ( '10' ) )
276-
277- // Execute the transfer
278276 await testValidOutboundTransfer ( tokenSender , defaultData )
279-
280- // The event checks are already done in testValidOutboundTransfer
281- // We just need to verify the balance change
282- const senderBalance = await grt . balanceOf ( tokenSender . address )
283- expect ( senderBalance ) . eq ( toGRT ( '990' ) )
284277 } )
285278 it ( 'decodes the sender address from messages sent by the router' , async function ( ) {
286279 await grt . connect ( tokenSender ) . approve ( l2GraphTokenGateway . address , toGRT ( '10' ) )
287280 const routerEncodedData = utils . defaultAbiCoder . encode ( [ 'address' , 'bytes' ] , [ tokenSender . address , defaultData ] )
288-
289- // Execute the transfer
290281 await testValidOutboundTransfer ( routerMock , routerEncodedData )
291-
292- // The event checks are already done in testValidOutboundTransfer
293- // We just need to verify the balance change
294- const senderBalance = await grt . balanceOf ( tokenSender . address )
295- expect ( senderBalance ) . eq ( toGRT ( '990' ) )
296282 } )
297283 it ( 'reverts when called with nonempty calldata' , async function ( ) {
298284 await grt . connect ( tokenSender ) . approve ( l2GraphTokenGateway . address , toGRT ( '10' ) )
0 commit comments