code.vegaprotocol.io/vega@v0.79.0/core/events/reward_payout.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 17 18 import ( 19 "context" 20 21 "code.vegaprotocol.io/vega/core/types" 22 "code.vegaprotocol.io/vega/libs/num" 23 vegapb "code.vegaprotocol.io/vega/protos/vega" 24 eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1" 25 ) 26 27 type RewardPayout struct { 28 *Base 29 Party string 30 EpochSeq string 31 Asset string 32 GameID *string 33 PercentageOfTotalReward string 34 Amount *num.Uint 35 QuantumAmount num.Decimal 36 Timestamp int64 37 RewardType types.AccountType 38 LockedUntilEpoch string 39 } 40 41 func NewRewardPayout(ctx context.Context, timestamp int64, party, epochSeq, asset string, amount *num.Uint, assetQuantum, percentageOfTotalReward num.Decimal, rewardType types.AccountType, gameID *string, lockedUntilEpoch string) *RewardPayout { 42 return &RewardPayout{ 43 Base: newBase(ctx, RewardPayoutEvent), 44 Party: party, 45 EpochSeq: epochSeq, 46 Asset: asset, 47 PercentageOfTotalReward: percentageOfTotalReward.String(), 48 Amount: amount, 49 QuantumAmount: amount.ToDecimal().Div(assetQuantum).Truncate(6), 50 Timestamp: timestamp, 51 RewardType: rewardType, 52 GameID: gameID, 53 LockedUntilEpoch: lockedUntilEpoch, 54 } 55 } 56 57 func (rp RewardPayout) RewardPayoutEvent() eventspb.RewardPayoutEvent { 58 return rp.Proto() 59 } 60 61 func (rp RewardPayout) Proto() eventspb.RewardPayoutEvent { 62 return eventspb.RewardPayoutEvent{ 63 Party: rp.Party, 64 EpochSeq: rp.EpochSeq, 65 Asset: rp.Asset, 66 Amount: rp.Amount.String(), 67 QuantumAmount: rp.QuantumAmount.String(), 68 PercentOfTotalReward: rp.PercentageOfTotalReward, 69 Timestamp: rp.Timestamp, 70 RewardType: vegapb.AccountType_name[int32(rp.RewardType)], 71 GameId: rp.GameID, 72 LockedUntilEpoch: rp.LockedUntilEpoch, 73 } 74 } 75 76 func (rp RewardPayout) StreamMessage() *eventspb.BusEvent { 77 p := rp.Proto() 78 busEvent := newBusEventFromBase(rp.Base) 79 busEvent.Event = &eventspb.BusEvent_RewardPayout{ 80 RewardPayout: &p, 81 } 82 83 return busEvent 84 } 85 86 func RewardPayoutEventFromStream(ctx context.Context, be *eventspb.BusEvent) *RewardPayout { 87 rp := be.GetRewardPayout() 88 if rp == nil { 89 return nil 90 } 91 92 amount, _ := num.UintFromString(rp.Amount, 10) 93 quantumAmount, _ := num.DecimalFromString(rp.QuantumAmount) 94 return &RewardPayout{ 95 Base: newBaseFromBusEvent(ctx, RewardPayoutEvent, be), 96 Party: rp.Party, 97 EpochSeq: rp.EpochSeq, 98 Asset: rp.Asset, 99 PercentageOfTotalReward: rp.PercentOfTotalReward, 100 Amount: amount, 101 QuantumAmount: quantumAmount, 102 Timestamp: rp.Timestamp, 103 GameID: rp.GameId, 104 LockedUntilEpoch: rp.LockedUntilEpoch, 105 RewardType: types.AccountType(vegapb.AccountType_value[rp.RewardType]), 106 } 107 }