From de8eb31abae3393a6742d837702b13f2b1e5fb01 Mon Sep 17 00:00:00 2001 From: Mohammed Sohail Date: Mon, 16 Jan 2023 13:03:22 +0000 Subject: [PATCH] feat: init base logic for rpc fetcher --- go.mod | 1 + go.sum | 2 ++ pkg/fetch/rpc.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 pkg/fetch/rpc.go diff --git a/go.mod b/go.mod index 2f11125..596a636 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/alitto/pond v1.8.2 github.com/celo-org/celo-blockchain v1.6.1 github.com/goccy/go-json v0.10.0 + github.com/grassrootseconomics/cic-celo-sdk v0.3.1 github.com/grassrootseconomics/w3-celo-patch v0.1.0 github.com/jackc/pgx/v5 v5.2.0 github.com/knadh/goyesql/v2 v2.2.0 diff --git a/go.sum b/go.sum index af16632..3fe97c0 100644 --- a/go.sum +++ b/go.sum @@ -248,6 +248,8 @@ github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7 github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/graph-gophers/graphql-go v1.3.0/go.mod h1:9CQHMSxwO4MprSdzoIEobiHpoLtHm77vfxsvsIN5Vuc= +github.com/grassrootseconomics/cic-celo-sdk v0.3.1 h1:SzmMFrqxSIdgePqwbUdoS3PNP82MFnlOecycVk2ZYWg= +github.com/grassrootseconomics/cic-celo-sdk v0.3.1/go.mod h1:EiR6d03GYu6jlVKNL1MbTAw/bqAW2WP3J/lkrZxPMdU= github.com/grassrootseconomics/w3-celo-patch v0.1.0 h1:0fev2hYkGEyFX2D4oUG8yy4jXhtHv7qUtLLboXL5ycw= github.com/grassrootseconomics/w3-celo-patch v0.1.0/go.mod h1:JtkXc+yDUiQQJdhYTqddZI/itdYGHY7H8PNZzBo4hCk= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= diff --git a/pkg/fetch/rpc.go b/pkg/fetch/rpc.go new file mode 100644 index 0000000..33e9a67 --- /dev/null +++ b/pkg/fetch/rpc.go @@ -0,0 +1,64 @@ +package fetch + +import ( + "context" + "math/big" + + "github.com/celo-org/celo-blockchain/core/types" + celo "github.com/grassrootseconomics/cic-celo-sdk" + "github.com/grassrootseconomics/w3-celo-patch/module/eth" + "github.com/grassrootseconomics/w3-celo-patch/w3types" +) + +type RPCOpts struct { + RPCProvider *celo.Provider +} + +type RPC struct { + provider *celo.Provider +} + +func NewRPCFetcher(o RPCOpts) Fetch { + return &RPC{ + provider: o.RPCProvider, + } +} + +// This method makes 2 calls. 1 for the block and 1 batched call for txs + receipts. +// Should work on free tier RPC services. +func (f *RPC) Block(ctx context.Context, blockNumber uint64) (FetchResponse, error) { + var ( + block types.Block + + fetchResponse FetchResponse + ) + + if err := f.provider.Client.CallCtx( + ctx, + eth.BlockByNumber(big.NewInt(int64(blockNumber))).Returns(&block), + ); err != nil { + return FetchResponse{}, nil + } + + txCount := len(block.Transactions()) + batchCalls := make([]w3types.Caller, txCount*2) + + txs := make([]types.Transaction, txCount) + txsReceipt := make([]types.Receipt, txCount) + + for i, tx := range block.Transactions() { + batchCalls[i] = eth.Tx(tx.Hash()).Returns(&txs[i]) + batchCalls[txCount+i] = eth.TxReceipt(tx.Hash()).Returns(&txsReceipt[i]) + } + + if err := f.provider.Client.CallCtx( + ctx, + batchCalls..., + ); err != nil { + return FetchResponse{}, nil + } + + // TODO: Create FetchResponse + + return fetchResponse, nil +}