code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/ledger_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 "strconv" 20 "testing" 21 "time" 22 23 "code.vegaprotocol.io/vega/core/types" 24 "code.vegaprotocol.io/vega/datanode/entities" 25 "code.vegaprotocol.io/vega/datanode/sqlstore" 26 "code.vegaprotocol.io/vega/datanode/sqlstore/helpers" 27 "code.vegaprotocol.io/vega/libs/ptr" 28 "code.vegaprotocol.io/vega/protos/vega" 29 30 "github.com/shopspring/decimal" 31 "github.com/stretchr/testify/assert" 32 "github.com/stretchr/testify/require" 33 ) 34 35 func ledgerEntryEqual(t *testing.T, expected, actual entities.LedgerEntry) { 36 t.Helper() 37 38 assert.Equal(t, expected.FromAccountID, actual.FromAccountID) 39 assert.Equal(t, expected.ToAccountID, actual.ToAccountID) 40 assert.Equal(t, expected.Quantity, actual.Quantity) 41 assert.Equal(t, expected.ToAccountBalance, actual.ToAccountBalance) 42 assert.Equal(t, expected.FromAccountBalance, actual.FromAccountBalance) 43 assert.Equal(t, expected.TxHash, actual.TxHash) 44 assert.Equal(t, expected.VegaTime, actual.VegaTime) 45 assert.Equal(t, expected.Type, actual.Type) 46 } 47 48 func addTestLedgerEntry(t *testing.T, ledger *sqlstore.Ledger, 49 fromAccount entities.Account, 50 toAccount entities.Account, 51 block entities.Block, 52 quantity int64, 53 transferType entities.LedgerMovementType, 54 fromAccountBalance, toAccountBalance int64, 55 txHash entities.TxHash, 56 transferID *string, 57 ) entities.LedgerEntry { 58 t.Helper() 59 60 var tID entities.TransferID 61 if transferID != nil { 62 tID = entities.TransferID(*transferID) 63 } 64 65 ledgerEntry := entities.LedgerEntry{ 66 FromAccountID: fromAccount.ID, 67 ToAccountID: toAccount.ID, 68 Quantity: decimal.NewFromInt(quantity), 69 VegaTime: block.VegaTime, 70 TransferTime: block.VegaTime.Add(-time.Second), 71 Type: transferType, 72 FromAccountBalance: decimal.NewFromInt(fromAccountBalance), 73 ToAccountBalance: decimal.NewFromInt(toAccountBalance), 74 TxHash: txHash, 75 TransferID: tID, 76 } 77 78 err := ledger.Add(ledgerEntry) 79 require.NoError(t, err) 80 return ledgerEntry 81 } 82 83 func TestLedger(t *testing.T) { 84 ctx := tempTransaction(t) 85 86 // Prepare environment entities. 87 blockStore := sqlstore.NewBlocks(connectionSource) 88 assetStore := sqlstore.NewAssets(connectionSource) 89 accountStore := sqlstore.NewAccounts(connectionSource) 90 partyStore := sqlstore.NewParties(connectionSource) 91 marketStore := sqlstore.NewMarkets(connectionSource) 92 ledgerStore := sqlstore.NewLedger(connectionSource) 93 94 // Setup 4 assets 95 asset1 := addTestAsset(t, ctx, assetStore, addTestBlock(t, ctx, blockStore)) 96 asset2 := addTestAsset(t, ctx, assetStore, addTestBlock(t, ctx, blockStore)) 97 asset3 := addTestAsset(t, ctx, assetStore, addTestBlock(t, ctx, blockStore)) 98 99 var blocks []entities.Block 100 var parties []entities.Party 101 var markets []entities.Market 102 var accounts []entities.Account 103 104 /* 105 --- env --- 106 block 0 block 1 block 2 block 3 block 4 block 5 block 6 block 7 block 8 block 9 block 10 107 party 0 party 1 party 2 party 3 party 4 party 5 party 6 party 7 party 8 party 9 party 10 108 109 market 0 market 1 market 2 market 3 market 4 market 5 market 6 market 7 market 8 market 9 market 10 110 111 --- accounts --- 112 accounts[0] => asset1, parties[0], markets[0], vega.AccountType_ACCOUNT_TYPE_GENERAL 113 accounts[1] => asset1, parties[0], markets[1], vega.AccountType_ACCOUNT_TYPE_GENERAL 114 accounts[2] => asset1, parties[1], markets[1], vega.AccountType_ACCOUNT_TYPE_GENERAL 115 accounts[3] => asset1, parties[1], markets[2], vega.AccountType_ACCOUNT_TYPE_GENERAL 116 117 accounts[4] => asset2, parties[2], markets[2], vega.AccountType_ACCOUNT_TYPE_INSURANCE 118 accounts[5] => asset2, parties[2], markets[3], vega.AccountType_ACCOUNT_TYPE_INSURANCE 119 accounts[6] => asset2, parties[3], markets[3], vega.AccountType_ACCOUNT_TYPE_INSURANCE 120 accounts[7] => asset2, parties[3], markets[4], vega.AccountType_ACCOUNT_TYPE_INSURANCE 121 accounts[8] => asset2, parties[4], markets[4], vega.AccountType_ACCOUNT_TYPE_INSURANCE 122 accounts[9] => asset2, parties[4], markets[5], vega.AccountType_ACCOUNT_TYPE_INSURANCE 123 accounts[10] => asset2, parties[5], markets[5], vega.AccountType_ACCOUNT_TYPE_GENERAL 124 accounts[11] => asset2, parties[5], markets[6], vega.AccountType_ACCOUNT_TYPE_GENERAL 125 126 accounts[12] => asset3, parties[6], markets[6], vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY 127 accounts[13] => asset3, parties[6], markets[7], vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY 128 accounts[14] => asset3, parties[7], markets[7], vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY 129 accounts[15] => asset3, parties[7], markets[8], vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY 130 accounts[16] => asset3, parties[8], markets[8], vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY 131 accounts[17] => asset3, parties[8], markets[9], vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY 132 accounts[18] => asset3, parties[9], markets[9], vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY 133 accounts[19] => asset3, parties[9], markets[10], vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY 134 accounts[20] => asset3, parties[10], markets[10], vega.AccountType_ACCOUNT_TYPE_FEES_INSURANCE 135 accounts[21] => asset3, parties[10], markets[11], vega.AccountType_ACCOUNT_TYPE_FEES_INSURANCE 136 */ 137 for i := 0; i < 17; i++ { 138 blocks = append(blocks, addTestBlockForTime(t, ctx, blockStore, time.Now().Add((-26*time.Hour)-(time.Duration(5-i)*time.Second)))) 139 parties = append(parties, addTestParty(t, ctx, partyStore, blocks[i])) 140 markets = append(markets, helpers.GenerateMarkets(t, ctx, 1, blocks[0], marketStore)[0]) 141 } 142 143 for i := 0; i < 11; i++ { 144 var mt int 145 if i < 11-1 { 146 mt = i + 1 147 } else { 148 mt = i - 1 149 } 150 151 if i < 2 { 152 // accounts 0, 1, 2, 3 153 fromAccount := helpers.AddTestAccountWithMarketAndType(t, ctx, accountStore, parties[i], asset1, blocks[i], markets[i].ID, vega.AccountType_ACCOUNT_TYPE_GENERAL) 154 toAccount := helpers.AddTestAccountWithMarketAndType(t, ctx, accountStore, parties[i], asset1, blocks[i], markets[mt].ID, vega.AccountType_ACCOUNT_TYPE_GENERAL) 155 accounts = append(accounts, fromAccount) 156 accounts = append(accounts, toAccount) 157 continue 158 } 159 160 // accounts 4, 5, 6, 7, 8, 9, 10, 11 161 if i >= 2 && i < 5 { 162 fromAccount := helpers.AddTestAccountWithMarketAndType(t, ctx, accountStore, parties[i], asset2, blocks[i], markets[i].ID, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 163 toAccount := helpers.AddTestAccountWithMarketAndType(t, ctx, accountStore, parties[i], asset2, blocks[i], markets[mt].ID, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 164 accounts = append(accounts, fromAccount) 165 accounts = append(accounts, toAccount) 166 continue 167 } 168 169 // accounts 10, 11 170 if i >= 5 && i < 6 { 171 fromAccount := helpers.AddTestAccountWithMarketAndType(t, ctx, accountStore, parties[i], asset2, blocks[i], markets[i].ID, vega.AccountType_ACCOUNT_TYPE_GENERAL) 172 toAccount := helpers.AddTestAccountWithMarketAndType(t, ctx, accountStore, parties[i], asset2, blocks[i], markets[mt].ID, vega.AccountType_ACCOUNT_TYPE_GENERAL) 173 accounts = append(accounts, fromAccount) 174 accounts = append(accounts, toAccount) 175 continue 176 } 177 178 // accounts 12, 13, 14, 15, 16, 17, 18, 19 179 if i >= 6 && i < 10 { 180 fromAccount := helpers.AddTestAccountWithMarketAndType(t, ctx, accountStore, parties[i], asset3, blocks[i], markets[i].ID, vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY) 181 toAccount := helpers.AddTestAccountWithMarketAndType(t, ctx, accountStore, parties[i], asset3, blocks[i], markets[mt].ID, vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY) 182 accounts = append(accounts, fromAccount) 183 accounts = append(accounts, toAccount) 184 continue 185 } 186 187 // accounts 20, 21 188 fromAccount := helpers.AddTestAccountWithMarketAndType(t, ctx, accountStore, parties[i], asset3, blocks[i], markets[i].ID, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 189 toAccount := helpers.AddTestAccountWithMarketAndType(t, ctx, accountStore, parties[i], asset3, blocks[i], markets[mt].ID, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 190 accounts = append(accounts, fromAccount) 191 accounts = append(accounts, toAccount) 192 } 193 194 /* 195 --- Transfers --- 196 Asset1: 197 accounts[0]->accounts[1] => asset1, parties[0], markets[0-1], vega.AccountType_ACCOUNT_TYPE_GENERAL 198 accounts[2]->accounts[3] => asset1, parties[1], markets[1-2], vega.AccountType_ACCOUNT_TYPE_GENERAL 199 200 Asset2: 201 accounts[4]->accounts[5] => asset2, parties[2], markets[2-3], vega.AccountType_ACCOUNT_TYPE_INSURANCE 202 accounts[6]->accounts[7] => asset2, parties[3], markets[3-4], vega.AccountType_ACCOUNT_TYPE_INSURANCE 203 accounts[6]->accounts[7] => asset2, parties[3], markets[3-4], vega.AccountType_ACCOUNT_TYPE_INSURANCE 204 accounts[6]->accounts[7] => asset2, parties[3], markets[3-4], vega.AccountType_ACCOUNT_TYPE_INSURANCE 205 accounts[8]->accounts[9] => asset2, parties[4], markets[4-5], vega.AccountType_ACCOUNT_TYPE_INSURANCE 206 accounts[10]->accounts[11] => asset2, parties[5], markets[5-6], vega.AccountType_ACCOUNT_TYPE_GENERAL 207 208 accounts[5]->accounts[10] => asset2, parties[2-5], markets[3-5], vega.AccountType_ACCOUNT_TYPE_INSURANCE -> vega.AccountType_ACCOUNT_TYPE_GENERAL 209 accounts[5]->accounts[11] => asset2, parties[2-5], markets[3-6], vega.AccountType_ACCOUNT_TYPE_INSURANCE -> vega.AccountType_ACCOUNT_TYPE_GENERAL 210 accounts[4]->accounts[11] => asset2, parties[2-5], markets[2-6], vega.AccountType_ACCOUNT_TYPE_INSURANCE -> vega.AccountType_ACCOUNT_TYPE_GENERAL 211 212 Asset3: 213 accounts[14]->accounts[16] => asset3, parties[7-8], markets[7-8], vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY 214 215 accounts[17]->accounts[15] => asset3, parties[8-7], markets[9-8], vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY 216 217 accounts[21]->accounts[15] => asset3, parties[10-7], markets[9-8], vega.AccountType_ACCOUNT_TYPE_INSURANCE -> vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY 218 219 */ 220 var ledgerEntries []entities.LedgerEntry 221 ledgerEntries = append(ledgerEntries, addTestLedgerEntry(t, ledgerStore, accounts[0], accounts[1], blocks[1], int64(15), entities.LedgerMovementTypeBondSlashing, int64(500), int64(115), txHashFromString("ledger_entry_1"), ptr.From("deadbeef01"))) 222 ledgerEntries = append(ledgerEntries, addTestLedgerEntry(t, ledgerStore, accounts[2], accounts[3], blocks[2], int64(10), entities.LedgerMovementTypeBondSlashing, int64(170), int64(17890), txHashFromString("ledger_entry_2"), nil)) 223 ledgerEntries = append(ledgerEntries, addTestLedgerEntry(t, ledgerStore, accounts[4], accounts[5], blocks[3], int64(25), entities.LedgerMovementTypeBondSlashing, int64(1700), int64(2590), txHashFromString("ledger_entry_3"), nil)) 224 ledgerEntries = append(ledgerEntries, addTestLedgerEntry(t, ledgerStore, accounts[6], accounts[7], blocks[4], int64(80), entities.LedgerMovementTypeBondSlashing, int64(2310), int64(17000), txHashFromString("ledger_entry_4"), nil)) 225 ledgerEntries = append(ledgerEntries, addTestLedgerEntry(t, ledgerStore, accounts[8], accounts[9], blocks[5], int64(1), entities.LedgerMovementTypeDeposit, int64(120), int64(900), txHashFromString("ledger_entry_5"), nil)) 226 ledgerEntries = append(ledgerEntries, addTestLedgerEntry(t, ledgerStore, accounts[10], accounts[11], blocks[6], int64(40), entities.LedgerMovementTypeDeposit, int64(1500), int64(5680), txHashFromString("ledger_entry_6"), nil)) 227 ledgerEntries = append(ledgerEntries, addTestLedgerEntry(t, ledgerStore, accounts[14], accounts[16], blocks[7], int64(12), entities.LedgerMovementTypeDeposit, int64(5000), int64(9100), txHashFromString("ledger_entry_7"), nil)) 228 ledgerEntries = append(ledgerEntries, addTestLedgerEntry(t, ledgerStore, accounts[17], accounts[15], blocks[8], int64(14), entities.LedgerMovementTypeDeposit, int64(180), int64(1410), txHashFromString("ledger_entry_8"), nil)) 229 ledgerEntries = append(ledgerEntries, addTestLedgerEntry(t, ledgerStore, accounts[21], accounts[15], blocks[9], int64(28), entities.LedgerMovementTypeDeposit, int64(2180), int64(1438), txHashFromString("ledger_entry_9"), nil)) 230 ledgerEntries = append(ledgerEntries, addTestLedgerEntry(t, ledgerStore, accounts[5], accounts[11], blocks[10], int64(3), entities.LedgerMovementTypeRewardPayout, int64(2587), int64(5683), txHashFromString("ledger_entry_10"), nil)) 231 ledgerEntries = append(ledgerEntries, addTestLedgerEntry(t, ledgerStore, accounts[5], accounts[10], blocks[11], int64(5), entities.LedgerMovementTypeRewardPayout, int64(2582), int64(1510), txHashFromString("ledger_entry_11"), nil)) 232 ledgerEntries = append(ledgerEntries, addTestLedgerEntry(t, ledgerStore, accounts[6], accounts[7], blocks[12], int64(9), entities.LedgerMovementTypeRewardPayout, int64(2301), int64(17009), txHashFromString("ledger_entry_12"), nil)) 233 ledgerEntries = append(ledgerEntries, addTestLedgerEntry(t, ledgerStore, accounts[6], accounts[7], blocks[13], int64(41), entities.LedgerMovementTypeRewardPayout, int64(2260), int64(17050), txHashFromString("ledger_entry_13"), nil)) 234 _ = append(ledgerEntries, addTestLedgerEntry(t, ledgerStore, accounts[4], accounts[11], blocks[13], int64(72), entities.LedgerMovementTypeRewardPayout, int64(2188), int64(17122), txHashFromString("ledger_entry_14"), nil)) 235 236 tStart := time.Now().Add(-5 * 24 * time.Hour) 237 tEnd := time.Now() 238 239 t.Run("get all ledger records", func(t *testing.T) { 240 // Account store should be empty to begin with 241 ledgerEntries, err := ledgerStore.GetAll(ctx) 242 require.NoError(t, err) 243 assert.Empty(t, ledgerEntries) 244 }) 245 246 _, err := ledgerStore.Flush(ctx) 247 require.NoError(t, err) 248 249 t.Run("get by tx hash", func(t *testing.T) { 250 fetchedEntries, err := ledgerStore.GetByTxHash(ctx, ledgerEntries[0].TxHash) 251 require.NoError(t, err) 252 ledgerEntryEqual(t, ledgerEntries[0], fetchedEntries[0]) 253 254 fetchedEntries2, err := ledgerStore.GetByTxHash(ctx, ledgerEntries[2].TxHash) 255 require.NoError(t, err) 256 ledgerEntryEqual(t, ledgerEntries[2], fetchedEntries2[0]) 257 }) 258 259 t.Run("query ledger entries with no filters", func(t *testing.T) { 260 // Set filters for AccountFrom and AcountTo IDs 261 filter := &entities.LedgerEntryFilter{ 262 FromAccountFilter: entities.AccountFilter{}, 263 ToAccountFilter: entities.AccountFilter{}, 264 } 265 266 entries, _, err := ledgerStore.Query(ctx, 267 filter, 268 entities.DateRange{Start: &tStart, End: &tEnd}, 269 entities.CursorPagination{}, 270 ) 271 272 assert.ErrorIs(t, err, sqlstore.ErrLedgerEntryFilterForParty) 273 // Output entries for accounts positions: 274 // None 275 assert.Nil(t, entries) 276 }) 277 278 t.Run("query ledger entries with filters", func(t *testing.T) { 279 t.Run("by fromAccount filter", func(t *testing.T) { 280 // Set filters for FromAccount and AccountTo IDs 281 filter := &entities.LedgerEntryFilter{ 282 FromAccountFilter: entities.AccountFilter{ 283 AssetID: asset1.ID, 284 }, 285 ToAccountFilter: entities.AccountFilter{}, 286 } 287 288 entries, _, err := ledgerStore.Query(ctx, 289 filter, 290 entities.DateRange{Start: &tStart, End: &tEnd}, 291 entities.CursorPagination{}, 292 ) 293 294 assert.ErrorIs(t, err, sqlstore.ErrLedgerEntryFilterForParty) 295 // Output entries for accounts positions: 296 // None 297 assert.Nil(t, entries) 298 299 filter.FromAccountFilter.PartyIDs = []entities.PartyID{parties[3].ID} 300 entries, _, err = ledgerStore.Query(ctx, 301 filter, 302 entities.DateRange{Start: &tStart, End: &tEnd}, 303 entities.CursorPagination{}, 304 ) 305 306 require.NoError(t, err) 307 // Output entries for accounts positions: 308 // 0 309 assert.NotNil(t, entries) 310 assert.Equal(t, 0, len(*entries)) 311 312 filter.FromAccountFilter.AssetID = asset2.ID 313 entries, _, err = ledgerStore.Query(ctx, 314 filter, 315 entities.DateRange{Start: &tStart, End: &tEnd}, 316 entities.CursorPagination{}, 317 ) 318 319 require.NoError(t, err) 320 // Output entries for accounts positions: 321 // 6->7, 6->7, 6->7 322 assert.NotNil(t, entries) 323 assert.Equal(t, 3, len(*entries)) 324 325 for _, e := range *entries { 326 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 327 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 328 if e.Quantity.Abs().String() == strconv.Itoa(80) { 329 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeBondSlashing) 330 assert.Equal(t, strconv.Itoa(2310), e.FromAccountBalance.Abs().String()) 331 assert.Equal(t, strconv.Itoa(17000), e.ToAccountBalance.Abs().String()) 332 } 333 334 if e.Quantity.Abs().String() == strconv.Itoa(9) { 335 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeRewardPayout) 336 assert.Equal(t, strconv.Itoa(2301), e.FromAccountBalance.Abs().String()) 337 assert.Equal(t, strconv.Itoa(17009), e.ToAccountBalance.Abs().String()) 338 } 339 340 if e.Quantity.Abs().String() == strconv.Itoa(41) { 341 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeRewardPayout) 342 assert.Equal(t, strconv.Itoa(2260), e.FromAccountBalance.Abs().String()) 343 assert.Equal(t, strconv.Itoa(17050), e.ToAccountBalance.Abs().String()) 344 } 345 346 assert.Equal(t, e.FromAccountMarketID, markets[3].ID) 347 assert.Equal(t, e.ToAccountMarketID, markets[4].ID) 348 } 349 350 filter.FromAccountFilter.PartyIDs = append(filter.FromAccountFilter.PartyIDs, parties[4].ID) 351 352 entries, _, err = ledgerStore.Query(ctx, 353 filter, 354 entities.DateRange{Start: &tStart, End: &tEnd}, 355 entities.CursorPagination{}, 356 ) 357 358 assert.ErrorIs(t, err, sqlstore.ErrLedgerEntryFilterForParty) 359 // Output entries for accounts positions: 360 // None 361 assert.Nil(t, entries) 362 363 filter.FromAccountFilter.PartyIDs = []entities.PartyID{} 364 filter.FromAccountFilter.AccountTypes = []vega.AccountType{vega.AccountType_ACCOUNT_TYPE_GENERAL} 365 366 entries, _, err = ledgerStore.Query(ctx, 367 filter, 368 entities.DateRange{Start: &tStart, End: &tEnd}, 369 entities.CursorPagination{}, 370 ) 371 372 assert.ErrorIs(t, err, sqlstore.ErrLedgerEntryFilterForParty) 373 // Output entries for accounts positions: 374 // None 375 assert.Nil(t, entries) 376 }) 377 378 t.Run("by toAccount filter", func(t *testing.T) { 379 // Set filters for FromAccount and AcountTo IDs 380 filter := &entities.LedgerEntryFilter{ 381 FromAccountFilter: entities.AccountFilter{}, 382 ToAccountFilter: entities.AccountFilter{ 383 AssetID: asset2.ID, 384 PartyIDs: []entities.PartyID{parties[3].ID}, 385 }, 386 } 387 388 entries, _, err := ledgerStore.Query(ctx, 389 filter, 390 entities.DateRange{Start: &tStart, End: &tEnd}, 391 entities.CursorPagination{}, 392 ) 393 394 require.NoError(t, err) 395 // Output entries for accounts positions: 396 // 6->7, 6->7, 6->7 397 assert.NotNil(t, entries) 398 assert.Equal(t, 3, len(*entries)) 399 for _, e := range *entries { 400 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 401 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 402 if e.Quantity.Abs().String() == strconv.Itoa(80) { 403 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeBondSlashing) 404 assert.Equal(t, strconv.Itoa(2310), e.FromAccountBalance.Abs().String()) 405 assert.Equal(t, strconv.Itoa(17000), e.ToAccountBalance.Abs().String()) 406 } 407 408 if e.Quantity.Abs().String() == strconv.Itoa(9) { 409 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeRewardPayout) 410 assert.Equal(t, strconv.Itoa(2301), e.FromAccountBalance.Abs().String()) 411 assert.Equal(t, strconv.Itoa(17009), e.ToAccountBalance.Abs().String()) 412 } 413 414 if e.Quantity.Abs().String() == strconv.Itoa(41) { 415 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeRewardPayout) 416 assert.Equal(t, strconv.Itoa(2260), e.FromAccountBalance.Abs().String()) 417 assert.Equal(t, strconv.Itoa(17050), e.ToAccountBalance.Abs().String()) 418 } 419 420 assert.Equal(t, e.FromAccountMarketID, markets[3].ID) 421 assert.Equal(t, e.ToAccountMarketID, markets[4].ID) 422 } 423 424 filter.ToAccountFilter.AccountTypes = []vega.AccountType{vega.AccountType_ACCOUNT_TYPE_GENERAL, vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY} 425 426 entries, _, err = ledgerStore.Query(ctx, 427 filter, 428 entities.DateRange{Start: &tStart, End: &tEnd}, 429 entities.CursorPagination{}, 430 ) 431 432 require.NoError(t, err) 433 // Output entries for accounts positions: 434 // None 435 assert.NotNil(t, entries) 436 assert.Equal(t, 0, len(*entries)) 437 }) 438 439 t.Run("by toAccount filter with cursor", func(t *testing.T) { 440 // Set filters for FromAccount and AcountTo IDs 441 filter := &entities.LedgerEntryFilter{ 442 FromAccountFilter: entities.AccountFilter{}, 443 ToAccountFilter: entities.AccountFilter{ 444 AssetID: asset2.ID, 445 PartyIDs: []entities.PartyID{parties[3].ID}, 446 }, 447 } 448 449 first := int32(2) 450 451 cursor, err := entities.NewCursorPagination(&first, nil, nil, nil, false) 452 require.NoError(t, err) 453 454 entries, _, err := ledgerStore.Query(ctx, 455 filter, 456 entities.DateRange{Start: &tStart, End: &tEnd}, 457 cursor, 458 ) 459 460 require.NoError(t, err) 461 // Output entries for accounts positions: 462 // 6->7, 6->7, 6->7 463 assert.NotNil(t, entries) 464 assert.Equal(t, 2, len(*entries)) 465 for _, e := range *entries { 466 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 467 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 468 if e.Quantity.Abs().String() == strconv.Itoa(80) { 469 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeBondSlashing) 470 assert.Equal(t, strconv.Itoa(2310), e.FromAccountBalance.Abs().String()) 471 assert.Equal(t, strconv.Itoa(17000), e.ToAccountBalance.Abs().String()) 472 } 473 474 if e.Quantity.Abs().String() == strconv.Itoa(9) { 475 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeRewardPayout) 476 assert.Equal(t, strconv.Itoa(2301), e.FromAccountBalance.Abs().String()) 477 assert.Equal(t, strconv.Itoa(17009), e.ToAccountBalance.Abs().String()) 478 } 479 480 assert.Equal(t, e.FromAccountMarketID, markets[3].ID) 481 assert.Equal(t, e.ToAccountMarketID, markets[4].ID) 482 } 483 484 filter.ToAccountFilter.AccountTypes = []vega.AccountType{vega.AccountType_ACCOUNT_TYPE_GENERAL, vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY} 485 486 entries, _, err = ledgerStore.Query(ctx, 487 filter, 488 entities.DateRange{Start: &tStart, End: &tEnd}, 489 entities.CursorPagination{}, 490 ) 491 492 require.NoError(t, err) 493 // Output entries for accounts positions: 494 // None 495 assert.NotNil(t, entries) 496 assert.Equal(t, 0, len(*entries)) 497 }) 498 499 t.Run("by fromAccount+toAccount filters", func(t *testing.T) { 500 t.Run("open", func(t *testing.T) { 501 // Set filters for FromAccount and AcountTo IDs 502 filter := &entities.LedgerEntryFilter{ 503 FromAccountFilter: entities.AccountFilter{ 504 AssetID: asset1.ID, 505 }, 506 ToAccountFilter: entities.AccountFilter{ 507 AssetID: asset3.ID, 508 }, 509 } 510 511 entries, _, err := ledgerStore.Query(ctx, 512 filter, 513 entities.DateRange{Start: &tStart, End: &tEnd}, 514 entities.CursorPagination{}, 515 ) 516 517 assert.ErrorIs(t, err, sqlstore.ErrLedgerEntryFilterForParty) 518 // Output entries for accounts positions: 519 // None 520 assert.Nil(t, entries) 521 522 filter.ToAccountFilter.PartyIDs = append(filter.ToAccountFilter.PartyIDs, []entities.PartyID{parties[4].ID}...) 523 entries, _, err = ledgerStore.Query(ctx, 524 filter, 525 entities.DateRange{Start: &tStart, End: &tEnd}, 526 entities.CursorPagination{}, 527 ) 528 529 require.NoError(t, err) 530 // Output entries for accounts positions: 531 // 0->1, 2->3 532 assert.NotNil(t, entries) 533 assert.Equal(t, 2, len(*entries)) 534 for _, e := range *entries { 535 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_GENERAL) 536 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_GENERAL) 537 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeBondSlashing) 538 539 if e.Quantity.Abs().String() == strconv.Itoa(15) { 540 assert.Equal(t, e.FromAccountPartyID, parties[0].ID) 541 assert.Equal(t, e.ToAccountPartyID, parties[0].ID) 542 assert.Equal(t, e.FromAccountMarketID, markets[0].ID) 543 assert.Equal(t, e.ToAccountMarketID, markets[1].ID) 544 assert.Equal(t, strconv.Itoa(500), e.FromAccountBalance.Abs().String()) 545 assert.Equal(t, strconv.Itoa(115), e.ToAccountBalance.Abs().String()) 546 } 547 548 if e.Quantity.Abs().String() == strconv.Itoa(10) { 549 assert.Equal(t, e.FromAccountPartyID, parties[1].ID) 550 assert.Equal(t, e.ToAccountPartyID, parties[1].ID) 551 assert.Equal(t, e.FromAccountMarketID, markets[1].ID) 552 assert.Equal(t, e.ToAccountMarketID, markets[2].ID) 553 assert.Equal(t, strconv.Itoa(170), e.FromAccountBalance.Abs().String()) 554 assert.Equal(t, strconv.Itoa(17890), e.ToAccountBalance.Abs().String()) 555 } 556 } 557 558 filter.ToAccountFilter.AccountTypes = []vega.AccountType{vega.AccountType_ACCOUNT_TYPE_GENERAL, vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY} 559 entries, _, err = ledgerStore.Query(ctx, 560 filter, 561 entities.DateRange{Start: &tStart, End: &tEnd}, 562 entities.CursorPagination{}, 563 ) 564 565 require.NoError(t, err) 566 // Output entries for accounts positions: 567 // 0->1, 2->3 568 assert.NotNil(t, entries) 569 assert.Equal(t, 2, len(*entries)) 570 for _, e := range *entries { 571 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_GENERAL) 572 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_GENERAL) 573 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeBondSlashing) 574 575 if e.Quantity.Abs().String() == strconv.Itoa(15) { 576 assert.Equal(t, e.FromAccountPartyID, parties[0].ID) 577 assert.Equal(t, e.ToAccountPartyID, parties[0].ID) 578 assert.Equal(t, e.FromAccountMarketID, markets[0].ID) 579 assert.Equal(t, e.ToAccountMarketID, markets[1].ID) 580 assert.Equal(t, strconv.Itoa(500), e.FromAccountBalance.Abs().String()) 581 assert.Equal(t, strconv.Itoa(115), e.ToAccountBalance.Abs().String()) 582 } 583 584 if e.Quantity.Abs().String() == strconv.Itoa(10) { 585 assert.Equal(t, e.FromAccountPartyID, parties[1].ID) 586 assert.Equal(t, e.ToAccountPartyID, parties[1].ID) 587 assert.Equal(t, e.FromAccountMarketID, markets[1].ID) 588 assert.Equal(t, e.ToAccountMarketID, markets[2].ID) 589 assert.Equal(t, strconv.Itoa(170), e.FromAccountBalance.Abs().String()) 590 assert.Equal(t, strconv.Itoa(17890), e.ToAccountBalance.Abs().String()) 591 } 592 } 593 }) 594 595 t.Run("closed", func(t *testing.T) { 596 // Set filters for FromAccount and AcountTo IDs 597 filter := &entities.LedgerEntryFilter{ 598 FromAccountFilter: entities.AccountFilter{ 599 AssetID: asset2.ID, 600 }, 601 ToAccountFilter: entities.AccountFilter{}, 602 } 603 604 filter.CloseOnAccountFilters = true 605 entries, _, err := ledgerStore.Query(ctx, 606 filter, 607 entities.DateRange{Start: &tStart, End: &tEnd}, 608 entities.CursorPagination{}, 609 ) 610 611 assert.ErrorIs(t, err, sqlstore.ErrLedgerEntryFilterForParty) 612 // Output entries for accounts positions: 613 // None 614 assert.Nil(t, entries) 615 616 filter.FromAccountFilter.PartyIDs = []entities.PartyID{parties[5].ID} 617 entries, _, err = ledgerStore.Query(ctx, 618 filter, 619 entities.DateRange{Start: &tStart, End: &tEnd}, 620 entities.CursorPagination{}, 621 ) 622 623 require.NoError(t, err) 624 // Output entries for accounts positions -> should output transfers for asset2 only: 625 // 10->11 626 assert.NotNil(t, entries) 627 assert.Equal(t, 1, len(*entries)) 628 for _, e := range *entries { 629 assert.Equal(t, e.Quantity.Abs().String(), strconv.Itoa(40)) 630 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_GENERAL) 631 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_GENERAL) 632 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeDeposit) 633 634 assert.Equal(t, e.FromAccountPartyID, parties[5].ID) 635 assert.Equal(t, e.ToAccountPartyID, parties[5].ID) 636 assert.Equal(t, e.FromAccountMarketID, markets[5].ID) 637 assert.Equal(t, e.ToAccountMarketID, markets[6].ID) 638 assert.Equal(t, strconv.Itoa(1500), e.FromAccountBalance.Abs().String()) 639 assert.Equal(t, strconv.Itoa(5680), e.ToAccountBalance.Abs().String()) 640 } 641 642 // Add some grouping options 643 filter.ToAccountFilter = entities.AccountFilter{AssetID: asset3.ID} 644 entries, _, err = ledgerStore.Query(ctx, 645 filter, 646 entities.DateRange{Start: &tStart, End: &tEnd}, 647 entities.CursorPagination{}, 648 ) 649 650 require.NoError(t, err) 651 // Output entries for accounts positions: 652 // None 653 assert.NotNil(t, entries) 654 assert.Equal(t, 0, len(*entries)) 655 656 filter.FromAccountFilter = entities.AccountFilter{AssetID: asset3.ID} 657 filter.FromAccountFilter.PartyIDs = []entities.PartyID{parties[7].ID} 658 filter.ToAccountFilter.AccountTypes = []vega.AccountType{vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY} 659 entries, _, err = ledgerStore.Query(ctx, 660 filter, 661 entities.DateRange{Start: &tStart, End: &tEnd}, 662 entities.CursorPagination{}, 663 ) 664 665 require.NoError(t, err) 666 // Output entries for accounts positions: 667 // 14->16 668 assert.NotNil(t, entries) 669 assert.Equal(t, 1, len(*entries)) 670 for _, e := range *entries { 671 assert.Equal(t, e.Quantity.Abs().String(), strconv.Itoa(12)) 672 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY) 673 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY) 674 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeDeposit) 675 676 assert.Equal(t, e.FromAccountPartyID, parties[7].ID) 677 assert.Equal(t, e.ToAccountPartyID, parties[8].ID) 678 assert.Equal(t, e.FromAccountMarketID, markets[7].ID) 679 assert.Equal(t, e.ToAccountMarketID, markets[8].ID) 680 assert.Equal(t, strconv.Itoa(5000), e.FromAccountBalance.Abs().String()) 681 assert.Equal(t, strconv.Itoa(9100), e.ToAccountBalance.Abs().String()) 682 } 683 }) 684 }) 685 686 t.Run("by account filters+transferType", func(t *testing.T) { 687 // open on account filters 688 // Set filters for FromAccount and AcountTo IDs 689 filter := &entities.LedgerEntryFilter{ 690 FromAccountFilter: entities.AccountFilter{ 691 AssetID: asset2.ID, 692 PartyIDs: []entities.PartyID{parties[8].ID}, 693 }, 694 ToAccountFilter: entities.AccountFilter{ 695 AssetID: asset3.ID, 696 }, 697 TransferTypes: []entities.LedgerMovementType{ 698 entities.LedgerMovementTypeDeposit, 699 }, 700 } 701 702 entries, _, err := ledgerStore.Query(ctx, 703 filter, 704 entities.DateRange{Start: &tStart, End: &tEnd}, 705 entities.CursorPagination{}, 706 ) 707 708 require.NoError(t, err) 709 // Output entries for accounts positions -> should output transfers for asset3 only: 710 // 14->16, 17->15, 21->15 711 assert.NotNil(t, entries) 712 assert.Equal(t, 3, len(*entries)) 713 for _, e := range *entries { 714 if e.Quantity.Abs().String() == strconv.Itoa(12) { 715 assert.Equal(t, e.FromAccountPartyID, parties[7].ID) 716 assert.Equal(t, e.ToAccountPartyID, parties[8].ID) 717 assert.Equal(t, e.FromAccountMarketID, markets[7].ID) 718 assert.Equal(t, e.ToAccountMarketID, markets[8].ID) 719 assert.Equal(t, strconv.Itoa(5000), e.FromAccountBalance.Abs().String()) 720 assert.Equal(t, strconv.Itoa(9100), e.ToAccountBalance.Abs().String()) 721 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY) 722 } 723 724 if e.Quantity.Abs().String() == strconv.Itoa(14) { 725 assert.Equal(t, e.FromAccountPartyID, parties[8].ID) 726 assert.Equal(t, e.ToAccountPartyID, parties[7].ID) 727 assert.Equal(t, e.FromAccountMarketID, markets[9].ID) 728 assert.Equal(t, e.ToAccountMarketID, markets[8].ID) 729 assert.Equal(t, strconv.Itoa(180), e.FromAccountBalance.Abs().String()) 730 assert.Equal(t, strconv.Itoa(1410), e.ToAccountBalance.Abs().String()) 731 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY) 732 } 733 734 if e.Quantity.Abs().String() == strconv.Itoa(28) { 735 assert.Equal(t, e.FromAccountPartyID, parties[10].ID) 736 assert.Equal(t, e.ToAccountPartyID, parties[7].ID) 737 738 assert.Equal(t, e.FromAccountMarketID, markets[9].ID) 739 assert.Equal(t, e.ToAccountMarketID, markets[8].ID) 740 assert.Equal(t, strconv.Itoa(2180), e.FromAccountBalance.Abs().String()) 741 assert.Equal(t, strconv.Itoa(1438), e.ToAccountBalance.Abs().String()) 742 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 743 } 744 745 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY) 746 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeDeposit) 747 } 748 749 // closed on account filters 750 filter.CloseOnAccountFilters = true 751 entries, _, err = ledgerStore.Query(ctx, 752 filter, 753 entities.DateRange{Start: &tStart, End: &tEnd}, 754 entities.CursorPagination{}, 755 ) 756 757 require.NoError(t, err) 758 // Output entries for accounts positions: 759 // None 760 assert.NotNil(t, entries) 761 assert.Equal(t, 0, len(*entries)) 762 763 filter.ToAccountFilter = entities.AccountFilter{ 764 AssetID: asset3.ID, 765 AccountTypes: []vega.AccountType{vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY}, 766 } 767 768 entries, _, err = ledgerStore.Query(ctx, 769 filter, 770 entities.DateRange{Start: &tStart, End: &tEnd}, 771 entities.CursorPagination{}, 772 ) 773 774 require.NoError(t, err) 775 // Output entries for accounts positions: 776 // 0 777 assert.NotNil(t, entries) 778 assert.Equal(t, 0, len(*entries)) 779 }) 780 781 t.Run("by transferType only", func(t *testing.T) { 782 // open on account filters 783 // Set filters for FromAccount and AcountTo IDs 784 filter := &entities.LedgerEntryFilter{ 785 TransferTypes: []entities.LedgerMovementType{ 786 entities.LedgerMovementTypeDeposit, 787 }, 788 } 789 790 _, _, err := ledgerStore.Query(ctx, 791 filter, 792 entities.DateRange{Start: &tStart, End: &tEnd}, 793 entities.CursorPagination{}, 794 ) 795 796 assert.Error(t, err) 797 798 // closed on account filters 799 filter.CloseOnAccountFilters = true 800 _, _, err = ledgerStore.Query(ctx, 801 filter, 802 entities.DateRange{Start: &tStart, End: &tEnd}, 803 entities.CursorPagination{}, 804 ) 805 806 assert.Error(t, err) 807 }) 808 809 t.Run("test open/closing with different account and transfer types", func(t *testing.T) { 810 filter := &entities.LedgerEntryFilter{ 811 FromAccountFilter: entities.AccountFilter{ 812 PartyIDs: []entities.PartyID{parties[2].ID}, 813 }, 814 ToAccountFilter: entities.AccountFilter{ 815 PartyIDs: []entities.PartyID{parties[5].ID}, 816 }, 817 TransferTypes: []entities.LedgerMovementType{ 818 entities.LedgerMovementTypeRewardPayout, 819 }, 820 } 821 822 entries, _, err := ledgerStore.Query(ctx, 823 filter, 824 entities.DateRange{Start: &tStart, End: &tEnd}, 825 entities.CursorPagination{}, 826 ) 827 828 require.NoError(t, err) 829 // 4->5, 5->10, 5->11, 4->11 830 assert.NotNil(t, entries) 831 assert.Equal(t, 3, len(*entries)) 832 for _, e := range *entries { 833 if e.Quantity.Abs().String() == strconv.Itoa(3) { 834 assert.Equal(t, e.FromAccountPartyID, parties[2].ID) 835 assert.Equal(t, e.ToAccountPartyID, parties[5].ID) 836 assert.Equal(t, e.FromAccountMarketID, markets[3].ID) 837 assert.Equal(t, e.ToAccountMarketID, markets[6].ID) 838 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 839 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_GENERAL) 840 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeRewardPayout) 841 } 842 843 if e.Quantity.Abs().String() == strconv.Itoa(5) { 844 assert.Equal(t, e.FromAccountPartyID, parties[2].ID) 845 assert.Equal(t, e.ToAccountPartyID, parties[5].ID) 846 assert.Equal(t, e.FromAccountMarketID, markets[3].ID) 847 assert.Equal(t, e.ToAccountMarketID, markets[5].ID) 848 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 849 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_GENERAL) 850 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeRewardPayout) 851 } 852 853 if e.Quantity.Abs().String() == strconv.Itoa(72) { 854 assert.Equal(t, e.FromAccountPartyID, parties[2].ID) 855 assert.Equal(t, e.ToAccountPartyID, parties[5].ID) 856 857 assert.Equal(t, e.FromAccountMarketID, markets[2].ID) 858 assert.Equal(t, e.ToAccountMarketID, markets[6].ID) 859 assert.Equal(t, strconv.Itoa(2188), e.FromAccountBalance.Abs().String()) 860 assert.Equal(t, strconv.Itoa(17122), e.ToAccountBalance.Abs().String()) 861 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 862 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_GENERAL) 863 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeRewardPayout) 864 } 865 } 866 867 filter.CloseOnAccountFilters = true 868 entries, _, err = ledgerStore.Query(ctx, 869 filter, 870 entities.DateRange{Start: &tStart, End: &tEnd}, 871 entities.CursorPagination{}, 872 ) 873 874 require.NoError(t, err) 875 // Output entries for accounts positions -> should output transfers for asset3 only: 876 // 5->10, 5->11, 4->11 877 assert.NotNil(t, entries) 878 assert.Equal(t, 3, len(*entries)) 879 for _, e := range *entries { 880 if e.Quantity.Abs().String() == strconv.Itoa(3) { 881 assert.Equal(t, e.FromAccountPartyID, parties[2].ID) 882 assert.Equal(t, e.ToAccountPartyID, parties[5].ID) 883 assert.Equal(t, e.FromAccountMarketID, markets[3].ID) 884 assert.Equal(t, e.ToAccountMarketID, markets[6].ID) 885 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 886 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_GENERAL) 887 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeRewardPayout) 888 } 889 890 if e.Quantity.Abs().String() == strconv.Itoa(5) { 891 assert.Equal(t, e.FromAccountPartyID, parties[2].ID) 892 assert.Equal(t, e.ToAccountPartyID, parties[5].ID) 893 assert.Equal(t, e.FromAccountMarketID, markets[3].ID) 894 assert.Equal(t, e.ToAccountMarketID, markets[5].ID) 895 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 896 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_GENERAL) 897 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeRewardPayout) 898 } 899 900 if e.Quantity.Abs().String() == strconv.Itoa(72) { 901 assert.Equal(t, e.FromAccountPartyID, parties[2].ID) 902 assert.Equal(t, e.ToAccountPartyID, parties[5].ID) 903 904 assert.Equal(t, e.FromAccountMarketID, markets[2].ID) 905 assert.Equal(t, e.ToAccountMarketID, markets[6].ID) 906 assert.Equal(t, strconv.Itoa(2188), e.FromAccountBalance.Abs().String()) 907 assert.Equal(t, strconv.Itoa(17122), e.ToAccountBalance.Abs().String()) 908 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 909 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_GENERAL) 910 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeRewardPayout) 911 } 912 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeRewardPayout) 913 } 914 915 filter = &entities.LedgerEntryFilter{ 916 FromAccountFilter: entities.AccountFilter{ 917 PartyIDs: []entities.PartyID{parties[2].ID}, 918 }, 919 ToAccountFilter: entities.AccountFilter{ 920 AccountTypes: []types.AccountType{vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY}, 921 }, 922 TransferTypes: []entities.LedgerMovementType{ 923 entities.LedgerMovementTypeRewardPayout, 924 }, 925 } 926 filter.FromAccountFilter.AccountTypes = append(filter.FromAccountFilter.AccountTypes, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 927 entries, _, err = ledgerStore.Query(ctx, 928 filter, 929 entities.DateRange{Start: &tStart, End: &tEnd}, 930 entities.CursorPagination{}, 931 ) 932 933 require.NoError(t, err) 934 // 4->5, 5->10, 5->11, 4->11 935 assert.NotNil(t, entries) 936 assert.Equal(t, 3, len(*entries)) 937 for _, e := range *entries { 938 if e.Quantity.Abs().String() == strconv.Itoa(3) { 939 assert.Equal(t, e.FromAccountPartyID, parties[2].ID) 940 assert.Equal(t, e.ToAccountPartyID, parties[5].ID) 941 assert.Equal(t, e.FromAccountMarketID, markets[3].ID) 942 assert.Equal(t, e.ToAccountMarketID, markets[6].ID) 943 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 944 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_GENERAL) 945 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeRewardPayout) 946 } 947 948 if e.Quantity.Abs().String() == strconv.Itoa(5) { 949 assert.Equal(t, e.FromAccountPartyID, parties[2].ID) 950 assert.Equal(t, e.ToAccountPartyID, parties[5].ID) 951 assert.Equal(t, e.FromAccountMarketID, markets[3].ID) 952 assert.Equal(t, e.ToAccountMarketID, markets[5].ID) 953 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 954 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_GENERAL) 955 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeRewardPayout) 956 } 957 958 if e.Quantity.Abs().String() == strconv.Itoa(72) { 959 assert.Equal(t, e.FromAccountPartyID, parties[2].ID) 960 assert.Equal(t, e.ToAccountPartyID, parties[5].ID) 961 962 assert.Equal(t, e.FromAccountMarketID, markets[2].ID) 963 assert.Equal(t, e.ToAccountMarketID, markets[6].ID) 964 assert.Equal(t, strconv.Itoa(2188), e.FromAccountBalance.Abs().String()) 965 assert.Equal(t, strconv.Itoa(17122), e.ToAccountBalance.Abs().String()) 966 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 967 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_GENERAL) 968 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeRewardPayout) 969 } 970 } 971 972 filter = &entities.LedgerEntryFilter{ 973 FromAccountFilter: entities.AccountFilter{ 974 PartyIDs: []entities.PartyID{parties[2].ID}, 975 }, 976 ToAccountFilter: entities.AccountFilter{ 977 AccountTypes: []types.AccountType{vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY}, 978 }, 979 CloseOnAccountFilters: true, 980 TransferTypes: []entities.LedgerMovementType{ 981 entities.LedgerMovementTypeDeposit, 982 }, 983 } 984 985 entries, _, err = ledgerStore.Query(ctx, 986 filter, 987 entities.DateRange{Start: &tStart, End: &tEnd}, 988 entities.CursorPagination{}, 989 ) 990 991 require.NoError(t, err) 992 assert.NotNil(t, entries) 993 assert.Equal(t, 0, len(*entries)) 994 995 filter = &entities.LedgerEntryFilter{ 996 FromAccountFilter: entities.AccountFilter{ 997 PartyIDs: []entities.PartyID{parties[2].ID}, 998 }, 999 ToAccountFilter: entities.AccountFilter{}, 1000 CloseOnAccountFilters: true, 1001 TransferTypes: []entities.LedgerMovementType{ 1002 entities.LedgerMovementTypeBondSlashing, 1003 entities.LedgerMovementTypeDeposit, 1004 entities.LedgerMovementTypeRewardPayout, 1005 }, 1006 } 1007 1008 entries, _, err = ledgerStore.Query(ctx, 1009 filter, 1010 entities.DateRange{Start: &tStart, End: &tEnd}, 1011 entities.CursorPagination{}, 1012 ) 1013 1014 require.NoError(t, err) 1015 // List transfers: 1016 // accounts 5->11 - 3 - ACCOUNT_TYPE_INSURANCE ACCOUNT_TYPE_GENERAL 1017 // accounts 5->10 - 5 - ACCOUNT_TYPE_INSURANCE ACCOUNT_TYPE_GENERAL 1018 // accounts 4->11 - 72 - ACCOUNT_TYPE_INSURANCE ACCOUNT_TYPE_GENERAL 1019 // accounts 4->5 - 25 - ACCOUNT_TYPE_INSURANCE ACCOUNT_TYPE_INSURANCE 1020 assert.NotNil(t, entries) 1021 assert.Equal(t, 4, len(*entries)) 1022 for _, e := range *entries { 1023 if e.Quantity.Abs().String() == strconv.Itoa(3) { 1024 assert.Equal(t, e.FromAccountPartyID, parties[2].ID) 1025 assert.Equal(t, e.ToAccountPartyID, parties[5].ID) 1026 assert.Equal(t, e.FromAccountMarketID, markets[3].ID) 1027 assert.Equal(t, e.ToAccountMarketID, markets[6].ID) 1028 assert.Equal(t, strconv.Itoa(2587), e.FromAccountBalance.Abs().String()) 1029 assert.Equal(t, strconv.Itoa(5683), e.ToAccountBalance.Abs().String()) 1030 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 1031 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_GENERAL) 1032 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeRewardPayout) 1033 } 1034 1035 if e.Quantity.Abs().String() == strconv.Itoa(5) { 1036 assert.Equal(t, e.FromAccountPartyID, parties[2].ID) 1037 assert.Equal(t, e.ToAccountPartyID, parties[5].ID) 1038 assert.Equal(t, e.FromAccountMarketID, markets[3].ID) 1039 assert.Equal(t, e.ToAccountMarketID, markets[5].ID) 1040 assert.Equal(t, strconv.Itoa(2582), e.FromAccountBalance.Abs().String()) 1041 assert.Equal(t, strconv.Itoa(1510), e.ToAccountBalance.Abs().String()) 1042 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 1043 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_GENERAL) 1044 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeRewardPayout) 1045 } 1046 1047 if e.Quantity.Abs().String() == strconv.Itoa(72) { 1048 assert.Equal(t, e.FromAccountPartyID, parties[2].ID) 1049 assert.Equal(t, e.ToAccountPartyID, parties[5].ID) 1050 1051 assert.Equal(t, e.FromAccountMarketID, markets[2].ID) 1052 assert.Equal(t, e.ToAccountMarketID, markets[6].ID) 1053 assert.Equal(t, strconv.Itoa(2188), e.FromAccountBalance.Abs().String()) 1054 assert.Equal(t, strconv.Itoa(17122), e.ToAccountBalance.Abs().String()) 1055 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 1056 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_GENERAL) 1057 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeRewardPayout) 1058 } 1059 1060 if e.Quantity.Abs().String() == strconv.Itoa(25) { 1061 assert.Equal(t, e.FromAccountPartyID, parties[2].ID) 1062 assert.Equal(t, e.ToAccountPartyID, parties[2].ID) 1063 1064 assert.Equal(t, e.FromAccountMarketID, markets[2].ID) 1065 assert.Equal(t, e.ToAccountMarketID, markets[3].ID) 1066 assert.Equal(t, strconv.Itoa(1700), e.FromAccountBalance.Abs().String()) 1067 assert.Equal(t, strconv.Itoa(2590), e.ToAccountBalance.Abs().String()) 1068 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 1069 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 1070 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeBondSlashing) 1071 } 1072 } 1073 1074 filter = &entities.LedgerEntryFilter{ 1075 FromAccountFilter: entities.AccountFilter{ 1076 PartyIDs: []entities.PartyID{parties[2].ID}, 1077 }, 1078 ToAccountFilter: entities.AccountFilter{ 1079 AccountTypes: []types.AccountType{vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY}, 1080 }, 1081 CloseOnAccountFilters: true, 1082 TransferTypes: []entities.LedgerMovementType{ 1083 entities.LedgerMovementTypeBondSlashing, 1084 entities.LedgerMovementTypeDeposit, 1085 entities.LedgerMovementTypeRewardPayout, 1086 }, 1087 } 1088 1089 entries, _, err = ledgerStore.Query(ctx, 1090 filter, 1091 entities.DateRange{Start: &tStart, End: &tEnd}, 1092 entities.CursorPagination{}, 1093 ) 1094 1095 require.NoError(t, err) 1096 // List transfers: 1097 // 0 1098 assert.NotNil(t, entries) 1099 assert.Equal(t, 0, len(*entries)) 1100 }) 1101 1102 t.Run("test with same account and transfer types given multiple times", func(t *testing.T) { 1103 filter := &entities.LedgerEntryFilter{ 1104 FromAccountFilter: entities.AccountFilter{ 1105 PartyIDs: []entities.PartyID{parties[2].ID}, 1106 AccountTypes: []types.AccountType{vega.AccountType_ACCOUNT_TYPE_INSURANCE, vega.AccountType_ACCOUNT_TYPE_INSURANCE, vega.AccountType_ACCOUNT_TYPE_INSURANCE}, 1107 }, 1108 ToAccountFilter: entities.AccountFilter{ 1109 AccountTypes: []types.AccountType{vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY, vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY}, 1110 }, 1111 TransferTypes: []entities.LedgerMovementType{ 1112 entities.LedgerMovementTypeRewardPayout, entities.LedgerMovementTypeRewardPayout, entities.LedgerMovementTypeRewardPayout, 1113 }, 1114 } 1115 entries, _, err := ledgerStore.Query(ctx, 1116 filter, 1117 entities.DateRange{Start: &tStart, End: &tEnd}, 1118 entities.CursorPagination{}, 1119 ) 1120 1121 require.NoError(t, err) 1122 // 4->5, 5->10, 5->11, 4->11 1123 assert.NotNil(t, entries) 1124 assert.Equal(t, 3, len(*entries)) 1125 for _, e := range *entries { 1126 if e.Quantity.Abs().String() == strconv.Itoa(3) { 1127 assert.Equal(t, e.FromAccountPartyID, parties[2].ID) 1128 assert.Equal(t, e.ToAccountPartyID, parties[5].ID) 1129 assert.Equal(t, e.FromAccountMarketID, markets[3].ID) 1130 assert.Equal(t, e.ToAccountMarketID, markets[6].ID) 1131 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 1132 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_GENERAL) 1133 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeRewardPayout) 1134 } 1135 1136 if e.Quantity.Abs().String() == strconv.Itoa(5) { 1137 assert.Equal(t, e.FromAccountPartyID, parties[2].ID) 1138 assert.Equal(t, e.ToAccountPartyID, parties[5].ID) 1139 assert.Equal(t, e.FromAccountMarketID, markets[3].ID) 1140 assert.Equal(t, e.ToAccountMarketID, markets[5].ID) 1141 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 1142 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_GENERAL) 1143 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeRewardPayout) 1144 } 1145 1146 if e.Quantity.Abs().String() == strconv.Itoa(72) { 1147 assert.Equal(t, e.FromAccountPartyID, parties[2].ID) 1148 assert.Equal(t, e.ToAccountPartyID, parties[5].ID) 1149 1150 assert.Equal(t, e.FromAccountMarketID, markets[2].ID) 1151 assert.Equal(t, e.ToAccountMarketID, markets[6].ID) 1152 assert.Equal(t, strconv.Itoa(2188), e.FromAccountBalance.Abs().String()) 1153 assert.Equal(t, strconv.Itoa(17122), e.ToAccountBalance.Abs().String()) 1154 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 1155 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_GENERAL) 1156 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeRewardPayout) 1157 } 1158 } 1159 1160 filter = &entities.LedgerEntryFilter{ 1161 FromAccountFilter: entities.AccountFilter{ 1162 PartyIDs: []entities.PartyID{parties[2].ID}, 1163 AccountTypes: []types.AccountType{ 1164 vega.AccountType_ACCOUNT_TYPE_INSURANCE, vega.AccountType_ACCOUNT_TYPE_INSURANCE, vega.AccountType_ACCOUNT_TYPE_INSURANCE, 1165 vega.AccountType_ACCOUNT_TYPE_GENERAL, vega.AccountType_ACCOUNT_TYPE_GENERAL, 1166 }, 1167 }, 1168 ToAccountFilter: entities.AccountFilter{ 1169 AccountTypes: []types.AccountType{vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY, vega.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY}, 1170 }, 1171 TransferTypes: []entities.LedgerMovementType{ 1172 entities.LedgerMovementTypeRewardPayout, entities.LedgerMovementTypeRewardPayout, entities.LedgerMovementTypeRewardPayout, 1173 entities.LedgerMovementTypeBondSlashing, entities.LedgerMovementTypeBondSlashing, 1174 }, 1175 } 1176 entries, _, err = ledgerStore.Query(ctx, 1177 filter, 1178 entities.DateRange{Start: &tStart, End: &tEnd}, 1179 entities.CursorPagination{}, 1180 ) 1181 1182 require.NoError(t, err) 1183 // 4->5, 5->10, 5->11, 4->11 1184 assert.NotNil(t, entries) 1185 assert.Equal(t, 4, len(*entries)) 1186 for _, e := range *entries { 1187 if e.Quantity.Abs().String() == strconv.Itoa(3) { 1188 assert.Equal(t, e.FromAccountPartyID, parties[2].ID) 1189 assert.Equal(t, e.ToAccountPartyID, parties[5].ID) 1190 assert.Equal(t, e.FromAccountMarketID, markets[3].ID) 1191 assert.Equal(t, e.ToAccountMarketID, markets[6].ID) 1192 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 1193 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_GENERAL) 1194 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeRewardPayout) 1195 } 1196 1197 if e.Quantity.Abs().String() == strconv.Itoa(5) { 1198 assert.Equal(t, e.FromAccountPartyID, parties[2].ID) 1199 assert.Equal(t, e.ToAccountPartyID, parties[5].ID) 1200 assert.Equal(t, e.FromAccountMarketID, markets[3].ID) 1201 assert.Equal(t, e.ToAccountMarketID, markets[5].ID) 1202 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 1203 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_GENERAL) 1204 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeRewardPayout) 1205 } 1206 1207 if e.Quantity.Abs().String() == strconv.Itoa(25) { 1208 assert.Equal(t, e.FromAccountPartyID, parties[2].ID) 1209 assert.Equal(t, e.ToAccountPartyID, parties[2].ID) 1210 1211 assert.Equal(t, e.FromAccountMarketID, markets[2].ID) 1212 assert.Equal(t, e.ToAccountMarketID, markets[3].ID) 1213 assert.Equal(t, strconv.Itoa(1700), e.FromAccountBalance.Abs().String()) 1214 assert.Equal(t, strconv.Itoa(2590), e.ToAccountBalance.Abs().String()) 1215 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 1216 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 1217 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeBondSlashing) 1218 } 1219 1220 if e.Quantity.Abs().String() == strconv.Itoa(72) { 1221 assert.Equal(t, e.FromAccountPartyID, parties[2].ID) 1222 assert.Equal(t, e.ToAccountPartyID, parties[5].ID) 1223 1224 assert.Equal(t, e.FromAccountMarketID, markets[2].ID) 1225 assert.Equal(t, e.ToAccountMarketID, markets[6].ID) 1226 assert.Equal(t, strconv.Itoa(2188), e.FromAccountBalance.Abs().String()) 1227 assert.Equal(t, strconv.Itoa(17122), e.ToAccountBalance.Abs().String()) 1228 assert.Equal(t, e.FromAccountType, vega.AccountType_ACCOUNT_TYPE_INSURANCE) 1229 assert.Equal(t, e.ToAccountType, vega.AccountType_ACCOUNT_TYPE_GENERAL) 1230 assert.Equal(t, e.TransferType, entities.LedgerMovementTypeRewardPayout) 1231 } 1232 } 1233 }) 1234 1235 t.Run("by transfer id filter", func(t *testing.T) { 1236 filter := &entities.LedgerEntryFilter{ 1237 TransferID: "deadbeef01", 1238 } 1239 1240 entries, _, err := ledgerStore.Query(ctx, 1241 filter, 1242 entities.DateRange{Start: &tStart, End: &tEnd}, 1243 entities.CursorPagination{}, 1244 ) 1245 require.NoError(t, err) 1246 assert.NotNil(t, entries) 1247 1248 got := *entries 1249 want := ledgerEntries[0] 1250 assert.Len(t, got, 1) 1251 1252 assert.Equal(t, want.VegaTime, got[0].VegaTime) 1253 assert.Equal(t, want.FromAccountBalance, got[0].FromAccountBalance) 1254 assert.Equal(t, want.ToAccountBalance, got[0].ToAccountBalance) 1255 }) 1256 }) 1257 }