mirror of
https://github.com/grassrootseconomics/eth-tracker.git
synced 2026-05-18 18:55:19 +02:00
release: v1.0.0-rc
This commit is contained in:
94
internal/router/faucet_give.go
Normal file
94
internal/router/faucet_give.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math/big"
|
||||
|
||||
"github.com/celo-org/celo-blockchain/common"
|
||||
"github.com/grassrootseconomics/celo-tracker/pkg/event"
|
||||
"github.com/grassrootseconomics/celoutils/v3"
|
||||
"github.com/grassrootseconomics/w3-celo"
|
||||
)
|
||||
|
||||
type faucetGiveHandler struct{}
|
||||
|
||||
const faucetGiveEventName = "FAUCET_GIVE"
|
||||
|
||||
var (
|
||||
_ Handler = (*faucetGiveHandler)(nil)
|
||||
|
||||
faucetGiveEvent = w3.MustNewEvent("Give(address indexed _recipient, address indexed _token, uint256 _amount)")
|
||||
faucetGiveToSig = w3.MustNewFunc("giveTo(address)", "uint256")
|
||||
faucetGimmeSig = w3.MustNewFunc("gimme()", "uint256")
|
||||
)
|
||||
|
||||
func (h *faucetGiveHandler) Name() string {
|
||||
return faucetGiveEventName
|
||||
}
|
||||
|
||||
func (h *faucetGiveHandler) SuccessTx(ctx context.Context, tx SuccessTx, pubCB PubCallback) error {
|
||||
var (
|
||||
recipient common.Address
|
||||
token common.Address
|
||||
amount big.Int
|
||||
)
|
||||
|
||||
if err := faucetGiveEvent.DecodeArgs(tx.Log, &recipient, &token, &amount); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
faucetGiveEvent := event.Event{
|
||||
Index: tx.Log.Index,
|
||||
Block: tx.Log.BlockNumber,
|
||||
ContractAddress: tx.Log.Address.Hex(),
|
||||
Success: true,
|
||||
Timestamp: tx.Timestamp,
|
||||
TxHash: tx.Log.TxHash.Hex(),
|
||||
TxType: faucetGiveEventName,
|
||||
Payload: map[string]any{
|
||||
"recipient": recipient.Hex(),
|
||||
"token": token.Hex(),
|
||||
"amount": amount.String(),
|
||||
},
|
||||
}
|
||||
|
||||
return pubCB(ctx, faucetGiveEvent)
|
||||
}
|
||||
|
||||
func (h *faucetGiveHandler) RevertTx(ctx context.Context, tx RevertTx, pubCB PubCallback) error {
|
||||
faucetGiveEvent := event.Event{
|
||||
Block: tx.Block,
|
||||
ContractAddress: tx.ContractAddress,
|
||||
Success: false,
|
||||
Timestamp: tx.Timestamp,
|
||||
TxHash: tx.TxHash,
|
||||
TxType: faucetGiveEventName,
|
||||
}
|
||||
|
||||
switch tx.InputData[:8] {
|
||||
case "63e4bff4":
|
||||
var to common.Address
|
||||
|
||||
if err := faucetGiveToSig.DecodeArgs(w3.B(tx.InputData), &to); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
faucetGiveEvent.Payload = map[string]any{
|
||||
"recipient": to.Hex(),
|
||||
"token": celoutils.ZeroAddress,
|
||||
"amount": "0",
|
||||
}
|
||||
|
||||
return pubCB(ctx, faucetGiveEvent)
|
||||
case "de82efb4":
|
||||
faucetGiveEvent.Payload = map[string]any{
|
||||
"recipient": celoutils.ZeroAddress,
|
||||
"token": celoutils.ZeroAddress,
|
||||
"amount": "0",
|
||||
}
|
||||
|
||||
return pubCB(ctx, faucetGiveEvent)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
95
internal/router/index_add.go
Normal file
95
internal/router/index_add.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/celo-org/celo-blockchain/common"
|
||||
"github.com/grassrootseconomics/celo-tracker/internal/cache"
|
||||
"github.com/grassrootseconomics/celo-tracker/pkg/event"
|
||||
"github.com/grassrootseconomics/w3-celo"
|
||||
)
|
||||
|
||||
type indexAddHandler struct {
|
||||
cache cache.Cache
|
||||
}
|
||||
|
||||
const indexAddEventName = "INDEX_ADD"
|
||||
|
||||
var (
|
||||
_ Handler = (*indexAddHandler)(nil)
|
||||
|
||||
indexAddEvent = w3.MustNewEvent("AddressAdded(address _token)")
|
||||
indexAddSig = w3.MustNewFunc("add(address)", "bool")
|
||||
indexRegisterSig = w3.MustNewFunc("register(address)", "bool")
|
||||
)
|
||||
|
||||
func (h *indexAddHandler) Name() string {
|
||||
return indexAddEventName
|
||||
}
|
||||
|
||||
func (h *indexAddHandler) SuccessTx(ctx context.Context, tx SuccessTx, pubCB PubCallback) error {
|
||||
var address common.Address
|
||||
|
||||
if err := indexAddEvent.DecodeArgs(tx.Log, &address); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
indexAddEvent := event.Event{
|
||||
Index: tx.Log.Index,
|
||||
Block: tx.Log.BlockNumber,
|
||||
ContractAddress: tx.Log.Address.Hex(),
|
||||
Success: true,
|
||||
Timestamp: tx.Timestamp,
|
||||
TxHash: tx.Log.TxHash.Hex(),
|
||||
TxType: indexAddEventName,
|
||||
Payload: map[string]any{
|
||||
"address": address.Hex(),
|
||||
},
|
||||
}
|
||||
|
||||
if err := h.cache.Add(ctx, address.Hex()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return pubCB(ctx, indexAddEvent)
|
||||
}
|
||||
|
||||
func (h *indexAddHandler) RevertTx(ctx context.Context, tx RevertTx, pubCB PubCallback) error {
|
||||
indexAddEvent := event.Event{
|
||||
Block: tx.Block,
|
||||
ContractAddress: tx.ContractAddress,
|
||||
Success: false,
|
||||
Timestamp: tx.Timestamp,
|
||||
TxHash: tx.TxHash,
|
||||
TxType: indexAddEventName,
|
||||
}
|
||||
|
||||
switch tx.InputData[:8] {
|
||||
case "0a3b0a4f":
|
||||
var address common.Address
|
||||
|
||||
indexAddEvent.Payload = map[string]any{
|
||||
"address": address.Hex(),
|
||||
}
|
||||
|
||||
if err := indexAddSig.DecodeArgs(w3.B(tx.InputData), &address); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return pubCB(ctx, indexAddEvent)
|
||||
case "4420e486":
|
||||
var address common.Address
|
||||
|
||||
indexAddEvent.Payload = map[string]any{
|
||||
"address": address.Hex(),
|
||||
}
|
||||
|
||||
if err := indexRegisterSig.DecodeArgs(w3.B(tx.InputData), &address); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return pubCB(ctx, indexAddEvent)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
77
internal/router/index_remove.go
Normal file
77
internal/router/index_remove.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/celo-org/celo-blockchain/common"
|
||||
"github.com/grassrootseconomics/celo-tracker/internal/cache"
|
||||
"github.com/grassrootseconomics/celo-tracker/pkg/event"
|
||||
"github.com/grassrootseconomics/w3-celo"
|
||||
)
|
||||
|
||||
type indexRemoveHandler struct {
|
||||
cache cache.Cache
|
||||
}
|
||||
|
||||
const indexRemoveEventName = "INDEX_REMOVE"
|
||||
|
||||
var (
|
||||
_ Handler = (*indexRemoveHandler)(nil)
|
||||
|
||||
indexRemoveEvent = w3.MustNewEvent("AddressRemoved(address _token)")
|
||||
indexRemoveSig = w3.MustNewFunc("remove(address)", "bool")
|
||||
)
|
||||
|
||||
func (h *indexRemoveHandler) Name() string {
|
||||
return indexRemoveEventName
|
||||
}
|
||||
|
||||
func (h *indexRemoveHandler) SuccessTx(ctx context.Context, tx SuccessTx, pubCB PubCallback) error {
|
||||
var address common.Address
|
||||
|
||||
if err := indexRemoveEvent.DecodeArgs(tx.Log, &address); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
indexRemoveEvent := event.Event{
|
||||
Index: tx.Log.Index,
|
||||
Block: tx.Log.BlockNumber,
|
||||
ContractAddress: tx.Log.Address.Hex(),
|
||||
Success: true,
|
||||
Timestamp: tx.Timestamp,
|
||||
TxHash: tx.Log.TxHash.Hex(),
|
||||
TxType: indexRemoveEventName,
|
||||
Payload: map[string]any{
|
||||
"address": address.Hex(),
|
||||
},
|
||||
}
|
||||
|
||||
if err := h.cache.Remove(ctx, address.Hex()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return pubCB(ctx, indexRemoveEvent)
|
||||
|
||||
}
|
||||
|
||||
func (h *indexRemoveHandler) RevertTx(ctx context.Context, tx RevertTx, pubCB PubCallback) error {
|
||||
var address common.Address
|
||||
|
||||
if err := indexRemoveSig.DecodeArgs(w3.B(tx.InputData), &address); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
indexRemoveEvent := event.Event{
|
||||
Block: tx.Block,
|
||||
ContractAddress: tx.ContractAddress,
|
||||
Success: false,
|
||||
Timestamp: tx.Timestamp,
|
||||
TxHash: tx.TxHash,
|
||||
TxType: indexRemoveEventName,
|
||||
Payload: map[string]any{
|
||||
"address": address.Hex(),
|
||||
},
|
||||
}
|
||||
|
||||
return pubCB(ctx, indexRemoveEvent)
|
||||
}
|
||||
77
internal/router/ownership.go
Normal file
77
internal/router/ownership.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/celo-org/celo-blockchain/common"
|
||||
"github.com/grassrootseconomics/celo-tracker/pkg/event"
|
||||
"github.com/grassrootseconomics/w3-celo"
|
||||
)
|
||||
|
||||
type ownershipHandler struct{}
|
||||
|
||||
const (
|
||||
ownershipEventName = "OWNERSHIP_TRANSFERRED"
|
||||
)
|
||||
|
||||
var (
|
||||
_ Handler = (*ownershipHandler)(nil)
|
||||
|
||||
ownershipEvent = w3.MustNewEvent("OwnershipTransferred(address indexed previousOwner, address indexed newOwner)")
|
||||
ownershipToSig = w3.MustNewFunc("transferOwnership(address)", "bool")
|
||||
)
|
||||
|
||||
func (h *ownershipHandler) Name() string {
|
||||
return ownershipEventName
|
||||
}
|
||||
|
||||
func (h *ownershipHandler) SuccessTx(ctx context.Context, tx SuccessTx, pubCB PubCallback) error {
|
||||
var (
|
||||
previousOwner common.Address
|
||||
newOwner common.Address
|
||||
)
|
||||
|
||||
if err := ownershipEvent.DecodeArgs(tx.Log, &previousOwner, &newOwner); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ownershipEvent := event.Event{
|
||||
Index: tx.Log.Index,
|
||||
Block: tx.Log.BlockNumber,
|
||||
ContractAddress: tx.Log.Address.Hex(),
|
||||
Success: true,
|
||||
Timestamp: tx.Timestamp,
|
||||
TxHash: tx.Log.TxHash.Hex(),
|
||||
TxType: ownershipEventName,
|
||||
Payload: map[string]any{
|
||||
"previousOwner": previousOwner.Hex(),
|
||||
"newOwner": newOwner.Hex(),
|
||||
},
|
||||
}
|
||||
|
||||
return pubCB(ctx, ownershipEvent)
|
||||
}
|
||||
|
||||
func (h *ownershipHandler) RevertTx(ctx context.Context, tx RevertTx, pubCB PubCallback) error {
|
||||
|
||||
var newOwner common.Address
|
||||
|
||||
if err := ownershipToSig.DecodeArgs(w3.B(tx.InputData), &newOwner); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ownershipEvent := event.Event{
|
||||
Block: tx.Block,
|
||||
ContractAddress: tx.ContractAddress,
|
||||
Success: false,
|
||||
Timestamp: tx.Timestamp,
|
||||
TxHash: tx.TxHash,
|
||||
TxType: ownershipEventName,
|
||||
Payload: map[string]any{
|
||||
"previousOwner": tx.From,
|
||||
"newOwner": newOwner.Hex(),
|
||||
},
|
||||
}
|
||||
|
||||
return pubCB(ctx, ownershipEvent)
|
||||
}
|
||||
86
internal/router/pool_deposit.go
Normal file
86
internal/router/pool_deposit.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math/big"
|
||||
|
||||
"github.com/celo-org/celo-blockchain/common"
|
||||
"github.com/grassrootseconomics/celo-tracker/pkg/event"
|
||||
"github.com/grassrootseconomics/w3-celo"
|
||||
)
|
||||
|
||||
type poolDepositHandler struct{}
|
||||
|
||||
const poolDepositEventName = "POOL_DEPOSIT"
|
||||
|
||||
var (
|
||||
_ Handler = (*poolDepositHandler)(nil)
|
||||
|
||||
poolDepositEvent = w3.MustNewEvent("Deposit(address indexed initiator, address indexed tokenIn, uint256 amountIn)")
|
||||
poolDepositSig = w3.MustNewFunc("deposit(address, uint256)", "")
|
||||
)
|
||||
|
||||
func (h *poolDepositHandler) Name() string {
|
||||
return poolDepositEventName
|
||||
}
|
||||
|
||||
func (h *poolDepositHandler) SuccessTx(ctx context.Context, tx SuccessTx, pubCB PubCallback) error {
|
||||
var (
|
||||
initiator common.Address
|
||||
tokenIn common.Address
|
||||
amountIn big.Int
|
||||
)
|
||||
|
||||
if err := poolDepositEvent.DecodeArgs(
|
||||
tx.Log,
|
||||
&initiator,
|
||||
&tokenIn,
|
||||
&amountIn,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
poolDepositEvent := event.Event{
|
||||
Index: tx.Log.Index,
|
||||
Block: tx.Log.BlockNumber,
|
||||
ContractAddress: tx.Log.Address.Hex(),
|
||||
Success: true,
|
||||
Timestamp: tx.Timestamp,
|
||||
TxHash: tx.Log.TxHash.Hex(),
|
||||
TxType: poolDepositEventName,
|
||||
Payload: map[string]any{
|
||||
"initiator": initiator.Hex(),
|
||||
"tokenIn": tokenIn.Hex(),
|
||||
"amountIn": amountIn.String(),
|
||||
},
|
||||
}
|
||||
|
||||
return pubCB(ctx, poolDepositEvent)
|
||||
}
|
||||
|
||||
func (h *poolDepositHandler) RevertTx(ctx context.Context, tx RevertTx, pubCB PubCallback) error {
|
||||
var (
|
||||
tokenIn common.Address
|
||||
amountIn big.Int
|
||||
)
|
||||
|
||||
if err := poolDepositSig.DecodeArgs(w3.B(tx.InputData), &tokenIn, &amountIn); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
poolDepositEvent := event.Event{
|
||||
Block: tx.Block,
|
||||
ContractAddress: tx.ContractAddress,
|
||||
Success: false,
|
||||
Timestamp: tx.Timestamp,
|
||||
TxHash: tx.TxHash,
|
||||
TxType: poolDepositEventName,
|
||||
Payload: map[string]any{
|
||||
"initiator": tx.From,
|
||||
"tokenIn": tokenIn.Hex(),
|
||||
"amountIn": amountIn.String(),
|
||||
},
|
||||
}
|
||||
|
||||
return pubCB(ctx, poolDepositEvent)
|
||||
}
|
||||
99
internal/router/pool_swap.go
Normal file
99
internal/router/pool_swap.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math/big"
|
||||
|
||||
"github.com/celo-org/celo-blockchain/common"
|
||||
"github.com/grassrootseconomics/celo-tracker/pkg/event"
|
||||
"github.com/grassrootseconomics/w3-celo"
|
||||
)
|
||||
|
||||
type poolSwapHandler struct{}
|
||||
|
||||
const poolSwapEventName = "POOL_SWAP"
|
||||
|
||||
var (
|
||||
_ Handler = (*poolSwapHandler)(nil)
|
||||
|
||||
poolSwapEvent = w3.MustNewEvent("Swap(address indexed initiator, address indexed tokenIn, address tokenOut, uint256 amountIn, uint256 amountOut, uint256 fee)")
|
||||
poolSwapSig = w3.MustNewFunc("withdraw(address, address, uint256)", "")
|
||||
)
|
||||
|
||||
func (h *poolSwapHandler) Name() string {
|
||||
return poolSwapEventName
|
||||
}
|
||||
|
||||
func (h *poolSwapHandler) SuccessTx(ctx context.Context, tx SuccessTx, pubCB PubCallback) error {
|
||||
var (
|
||||
initiator common.Address
|
||||
tokenIn common.Address
|
||||
tokenOut common.Address
|
||||
amountIn big.Int
|
||||
amountOut big.Int
|
||||
fee big.Int
|
||||
)
|
||||
|
||||
if err := poolSwapEvent.DecodeArgs(
|
||||
tx.Log,
|
||||
&initiator,
|
||||
&tokenIn,
|
||||
&tokenOut,
|
||||
&amountIn,
|
||||
&amountOut,
|
||||
&fee,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
poolSwapEvent := event.Event{
|
||||
Index: tx.Log.Index,
|
||||
Block: tx.Log.BlockNumber,
|
||||
ContractAddress: tx.Log.Address.Hex(),
|
||||
Success: true,
|
||||
Timestamp: tx.Timestamp,
|
||||
TxHash: tx.Log.TxHash.Hex(),
|
||||
TxType: poolSwapEventName,
|
||||
Payload: map[string]any{
|
||||
"initiator": initiator.Hex(),
|
||||
"tokenIn": tokenIn.Hex(),
|
||||
"tokenOut": tokenOut.Hex(),
|
||||
"amountIn": amountIn.String(),
|
||||
"amountOut": amountOut.String(),
|
||||
"fee": fee.String(),
|
||||
},
|
||||
}
|
||||
|
||||
return pubCB(ctx, poolSwapEvent)
|
||||
}
|
||||
|
||||
func (h *poolSwapHandler) RevertTx(ctx context.Context, tx RevertTx, pubCB PubCallback) error {
|
||||
var (
|
||||
tokenOut common.Address
|
||||
tokenIn common.Address
|
||||
amountIn big.Int
|
||||
)
|
||||
|
||||
if err := poolSwapSig.DecodeArgs(w3.B(tx.InputData), &tokenOut, &tokenIn, &amountIn); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
poolSwapEvent := event.Event{
|
||||
Block: tx.Block,
|
||||
ContractAddress: tx.ContractAddress,
|
||||
Success: false,
|
||||
Timestamp: tx.Timestamp,
|
||||
TxHash: tx.TxHash,
|
||||
TxType: poolSwapEventName,
|
||||
Payload: map[string]any{
|
||||
"initiator": tx.From,
|
||||
"tokenIn": tokenIn.Hex(),
|
||||
"tokenOut": tokenOut.Hex(),
|
||||
"amountIn": amountIn.String(),
|
||||
"amountOut": "0",
|
||||
"fee": "0",
|
||||
},
|
||||
}
|
||||
|
||||
return pubCB(ctx, poolSwapEvent)
|
||||
}
|
||||
78
internal/router/quoter_price.go
Normal file
78
internal/router/quoter_price.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math/big"
|
||||
|
||||
"github.com/celo-org/celo-blockchain/common"
|
||||
"github.com/grassrootseconomics/celo-tracker/pkg/event"
|
||||
"github.com/grassrootseconomics/w3-celo"
|
||||
)
|
||||
|
||||
type quoterPriceHandler struct{}
|
||||
|
||||
const quoterPriceEventName = "QUOTER_PRICE_INDEX_UPDATED"
|
||||
|
||||
var (
|
||||
_ Handler = (*quoterPriceHandler)(nil)
|
||||
|
||||
quoterPriceEvent = w3.MustNewEvent("PriceIndexUpdated(address _tokenAddress, uint256 _exchangeRate)")
|
||||
quoterPriceToSig = w3.MustNewFunc("setPriceIndexValue(address, uint256)", "uint256")
|
||||
)
|
||||
|
||||
func (h *quoterPriceHandler) Name() string {
|
||||
return quoterPriceEventName
|
||||
}
|
||||
|
||||
func (h *quoterPriceHandler) SuccessTx(ctx context.Context, tx SuccessTx, pubCB PubCallback) error {
|
||||
var (
|
||||
token common.Address
|
||||
exchangeRate big.Int
|
||||
)
|
||||
|
||||
if err := quoterPriceEvent.DecodeArgs(tx.Log, &token, &exchangeRate); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
quoterPriceEvent := event.Event{
|
||||
Index: tx.Log.Index,
|
||||
Block: tx.Log.BlockNumber,
|
||||
ContractAddress: tx.Log.Address.Hex(),
|
||||
Success: true,
|
||||
Timestamp: tx.Timestamp,
|
||||
TxHash: tx.Log.TxHash.Hex(),
|
||||
TxType: quoterPriceEventName,
|
||||
Payload: map[string]any{
|
||||
"token": token.Hex(),
|
||||
"exchangeRate": exchangeRate.String(),
|
||||
},
|
||||
}
|
||||
|
||||
return pubCB(ctx, quoterPriceEvent)
|
||||
}
|
||||
|
||||
func (h *quoterPriceHandler) RevertTx(ctx context.Context, tx RevertTx, pubCB PubCallback) error {
|
||||
var (
|
||||
token common.Address
|
||||
exchangeRate big.Int
|
||||
)
|
||||
|
||||
if err := quoterPriceToSig.DecodeArgs(w3.B(tx.InputData), &token, &exchangeRate); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
quoterPriceEvent := event.Event{
|
||||
Block: tx.Block,
|
||||
ContractAddress: tx.ContractAddress,
|
||||
Success: false,
|
||||
Timestamp: tx.Timestamp,
|
||||
TxHash: tx.TxHash,
|
||||
TxType: quoterPriceEventName,
|
||||
Payload: map[string]any{
|
||||
"token": token.Hex(),
|
||||
"exchangeRate": exchangeRate.String(),
|
||||
},
|
||||
}
|
||||
|
||||
return pubCB(ctx, quoterPriceEvent)
|
||||
}
|
||||
123
internal/router/router.go
Normal file
123
internal/router/router.go
Normal file
@@ -0,0 +1,123 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/celo-org/celo-blockchain/common"
|
||||
"github.com/celo-org/celo-blockchain/core/types"
|
||||
"github.com/grassrootseconomics/celo-tracker/internal/cache"
|
||||
"github.com/grassrootseconomics/celo-tracker/internal/pub"
|
||||
"github.com/grassrootseconomics/celo-tracker/pkg/event"
|
||||
"github.com/grassrootseconomics/w3-celo"
|
||||
)
|
||||
|
||||
type (
|
||||
PubCallback func(context.Context, event.Event) error
|
||||
|
||||
SuccessTx struct {
|
||||
Log *types.Log
|
||||
Timestamp uint64
|
||||
}
|
||||
|
||||
RevertTx struct {
|
||||
From string
|
||||
InputData string
|
||||
Block uint64
|
||||
ContractAddress string
|
||||
Timestamp uint64
|
||||
TxHash string
|
||||
}
|
||||
|
||||
Handler interface {
|
||||
Name() string
|
||||
SuccessTx(context.Context, SuccessTx, PubCallback) error
|
||||
RevertTx(context.Context, RevertTx, PubCallback) error
|
||||
}
|
||||
|
||||
RouterOpts struct {
|
||||
Pub pub.Pub
|
||||
Cache cache.Cache
|
||||
}
|
||||
|
||||
Router struct {
|
||||
pub pub.Pub
|
||||
cache cache.Cache
|
||||
logHandlers map[common.Hash]Handler
|
||||
inputDataHandlers map[string]Handler
|
||||
}
|
||||
)
|
||||
|
||||
func New(o RouterOpts) *Router {
|
||||
var (
|
||||
indexAddHandler *indexAddHandler = &indexAddHandler{cache: o.Cache}
|
||||
indexRemoveHandler *indexRemoveHandler = &indexRemoveHandler{cache: o.Cache}
|
||||
tokenTransferHandler *tokenTransferHandler = &tokenTransferHandler{cache: o.Cache}
|
||||
faucetGiveHandler *faucetGiveHandler = &faucetGiveHandler{}
|
||||
ownershipHandler *ownershipHandler = &ownershipHandler{}
|
||||
poolDepositHandler *poolDepositHandler = &poolDepositHandler{}
|
||||
poolSwapHandler *poolSwapHandler = &poolSwapHandler{}
|
||||
quoterPriceHandler *quoterPriceHandler = "erPriceHandler{}
|
||||
sealHandler *sealHandler = &sealHandler{}
|
||||
tokenBurnHandler *tokenBurnHandler = &tokenBurnHandler{}
|
||||
tokenMintHandler *tokenMintHandler = &tokenMintHandler{}
|
||||
)
|
||||
|
||||
logHandlers := map[common.Hash]Handler{
|
||||
w3.H("0x26162814817e23ec5035d6a2edc6c422da2da2119e27cfca6be65cc2dc55ca4c"): faucetGiveHandler,
|
||||
w3.H("0xa226db3f664042183ee0281230bba26cbf7b5057e50aee7f25a175ff45ce4d7f"): indexAddHandler,
|
||||
w3.H("0x24a12366c02e13fe4a9e03d86a8952e85bb74a456c16e4a18b6d8295700b74bb"): indexRemoveHandler,
|
||||
w3.H("0x8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0"): ownershipHandler,
|
||||
w3.H("0x5548c837ab068cf56a2c2479df0882a4922fd203edb7517321831d95078c5f62"): poolDepositHandler,
|
||||
w3.H("0xd6d34547c69c5ee3d2667625c188acf1006abb93e0ee7cf03925c67cf7760413"): poolSwapHandler,
|
||||
w3.H("0xdb9ce1a76955721ca61ac50cd1b87f9ab8620325c8619a62192c2dc7871d56b1"): quoterPriceHandler,
|
||||
w3.H("0x6b7e2e653f93b645d4ed7292d6429f96637084363e477c8aaea1a43ed13c284e"): sealHandler,
|
||||
w3.H("0xcc16f5dbb4873280815c1ee09dbd06736cffcc184412cf7a71a0fdb75d397ca5"): tokenBurnHandler,
|
||||
w3.H("0xab8530f87dc9b59234c4623bf917212bb2536d647574c8e7e5da92c2ede0c9f8"): tokenMintHandler,
|
||||
w3.H("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"): tokenTransferHandler,
|
||||
}
|
||||
|
||||
inputDataHandlers := map[string]Handler{
|
||||
"63e4bff4": faucetGiveHandler,
|
||||
"de82efb4": faucetGiveHandler,
|
||||
"0a3b0a4f": indexAddHandler,
|
||||
"4420e486": indexAddHandler,
|
||||
"29092d0e": indexRemoveHandler,
|
||||
"f2fde38b": ownershipHandler,
|
||||
"47e7ef24": poolDepositHandler,
|
||||
"d9caed12": poolSwapHandler,
|
||||
"ebc59dff": quoterPriceHandler,
|
||||
"86fe212d": sealHandler,
|
||||
"42966c68": tokenBurnHandler,
|
||||
"449a52f8": tokenMintHandler,
|
||||
"a9059cbb": tokenTransferHandler,
|
||||
"23b872dd": tokenTransferHandler,
|
||||
}
|
||||
|
||||
return &Router{
|
||||
pub: o.Pub,
|
||||
logHandlers: logHandlers,
|
||||
inputDataHandlers: inputDataHandlers,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Router) RouteSuccessTx(ctx context.Context, msg SuccessTx) error {
|
||||
handler, ok := r.logHandlers[msg.Log.Topics[0]]
|
||||
if ok {
|
||||
return handler.SuccessTx(ctx, msg, r.pub.Send)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Router) RouteRevertTx(ctx context.Context, msg RevertTx) error {
|
||||
if len(msg.InputData) < 8 {
|
||||
return nil
|
||||
}
|
||||
|
||||
handler, ok := r.inputDataHandlers[msg.InputData[:8]]
|
||||
if ok {
|
||||
return handler.RevertTx(ctx, msg, r.pub.Send)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
73
internal/router/seal.go
Normal file
73
internal/router/seal.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math/big"
|
||||
|
||||
"github.com/grassrootseconomics/celo-tracker/pkg/event"
|
||||
"github.com/grassrootseconomics/w3-celo"
|
||||
)
|
||||
|
||||
type sealHandler struct{}
|
||||
|
||||
const sealEventName = "SEAL_STATE_CHANGE"
|
||||
|
||||
var (
|
||||
_ Handler = (*sealHandler)(nil)
|
||||
|
||||
sealEvent = w3.MustNewEvent("SealStateChange(bool indexed _final, uint256 _sealState)")
|
||||
sealToSig = w3.MustNewFunc("seal(uint256)", "uint256")
|
||||
)
|
||||
|
||||
func (h *sealHandler) Name() string {
|
||||
return sealEventName
|
||||
}
|
||||
|
||||
func (h *sealHandler) SuccessTx(ctx context.Context, tx SuccessTx, pubCB PubCallback) error {
|
||||
var (
|
||||
final bool
|
||||
sealState big.Int
|
||||
)
|
||||
|
||||
if err := sealEvent.DecodeArgs(tx.Log, &final, &sealState); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sealEvent := event.Event{
|
||||
Index: tx.Log.Index,
|
||||
Block: tx.Log.BlockNumber,
|
||||
ContractAddress: tx.Log.Address.Hex(),
|
||||
Success: true,
|
||||
Timestamp: tx.Timestamp,
|
||||
TxHash: tx.Log.TxHash.Hex(),
|
||||
TxType: sealEventName,
|
||||
Payload: map[string]any{
|
||||
"final": final,
|
||||
"sealState": sealState.String(),
|
||||
},
|
||||
}
|
||||
|
||||
return pubCB(ctx, sealEvent)
|
||||
}
|
||||
|
||||
func (h *sealHandler) RevertTx(ctx context.Context, tx RevertTx, pubCB PubCallback) error {
|
||||
var sealState big.Int
|
||||
|
||||
if err := sealToSig.DecodeArgs(w3.B(tx.InputData), &sealState); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sealEvent := event.Event{
|
||||
Block: tx.Block,
|
||||
ContractAddress: tx.ContractAddress,
|
||||
Success: false,
|
||||
Timestamp: tx.Timestamp,
|
||||
TxHash: tx.TxHash,
|
||||
TxType: sealEventName,
|
||||
Payload: map[string]any{
|
||||
"sealState": sealState.String(),
|
||||
},
|
||||
}
|
||||
|
||||
return pubCB(ctx, sealEvent)
|
||||
}
|
||||
75
internal/router/token_burn.go
Normal file
75
internal/router/token_burn.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math/big"
|
||||
|
||||
"github.com/celo-org/celo-blockchain/common"
|
||||
"github.com/grassrootseconomics/celo-tracker/pkg/event"
|
||||
"github.com/grassrootseconomics/w3-celo"
|
||||
)
|
||||
|
||||
type tokenBurnHandler struct{}
|
||||
|
||||
const burnEventName = "TOKEN_BURN"
|
||||
|
||||
var (
|
||||
_ Handler = (*tokenBurnHandler)(nil)
|
||||
|
||||
tokenBurnEvent = w3.MustNewEvent("Burn(address indexed _tokenBurner, uint256 _value)")
|
||||
tokenBurnToSig = w3.MustNewFunc("Burn(uint256)", "bool")
|
||||
)
|
||||
|
||||
func (h *tokenBurnHandler) Name() string {
|
||||
return burnEventName
|
||||
}
|
||||
|
||||
func (h *tokenBurnHandler) SuccessTx(ctx context.Context, tx SuccessTx, pubCB PubCallback) error {
|
||||
var (
|
||||
tokenBurner common.Address
|
||||
value big.Int
|
||||
)
|
||||
|
||||
if err := tokenBurnEvent.DecodeArgs(tx.Log, &tokenBurner, &value); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tokenBurnEvent := event.Event{
|
||||
Index: tx.Log.Index,
|
||||
Block: tx.Log.BlockNumber,
|
||||
ContractAddress: tx.Log.Address.Hex(),
|
||||
Success: true,
|
||||
Timestamp: tx.Timestamp,
|
||||
TxHash: tx.Log.TxHash.Hex(),
|
||||
TxType: burnEventName,
|
||||
Payload: map[string]any{
|
||||
"tokenBurner": tokenBurner.Hex(),
|
||||
"value": value.String(),
|
||||
},
|
||||
}
|
||||
|
||||
return pubCB(ctx, tokenBurnEvent)
|
||||
}
|
||||
|
||||
func (h *tokenBurnHandler) RevertTx(ctx context.Context, tx RevertTx, pubCB PubCallback) error {
|
||||
var value big.Int
|
||||
|
||||
if err := tokenBurnToSig.DecodeArgs(w3.B(tx.InputData), &value); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tokenBurnEvent := event.Event{
|
||||
Block: tx.Block,
|
||||
ContractAddress: tx.ContractAddress,
|
||||
Success: false,
|
||||
Timestamp: tx.Timestamp,
|
||||
TxHash: tx.TxHash,
|
||||
TxType: burnEventName,
|
||||
Payload: map[string]any{
|
||||
"tokenBurner": tx.From,
|
||||
"value": value.String(),
|
||||
},
|
||||
}
|
||||
|
||||
return pubCB(ctx, tokenBurnEvent)
|
||||
}
|
||||
82
internal/router/token_mint.go
Normal file
82
internal/router/token_mint.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math/big"
|
||||
|
||||
"github.com/celo-org/celo-blockchain/common"
|
||||
"github.com/grassrootseconomics/celo-tracker/pkg/event"
|
||||
"github.com/grassrootseconomics/w3-celo"
|
||||
)
|
||||
|
||||
type tokenMintHandler struct{}
|
||||
|
||||
const mintEventName = "TOKEN_MINT"
|
||||
|
||||
var (
|
||||
_ Handler = (*tokenMintHandler)(nil)
|
||||
|
||||
tokenMintEvent = w3.MustNewEvent("Mint(address indexed _tokenMinter, address indexed _beneficiary, uint256 _value)")
|
||||
tokenMintToSig = w3.MustNewFunc("MintTo(address, uint256)", "bool")
|
||||
)
|
||||
|
||||
func (h *tokenMintHandler) Name() string {
|
||||
return mintEventName
|
||||
}
|
||||
|
||||
func (h *tokenMintHandler) SuccessTx(ctx context.Context, tx SuccessTx, pubCB PubCallback) error {
|
||||
var (
|
||||
tokenMinter common.Address
|
||||
to common.Address
|
||||
value big.Int
|
||||
)
|
||||
|
||||
if err := tokenMintEvent.DecodeArgs(tx.Log, &tokenMinter, &to, &value); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tokenMintEvent := event.Event{
|
||||
Index: tx.Log.Index,
|
||||
Block: tx.Log.BlockNumber,
|
||||
ContractAddress: tx.Log.Address.Hex(),
|
||||
Success: true,
|
||||
Timestamp: tx.Timestamp,
|
||||
TxHash: tx.Log.TxHash.Hex(),
|
||||
TxType: mintEventName,
|
||||
Payload: map[string]any{
|
||||
"tokenMinter": tokenMinter.Hex(),
|
||||
"to": to.Hex(),
|
||||
"value": value.String(),
|
||||
},
|
||||
}
|
||||
|
||||
return pubCB(ctx, tokenMintEvent)
|
||||
}
|
||||
|
||||
func (h *tokenMintHandler) RevertTx(ctx context.Context, tx RevertTx, pubCB PubCallback) error {
|
||||
|
||||
var (
|
||||
to common.Address
|
||||
value big.Int
|
||||
)
|
||||
|
||||
if err := tokenMintToSig.DecodeArgs(w3.B(tx.InputData), &to, &value); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tokenMintEvent := event.Event{
|
||||
Block: tx.Block,
|
||||
ContractAddress: tx.ContractAddress,
|
||||
Success: false,
|
||||
Timestamp: tx.Timestamp,
|
||||
TxHash: tx.TxHash,
|
||||
TxType: mintEventName,
|
||||
Payload: map[string]any{
|
||||
"tokenMinter": tx.From,
|
||||
"to": to.Hex(),
|
||||
"value": value.String(),
|
||||
},
|
||||
}
|
||||
|
||||
return pubCB(ctx, tokenMintEvent)
|
||||
}
|
||||
161
internal/router/token_transfer.go
Normal file
161
internal/router/token_transfer.go
Normal file
@@ -0,0 +1,161 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math/big"
|
||||
|
||||
"github.com/celo-org/celo-blockchain/common"
|
||||
"github.com/grassrootseconomics/celo-tracker/internal/cache"
|
||||
"github.com/grassrootseconomics/celo-tracker/pkg/event"
|
||||
"github.com/grassrootseconomics/celoutils/v3"
|
||||
"github.com/grassrootseconomics/w3-celo"
|
||||
)
|
||||
|
||||
type tokenTransferHandler struct {
|
||||
cache cache.Cache
|
||||
}
|
||||
|
||||
const transferEventName = "TOKEN_TRANSFER"
|
||||
|
||||
var (
|
||||
_ Handler = (*tokenTransferHandler)(nil)
|
||||
|
||||
tokenTransferEvent = w3.MustNewEvent("Transfer(address indexed _from, address indexed _to, uint256 _value)")
|
||||
tokenTransferSig = w3.MustNewFunc("transfer(address, uint256)", "bool")
|
||||
tokenTransferFromSig = w3.MustNewFunc("transferFrom(address, address, uint256)", "bool")
|
||||
|
||||
stables = map[string]bool{
|
||||
celoutils.CUSDContractMainnet: true,
|
||||
celoutils.CKESContractMainnet: true,
|
||||
celoutils.USDTContractMainnet: true,
|
||||
celoutils.USDCContractMainnet: true,
|
||||
}
|
||||
)
|
||||
|
||||
func (h *tokenTransferHandler) Name() string {
|
||||
return transferEventName
|
||||
}
|
||||
|
||||
func (h *tokenTransferHandler) SuccessTx(ctx context.Context, tx SuccessTx, pubCB PubCallback) error {
|
||||
var (
|
||||
from common.Address
|
||||
to common.Address
|
||||
value big.Int
|
||||
)
|
||||
|
||||
if err := tokenTransferEvent.DecodeArgs(tx.Log, &from, &to, &value); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
proceed, err := h.checkStables(ctx, from.Hex(), to.Hex(), tx.Log.Address.Hex())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !proceed {
|
||||
return nil
|
||||
}
|
||||
|
||||
tokenTransferEvent := event.Event{
|
||||
Index: tx.Log.Index,
|
||||
Block: tx.Log.BlockNumber,
|
||||
ContractAddress: tx.Log.Address.Hex(),
|
||||
Success: true,
|
||||
Timestamp: tx.Timestamp,
|
||||
TxHash: tx.Log.TxHash.Hex(),
|
||||
TxType: transferEventName,
|
||||
Payload: map[string]any{
|
||||
"from": from.Hex(),
|
||||
"to": to.Hex(),
|
||||
"value": value.String(),
|
||||
},
|
||||
}
|
||||
|
||||
return pubCB(ctx, tokenTransferEvent)
|
||||
}
|
||||
|
||||
func (h *tokenTransferHandler) RevertTx(ctx context.Context, tx RevertTx, pubCB PubCallback) error {
|
||||
tokenTransferEvent := event.Event{
|
||||
Block: tx.Block,
|
||||
ContractAddress: tx.ContractAddress,
|
||||
Success: false,
|
||||
Timestamp: tx.Timestamp,
|
||||
TxHash: tx.TxHash,
|
||||
TxType: transferEventName,
|
||||
}
|
||||
|
||||
switch tx.InputData[:8] {
|
||||
case "a9059cbb":
|
||||
var (
|
||||
to common.Address
|
||||
value big.Int
|
||||
)
|
||||
|
||||
if err := tokenTransferSig.DecodeArgs(w3.B(tx.InputData), &to, &value); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
proceed, err := h.checkStables(ctx, tx.From, to.Hex(), tx.ContractAddress)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !proceed {
|
||||
return nil
|
||||
}
|
||||
|
||||
tokenTransferEvent.Payload = map[string]any{
|
||||
"from": tx.From,
|
||||
"to": to.Hex(),
|
||||
"value": value.String(),
|
||||
}
|
||||
|
||||
return pubCB(ctx, tokenTransferEvent)
|
||||
case "23b872dd":
|
||||
var (
|
||||
from common.Address
|
||||
to common.Address
|
||||
value big.Int
|
||||
)
|
||||
|
||||
if err := tokenTransferFromSig.DecodeArgs(w3.B(tx.InputData), &from, &to, &value); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
proceed, err := h.checkStables(ctx, from.Hex(), to.Hex(), tx.ContractAddress)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !proceed {
|
||||
return nil
|
||||
}
|
||||
|
||||
tokenTransferEvent.Payload = map[string]any{
|
||||
"from": from.Hex(),
|
||||
"to": to.Hex(),
|
||||
"value": value.String(),
|
||||
}
|
||||
|
||||
return pubCB(ctx, tokenTransferEvent)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *tokenTransferHandler) checkStables(ctx context.Context, from string, to string, contractAddress string) (bool, error) {
|
||||
_, ok := stables[contractAddress]
|
||||
if !ok {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// TODO: Pipeline this check on Redis with a new method
|
||||
fromExists, err := h.cache.Exists(ctx, from)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
toExists, err := h.cache.Exists(ctx, to)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return fromExists || toExists, nil
|
||||
}
|
||||
Reference in New Issue
Block a user