@@ -748,12 +748,23 @@ export function createOrLoadGraphNetwork(
748748 graphNetwork . lastLengthUpdateEpoch = 0
749749 graphNetwork . lastLengthUpdateBlock = 0
750750 if ( addresses . isL1 ) {
751- graphNetwork . lastLengthUpdateBlock = blockNumber . toI32 ( ) // Use chain native block
751+ // L1 - Use chain native block
752+ graphNetwork . lastLengthUpdateBlock = blockNumber . toI32 ( )
753+ graphNetwork . currentL1BlockNumber = blockNumber
752754 } else {
753- let contract = EpochManager . bind ( changetype < Address > ( graphNetwork . epochManager ) )
755+ // L2 - Get L1 block number from EpochManager
756+ let epochManagerAddress = Address . fromString ( addresses . epochManager )
757+ let contract = EpochManager . bind ( epochManagerAddress )
754758 let response = contract . try_blockNum ( )
755759 if ( ! response . reverted ) {
756- graphNetwork . lastLengthUpdateBlock = response . value . toI32 ( ) // Use L1 block
760+ graphNetwork . lastLengthUpdateBlock = response . value . toI32 ( )
761+ graphNetwork . currentL1BlockNumber = response . value
762+ } else {
763+ graphNetwork . lastLengthUpdateBlock = 0
764+ graphNetwork . currentL1BlockNumber = BigInt . fromI32 ( 0 )
765+ log . warning ( 'Failed to query EpochManager to get L1 block number. Transaction reverted. Address used: {}' , [
766+ epochManagerAddress . toHexString ( ) ,
767+ ] )
757768 }
758769 }
759770 graphNetwork . currentEpoch = 0
@@ -793,13 +804,12 @@ export function createOrLoadGraphNetwork(
793804
794805 graphNetwork . save ( )
795806 }
796- if ( ! addresses . isL1 ) {
797- graphNetwork = updateL1BlockNumber ( graphNetwork )
798- }
799807
800- // Update epoch
801- let epoch = createOrLoadEpoch ( addresses . isL1 ? blockNumber : graphNetwork . currentL1BlockNumber ! , graphNetwork )
802- epoch . save ( )
808+ // Update epoch - only if we have a non zero L1 block number, which might be zero until contracts are deployed and the first epoch is run
809+ if ( ! graphNetwork . currentL1BlockNumber ! . equals ( BigInt . fromI32 ( 0 ) ) ) {
810+ let epoch = createOrLoadEpoch ( addresses . isL1 ? blockNumber : graphNetwork . currentL1BlockNumber ! , graphNetwork )
811+ epoch . save ( )
812+ }
803813
804814 return graphNetwork as GraphNetwork
805815}
@@ -1284,21 +1294,6 @@ export function getSubgraphID(graphAccount: Address, subgraphNumber: BigInt): Bi
12841294 return bigIntRepresentation
12851295}
12861296
1287- export function updateL1BlockNumber ( graphNetwork : GraphNetwork ) : GraphNetwork {
1288- let epochManagerAddress = Address . fromString ( addresses . epochManager )
1289- let contract = EpochManager . bind ( epochManagerAddress )
1290- let response = contract . try_blockNum ( )
1291- if ( ! response . reverted ) {
1292- graphNetwork . currentL1BlockNumber = response . value
1293- graphNetwork . save ( )
1294- } else {
1295- log . warning ( 'Failed to update L1BlockNumber. Transaction reverted. Address used: {}' , [
1296- epochManagerAddress . toHexString ( ) ,
1297- ] )
1298- }
1299- return graphNetwork
1300- }
1301-
13021297export function getAliasedL2SubgraphID ( id : BigInt ) : BigInt {
13031298 // offset === 0x1111000000000000000000000000000000000000000000000000000000001111 or "7719354826016761135949426780745810995650277145449579228033297493447455805713"
13041299 // base === 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + 1 or "115792089237316195423570985008687907853269984665640564039457584007913129639936"
0 commit comments