forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeeper.go
67 lines (53 loc) · 1.54 KB
/
keeper.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
package bank
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
)
const moduleName = "bank"
// CoinKeeper manages transfers between accounts
type CoinKeeper struct {
am sdk.AccountMapper
}
// NewCoinKeeper returns a new CoinKeeper
func NewCoinKeeper(am sdk.AccountMapper) CoinKeeper {
return CoinKeeper{am: am}
}
// SubtractCoins subtracts amt from the coins at the addr.
func (ck CoinKeeper) SubtractCoins(ctx sdk.Context, addr sdk.Address, amt sdk.Coins) (sdk.Coins, sdk.Error) {
acc := ck.am.GetAccount(ctx, addr)
if acc == nil {
return amt, sdk.ErrUnknownAddress(addr.String())
}
coins := acc.GetCoins()
newCoins := coins.Minus(amt)
if !newCoins.IsNotNegative() {
return amt, sdk.ErrInsufficientCoins(fmt.Sprintf("%s < %s", coins, amt))
}
acc.SetCoins(newCoins)
ck.am.SetAccount(ctx, acc)
return newCoins, nil
}
// AddCoins adds amt to the coins at the addr.
func (ck CoinKeeper) AddCoins(ctx sdk.Context, addr sdk.Address, amt sdk.Coins) (sdk.Coins, sdk.Error) {
acc := ck.am.GetAccount(ctx, addr)
if acc == nil {
acc = ck.am.NewAccountWithAddress(ctx, addr)
}
coins := acc.GetCoins()
newCoins := coins.Plus(amt)
acc.SetCoins(newCoins)
ck.am.SetAccount(ctx, acc)
return newCoins, nil
}
// SendCoins moves coins from one account to another
func (ck CoinKeeper) SendCoins(ctx sdk.Context, fromAddr sdk.Address, toAddr sdk.Address, amt sdk.Coins) sdk.Error {
_, err := ck.SubtractCoins(ctx, fromAddr, amt)
if err != nil {
return err
}
_, err = ck.AddCoins(ctx, toAddr, amt)
if err != nil {
return err
}
return nil
}