Skip to content

Commit

Permalink
feat: add approve filter (tested on chain) (#52)
Browse files Browse the repository at this point in the history
* wip: add approve filter (untested)

* main: register approve filter

* fix: update tofrom addresses in event payload
  • Loading branch information
kamikazechaser authored Jul 10, 2023
1 parent a291512 commit a326593
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
7 changes: 7 additions & 0 deletions cmd/service/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions cmd/service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func main() {
initGasGiftFilter(jsPub),
initTransferFilter(jsPub),
initRegisterFilter(jsPub),
initApproveFilter(jsPub),
initTokenIndexFilter(cache, jsPub),
},
Logg: lo,
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
85 changes: 85 additions & 0 deletions internal/filter/approve_filter.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit a326593

Please sign in to comment.