mirror of
https://github.com/grassrootseconomics/cic-chain-events.git
synced 2024-11-21 15:26:46 +01:00
feat: add approve filter (tested on chain) (#52)
* wip: add approve filter (untested) * main: register approve filter * fix: update tofrom addresses in event payload
This commit is contained in:
parent
a291512d95
commit
a326593b21
@ -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,
|
||||
|
@ -65,6 +65,7 @@ func main() {
|
||||
initGasGiftFilter(jsPub),
|
||||
initTransferFilter(jsPub),
|
||||
initRegisterFilter(jsPub),
|
||||
initApproveFilter(jsPub),
|
||||
initTokenIndexFilter(cache, jsPub),
|
||||
},
|
||||
Logg: lo,
|
||||
|
2
go.sum
2
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=
|
||||
|
85
internal/filter/approve_filter.go
Normal file
85
internal/filter/approve_filter.go
Normal file
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user