github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/tequilapi/contract/tokens_test.go (about)

     1  /*
     2   * Copyright (C) 2022 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package contract
    19  
    20  import (
    21  	"fmt"
    22  	"math/big"
    23  	"testing"
    24  
    25  	"github.com/shopspring/decimal"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestTokens(t *testing.T) {
    30  	for _, data := range []struct {
    31  		amount        *big.Int
    32  		expectedWei   string
    33  		expectedEther string
    34  		expectedHuman string
    35  	}{
    36  		{
    37  			amount:        big.NewInt(6_123_456_789_123_456_789),
    38  			expectedWei:   "6123456789123456789",
    39  			expectedEther: "6.123456789123456789",
    40  			expectedHuman: "6.123456", // hence no rounding
    41  		},
    42  		{
    43  			amount:        big.NewInt(0),
    44  			expectedWei:   "0",
    45  			expectedEther: "0",
    46  			expectedHuman: "0",
    47  		},
    48  		{
    49  			amount:        big.NewInt(1),
    50  			expectedWei:   "1",
    51  			expectedEther: "0.000000000000000001",
    52  			expectedHuman: "0",
    53  		},
    54  		{
    55  			amount:        big.NewInt(-1),
    56  			expectedWei:   "-1",
    57  			expectedEther: "-0.000000000000000001",
    58  			expectedHuman: "0",
    59  		},
    60  		{
    61  			amount:        big.NewInt(-6_123_456_789_123_456_789),
    62  			expectedWei:   "-6123456789123456789",
    63  			expectedEther: "-6.123456789123456789",
    64  			expectedHuman: "-6.123456", // hence no rounding
    65  		},
    66  		{
    67  			amount:        nil,
    68  			expectedWei:   "0",
    69  			expectedEther: "0",
    70  			expectedHuman: "0",
    71  		},
    72  	} {
    73  		t.Run(fmt.Sprintf("%+v", data), func(t *testing.T) {
    74  			tokens := NewTokens(data.amount)
    75  			assert.Equal(t, tokens.Wei, data.expectedWei)
    76  			assert.Equal(t, tokens.Ether, data.expectedEther)
    77  			assert.Equal(t, tokens.Human, data.expectedHuman)
    78  		})
    79  	}
    80  }
    81  
    82  func TestTokensFromString(t *testing.T) {
    83  	for _, data := range []struct {
    84  		amount        decimal.Decimal
    85  		expectedWei   string
    86  		expectedEther string
    87  		expectedHuman string
    88  	}{
    89  		{
    90  			amount:        decimal.RequireFromString("6.123456789123456789"),
    91  			expectedWei:   "6123456789123456789",
    92  			expectedEther: "6.123456789123456789",
    93  			expectedHuman: "6.123456", // hence no rounding
    94  		},
    95  		{
    96  			amount:        decimal.Zero,
    97  			expectedWei:   "0",
    98  			expectedEther: "0",
    99  			expectedHuman: "0",
   100  		},
   101  		{
   102  			amount:        decimal.NewFromInt32(1),
   103  			expectedWei:   "1000000000000000000",
   104  			expectedEther: "1",
   105  			expectedHuman: "1",
   106  		},
   107  		{
   108  			amount:        decimal.NewFromInt32(-1),
   109  			expectedWei:   "-1000000000000000000",
   110  			expectedEther: "-1",
   111  			expectedHuman: "-1",
   112  		},
   113  		{
   114  			amount:        decimal.RequireFromString("-6.123456789123456789"),
   115  			expectedWei:   "-6123456789123456789",
   116  			expectedEther: "-6.123456789123456789",
   117  			expectedHuman: "-6.123456", // hence no rounding
   118  		},
   119  		{
   120  			amount:        decimal.Decimal{},
   121  			expectedWei:   "0",
   122  			expectedEther: "0",
   123  			expectedHuman: "0",
   124  		},
   125  	} {
   126  		t.Run(fmt.Sprintf("%+v", data), func(t *testing.T) {
   127  			tokens := NewTokensFromDecimal(data.amount)
   128  			assert.Equal(t, data.expectedWei, tokens.Wei)
   129  			assert.Equal(t, data.expectedEther, tokens.Ether)
   130  			assert.Equal(t, data.expectedHuman, tokens.Human)
   131  		})
   132  	}
   133  }