github.com/0xsequence/ethkit@v1.25.0/ethgas/ethgas_test.go (about)

     1  package ethgas_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/0xsequence/ethkit/ethgas"
     9  	"github.com/0xsequence/ethkit/ethmonitor"
    10  	"github.com/0xsequence/ethkit/ethrpc"
    11  	"github.com/0xsequence/ethkit/util"
    12  	"github.com/go-chi/httpvcr"
    13  	"github.com/goware/logger"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestGasGauge(t *testing.T) {
    18  	t.Logf("disabling test as it has concurrency issues")
    19  	return
    20  
    21  	testConfig, err := util.ReadTestConfig("../ethkit-test.json")
    22  	if err != nil {
    23  		t.Error(err)
    24  	}
    25  
    26  	ethNodeURL := testConfig["MAINNET_URL"]
    27  	if ethNodeURL == "" {
    28  		ethNodeURL = "http://localhost:8545"
    29  	}
    30  
    31  	ctx := context.Background()
    32  
    33  	vcr := httpvcr.New("ethgas_mainnet")
    34  	vcr.Start(ctx)
    35  	defer vcr.Stop()
    36  
    37  	vcr.URLRewriter = func(url string) string {
    38  		// rewrite the url to hide the API keys
    39  		return "http://mainnet/"
    40  	}
    41  
    42  	monitorOptions := ethmonitor.DefaultOptions
    43  	// monitorOptions.StrictSubscribers = false
    44  	if vcr.Mode() == httpvcr.ModeReplay {
    45  		// change options to run replay tests faster
    46  		monitorOptions.PollingInterval = 100 * time.Millisecond
    47  	}
    48  
    49  	// Setup provider and monitor
    50  	provider, err := ethrpc.NewProvider(ethNodeURL)
    51  	assert.NoError(t, err)
    52  
    53  	monitor, err := ethmonitor.NewMonitor(provider, monitorOptions)
    54  	assert.NoError(t, err)
    55  
    56  	// Setup gas tracker
    57  	gasGauge, err := ethgas.NewGasGauge(logger.NewLogger(logger.LogLevel_DEBUG), monitor, 1, false)
    58  	assert.NoError(t, err)
    59  
    60  	// wait before we start to ensure any other http requests above are completed
    61  	// for the vcr -- as both the gas gauge and monitor leverage the same provider http client
    62  	// which is recorded. this is also why we start the monitor below after the gas gauge is
    63  	// instantiated.
    64  	//
    65  	// NOTE: this doesn't seem to work on github actions anyways, so we just completely disable
    66  	// the test
    67  	time.Sleep(1 * time.Second)
    68  
    69  	go func() {
    70  		err := monitor.Run(ctx)
    71  		if err != nil {
    72  			panic(err)
    73  		}
    74  	}()
    75  	defer monitor.Stop()
    76  
    77  	go func() {
    78  		err := gasGauge.Run(ctx)
    79  		if err != nil {
    80  			panic(err)
    81  		}
    82  	}()
    83  	defer gasGauge.Stop()
    84  
    85  	// Wait for requests to complete
    86  	select {
    87  	case <-vcr.Done():
    88  		break
    89  	case <-time.After(1 * time.Minute): // max amount of time to run the vcr recorder
    90  		break
    91  	}
    92  
    93  	gasGauge.Stop()
    94  	monitor.Stop()
    95  
    96  	// assertions
    97  	suggestedGasPrice := gasGauge.SuggestedGasPrice()
    98  	assert.Equal(t, "12602173807", suggestedGasPrice.InstantWei.String())
    99  	assert.Equal(t, "12602173807", suggestedGasPrice.FastWei.String())
   100  	assert.Equal(t, "12602173807", suggestedGasPrice.StandardWei.String())
   101  	assert.Equal(t, "10711847736", suggestedGasPrice.SlowWei.String())
   102  	assert.Equal(t, uint64(12), suggestedGasPrice.Instant)
   103  	assert.Equal(t, uint64(12), suggestedGasPrice.Fast)
   104  	assert.Equal(t, uint64(12), suggestedGasPrice.Standard)
   105  	assert.Equal(t, uint64(10), suggestedGasPrice.Slow)
   106  	assert.Equal(t, uint64(0xec2e0f), suggestedGasPrice.BlockNum.Uint64())
   107  	assert.Equal(t, uint64(0x6316023e), suggestedGasPrice.BlockTime)
   108  }