diff --git a/cmd/service/filters.go b/cmd/service/filters.go index ff1c3b5..2a9041c 100644 --- a/cmd/service/filters.go +++ b/cmd/service/filters.go @@ -91,6 +91,13 @@ func initRegisterFilter(pub *pub.Pub) filter.Filter { }) } +func initApproveFilter(pub *pub.Pub) filter.Filter { + return filter.NewApproveFilter(filter.ApproveFilterOpts{ + Pub: pub, + Logg: lo, + }) +} + func initTokenIndexFilter(cache *sync.Map, pub *pub.Pub) filter.Filter { return filter.NewTokenIndexFilter(filter.TokenIndexFilterOpts{ Cache: cache, diff --git a/cmd/service/main.go b/cmd/service/main.go index ea82a5f..3a7dac8 100644 --- a/cmd/service/main.go +++ b/cmd/service/main.go @@ -65,6 +65,7 @@ func main() { initGasGiftFilter(jsPub), initTransferFilter(jsPub), initRegisterFilter(jsPub), + initApproveFilter(jsPub), initTokenIndexFilter(cache, jsPub), }, Logg: lo, diff --git a/go.sum b/go.sum index 3945554..7437f5a 100644 --- a/go.sum +++ b/go.sum @@ -267,8 +267,6 @@ 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/celoutils v1.1.1 h1:REsndvfBkPN8UKOoQFNEGm/sCwKtTm+woYtgMl3bfZ0= -github.com/grassrootseconomics/celoutils v1.1.1/go.mod h1:Uo5YRy6AGLAHDZj9jaOI+AWoQ1H3L0v79728pPMkm9Q= github.com/grassrootseconomics/celoutils v1.2.1 h1:ndM4h7Df0d57m2kdRXRStrnunqOL61wQ51rnOanX1KI= github.com/grassrootseconomics/celoutils v1.2.1/go.mod h1:Uo5YRy6AGLAHDZj9jaOI+AWoQ1H3L0v79728pPMkm9Q= github.com/grassrootseconomics/w3-celo-patch v0.2.0 h1:YqibbPzX0tQKmxU1nUGzThPKk/fiYeYZY6Aif3eyu8U= diff --git a/internal/filter/approve_filter.go b/internal/filter/approve_filter.go new file mode 100644 index 0000000..10fedd9 --- /dev/null +++ b/internal/filter/approve_filter.go @@ -0,0 +1,85 @@ +package filter + +import ( + "context" + "math/big" + + "github.com/celo-org/celo-blockchain/common" + "github.com/celo-org/celo-blockchain/common/hexutil" + "github.com/grassrootseconomics/celoutils" + "github.com/grassrootseconomics/cic-chain-events/internal/pub" + "github.com/grassrootseconomics/cic-chain-events/pkg/fetch" + "github.com/grassrootseconomics/w3-celo-patch" + "github.com/zerodha/logf" +) + +type ( + ApproveFilterOpts struct { + Logg logf.Logger + Pub *pub.Pub + } + + ApproveFilter struct { + logg logf.Logger + pub *pub.Pub + } +) + +const ( + approveEventSubject = "CHAIN.approve" +) + +var ( + approveSig = w3.MustNewFunc("approve(address, uint256)", "bool") +) + +func NewApproveFilter(o ApproveFilterOpts) Filter { + return &ApproveFilter{ + logg: o.Logg, + pub: o.Pub, + } +} + +func (f *ApproveFilter) Execute(_ context.Context, transaction *fetch.Transaction) (bool, error) { + if len(transaction.InputData) < 10 { + return true, nil + } + + if transaction.InputData[:10] == "0x095ea7b3" { + var ( + address common.Address + value big.Int + ) + + if err := approveSig.DecodeArgs(w3.B(transaction.InputData), &address, &value); err != nil { + return false, err + } + + approveEvent := &pub.MinimalTxInfo{ + Block: transaction.Block.Number, + ContractAddress: celoutils.ChecksumAddress(transaction.To.Address), + Timestamp: hexutil.MustDecodeUint64(transaction.Block.Timestamp), + From: celoutils.ChecksumAddress(transaction.From.Address), + To: address.Hex(), + TxHash: transaction.Hash, + TxIndex: transaction.Index, + TXType: "approve", + } + + if transaction.Status == 1 { + approveEvent.Success = true + } + + if err := f.pub.Publish( + approveEventSubject, + transaction.Hash, + approveEvent, + ); err != nil { + return false, err + } + + return true, nil + } + + return true, nil +}