Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved Filtering options #131

Merged
merged 8 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var (
Name = "inx-indexer"

// Version of the app.
Version = "1.0.0-rc.3"
Version = "1.0.0-rc.4"
)

func App() *app.App {
Expand Down
38 changes: 32 additions & 6 deletions pkg/indexer/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"time"

"gorm.io/gorm"

iotago "github.com/iotaledger/iota.go/v3"
)

Expand All @@ -27,6 +29,7 @@ type AliasFilterOptions struct {
hasNativeTokens *bool
minNativeTokenCount *uint32
maxNativeTokenCount *uint32
unlockableByAddress *iotago.Address
stateController *iotago.Address
governor *iotago.Address
issuer *iotago.Address
Expand Down Expand Up @@ -57,6 +60,12 @@ func AliasMaxNativeTokenCount(value uint32) AliasFilterOption {
}
}

func AliasUnlockableByAddress(address iotago.Address) AliasFilterOption {
return func(args *AliasFilterOptions) {
args.unlockableByAddress = &address
}
}

func AliasStateController(address iotago.Address) AliasFilterOption {
return func(args *AliasFilterOptions) {
args.stateController = &address
Expand Down Expand Up @@ -123,8 +132,7 @@ func (i *Indexer) AliasOutput(aliasID *iotago.AliasID) *IndexerResult {
return i.combineOutputIDFilteredQuery(query, 0, nil)
}

func (i *Indexer) AliasOutputsWithFilters(filter ...AliasFilterOption) *IndexerResult {
opts := aliasFilterOptions(filter)
func (i *Indexer) aliasQueryWithFilter(opts *AliasFilterOptions) (*gorm.DB, error) {
query := i.db.Model(&alias{})

if opts.hasNativeTokens != nil {
Expand All @@ -143,34 +151,42 @@ func (i *Indexer) AliasOutputsWithFilters(filter ...AliasFilterOption) *IndexerR
query = query.Where("native_token_count <= ?", *opts.maxNativeTokenCount)
}

if opts.unlockableByAddress != nil {
addr, err := addressBytesForAddress(*opts.unlockableByAddress)
if err != nil {
return nil, err
}
query = query.Where("(state_controller = ? OR governor = ?)", addr[:], addr[:])
}

if opts.stateController != nil {
addr, err := addressBytesForAddress(*opts.stateController)
if err != nil {
return errorResult(err)
return nil, err
}
query = query.Where("state_controller = ?", addr[:])
}

if opts.governor != nil {
addr, err := addressBytesForAddress(*opts.governor)
if err != nil {
return errorResult(err)
return nil, err
}
query = query.Where("governor = ?", addr[:])
}

if opts.sender != nil {
addr, err := addressBytesForAddress(*opts.sender)
if err != nil {
return errorResult(err)
return nil, err
}
query = query.Where("sender = ?", addr[:])
}

if opts.issuer != nil {
addr, err := addressBytesForAddress(*opts.issuer)
if err != nil {
return errorResult(err)
return nil, err
}
query = query.Where("issuer = ?", addr[:])
}
Expand All @@ -183,5 +199,15 @@ func (i *Indexer) AliasOutputsWithFilters(filter ...AliasFilterOption) *IndexerR
query = query.Where("created_at > ?", *opts.createdAfter)
}

return query, nil
}

func (i *Indexer) AliasOutputsWithFilters(filter ...AliasFilterOption) *IndexerResult {
opts := aliasFilterOptions(filter)
query, err := i.aliasQueryWithFilter(opts)
if err != nil {
return errorResult(err)
}

return i.combineOutputIDFilteredQuery(query, opts.pageSize, opts.cursor)
}
38 changes: 32 additions & 6 deletions pkg/indexer/basic_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"time"

"gorm.io/gorm"

iotago "github.com/iotaledger/iota.go/v3"
)

Expand All @@ -31,6 +33,7 @@ type BasicOutputFilterOptions struct {
minNativeTokenCount *uint32
maxNativeTokenCount *uint32
unlockableByAddress *iotago.Address
address *iotago.Address
hasStorageDepositReturnCondition *bool
storageDepositReturnAddress *iotago.Address
hasExpirationCondition *bool
Expand Down Expand Up @@ -74,6 +77,12 @@ func BasicOutputUnlockableByAddress(address iotago.Address) BasicOutputFilterOpt
}
}

func BasicOutputUnlockAddress(address iotago.Address) BasicOutputFilterOption {
return func(args *BasicOutputFilterOptions) {
args.address = &address
}
}

