code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/funding_payments_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  	"testing"
    20  	"time"
    21  
    22  	"code.vegaprotocol.io/vega/datanode/entities"
    23  	"code.vegaprotocol.io/vega/datanode/sqlstore"
    24  	"code.vegaprotocol.io/vega/libs/num"
    25  	"code.vegaprotocol.io/vega/libs/ptr"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  type fundingPaymentTestStore struct {
    31  	fp *sqlstore.FundingPayments
    32  }
    33  
    34  func newFundingPaymentTestStore(t *testing.T) *fundingPaymentTestStore {
    35  	t.Helper()
    36  	return &fundingPaymentTestStore{
    37  		fp: sqlstore.NewFundingPayments(connectionSource),
    38  	}
    39  }
    40  
    41  func TestFundingPayments(t *testing.T) {
    42  	ctx := tempTransaction(t)
    43  	store := newFundingPaymentTestStore(t)
    44  
    45  	now := time.Now()
    46  
    47  	fundingPayments := []*entities.FundingPayment{
    48  		{
    49  			PartyID:          entities.PartyID("09d82547b823da327af14727d02936db75c33cffe8e09341a9fc729fe53865e0"),
    50  			MarketID:         entities.MarketID("46d66ea0a00609615e04aaf6b41e5e9f552650535ed85059444d68bb6456852a"),
    51  			FundingPeriodSeq: 1,
    52  			Amount:           num.MustDecimalFromString("-100"),
    53  			VegaTime:         now,
    54  			TxHash:           "09d82547b823da327af14727d02936db75c33cffe8e09341a9fc729fe53865e0",
    55  		},
    56  		{
    57  			PartyID:          entities.PartyID("947a700141e3d175304ee176d0beecf9ee9f462e09330e33c386952caf21f679"),
    58  			MarketID:         entities.MarketID("46d66ea0a00609615e04aaf6b41e5e9f552650535ed85059444d68bb6456852a"),
    59  			FundingPeriodSeq: 1,
    60  			Amount:           num.MustDecimalFromString("100"),
    61  			VegaTime:         now,
    62  			TxHash:           "f1e520d7612de709503d493a3335a4aa8e8b3125b5d5661b7bed7f509b67bf53",
    63  		},
    64  		{
    65  			PartyID:          entities.PartyID("09d82547b823da327af14727d02936db75c33cffe8e09341a9fc729fe53865e0"),
    66  			MarketID:         entities.MarketID("46d66ea0a00609615e04aaf6b41e5e9f552650535ed85059444d68bb6456852a"),
    67  			FundingPeriodSeq: 2,
    68  			Amount:           num.MustDecimalFromString("42"),
    69  			VegaTime:         now.Add(5 * time.Second),
    70  			TxHash:           "09d82547b823da327af14727d02936db75c33cffe8e09341a9fc729fe53865e0",
    71  		},
    72  		{
    73  			PartyID:          entities.PartyID("947a700141e3d175304ee176d0beecf9ee9f462e09330e33c386952caf21f679"),
    74  			MarketID:         entities.MarketID("46d66ea0a00609615e04aaf6b41e5e9f552650535ed85059444d68bb6456852a"),
    75  			FundingPeriodSeq: 2,
    76  			Amount:           num.MustDecimalFromString("-42"),
    77  			VegaTime:         now.Add(5 * time.Second),
    78  			TxHash:           "f1e520d7612de709503d493a3335a4aa8e8b3125b5d5661b7bed7f509b67bf53",
    79  		},
    80  		{
    81  			PartyID:          entities.PartyID("09d82547b823da327af14727d02936db75c33cffe8e09341a9fc729fe53865e0"),
    82  			MarketID:         entities.MarketID("46d66ea0a00609615e04aaf6b41e5e9f552650535ed85059444d68bb6456852a"),
    83  			FundingPeriodSeq: 3,
    84  			Amount:           num.MustDecimalFromString("25"),
    85  			VegaTime:         now.Add(10 * time.Second),
    86  			TxHash:           "09d82547b823da327af14727d02936db75c33cffe8e09341a9fc729fe53865e0",
    87  		},
    88  		{
    89  			PartyID:          entities.PartyID("947a700141e3d175304ee176d0beecf9ee9f462e09330e33c386952caf21f679"),
    90  			MarketID:         entities.MarketID("46d66ea0a00609615e04aaf6b41e5e9f552650535ed85059444d68bb6456852a"),
    91  			FundingPeriodSeq: 3,
    92  			Amount:           num.MustDecimalFromString("-25"),
    93  			VegaTime:         now.Add(10 * time.Second),
    94  			TxHash:           "f1e520d7612de709503d493a3335a4aa8e8b3125b5d5661b7bed7f509b67bf53",
    95  		},
    96  		{
    97  			PartyID:          entities.PartyID("09d82547b823da327af14727d02936db75c33cffe8e09341a9fc729fe53865e0"),
    98  			MarketID:         entities.MarketID("f1e520d7612de709503d493a3335a4aa8e8b3125b5d5661b7bed7f509b67bf53"),
    99  			FundingPeriodSeq: 1,
   100  			Amount:           num.MustDecimalFromString("2400"),
   101  			VegaTime:         now.Add(10 * time.Second),
   102  			TxHash:           "09d82547b823da327af14727d02936db75c33cffe8e09341a9fc729fe53865e0",
   103  		},
   104  	}
   105  
   106  	t.Run("can insert successfully", func(t *testing.T) {
   107  		assert.NoError(t, store.fp.Add(ctx, fundingPayments))
   108  	})
   109  
   110  	t.Run("can get for a party, no market", func(t *testing.T) {
   111  		pagination, _ := entities.NewCursorPagination(nil, nil, nil, nil, true)
   112  		partyID := entities.PartyID("09d82547b823da327af14727d02936db75c33cffe8e09341a9fc729fe53865e0")
   113  		// get only the first party,
   114  		// and ensure the ordering is correct
   115  		payments, _, err := store.fp.List(
   116  			ctx,
   117  			partyID,
   118  			nil,
   119  			pagination,
   120  		)
   121  
   122  		assert.NoError(t, err)
   123  		assert.Len(t, payments, 4)
   124  
   125  		// expected in this order
   126  		// newest first
   127  		amounts := []num.Decimal{
   128  			num.MustDecimalFromString("2400"),
   129  			num.MustDecimalFromString("25"),
   130  			num.MustDecimalFromString("42"),
   131  			num.MustDecimalFromString("-100"),
   132  		}
   133  
   134  		for i, p := range payments {
   135  			assert.Equal(t, p.PartyID, partyID)
   136  			assert.Equal(t, p.Amount, amounts[i])
   137  		}
   138  	})
   139  
   140  	t.Run("can get for a party and a market", func(t *testing.T) {
   141  		pagination, _ := entities.NewCursorPagination(nil, nil, nil, nil, true)
   142  		partyID := entities.PartyID("09d82547b823da327af14727d02936db75c33cffe8e09341a9fc729fe53865e0")
   143  		marketID := entities.MarketID("f1e520d7612de709503d493a3335a4aa8e8b3125b5d5661b7bed7f509b67bf53")
   144  		// get only the first party,
   145  		// and ensure the ordering is correct
   146  		payments, _, err := store.fp.List(
   147  			ctx,
   148  			partyID,
   149  			ptr.From(marketID),
   150  			pagination,
   151  		)
   152  
   153  		assert.NoError(t, err)
   154  		assert.Len(t, payments, 1)
   155  
   156  		// expected in this order
   157  		// newest first
   158  		amounts := []num.Decimal{
   159  			num.MustDecimalFromString("2400"),
   160  		}
   161  
   162  		for i, p := range payments {
   163  			assert.Equal(t, p.PartyID, partyID)
   164  			assert.Equal(t, p.MarketID, marketID)
   165  			assert.Equal(t, p.Amount, amounts[i])
   166  		}
   167  	})
   168  }