Skip to content

Commit

Permalink
fix: total-gas query fix (#1641)
Browse files Browse the repository at this point in the history
* total gas query fix

* lint fix

* small fixes

---------

Co-authored-by: Omer <[email protected]>
Co-authored-by: Yaroms <[email protected]>
  • Loading branch information
3 people authored Aug 26, 2024
1 parent 802730b commit 359da07
Showing 1 changed file with 36 additions and 11 deletions.
47 changes: 36 additions & 11 deletions utils/cli/multisend.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cli

import (
"context"
"encoding/csv"
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"time"

"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -17,6 +17,7 @@ import (
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
pairingtypes "github.com/lavanet/lava/v2/x/pairing/types"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -353,11 +354,11 @@ func loadProgress(filename string) Progress {

func NewQueryTotalGasCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "total-gas address chainid",
Short: `calculate the total gas used by a provider in 24H`,
Long: `calculate the total gas used by a provider in 24H`,
Example: "total-gas lava@... NEAR",
Args: cobra.ExactArgs(2),
Use: "total-gas address",
Short: `calculate the total gas used by a provider in 12H for all chains`,
Long: `calculate the total gas used by a provider in 12H for all chains`,
Example: "total-gas lava@...",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
Expand All @@ -366,7 +367,6 @@ func NewQueryTotalGasCmd() *cobra.Command {
authquerier := authtypes.NewQueryClient(clientCtx)

account := args[0]
chainid := args[1]

getSequence := func(account string) (uint64, error) {
res, err := authquerier.Account(cmd.Context(), &authtypes.QueryAccountRequest{Address: account})
Expand Down Expand Up @@ -405,15 +405,39 @@ func NewQueryTotalGasCmd() *cobra.Command {
}

now := time.Now().UTC()
if clientCtx.Height != 0 {
res, err := clientCtx.Client.Block(context.Background(), &clientCtx.Height)
if err != nil {
fmt.Println("failed to get latest block time")
return nil
}
now = res.Block.Header.Time
}

txtime := now
totalgas := int64(0)
numRelays := int64(0)
sequence, _ := getSequence(account)
layout := time.RFC3339
for now.Sub(txtime) < 24*time.Hour {
for now.Sub(txtime) < 12*time.Hour {
sequence--
tx := getResponse(account, sequence)

if strings.Contains(tx.RawLog, chainid) && strings.Contains(tx.RawLog, "MsgRelayPayment") {
if tx == nil {
continue
}

msgs := tx.GetTx().GetMsgs()
foundPayment := false
for _, msg := range msgs {
msg, ok := msg.(*pairingtypes.MsgRelayPayment)
if !ok {
continue
}
numRelays += int64(len(msg.Relays))
foundPayment = true
}
if foundPayment {
totalgas += tx.GasUsed
}
// Parse the time string
Expand All @@ -422,9 +446,10 @@ func NewQueryTotalGasCmd() *cobra.Command {
return err
}

fmt.Printf("sequence %d, totalgas %d txdiff %f sec\n", sequence, totalgas, now.Sub(txtime).Seconds())
fmt.Printf("\rsequence %d, totalgas %d txdiff %f sec", sequence, totalgas, now.Sub(txtime).Seconds())
}

totalgas /= numRelays
fmt.Printf("\navg totalgas %d numTX %d \n", totalgas, numRelays)
return nil
},
}
Expand Down

0 comments on commit 359da07

Please sign in to comment.