-
Notifications
You must be signed in to change notification settings - Fork 12
/
transfer_relation.go
72 lines (61 loc) · 2.3 KB
/
transfer_relation.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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/functionx/fx-core/v7/x/erc20/types"
)
func (k Keeper) RefundAfter(ctx sdk.Context, channel string, sequence uint64, sender sdk.AccAddress, amount sdk.Coin) {
// check exist
if !k.DeleteIBCTransferRelation(ctx, channel, sequence) {
return
}
cacheCtx, commit := ctx.CacheContext()
_, err := k.ConvertCoin(sdk.WrapSDKContext(cacheCtx), &types.MsgConvertCoin{
Coin: amount,
Receiver: common.BytesToAddress(sender.Bytes()).String(),
Sender: sender.String(),
})
if err != nil {
k.Logger(ctx).Info("refund after", "channel", channel, "sequence", sequence, "sender", sender, "error", err)
return
}
commit()
}
func (k Keeper) AckAfter(ctx sdk.Context, channel string, sequence uint64) {
k.DeleteIBCTransferRelation(ctx, channel, sequence)
}
func (k Keeper) SetIBCTransferRelation(ctx sdk.Context, channel string, sequence uint64) {
store := ctx.KVStore(k.storeKey)
store.Set(types.GetIBCTransferKey(channel, sequence), []byte{})
}
func (k Keeper) DeleteIBCTransferRelation(ctx sdk.Context, channel string, sequence uint64) bool {
store := ctx.KVStore(k.storeKey)
key := types.GetIBCTransferKey(channel, sequence)
if !store.Has(key) {
return false
}
store.Delete(key)
return true
}
func (k Keeper) HookOutgoingRefund(ctx sdk.Context, moduleName string, txID uint64, sender sdk.AccAddress, totalCoin sdk.Coin) error {
if _, err := k.ConvertCoin(sdk.WrapSDKContext(ctx), &types.MsgConvertCoin{
Coin: totalCoin,
Receiver: common.BytesToAddress(sender.Bytes()).String(),
Sender: sender.String(),
}); err != nil {
return err
}
k.DeleteOutgoingTransferRelation(ctx, moduleName, txID)
return nil
}
func (k Keeper) SetOutgoingTransferRelation(ctx sdk.Context, moduleName string, txID uint64) {
store := ctx.KVStore(k.storeKey)
store.Set(types.GetOutgoingTransferKey(moduleName, txID), []byte{})
}
func (k Keeper) DeleteOutgoingTransferRelation(ctx sdk.Context, moduleName string, txID uint64) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetOutgoingTransferKey(moduleName, txID))
}
func (k Keeper) HasOutgoingTransferRelation(ctx sdk.Context, moduleName string, txID uint64) bool {
return ctx.KVStore(k.storeKey).Has(types.GetOutgoingTransferKey(moduleName, txID))
}