55 "fmt"
66 "time"
77
8+ "golang.org/x/exp/slices"
89 "gorm.io/gorm"
910)
1011
@@ -43,7 +44,7 @@ func CreatePChainEntities(db *gorm.DB, txs []*PChainTx, ins []*PChainTxInput, ou
4344// - if nodeID is not empty, only returns transactions where the given node ID is the validator node ID
4445func FetchPChainStakingTransactions (
4546 db * gorm.DB ,
46- txType PChainTxType ,
47+ txTypes [] PChainTxType ,
4748 nodeID string ,
4849 address string ,
4950 time time.Time ,
@@ -52,8 +53,10 @@ func FetchPChainStakingTransactions(
5253) ([]string , error ) {
5354 var validatorTxs []PChainTx
5455
55- if txType != PChainAddValidatorTx && txType != PChainAddDelegatorTx {
56- return nil , errInvalidTransactionType
56+ for _ , txType := range txTypes {
57+ if ! slices .Contains (PChainStakingTransactions [:], txType ) {
58+ return nil , errInvalidTransactionType
59+ }
5760 }
5861 if limit <= 0 {
5962 limit = 100
@@ -62,7 +65,7 @@ func FetchPChainStakingTransactions(
6265 offset = 0
6366 }
6467
65- query := db .Where (& PChainTx { Type : txType } )
68+ query := db .Where ("type IN ?" , txTypes )
6669 if len (nodeID ) > 0 {
6770 query = query .Where ("node_id = ?" , nodeID )
6871 }
@@ -87,7 +90,7 @@ func FetchPChainStakingTransactions(
8790func FetchPChainStakingData (
8891 db * gorm.DB ,
8992 time time.Time ,
90- txType PChainTxType ,
93+ txTypes [] PChainTxType ,
9194 offset int ,
9295 limit int ,
9396) ([]PChainTxData , error ) {
@@ -104,7 +107,7 @@ func FetchPChainStakingData(
104107 Table ("p_chain_txes" ).
105108 Joins ("left join p_chain_tx_inputs as inputs on inputs.tx_id = p_chain_txes.tx_id" ).
106109 Where ("start_time <= ?" , time ).Where ("? <= end_time" , time ).
107- Where ("type = ?" , txType ).
110+ Where ("type IN ?" , txTypes ).
108111 Group ("p_chain_txes.id" ).
109112 Order ("p_chain_txes.id" ).Offset (offset ).Limit (limit ).
110113 Select ("p_chain_txes.*, group_concat(distinct(inputs.address)) as input_address" ).
@@ -238,7 +241,7 @@ func FetchPChainVotingData(db *gorm.DB, from time.Time, to time.Time) ([]PChainT
238241 query := db .
239242 Table ("p_chain_txes" ).
240243 Joins ("left join p_chain_tx_inputs as inputs on inputs.tx_id = p_chain_txes.tx_id" ).
241- Where ("type = ? OR type = ? " , PChainAddValidatorTx , PChainAddDelegatorTx ).
244+ Where ("type IN ? " , PChainStakingTransactions ).
242245 Where ("start_time >= ?" , from ).Where ("start_time < ?" , to ).
243246 Select ("p_chain_txes.*, inputs.address as input_address, inputs.in_idx as input_index" ).
244247 Scan (& data )
@@ -258,10 +261,7 @@ func GetPChainTxsForEpoch(in *GetPChainTxsForEpochInput) ([]PChainTxData, error)
258261 Joins ("left join p_chain_tx_inputs as inputs on inputs.tx_id = p_chain_txes.tx_id" ).
259262 Where ("p_chain_txes.start_time >= ?" , in .StartTimestamp ).
260263 Where ("p_chain_txes.start_time < ?" , in .EndTimestamp ).
261- Where (
262- in .DB .Where ("p_chain_txes.type = ?" , PChainAddDelegatorTx ).
263- Or ("p_chain_txes.type = ?" , PChainAddValidatorTx ),
264- ).
264+ Where ("p_chain_txes.type IN ?" , PChainStakingTransactions ).
265265 Select ("p_chain_txes.*, inputs.address as input_address, inputs.in_idx as input_index" ).
266266 Find (& txs ).
267267 Error
@@ -273,13 +273,15 @@ func GetPChainTxsForEpoch(in *GetPChainTxsForEpochInput) ([]PChainTxData, error)
273273}
274274
275275// Fetches all P-chain staking transactions of type txType intersecting the given time interval
276- func FetchNodeStakingIntervals (db * gorm.DB , txType PChainTxType , startTime time.Time , endTime time.Time ) ([]PChainTx , error ) {
277- if txType != PChainAddValidatorTx && txType != PChainAddDelegatorTx {
278- return nil , errInvalidTransactionType
276+ func FetchNodeStakingIntervals (db * gorm.DB , txTypes []PChainTxType , startTime time.Time , endTime time.Time ) ([]PChainTx , error ) {
277+ for _ , txType := range txTypes {
278+ if ! slices .Contains (PChainStakingTransactions [:], txType ) {
279+ return nil , errInvalidTransactionType
280+ }
279281 }
280282
281283 var txs []PChainTx
282- err := db .Where (& PChainTx { Type : txType } ).
284+ err := db .Where ("type IN ?" , txTypes ).
283285 Where ("start_time <= ?" , endTime ).
284286 Where ("end_time >= ?" , startTime ).
285287 Find (& txs ).Error
0 commit comments