Skip to content

Commit

Permalink
read pending block not latest (#465) (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
augustbleeds authored Aug 1, 2024
1 parent 6f39681 commit d35782c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
13 changes: 12 additions & 1 deletion relayer/pkg/chainlink/ocr2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions relayer/pkg/chainlink/ocr2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
2 changes: 1 addition & 1 deletion relayer/pkg/starknet/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit d35782c

Please sign in to comment.