-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add approve filter (tested on chain) (#52)
* wip: add approve filter (untested) * main: register approve filter * fix: update tofrom addresses in event payload
- Loading branch information
1 parent
a291512
commit a326593
Showing
4 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |