code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/transfers_helpers_for_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package sqlstore_test
    17  
    18  import (
    19  	"context"
    20  	"testing"
    21  	"time"
    22  
    23  	"code.vegaprotocol.io/vega/datanode/entities"
    24  	"code.vegaprotocol.io/vega/datanode/sqlstore"
    25  	vgtest "code.vegaprotocol.io/vega/libs/test"
    26  	vegapb "code.vegaprotocol.io/vega/protos/vega"
    27  	eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
    28  
    29  	"github.com/shopspring/decimal"
    30  	"github.com/stretchr/testify/require"
    31  )
    32  
    33  type TransferOption func(*testing.T, *eventspb.Transfer)
    34  
    35  func CreateTransfer(t *testing.T, ctx context.Context, transferStore *sqlstore.Transfers, accountsStore *sqlstore.Accounts, block entities.Block, options ...TransferOption) *entities.Transfer {
    36  	t.Helper()
    37  
    38  	transfer := NewTransfer(t, ctx, accountsStore, block, options...)
    39  
    40  	require.NoError(t, transferStore.Upsert(ctx, transfer))
    41  
    42  	fee := entities.TransferFees{
    43  		TransferID:      transfer.ID,
    44  		EpochSeq:        1,
    45  		Amount:          decimal.NewFromInt(1),
    46  		DiscountApplied: decimal.NewFromInt(0),
    47  		VegaTime:        transfer.VegaTime,
    48  	}
    49  
    50  	require.NoError(t, transferStore.UpsertFees(ctx, &fee))
    51  	fee.EpochSeq = 2
    52  	fee.VegaTime = fee.VegaTime.Add(time.Second)
    53  
    54  	require.NoError(t, transferStore.UpsertFees(ctx, &fee))
    55  
    56  	return transfer
    57  }
    58  
    59  func NewTransfer(t *testing.T, ctx context.Context, accountsStore *sqlstore.Accounts, block entities.Block, options ...TransferOption) *entities.Transfer {
    60  	t.Helper()
    61  
    62  	// Postgres only stores timestamps in microsecond resolution.
    63  	// Without truncating, the timestamp will mismatch in test assertions.
    64  	blockTimeMs := block.VegaTime.Truncate(time.Microsecond)
    65  
    66  	transferEvent := &eventspb.Transfer{
    67  		Id:              GenerateID(),
    68  		From:            GenerateID(),
    69  		FromAccountType: vegapb.AccountType_ACCOUNT_TYPE_GENERAL,
    70  		To:              GenerateID(),
    71  		ToAccountType:   vegapb.AccountType_ACCOUNT_TYPE_GENERAL,
    72  		Asset:           GenerateID(),
    73  		Amount:          vgtest.RandomPositiveU64AsString(),
    74  		Reference:       GenerateID(),
    75  		Status:          eventspb.Transfer_STATUS_PENDING,
    76  		Timestamp:       blockTimeMs.UnixNano(),
    77  	}
    78  
    79  	for _, option := range options {
    80  		option(t, transferEvent)
    81  	}
    82  
    83  	if transferEvent.Kind == nil {
    84  		t.Fatal("transfer is missing a kind")
    85  	}
    86  
    87  	transfer, err := entities.TransferFromProto(ctx, transferEvent, generateTxHash(), blockTimeMs, accountsStore)
    88  	require.NoError(t, err)
    89  
    90  	return transfer
    91  }
    92  
    93  func TransferWithID(id entities.TransferID) TransferOption {
    94  	return func(t *testing.T, transfer *eventspb.Transfer) {
    95  		t.Helper()
    96  		transfer.Id = id.String()
    97  	}
    98  }
    99  
   100  func TransferWithStatus(status entities.TransferStatus) TransferOption {
   101  	return func(t *testing.T, transfer *eventspb.Transfer) {
   102  		t.Helper()
   103  		transfer.Status = eventspb.Transfer_Status(status)
   104  	}
   105  }
   106  
   107  func TransferWithAsset(asset *entities.Asset) TransferOption {
   108  	return func(t *testing.T, transfer *eventspb.Transfer) {
   109  		t.Helper()
   110  		transfer.Asset = asset.ID.String()
   111  	}
   112  }
   113  
   114  func TransferFromToAccounts(from, to *entities.Account) TransferOption {
   115  	return func(t *testing.T, transfer *eventspb.Transfer) {
   116  		t.Helper()
   117  		transfer.From = from.PartyID.String()
   118  		transfer.FromAccountType = from.Type
   119  		transfer.To = to.PartyID.String()
   120  		transfer.ToAccountType = to.Type
   121  	}
   122  }
   123  
   124  func TransferAsRecurring(config *eventspb.RecurringTransfer) TransferOption {
   125  	return func(t *testing.T, transfer *eventspb.Transfer) {
   126  		t.Helper()
   127  		transfer.Kind = &eventspb.Transfer_Recurring{
   128  			Recurring: config,
   129  		}
   130  	}
   131  }
   132  
   133  func TransferAsRecurringGovernance(config eventspb.RecurringGovernanceTransfer) TransferOption {
   134  	return func(t *testing.T, transfer *eventspb.Transfer) {
   135  		t.Helper()
   136  		transfer.Kind = &eventspb.Transfer_RecurringGovernance{
   137  			RecurringGovernance: &config,
   138  		}
   139  	}
   140  }
   141  
   142  func TransferAsOneOff(config eventspb.OneOffTransfer) TransferOption {
   143  	return func(t *testing.T, transfer *eventspb.Transfer) {
   144  		t.Helper()
   145  		transfer.Kind = &eventspb.Transfer_OneOff{
   146  			OneOff: &config,
   147  		}
   148  	}
   149  }
   150  
   151  func TransferDetailsAsTransfers(t *testing.T, details []entities.TransferDetails) []entities.Transfer {
   152  	t.Helper()
   153  
   154  	transfers := make([]entities.Transfer, 0, len(details))
   155  	for i := range details {
   156  		transfers = append(transfers, details[i].Transfer)
   157  	}
   158  	return transfers
   159  }
   160  
   161  func TransferWithGameID(gameID *string) TransferOption {
   162  	return func(t *testing.T, transfer *eventspb.Transfer) {
   163  		t.Helper()
   164  		transfer.GameId = gameID
   165  	}
   166  }
   167  
   168  func TransferWithType(typ entities.TransferType) TransferOption {
   169  	return func(t *testing.T, transfer *eventspb.Transfer) {
   170  		t.Helper()
   171  	}
   172  }