mirror of
https://github.com/grassrootseconomics/eth-tracker.git
synced 2025-02-22 16:00:37 +01:00
feat: change transfer event lookup to enforce both token AND address existence before emitting event
* Add ExistsNetwork cache lookup * Exists now only accepts a single param
This commit is contained in:
parent
faa428c583
commit
cf263c7d15
3
internal/cache/cache.go
vendored
3
internal/cache/cache.go
vendored
@ -11,7 +11,8 @@ type (
|
||||
Cache interface {
|
||||
Add(context.Context, string) error
|
||||
Remove(context.Context, string) error
|
||||
Exists(context.Context, ...string) (bool, error)
|
||||
Exists(context.Context, string) (bool, error)
|
||||
ExistsNetwork(context.Context, string, ...string) (bool, error)
|
||||
Size(context.Context) (int64, error)
|
||||
}
|
||||
|
||||
|
22
internal/cache/redis.go
vendored
22
internal/cache/redis.go
vendored
@ -40,8 +40,26 @@ func (c *redisCache) Remove(ctx context.Context, key string) error {
|
||||
return c.client.Do(ctx, cmd).Error()
|
||||
}
|
||||
|
||||
func (c *redisCache) Exists(ctx context.Context, keys ...string) (bool, error) {
|
||||
cmd := c.client.B().Exists().Key(keys...).Build()
|
||||
func (c *redisCache) Exists(ctx context.Context, key string) (bool, error) {
|
||||
cmd := c.client.B().Exists().Key(key).Build()
|
||||
res, err := c.client.Do(ctx, cmd).AsBool()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (c *redisCache) ExistsNetwork(ctx context.Context, token string, addresses ...string) (bool, error) {
|
||||
tokenCmd := c.client.B().Exists().Key(token).Build()
|
||||
tokenRes, err := c.client.Do(ctx, tokenCmd).AsBool()
|
||||
if err != nil {
|
||||
return false, err
|
||||
} else if !tokenRes {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
cmd := c.client.B().Exists().Key(addresses...).Build()
|
||||
res, err := c.client.Do(ctx, cmd).AsBool()
|
||||
if err != nil {
|
||||
return false, err
|
||||
|
19
internal/cache/xmap.go
vendored
19
internal/cache/xmap.go
vendored
@ -26,8 +26,23 @@ func (c *mapCache) Remove(_ context.Context, key string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *mapCache) Exists(_ context.Context, key ...string) (bool, error) {
|
||||
for _, v := range key {
|
||||
func (c *mapCache) Exists(_ context.Context, key string) (bool, error) {
|
||||
_, ok := c.xmap.Load(key)
|
||||
if ok {
|
||||
return true, nil
|
||||
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (c *mapCache) ExistsNetwork(_ context.Context, token string, addresses ...string) (bool, error) {
|
||||
_, ok := c.xmap.Load(token)
|
||||
if !ok {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
for _, v := range addresses {
|
||||
_, ok := c.xmap.Load(v)
|
||||
if ok {
|
||||
return true, nil
|
||||
|
@ -10,27 +10,12 @@ import (
|
||||
"github.com/lmittmann/w3"
|
||||
)
|
||||
|
||||
const (
|
||||
transferEventName = "TOKEN_TRANSFER"
|
||||
|
||||
CUSDContractMainnet = "0x765DE816845861e75A25fCA122bb6898B8B1282a"
|
||||
CKESContractMainnet = "0x456a3D042C0DbD3db53D5489e98dFb038553B0d0"
|
||||
CEURContractmainnet = "0xD8763CBa276a3738E6DE85b4b3bF5FDed6D6cA73"
|
||||
USDCContractMainnet = "0xcebA9300f2b948710d2653dD7B07f33A8B32118C"
|
||||
USDTContractMainnet = "0x617f3112bf5397D0467D315cC709EF968D9ba546"
|
||||
)
|
||||
const transferEventName = "TOKEN_TRANSFER"
|
||||
|
||||
var (
|
||||
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{
|
||||
CUSDContractMainnet: true,
|
||||
CKESContractMainnet: true,
|
||||
USDTContractMainnet: true,
|
||||
USDCContractMainnet: true,
|
||||
}
|
||||
)
|
||||
|
||||
func HandleTokenTransferLog(hc *HandlerContainer) router.LogHandlerFunc {
|
||||
@ -45,7 +30,7 @@ func HandleTokenTransferLog(hc *HandlerContainer) router.LogHandlerFunc {
|
||||
return err
|
||||
}
|
||||
|
||||
proceed, err := hc.checkStables(ctx, from.Hex(), to.Hex(), lp.Log.Address.Hex())
|
||||
proceed, err := hc.checkTransferWithinNetwork(ctx, lp.Log.Address.Hex(), from.Hex(), to.Hex())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -94,7 +79,7 @@ func HandleTokenTransferInputData(hc *HandlerContainer) router.InputDataHandlerF
|
||||
return err
|
||||
}
|
||||
|
||||
proceed, err := hc.checkStables(ctx, idp.From, to.Hex(), idp.ContractAddress)
|
||||
proceed, err := hc.checkTransferWithinNetwork(ctx, idp.ContractAddress, idp.From, to.Hex())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -120,7 +105,7 @@ func HandleTokenTransferInputData(hc *HandlerContainer) router.InputDataHandlerF
|
||||
return err
|
||||
}
|
||||
|
||||
proceed, err := hc.checkStables(ctx, from.Hex(), to.Hex(), idp.ContractAddress)
|
||||
proceed, err := hc.checkTransferWithinNetwork(ctx, idp.ContractAddress, from.Hex(), to.Hex())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -141,13 +126,8 @@ func HandleTokenTransferInputData(hc *HandlerContainer) router.InputDataHandlerF
|
||||
}
|
||||
}
|
||||
|
||||
func (hc *HandlerContainer) checkStables(ctx context.Context, from string, to string, contractAddress string) (bool, error) {
|
||||
_, ok := stables[contractAddress]
|
||||
if !ok {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
exists, err := hc.cache.Exists(ctx, from, to)
|
||||
func (hc *HandlerContainer) checkTransferWithinNetwork(ctx context.Context, contractAddress string, from string, to string) (bool, error) {
|
||||
exists, err := hc.cache.ExistsNetwork(ctx, contractAddress, from, to)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user