forked from Finschia/finschia-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreasury.go
73 lines (58 loc) · 1.94 KB
/
treasury.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package internal
import (
sdk "github.com/line/lbm-sdk/types"
sdkerrors "github.com/line/lbm-sdk/types/errors"
"github.com/line/lbm-sdk/x/foundation"
)
func (k Keeper) CollectFoundationTax(ctx sdk.Context) error {
feeCollector := k.authKeeper.GetModuleAccount(ctx, k.feeCollectorName).GetAddress()
feesCollectedInt := k.bankKeeper.GetAllBalances(ctx, feeCollector)
if feesCollectedInt.Empty() {
return nil
}
feesCollected := sdk.NewDecCoinsFromCoins(feesCollectedInt...)
// calculate the tax
taxRatio := k.GetFoundationTax(ctx)
tax, _ := feesCollected.MulDecTruncate(taxRatio).TruncateDecimal()
if tax.Empty() {
return nil
}
// collect the tax
if err := k.FundTreasury(ctx, feeCollector, tax); err != nil {
return err
}
return nil
}
func (k Keeper) GetTreasury(ctx sdk.Context) sdk.DecCoins {
return k.GetPool(ctx).Treasury
}
func (k Keeper) FundTreasury(ctx sdk.Context, from sdk.AccAddress, amt sdk.Coins) error {
pool := k.GetPool(ctx)
pool.Treasury = pool.Treasury.Add(sdk.NewDecCoinsFromCoins(amt...)...)
k.SetPool(ctx, pool)
return k.bankKeeper.SendCoinsFromAccountToModule(ctx, from, foundation.TreasuryName, amt)
}
func (k Keeper) WithdrawFromTreasury(ctx sdk.Context, to sdk.AccAddress, amt sdk.Coins) error {
pool := k.GetPool(ctx)
remains, hasNeg := pool.Treasury.SafeSub(sdk.NewDecCoinsFromCoins(amt...))
if hasNeg {
return sdkerrors.ErrInsufficientFunds.Wrapf("not enough coins in treasury, %s", pool.Treasury)
}
pool.Treasury = remains
k.SetPool(ctx, pool)
return k.bankKeeper.SendCoinsFromModuleToAccount(ctx, foundation.TreasuryName, to, amt)
}
func (k Keeper) GetPool(ctx sdk.Context) foundation.Pool {
store := ctx.KVStore(k.storeKey)
key := poolKey
bz := store.Get(key)
var pool foundation.Pool
k.cdc.MustUnmarshal(bz, &pool)
return pool
}
func (k Keeper) SetPool(ctx sdk.Context, pool foundation.Pool) {
bz := k.cdc.MustMarshal(&pool)
store := ctx.KVStore(k.storeKey)
key := poolKey
store.Set(key, bz)
}