code.vegaprotocol.io/vega@v0.79.0/core/events/ledger_movements_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 events_test
    17  
    18  import (
    19  	"context"
    20  	"testing"
    21  
    22  	"code.vegaprotocol.io/vega/core/events"
    23  	"code.vegaprotocol.io/vega/core/types"
    24  	"code.vegaprotocol.io/vega/libs/num"
    25  	proto "code.vegaprotocol.io/vega/protos/vega"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func TestTransferResponseDeepClone(t *testing.T) {
    32  	ctx := context.Background()
    33  
    34  	tr := []*types.LedgerMovement{
    35  		{
    36  			Entries: []*types.LedgerEntry{
    37  				{
    38  					FromAccount:        &types.AccountDetails{Owner: "FromAccount"},
    39  					ToAccount:          &types.AccountDetails{Owner: "ToAccount"},
    40  					Amount:             num.NewUint(1000),
    41  					Type:               types.TransferTypeBondLow,
    42  					Timestamp:          2000,
    43  					FromAccountBalance: num.NewUint(3000),
    44  					ToAccountBalance:   num.NewUint(4000),
    45  				},
    46  			},
    47  			Balances: []*types.PostTransferBalance{
    48  				{
    49  					Account: &types.Account{
    50  						ID:       "Id",
    51  						Owner:    "Owner",
    52  						Balance:  num.NewUint(3000),
    53  						Asset:    "Asset",
    54  						MarketID: "MarketId",
    55  						Type:     types.AccountTypeBond,
    56  					},
    57  					Balance: num.NewUint(4000),
    58  				},
    59  			},
    60  		},
    61  	}
    62  
    63  	trEvent := events.NewLedgerMovements(ctx, tr)
    64  	tr2 := trEvent.LedgerMovements()
    65  
    66  	// Change the original values
    67  	tr[0].Entries[0].Amount = num.NewUint(999)
    68  	tr[0].Entries[0].FromAccount = &types.AccountDetails{Owner: "Changed"}
    69  	tr[0].Entries[0].Timestamp = 999
    70  	tr[0].Entries[0].ToAccount = &types.AccountDetails{Owner: "Changed"}
    71  	tr[0].Entries[0].Type = types.TransferTypeBondHigh
    72  	tr[0].Entries[0].FromAccountBalance = num.NewUint(1000)
    73  	tr[0].Entries[0].ToAccountBalance = num.NewUint(1700)
    74  
    75  	tr[0].Balances[0].Account.Asset = "Changed"
    76  	tr[0].Balances[0].Account.Balance = num.NewUint(999)
    77  	tr[0].Balances[0].Account.ID = "Changed"
    78  	tr[0].Balances[0].Account.MarketID = "Changed"
    79  	tr[0].Balances[0].Account.Owner = "Changed"
    80  	tr[0].Balances[0].Account.Type = proto.AccountType_ACCOUNT_TYPE_UNSPECIFIED
    81  	tr[0].Balances[0].Balance = num.NewUint(999)
    82  
    83  	// Check things have changed
    84  	assert.NotEqual(t, tr[0].Entries[0].Amount, tr2[0].Entries[0].Amount)
    85  	assert.NotEqual(t, tr[0].Entries[0].FromAccount, tr2[0].Entries[0].FromAccount)
    86  	assert.NotEqual(t, tr[0].Entries[0].Timestamp, tr2[0].Entries[0].Timestamp)
    87  	assert.NotEqual(t, tr[0].Entries[0].ToAccount, tr2[0].Entries[0].ToAccount)
    88  	assert.NotEqual(t, tr[0].Entries[0].Type, tr2[0].Entries[0].Type)
    89  	assert.NotEqual(t, tr[0].Entries[0].FromAccountBalance, tr2[0].Entries[0].FromAccountBalance)
    90  	assert.NotEqual(t, tr[0].Entries[0].ToAccountBalance, tr2[0].Entries[0].ToAccountBalance)
    91  
    92  	assert.NotEqual(t, tr[0].Balances[0].Account.Asset, tr2[0].Balances[0].Account.AssetId)
    93  	assert.NotEqual(t, tr[0].Balances[0].Balance, tr2[0].Balances[0].Balance)
    94  	assert.NotEqual(t, tr[0].Balances[0].Account.MarketID, tr2[0].Balances[0].Account.MarketId)
    95  	assert.NotEqual(t, tr[0].Balances[0].Account.Owner, tr2[0].Balances[0].Account.Owner)
    96  	assert.NotEqual(t, tr[0].Balances[0].Account.Type, tr2[0].Balances[0].Account.Type)
    97  	assert.NotEqual(t, tr[0].Balances[0].Balance, tr2[0].Balances[0].Balance)
    98  }
    99  
   100  func TestNilOwner(t *testing.T) {
   101  	ctx := context.Background()
   102  	systemOwner := "*"
   103  	tr1 := []*types.LedgerMovement{
   104  		{
   105  			Entries: []*types.LedgerEntry{
   106  				{
   107  					FromAccount:        &types.AccountDetails{Owner: "FromZohar"},
   108  					ToAccount:          &types.AccountDetails{Owner: "ToZohar"},
   109  					Amount:             num.NewUint(1000),
   110  					Type:               types.TransferTypeBondLow,
   111  					Timestamp:          2000,
   112  					FromAccountBalance: num.NewUint(2000),
   113  					ToAccountBalance:   num.NewUint(1400),
   114  				},
   115  			},
   116  		},
   117  	}
   118  	trNilFrom := []*types.LedgerMovement{
   119  		{
   120  			Entries: []*types.LedgerEntry{
   121  				{
   122  					FromAccount:        &types.AccountDetails{Owner: systemOwner},
   123  					ToAccount:          &types.AccountDetails{Owner: "ToZohar"},
   124  					Amount:             num.NewUint(1000),
   125  					Type:               types.TransferTypeBondLow,
   126  					Timestamp:          2000,
   127  					FromAccountBalance: num.NewUint(100),
   128  					ToAccountBalance:   num.NewUint(3800),
   129  				},
   130  			},
   131  		},
   132  	}
   133  	trNilTo := []*types.LedgerMovement{
   134  		{
   135  			Entries: []*types.LedgerEntry{
   136  				{
   137  					FromAccount:        &types.AccountDetails{Owner: "FromZohar"},
   138  					ToAccount:          &types.AccountDetails{Owner: systemOwner},
   139  					Amount:             num.NewUint(1000),
   140  					Type:               types.TransferTypeBondLow,
   141  					Timestamp:          2000,
   142  					FromAccountBalance: num.NewUint(500),
   143  					ToAccountBalance:   num.NewUint(2300),
   144  				},
   145  			},
   146  		},
   147  	}
   148  
   149  	trNilFromTo := []*types.LedgerMovement{
   150  		{
   151  			Entries: []*types.LedgerEntry{
   152  				{
   153  					FromAccount:        &types.AccountDetails{Owner: systemOwner},
   154  					ToAccount:          &types.AccountDetails{Owner: systemOwner},
   155  					Amount:             num.NewUint(1000),
   156  					Type:               types.TransferTypeBondLow,
   157  					Timestamp:          2000,
   158  					FromAccountBalance: num.NewUint(1000),
   159  					ToAccountBalance:   num.NewUint(2900),
   160  				},
   161  			},
   162  		},
   163  	}
   164  
   165  	require.True(t, events.NewLedgerMovements(ctx, tr1).IsParty("FromZohar"))
   166  	require.True(t, events.NewLedgerMovements(ctx, tr1).IsParty("ToZohar"))
   167  	require.True(t, events.NewLedgerMovements(ctx, trNilTo).IsParty("FromZohar"))
   168  	require.True(t, events.NewLedgerMovements(ctx, trNilFrom).IsParty("ToZohar"))
   169  	require.False(t, events.NewLedgerMovements(ctx, trNilFromTo).IsParty("FromZohar"))
   170  	require.False(t, events.NewLedgerMovements(ctx, trNilFromTo).IsParty("ToZohar"))
   171  	require.True(t, events.TransferResponseEventFromStream(ctx, events.NewLedgerMovements(ctx, tr1).StreamMessage()).IsParty("FromZohar"))
   172  	require.True(t, events.TransferResponseEventFromStream(ctx, events.NewLedgerMovements(ctx, tr1).StreamMessage()).IsParty("ToZohar"))
   173  	require.True(t, events.TransferResponseEventFromStream(ctx, events.NewLedgerMovements(ctx, trNilTo).StreamMessage()).IsParty("FromZohar"))
   174  	require.True(t, events.TransferResponseEventFromStream(ctx, events.NewLedgerMovements(ctx, trNilFrom).StreamMessage()).IsParty("ToZohar"))
   175  	require.False(t, events.TransferResponseEventFromStream(ctx, events.NewLedgerMovements(ctx, trNilFromTo).StreamMessage()).IsParty("FromZohar"))
   176  	require.False(t, events.TransferResponseEventFromStream(ctx, events.NewLedgerMovements(ctx, trNilFromTo).StreamMessage()).IsParty("ToZohar"))
   177  }