github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/mw/coin/coin_test.go (about)

     1  package coin
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"strconv"
     8  	"testing"
     9  
    10  	testinit "github.com/NpoolPlatform/chain-middleware/pkg/testinit"
    11  	npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/coin"
    12  
    13  	basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1"
    14  
    15  	"github.com/google/uuid"
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  func init() {
    20  	if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction {
    21  		return
    22  	}
    23  	if err := testinit.Init(); err != nil {
    24  		fmt.Printf("cannot init test stub: %v\n", err)
    25  	}
    26  }
    27  
    28  var chainType = uuid.NewString()
    29  var chainAtomicUnit = uuid.NewString()
    30  var chainUnitExp = uint32(1)
    31  var gasType = basetypes.GasType_FixedGas
    32  var chainID = uuid.NewString()
    33  var chainNickname = uuid.NewString()
    34  var chainNativeCoinName = uuid.NewString()
    35  var ret = &npool.Coin{
    36  	Name:                        uuid.NewString(),
    37  	Unit:                        "BTC1",
    38  	ENV:                         "test",
    39  	ReservedAmount:              "0.000000000000000000",
    40  	WithdrawFeeByStableUSD:      true,
    41  	WithdrawFeeAmount:           "0.000000000000000000",
    42  	CollectFeeAmount:            "0.000000000000000000",
    43  	HotWalletFeeAmount:          "0.000000000000000000",
    44  	LowFeeAmount:                "0.000000000000000000",
    45  	HotLowFeeAmount:             "0.000000000000000000",
    46  	HotWalletAccountAmount:      "0.000000000000000000",
    47  	PaymentAccountCollectAmount: "0.000000000000000000",
    48  	FeeCoinUnit:                 "BTC1",
    49  	FeeCoinENV:                  "test",
    50  	LeastTransferAmount:         "0.000000000000000000",
    51  	CheckNewAddressBalance:      true,
    52  }
    53  
    54  var req = &npool.CoinReq{
    55  	Name:                &ret.Name,
    56  	Unit:                &ret.Unit,
    57  	ENV:                 &ret.ENV,
    58  	ChainType:           &chainType,
    59  	ChainAtomicUnit:     &chainAtomicUnit,
    60  	ChainUnitExp:        &chainUnitExp,
    61  	GasType:             &gasType,
    62  	ChainID:             &chainID,
    63  	ChainNickname:       &chainNickname,
    64  	ChainNativeCoinName: &chainNativeCoinName,
    65  	ChainNativeUnit:     &ret.Unit,
    66  }
    67  
    68  //nolint
    69  func setupCoin(t *testing.T) func(*testing.T) {
    70  	ret.FeeCoinName = ret.Name
    71  	return func(*testing.T) {}
    72  }
    73  
    74  func create(t *testing.T) {
    75  	h1, err := NewHandler(
    76  		context.Background(),
    77  		WithName(req.Name, true),
    78  		WithUnit(req.Unit, true),
    79  		WithENV(req.ENV, true),
    80  		WithChainType(req.ChainType, true),
    81  		WithChainNativeUnit(req.ChainNativeUnit, true),
    82  		WithChainAtomicUnit(req.ChainAtomicUnit, true),
    83  		WithChainUnitExp(req.ChainUnitExp, true),
    84  		WithGasType(req.GasType, true),
    85  		WithChainID(req.ChainID, true),
    86  		WithChainNickname(req.ChainNickname, true),
    87  		WithChainNativeCoinName(req.ChainNativeCoinName, true),
    88  	)
    89  	assert.Nil(t, err)
    90  
    91  	info, err := h1.CreateCoin(context.Background())
    92  	if assert.Nil(t, err) {
    93  		ret.UpdatedAt = info.UpdatedAt
    94  		ret.CreatedAt = info.CreatedAt
    95  		ret.ID = info.ID
    96  		ret.EntID = info.EntID
    97  		ret.FeeCoinTypeID = info.EntID
    98  		assert.Equal(t, info.String(), ret.String())
    99  	}
   100  }
   101  
   102  func update(t *testing.T) {
   103  	feeByUSD := false
   104  	amount := "123.700000000000000000"
   105  	logo := uuid.NewString()
   106  
   107  	ret.Logo = logo
   108  	ret.WithdrawFeeByStableUSD = feeByUSD
   109  	ret.ReservedAmount = amount
   110  	ret.WithdrawFeeAmount = amount
   111  	ret.CollectFeeAmount = amount
   112  	ret.HotWalletFeeAmount = amount
   113  	ret.LowFeeAmount = amount
   114  	ret.HotLowFeeAmount = amount
   115  	ret.HotWalletAccountAmount = amount
   116  	ret.PaymentAccountCollectAmount = amount
   117  	ret.LeastTransferAmount = amount
   118  	ret.FeeCoinLogo = logo
   119  
   120  	req.ID = &ret.ID
   121  	req.Logo = &logo
   122  	req.WithdrawFeeByStableUSD = &feeByUSD
   123  	req.ReservedAmount = &amount
   124  	req.WithdrawFeeAmount = &amount
   125  	req.CollectFeeAmount = &amount
   126  	req.HotWalletFeeAmount = &amount
   127  	req.LowFeeAmount = &amount
   128  	req.HotLowFeeAmount = &amount
   129  	req.HotWalletAccountAmount = &amount
   130  	req.PaymentAccountCollectAmount = &amount
   131  	req.LeastTransferAmount = &amount
   132  
   133  	h1, err := NewHandler(
   134  		context.Background(),
   135  		WithID(req.ID, true),
   136  		WithName(req.Name, false),
   137  		WithUnit(req.Unit, false),
   138  		WithLogo(req.Logo, false),
   139  		WithReservedAmount(req.ReservedAmount, false),
   140  		WithHomePage(req.HomePage, false),
   141  		WithSpecs(req.Specs, false),
   142  		// TODO: this should be get from chain type
   143  		WithFeeCoinTypeID(req.FeeCoinTypeID, false),
   144  		WithWithdrawFeeByStableUSD(req.WithdrawFeeByStableUSD, false),
   145  		WithWithdrawFeeAmount(req.WithdrawFeeAmount, false),
   146  		WithCollectFeeAmount(req.CollectFeeAmount, false),
   147  		WithHotWalletFeeAmount(req.HotWalletFeeAmount, false),
   148  		WithLowFeeAmount(req.LowFeeAmount, false),
   149  		WithHotLowFeeAmount(req.HotLowFeeAmount, false),
   150  		WithHotWalletFeeAmount(req.HotWalletFeeAmount, false),
   151  		WithHotWalletAccountAmount(req.HotWalletAccountAmount, false),
   152  		WithPaymentAccountCollectAmount(req.PaymentAccountCollectAmount, false),
   153  		WithLeastTransferAmount(req.LeastTransferAmount, false),
   154  		WithPresale(req.Presale, false),
   155  		WithForPay(req.ForPay, false),
   156  		WithDisabled(req.Disabled, false),
   157  		// TODO: this should be in create from register coin
   158  		WithStableUSD(req.StableUSD, false),
   159  		// TODO: this should be in create from register coin
   160  		WithNeedMemo(req.NeedMemo, false),
   161  		WithRefreshCurrency(req.RefreshCurrency, false),
   162  		WithCheckNewAddressBalance(req.CheckNewAddressBalance, false),
   163  	)
   164  	assert.Nil(t, err)
   165  
   166  	info, err := h1.UpdateCoin(context.Background())
   167  	if assert.Nil(t, err) {
   168  		ret.UpdatedAt = info.UpdatedAt
   169  		assert.Equal(t, info.String(), ret.String())
   170  	}
   171  }
   172  
   173  func _delete(t *testing.T) {
   174  	h1, err := NewHandler(
   175  		context.Background(),
   176  		WithID(&ret.ID, true),
   177  	)
   178  	assert.Nil(t, err)
   179  
   180  	_, err = h1.DeleteCoin(context.Background())
   181  	assert.Nil(t, err)
   182  }
   183  
   184  func TestCoin(t *testing.T) {
   185  	if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction {
   186  		return
   187  	}
   188  
   189  	teardown := setupCoin(t)
   190  	defer teardown(t)
   191  
   192  	t.Run("create", create)
   193  	t.Run("update", update)
   194  	t.Run("delete", _delete)
   195  }