/
mint.go
66 lines (58 loc) · 2.14 KB
/
mint.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
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sideprotocol/ibcswap/v6/modules/apps/101-interchain-swap/types"
)
func (k Keeper) LockTokens(ctx sdk.Context, sourcePort string, sourceChannel string, sender sdk.AccAddress, tokens sdk.Coins) error {
// create the escrow address for the tokens
escrowAddress := types.GetEscrowAddress(sourcePort, sourceChannel)
// escrow source tokens. It fails if balance insufficient
if err := k.bankKeeper.SendCoins(
ctx, sender, escrowAddress, tokens,
); err != nil {
return err
}
return nil
}
func (k Keeper) BurnTokens(ctx sdk.Context, sender sdk.AccAddress, tokens sdk.Coin) error {
// transfer the coins to the module account and burn them
if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, sdk.NewCoins(tokens)); err != nil {
return err
}
if err := k.bankKeeper.BurnCoins(
ctx, types.ModuleName, sdk.NewCoins(tokens),
); err != nil {
// NOTE: should not happen as the module account was
// retrieved on the step above and it has enough balance
// to burn.
panic(fmt.Sprintf("cannot burn coins after a successful send to a module account: %v", err))
}
return nil
}
func (k Keeper) MintTokens(ctx sdk.Context, receiver sdk.AccAddress, tokens sdk.Coin) error {
// mint new tokens if the source of the transfer is the same chain
if err := k.bankKeeper.MintCoins(
ctx, types.ModuleName, sdk.NewCoins(tokens),
); err != nil {
return err
}
// send to receiver
if err := k.bankKeeper.SendCoinsFromModuleToAccount(
ctx, types.ModuleName, receiver, sdk.NewCoins(tokens),
); err != nil {
panic(fmt.Sprintf("unable to send coins from module to account despite previously minting coins to module account: %v", err))
}
return nil
}
func (k Keeper) UnlockTokens(ctx sdk.Context, port string, channel string, receiver sdk.AccAddress, tokens sdk.Coins) error {
// create the escrow address for the tokens
escrowAddress := types.GetEscrowAddress(port, channel)
// escrow source tokens. It fails if balance insufficient
if err := k.bankKeeper.SendCoins(
ctx, escrowAddress, receiver, tokens,
); err != nil {
return err
}
return nil
}