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:
2024-11-20 09:53:54 +03:00
parent faa428c583
commit cf263c7d15
4 changed files with 45 additions and 31 deletions

View File

@@ -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)
}

View File

@@ -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

View File

@@ -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