code.vegaprotocol.io/vega@v0.79.0/core/execution/common/fees_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 common_test 17 18 import ( 19 "testing" 20 "time" 21 22 "code.vegaprotocol.io/vega/core/execution/common" 23 "code.vegaprotocol.io/vega/libs/num" 24 25 "github.com/stretchr/testify/require" 26 ) 27 28 func TestFeeSplitter(t *testing.T) { 29 var ( 30 totalStake = num.NewUint(100) 31 timeWindowStart = time.Now() 32 marketValueWindowLength = 1 * time.Minute 33 ) 34 35 tests := []struct { 36 currentTime time.Time 37 tradedValue *num.Uint 38 expectedValueProxy num.Decimal 39 }{ 40 { 41 tradedValue: num.UintZero(), 42 currentTime: timeWindowStart, 43 expectedValueProxy: num.DecimalFromFloat(100), 44 }, 45 { 46 tradedValue: num.NewUint(10), 47 currentTime: timeWindowStart.Add(10 * time.Second), 48 expectedValueProxy: num.DecimalFromFloat(100), 49 }, 50 { 51 tradedValue: num.NewUint(100), 52 currentTime: timeWindowStart.Add(30 * time.Second), 53 expectedValueProxy: num.DecimalFromFloat(200), 54 }, 55 { 56 tradedValue: num.NewUint(300), 57 currentTime: timeWindowStart.Add(3 * marketValueWindowLength), 58 expectedValueProxy: num.DecimalFromFloat(300), 59 }, 60 } 61 62 for _, test := range tests { 63 t.Run("", func(t *testing.T) { 64 fs := common.NewFeeSplitter() 65 fs.TimeWindowStart(timeWindowStart) 66 require.NoError(t, 67 fs.SetCurrentTime(test.currentTime), 68 ) 69 fs.AddTradeValue(test.tradedValue) 70 71 got := fs.MarketValueProxy(marketValueWindowLength, totalStake) 72 require.True(t, test.expectedValueProxy.Equal(got)) 73 }) 74 } 75 } 76 77 func TestFeeSplitterSnapshot(t *testing.T) { 78 fs := common.NewFeeSplitter() 79 require.True(t, fs.Changed()) 80 81 // reset changed flags 82 fs.GetState() 83 require.False(t, fs.Changed()) 84 85 // add a trade value to cause a change 86 fs.AddTradeValue(num.NewUint(12)) 87 require.True(t, fs.Changed()) 88 89 // reset changed flag 90 fs.GetState() 91 require.False(t, fs.Changed()) 92 93 // set time window to cause a change 94 fs.TimeWindowStart(time.Now()) 95 require.True(t, fs.Changed()) 96 97 currentTime := time.Now().Add(time.Minute) 98 err := fs.SetCurrentTime(currentTime) 99 require.NoError(t, err) 100 101 // load state and check its the same 102 snap := common.NewFeeSplitterFromSnapshot(fs.GetState(), currentTime) 103 require.Equal(t, fs.Elapsed(), snap.Elapsed()) 104 require.Equal(t, fs.MarketValueProxy(3*time.Second, num.NewUint(5)), snap.MarketValueProxy(3*time.Second, num.NewUint(5))) 105 }