2023-01-05 12:45:09 +01:00
|
|
|
package main
|
|
|
|
|
2023-01-06 12:32:20 +01:00
|
|
|
import (
|
2023-03-29 18:17:30 +02:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2023-01-14 11:17:34 +01:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2023-03-29 18:17:30 +02:00
|
|
|
"time"
|
2023-01-14 11:17:34 +01:00
|
|
|
|
2023-03-29 18:17:30 +02:00
|
|
|
"github.com/celo-org/celo-blockchain/common"
|
|
|
|
"github.com/grassrootseconomics/celoutils"
|
2023-01-19 09:42:59 +01:00
|
|
|
"github.com/grassrootseconomics/cic-chain-events/internal/filter"
|
2023-03-08 15:30:40 +01:00
|
|
|
"github.com/grassrootseconomics/cic-chain-events/internal/pub"
|
2023-03-29 18:17:30 +02:00
|
|
|
"github.com/grassrootseconomics/w3-celo-patch"
|
|
|
|
"github.com/grassrootseconomics/w3-celo-patch/module/eth"
|
|
|
|
"github.com/grassrootseconomics/w3-celo-patch/w3types"
|
2023-01-06 12:32:20 +01:00
|
|
|
)
|
|
|
|
|
2023-03-29 18:17:30 +02:00
|
|
|
func initAddressFilter(celoProvider *celoutils.Provider, cache *sync.Map) filter.Filter {
|
|
|
|
var (
|
|
|
|
tokenIndexEntryCount big.Int
|
|
|
|
)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
registryMap, err := celoProvider.RegistryMap(ctx, celoutils.HexToAddress(ko.MustString("chain.registry_address")))
|
|
|
|
if err != nil {
|
|
|
|
lo.Fatal("init: critical error creating address filter", "error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range registryMap {
|
|
|
|
cache.Store(strings.ToLower(v.Hex()), k)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := celoProvider.Client.CallCtx(
|
|
|
|
ctx,
|
|
|
|
eth.CallFunc(w3.MustNewFunc("entryCount()", "uint256"), registryMap[celoutils.TokenIndex]).Returns(&tokenIndexEntryCount),
|
|
|
|
); err != nil {
|
|
|
|
lo.Fatal("init: critical error creating address filter", "error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
calls := make([]w3types.Caller, tokenIndexEntryCount.Int64())
|
|
|
|
tokenAddresses := make([]common.Address, tokenIndexEntryCount.Int64())
|
2023-02-24 11:28:30 +01:00
|
|
|
|
2023-03-29 18:17:30 +02:00
|
|
|
entrySig := w3.MustNewFunc("entry(uint256 _idx)", "address")
|
2023-03-11 10:39:26 +01:00
|
|
|
|
2023-03-29 18:17:30 +02:00
|
|
|
// TODO: There is a 5MB limit to a RPC batch call size.
|
|
|
|
// Test if 10k entries will raise an error (future proofed for a lot of years)
|
|
|
|
for i := 0; i < int(tokenIndexEntryCount.Int64()); i++ {
|
|
|
|
calls[i] = eth.CallFunc(entrySig, registryMap[celoutils.TokenIndex], new(big.Int).SetInt64(int64(i))).Returns(&tokenAddresses[i])
|
|
|
|
}
|
2023-01-14 11:17:34 +01:00
|
|
|
|
2023-03-29 18:17:30 +02:00
|
|
|
if err := celoProvider.Client.CallCtx(
|
|
|
|
ctx,
|
|
|
|
calls...,
|
|
|
|
); err != nil {
|
|
|
|
lo.Fatal("init: critical error creating address filter", "error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, v := range tokenAddresses {
|
|
|
|
cache.Store(strings.ToLower(v.Hex()), fmt.Sprintf("TOKEN_%d", i))
|
|
|
|
}
|
2023-01-14 11:17:34 +01:00
|
|
|
|
2023-01-06 12:32:20 +01:00
|
|
|
return filter.NewAddressFilter(filter.AddressFilterOpts{
|
2023-03-29 18:17:30 +02:00
|
|
|
Cache: cache,
|
|
|
|
Logg: lo,
|
2023-02-24 11:28:30 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-08 15:30:40 +01:00
|
|
|
func initTransferFilter(pub *pub.Pub) filter.Filter {
|
2023-02-24 11:28:30 +01:00
|
|
|
return filter.NewTransferFilter(filter.TransferFilterOpts{
|
2023-03-08 15:30:40 +01:00
|
|
|
Pub: pub,
|
|
|
|
Logg: lo,
|
2023-01-06 12:32:20 +01:00
|
|
|
})
|
2023-02-24 11:28:30 +01:00
|
|
|
|
2023-01-06 12:32:20 +01:00
|
|
|
}
|
|
|
|
|
2023-03-08 15:30:40 +01:00
|
|
|
func initGasGiftFilter(pub *pub.Pub) filter.Filter {
|
2023-02-24 11:28:30 +01:00
|
|
|
return filter.NewGasFilter(filter.GasFilterOpts{
|
2023-03-29 18:17:30 +02:00
|
|
|
Pub: pub,
|
|
|
|
Logg: lo,
|
2023-02-24 11:28:30 +01:00
|
|
|
})
|
|
|
|
}
|
2023-01-14 11:17:34 +01:00
|
|
|
|
2023-03-08 15:30:40 +01:00
|
|
|
func initRegisterFilter(pub *pub.Pub) filter.Filter {
|
2023-02-24 11:28:30 +01:00
|
|
|
return filter.NewRegisterFilter(filter.RegisterFilterOpts{
|
2023-03-08 15:30:40 +01:00
|
|
|
Pub: pub,
|
|
|
|
Logg: lo,
|
2023-01-06 12:32:20 +01:00
|
|
|
})
|
|
|
|
}
|
2023-03-29 18:17:30 +02:00
|
|
|
|
2023-07-10 06:20:50 +02:00
|
|
|
func initApproveFilter(pub *pub.Pub) filter.Filter {
|
|
|
|
return filter.NewApproveFilter(filter.ApproveFilterOpts{
|
|
|
|
Pub: pub,
|
|
|
|
Logg: lo,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-29 18:17:30 +02:00
|
|
|
func initTokenIndexFilter(cache *sync.Map, pub *pub.Pub) filter.Filter {
|
|
|
|
return filter.NewTokenIndexFilter(filter.TokenIndexFilterOpts{
|
|
|
|
Cache: cache,
|
|
|
|
Pub: pub,
|
|
|
|
Logg: lo,
|
|
|
|
})
|
|
|
|
}
|