2023-01-06 12:32:20 +01:00
|
|
|
package filter
|
|
|
|
|
|
|
|
import (
|
2023-01-11 09:13:59 +01:00
|
|
|
"context"
|
2023-01-14 10:12:34 +01:00
|
|
|
"sync"
|
2023-01-11 09:13:59 +01:00
|
|
|
|
2023-01-14 10:20:16 +01:00
|
|
|
"github.com/grassrootseconomics/cic-chain-events/pkg/fetch"
|
2023-01-06 12:32:20 +01:00
|
|
|
"github.com/zerodha/logf"
|
|
|
|
)
|
|
|
|
|
2023-03-08 15:30:40 +01:00
|
|
|
type (
|
|
|
|
AddressFilterOpts struct {
|
|
|
|
Cache *sync.Map
|
|
|
|
Logg logf.Logger
|
|
|
|
SystemAddress string
|
|
|
|
}
|
2023-01-06 12:32:20 +01:00
|
|
|
|
2023-03-08 15:30:40 +01:00
|
|
|
AddressFilter struct {
|
|
|
|
cache *sync.Map
|
|
|
|
logg logf.Logger
|
|
|
|
systemAddress string
|
|
|
|
}
|
|
|
|
)
|
2023-01-06 12:32:20 +01:00
|
|
|
|
|
|
|
func NewAddressFilter(o AddressFilterOpts) Filter {
|
|
|
|
return &AddressFilter{
|
2023-02-24 11:28:30 +01:00
|
|
|
cache: o.Cache,
|
|
|
|
logg: o.Logg,
|
|
|
|
systemAddress: o.SystemAddress,
|
2023-01-06 12:32:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-24 11:28:30 +01:00
|
|
|
func (f *AddressFilter) Execute(_ context.Context, transaction fetch.Transaction) (bool, error) {
|
|
|
|
if transaction.From.Address == f.systemAddress {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2023-01-14 10:12:34 +01:00
|
|
|
if _, found := f.cache.Load(transaction.To.Address); found {
|
2023-01-06 12:32:20 +01:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|