decred.org/dcrdex@v1.0.5/dex/networks/eth/params_test.go (about)

     1  // This code is available on the terms of the project LICENSE.md file,
     2  // also available online at https://blueoakcouncil.org/license/1.0.0.
     3  
     4  package eth
     5  
     6  import (
     7  	"math"
     8  	"math/big"
     9  	"reflect"
    10  	"testing"
    11  )
    12  
    13  func TestConversions(t *testing.T) {
    14  	newBigIntFromStr := func(str string) *big.Int {
    15  		t.Helper()
    16  		bi, ok := new(big.Int).SetString(str, 10)
    17  		if !ok {
    18  			t.Fatalf("bad int str %v", str)
    19  		}
    20  		return bi
    21  	}
    22  	tests := []struct {
    23  		name string
    24  		gwei uint64
    25  		wei  *big.Int
    26  	}{
    27  		{
    28  			"ok",
    29  			12,
    30  			newBigIntFromStr("12000000000"),
    31  		},
    32  	}
    33  	for _, tt := range tests {
    34  		t.Run(tt.name, func(t *testing.T) {
    35  			if gotWei := GweiToWei(tt.gwei); !reflect.DeepEqual(gotWei, tt.wei) {
    36  				t.Errorf("GweiToWei() = %v, want %v", gotWei, tt.wei)
    37  			}
    38  			if gotGwei := WeiToGwei(tt.wei); !reflect.DeepEqual(gotGwei, tt.gwei) {
    39  				t.Errorf("WeiToGwei() = %v, want %v", gotGwei, tt.wei)
    40  			}
    41  		})
    42  
    43  	}
    44  }
    45  
    46  func TestVersionedGases(t *testing.T) {
    47  	type test struct {
    48  		ver            uint32
    49  		expRedeemGases []uint64
    50  		expInitGases   []uint64
    51  		expRefundGas   uint64
    52  	}
    53  
    54  	tests := []*test{
    55  		{
    56  			ver: 0,
    57  			expInitGases: []uint64{
    58  				0,
    59  				v0Gases.Swap,
    60  				v0Gases.Swap + v0Gases.SwapAdd,
    61  				v0Gases.Swap + v0Gases.SwapAdd*2,
    62  			},
    63  			expRedeemGases: []uint64{
    64  				0,
    65  				v0Gases.Redeem,
    66  				v0Gases.Redeem + v0Gases.RedeemAdd,
    67  				v0Gases.Redeem + v0Gases.RedeemAdd*2,
    68  			},
    69  			expRefundGas: v0Gases.Refund,
    70  		},
    71  		{
    72  			ver:            1,
    73  			expInitGases:   []uint64{0, math.MaxUint64},
    74  			expRedeemGases: []uint64{0, math.MaxUint64},
    75  			expRefundGas:   math.MaxUint64,
    76  		},
    77  	}
    78  
    79  	for _, tt := range tests {
    80  		for n, expGas := range tt.expInitGases {
    81  			gas := InitGas(n, tt.ver)
    82  			if gas != expGas {
    83  				t.Fatalf("wrong gas for %d inits, version %d. wanted %d, got %d", n, tt.ver, expGas, gas)
    84  			}
    85  		}
    86  		for n, expGas := range tt.expRedeemGases {
    87  			gas := RedeemGas(n, tt.ver)
    88  			if gas != expGas {
    89  				t.Fatalf("wrong gas for %d redeems, version %d. wanted %d, got %d", n, tt.ver, expGas, gas)
    90  			}
    91  		}
    92  		gas := RefundGas(tt.ver)
    93  		if gas != tt.expRefundGas {
    94  			t.Fatalf("wrong gas for refund, version %d. wanted %d, got %d", tt.ver, tt.expRefundGas, gas)
    95  		}
    96  	}
    97  }