From 94dc063ccd21ed6296489d8e89ae03fdd1fe9ea7 Mon Sep 17 00:00:00 2001 From: Augustus <14297860+augustbleeds@users.noreply.github.com> Date: Thu, 1 Aug 2024 13:16:23 -0400 Subject: [PATCH] read pending block not latest (#465) --- relayer/pkg/chainlink/ocr2/client.go | 13 ++++++++++++- relayer/pkg/chainlink/ocr2/client_test.go | 2 ++ relayer/pkg/starknet/client.go | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/relayer/pkg/chainlink/ocr2/client.go b/relayer/pkg/chainlink/ocr2/client.go index 797e988e..d1dd2783 100644 --- a/relayer/pkg/chainlink/ocr2/client.go +++ b/relayer/pkg/chainlink/ocr2/client.go @@ -185,7 +185,18 @@ func (c *Client) LinkAvailableForPayment(ctx context.Context, address *felt.Felt } func (c *Client) fetchEventsFromBlock(ctx context.Context, address *felt.Felt, eventType string, blockNum uint64) (events []starknetrpc.EmittedEvent, err error) { - block := starknetrpc.WithBlockNumber(blockNum) + latestBlockHeight, err := c.r.LatestBlockHeight(ctx) + if err != nil { + return nil, fmt.Errorf("couldn't fetch latest block height: %w", err) + } + + var block starknetrpc.BlockID + // "blockNum" may be available on-chain, but unqueryable via the RPC because the block is pending + if blockNum > latestBlockHeight { + block = starknetrpc.WithBlockTag("pending") + } else { + block = starknetrpc.WithBlockNumber(blockNum) + } eventKey := starknetutils.GetSelectorFromNameFelt(eventType) diff --git a/relayer/pkg/chainlink/ocr2/client_test.go b/relayer/pkg/chainlink/ocr2/client_test.go index b2161aaa..cbe84a17 100644 --- a/relayer/pkg/chainlink/ocr2/client_test.go +++ b/relayer/pkg/chainlink/ocr2/client_test.go @@ -47,6 +47,8 @@ func TestOCR2Client(t *testing.T) { require.NoError(t, json.Unmarshal(req, &call)) switch call.Method { + case "starknet_blockNumber": + out = []byte(`{"result":777}`) case "starknet_chainId": out = []byte(`{"result":"0x534e5f4d41494e"}`) case "starknet_call": diff --git a/relayer/pkg/starknet/client.go b/relayer/pkg/starknet/client.go index 29ef1b03..9b473ea1 100644 --- a/relayer/pkg/starknet/client.go +++ b/relayer/pkg/starknet/client.go @@ -92,7 +92,7 @@ func (c *Client) CallContract(ctx context.Context, ops CallOps) (data []*felt.Fe Calldata: ops.Calldata, } - res, err := c.Call(ctx, tx, starknetrpc.WithBlockTag("latest")) + res, err := c.Call(ctx, tx, starknetrpc.WithBlockTag("pending")) if err != nil { return nil, fmt.Errorf("error in client.CallContract: %w", err) }