/
mapper.go
50 lines (40 loc) · 1.18 KB
/
mapper.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
package bank
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// 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
}