forked from lino-network/lino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
47 lines (41 loc) · 1.31 KB
/
event.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package account
import (
sdk "github.com/cosmos/cosmos-sdk/types"
types "github.com/lino-network/lino/types"
)
// ReturnCoinEvent - return a certain amount of coin to an account
type ReturnCoinEvent struct {
Username types.AccountKey `json:"username"`
Amount types.Coin `json:"amount"`
ReturnType types.TransferDetailType `json:"return_type"`
}
// Execute - execute coin return events
func (event ReturnCoinEvent) Execute(ctx sdk.Context, am AccountManager) sdk.Error {
if !am.DoesAccountExist(ctx, event.Username) {
return ErrAccountNotFound(event.Username)
}
if err := am.AddSavingCoin(
ctx, event.Username, event.Amount, "", "",
event.ReturnType); err != nil {
return err
}
return nil
}
// CreateCoinReturnEvents - create coin return events
func CreateCoinReturnEvents(
username types.AccountKey, times int64, interval int64, coin types.Coin,
returnType types.TransferDetailType) ([]types.Event, sdk.Error) {
events := []types.Event{}
for i := int64(0); i < times; i++ {
pieceRat := coin.ToRat().Quo(sdk.NewRat(times - i)).Round(types.PrecisionFactor)
piece := types.RatToCoin(pieceRat)
coin = coin.Minus(piece)
event := ReturnCoinEvent{
Username: username,
Amount: piece,
ReturnType: returnType,
}
events = append(events, event)
}
return events, nil
}