github.com/MetalBlockchain/metalgo@v1.11.9/vms/avm/pubsub_filterer.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package avm
     5  
     6  import (
     7  	"github.com/MetalBlockchain/metalgo/api"
     8  	"github.com/MetalBlockchain/metalgo/pubsub"
     9  	"github.com/MetalBlockchain/metalgo/vms/avm/txs"
    10  	"github.com/MetalBlockchain/metalgo/vms/components/avax"
    11  )
    12  
    13  var _ pubsub.Filterer = (*connector)(nil)
    14  
    15  type connector struct {
    16  	tx *txs.Tx
    17  }
    18  
    19  func NewPubSubFilterer(tx *txs.Tx) pubsub.Filterer {
    20  	return &connector{tx: tx}
    21  }
    22  
    23  // Apply the filter on the addresses.
    24  func (f *connector) Filter(filters []pubsub.Filter) ([]bool, interface{}) {
    25  	resp := make([]bool, len(filters))
    26  	for _, utxo := range f.tx.UTXOs() {
    27  		addressable, ok := utxo.Out.(avax.Addressable)
    28  		if !ok {
    29  			continue
    30  		}
    31  
    32  		for _, address := range addressable.Addresses() {
    33  			for i, c := range filters {
    34  				if resp[i] {
    35  					continue
    36  				}
    37  				resp[i] = c.Check(address)
    38  			}
    39  		}
    40  	}
    41  	return resp, api.JSONTxID{
    42  		TxID: f.tx.ID(),
    43  	}
    44  }