func BasicOutputHasStorageDepositReturnCondition(value bool) BasicOutputFilterOption {
return func(args *BasicOutputFilterOptions) {
args.hasStorageDepositReturnCondition = &value
Expand Down Expand Up @@ -174,8 +183,7 @@ func basicOutputFilterOptions(optionalOptions []BasicOutputFilterOption) *BasicO
return result
}

func (i *Indexer) BasicOutputsWithFilters(filters ...BasicOutputFilterOption) *IndexerResult {
opts := basicOutputFilterOptions(filters)
func (i *Indexer) basicOutputsQueryWithFilter(opts *BasicOutputFilterOptions) (*gorm.DB, error) {
query := i.db.Model(&basicOutput{})

if opts.hasNativeTokens != nil {
Expand All @@ -197,7 +205,15 @@ func (i *Indexer) BasicOutputsWithFilters(filters ...BasicOutputFilterOption) *I
if opts.unlockableByAddress != nil {
addr, err := addressBytesForAddress(*opts.unlockableByAddress)
if err != nil {
return errorResult(err)
return nil, err
}
query = query.Where("(address = ? OR expiration_return_address = ? OR storage_deposit_return_address = ?)", addr[:], addr[:], addr[:])
}

if opts.address != nil {
addr, err := addressBytesForAddress(*opts.address)
if err != nil {
return nil, err
}
query = query.Where("address = ?", addr[:])
}
Expand All @@ -213,7 +229,7 @@ func (i *Indexer) BasicOutputsWithFilters(filters ...BasicOutputFilterOption) *I
if opts.storageDepositReturnAddress != nil {
addr, err := addressBytesForAddress(*opts.storageDepositReturnAddress)
if err != nil {
return errorResult(err)
return nil, err
}
query = query.Where("storage_deposit_return_address = ?", addr[:])
}
Expand All @@ -229,7 +245,7 @@ func (i *Indexer) BasicOutputsWithFilters(filters ...BasicOutputFilterOption) *I
if opts.expirationReturnAddress != nil {
addr, err := addressBytesForAddress(*opts.expirationReturnAddress)
if err != nil {
return errorResult(err)
return nil, err
}
query = query.Where("expiration_return_address = ?", addr[:])
}
Expand Down Expand Up @@ -261,7 +277,7 @@ func (i *Indexer) BasicOutputsWithFilters(filters ...BasicOutputFilterOption) *I
if opts.sender != nil {
addr, err := addressBytesForAddress(*opts.sender)
if err != nil {
return errorResult(err)
return nil, err
}
query = query.Where("sender = ?", addr[:])
}
Expand All @@ -278,5 +294,15 @@ func (i *Indexer) BasicOutputsWithFilters(filters ...BasicOutputFilterOption) *I
query = query.Where("created_at > ?", *opts.createdAfter)
}

return query, nil
}

func (i *Indexer) BasicOutputsWithFilters(filters ...BasicOutputFilterOption) *IndexerResult {
opts := basicOutputFilterOptions(filters)
query, err := i.basicOutputsQueryWithFilter(opts)
if err != nil {
return errorResult(err)
}

return i.combineOutputIDFilteredQuery(query, opts.pageSize, opts.cursor)
}
142 changes: 142 additions & 0 deletions pkg/indexer/combined.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package indexer

import (
"time"

"gorm.io/gorm"

iotago "github.com/iotaledger/iota.go/v3"
)

type CombinedFilterOptions struct {
hasNativeTokens *bool
minNativeTokenCount *uint32
maxNativeTokenCount *uint32
unlockableByAddress *iotago.Address
pageSize uint32
cursor *string
createdBefore *time.Time
createdAfter *time.Time
}

type CombinedFilterOption func(*CombinedFilterOptions)

func CombinedHasNativeTokens(value bool) CombinedFilterOption {
return func(args *CombinedFilterOptions) {
args.hasNativeTokens = &value
}
}

func CombinedMinNativeTokenCount(value uint32) CombinedFilterOption {
return func(args *CombinedFilterOptions) {
args.minNativeTokenCount = &value
}
}

func CombinedMaxNativeTokenCount(value uint32) CombinedFilterOption {
return func(args *CombinedFilterOptions) {
args.maxNativeTokenCount = &value
}
}

func CombinedUnlockableByAddress(address iotago.Address) CombinedFilterOption {
return func(args *CombinedFilterOptions) {
args.unlockableByAddress = &address
}
}

func CombinedPageSize(pageSize uint32) CombinedFilterOption {
return func(args *CombinedFilterOptions) {
args.pageSize = pageSize
}
}

func CombinedCursor(cursor string) CombinedFilterOption {
return func(args *CombinedFilterOptions) {
args.cursor = &cursor
}
}

func CombinedCreatedBefore(time time.Time) CombinedFilterOption {
return func(args *CombinedFilterOptions) {
args.createdBefore = &time
}
}

func CombinedCreatedAfter(time time.Time) CombinedFilterOption {
return func(args *CombinedFilterOptions) {
args.createdAfter = &time
}
}

func combinedFilterOptions(optionalOptions []CombinedFilterOption) *CombinedFilterOptions {
result := &CombinedFilterOptions{}

for _, optionalOption := range optionalOptions {
optionalOption(result)
}

return result
}

func (o *CombinedFilterOptions) BasicFilterOptions() *BasicOutputFilterOptions {
return &BasicOutputFilterOptions{
hasNativeTokens: o.hasNativeTokens,
minNativeTokenCount: o.minNativeTokenCount,
maxNativeTokenCount: o.maxNativeTokenCount,
unlockableByAddress: o.unlockableByAddress,
pageSize: o.pageSize,
cursor: o.cursor,
createdBefore: o.createdBefore,
createdAfter: o.createdAfter,
}
}

func (o *CombinedFilterOptions) AliasFilterOptions() *AliasFilterOptions {
return &AliasFilterOptions{
hasNativeTokens: o.hasNativeTokens,
minNativeTokenCount: o.minNativeTokenCount,
maxNativeTokenCount: o.maxNativeTokenCount,
unlockableByAddress: o.unlockableByAddress,
pageSize: o.pageSize,
cursor: o.cursor,
createdBefore: o.createdBefore,
createdAfter: o.createdAfter,
}
}

func (o *CombinedFilterOptions) NFTFilterOptions() *NFTFilterOptions {
return &NFTFilterOptions{
hasNativeTokens: o.hasNativeTokens,
minNativeTokenCount: o.minNativeTokenCount,
maxNativeTokenCount: o.maxNativeTokenCount,
unlockableByAddress: o.unlockableByAddress,
pageSize: o.pageSize,
cursor: o.cursor,
createdBefore: o.createdBefore,
createdAfter: o.createdAfter,
}
}

func (i *Indexer) CombinedOutputsWithFilters(filters ...CombinedFilterOption) *IndexerResult {
opts := combinedFilterOptions(filters)

basicQuery, err := i.basicOutputsQueryWithFilter(opts.BasicFilterOptions())
if err != nil {
return errorResult(err)
}

aliasQuery, err := i.aliasQueryWithFilter(opts.AliasFilterOptions())
if err != nil {
return errorResult(err)
}

nftQuery, err := i.nftOutputsQueryWithFilter(opts.NFTFilterOptions())
if err != nil {
return errorResult(err)
}

queries := []*gorm.DB{basicQuery, aliasQuery, nftQuery}

return i.combineOutputIDFilteredQueries(queries, opts.pageSize, opts.cursor)
}
17 changes: 14 additions & 3 deletions pkg/indexer/foundry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"time"

"gorm.io/gorm"

iotago "github.com/iotaledger/iota.go/v3"
)

Expand Down Expand Up @@ -99,8 +101,7 @@ func (i *Indexer) FoundryOutput(foundryID *iotago.FoundryID) *IndexerResult {
return i.combineOutputIDFilteredQuery(query, 0, nil)
}

func (i *Indexer) FoundryOutputsWithFilters(filters ...FoundryFilterOption) *IndexerResult {
opts := foundryFilterOptions(filters)
func (i *Indexer) foundryOutputsQueryWithFilter(opts *FoundryFilterOptions) (*gorm.DB, error) {
query := i.db.Model(&foundry{})

if opts.hasNativeTokens != nil {
Expand All @@ -122,7 +123,7 @@ func (i *Indexer) FoundryOutputsWithFilters(filters ...FoundryFilterOption) *Ind
if opts.aliasAddress != nil {
addr, err := addressBytesForAddress(opts.aliasAddress)
if err != nil {
return errorResult(err)
return nil, err
}
query = query.Where("alias_address = ?", addr[:])
}
Expand All @@ -135,5 +136,15 @@ func (i *Indexer) FoundryOutputsWithFilters(filters ...FoundryFilterOption) *Ind
query = query.Where("created_at > ?", *opts.createdAfter)
}

return query, nil
}

func (i *Indexer) FoundryOutputsWithFilters(filters ...FoundryFilterOption) *IndexerResult {
opts := foundryFilterOptions(filters)
query, err := i.foundryOutputsQueryWithFilter(opts)
if err != nil {
return errorResult(err)
}

return i.combineOutputIDFilteredQuery(query, opts.pageSize, opts.cursor)
}
Loading
Loading