-
Notifications
You must be signed in to change notification settings - Fork 127
/
migrate.go
133 lines (116 loc) · 3.56 KB
/
migrate.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package htlc
import (
"time"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
htlckeeper "github.com/irisnet/irismod/modules/htlc/keeper"
htlctypes "github.com/irisnet/irismod/modules/htlc/types"
)
func Migrate(ctx sdk.Context, cdc codec.Codec, k htlckeeper.Keeper, bk bankkeeper.Keeper, key *storetypes.KVStoreKey) error {
if err := k.EnsureModuleAccountPermissions(ctx); err != nil {
return err
}
store := ctx.KVStore(key)
// Delete expired queue
store.Delete(HTLCExpiredQueueKey)
iterator := sdk.KVStorePrefixIterator(store, HTLCKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
hashLock := tmbytes.HexBytes(iterator.Key()[1:])
var htlc OldHTLC
cdc.MustUnmarshal(iterator.Value(), &htlc)
sender, err := sdk.AccAddressFromBech32(htlc.Sender)
if err != nil {
return err
}
receiver, err := sdk.AccAddressFromBech32(htlc.To)
if err != nil {
return err
}
id := htlctypes.GetID(sender, receiver, htlc.Amount, hashLock)
expirationHeight := htlc.ExpirationHeight
closedBlock := uint64(0)
var state htlctypes.HTLCState
switch htlc.State {
case Open:
state = htlctypes.Open
// Add to expired queue
k.AddHTLCToExpiredQueue(ctx, expirationHeight, id)
case Completed:
state = htlctypes.Completed
case Expired:
// Refund expired htlc
state = htlctypes.Refunded
if err := bk.SendCoinsFromModuleToAccount(ctx, htlctypes.ModuleName, sender, htlc.Amount); err != nil {
return err
}
closedBlock = uint64(ctx.BlockHeight())
case Refunded:
state = htlctypes.Refunded
}
// Delete origin htlc
store.Delete(GetHTLCKey(hashLock))
newHTLC := htlctypes.HTLC{
Id: id.String(),
Sender: htlc.Sender,
To: htlc.To,
ReceiverOnOtherChain: htlc.ReceiverOnOtherChain,
SenderOnOtherChain: "",
Amount: htlc.Amount,
HashLock: hashLock.String(),
Secret: htlc.Secret,
Timestamp: htlc.Timestamp,
ExpirationHeight: expirationHeight,
State: state,
ClosedBlock: closedBlock,
Transfer: false,
Direction: htlctypes.None,
}
// Set new htlc
k.SetHTLC(ctx, newHTLC, id)
}
// Set default params
k.SetParams(ctx, PresetHTLTParams())
return nil
}
func PresetHTLTParams() htlctypes.Params {
return htlctypes.Params{
AssetParams: []htlctypes.AssetParam{
{
Denom: "htltbcbnb",
SupplyLimit: htlctypes.SupplyLimit{
Limit: sdk.NewInt(100000000000000),
TimeLimited: false,
TimeBasedLimit: sdk.ZeroInt(),
TimePeriod: time.Duration(0),
},
Active: true,
DeputyAddress: "iaa1junhkdhuamtdz3ah6d5mfp6w9sxmlwera7mruz",
FixedFee: sdk.NewInt(1000),
MinSwapAmount: sdk.NewInt(1001),
MaxSwapAmount: sdk.NewInt(1000000000000),
MinBlockLock: 50,
MaxBlockLock: 34560,
},
{
Denom: "htltbcbusd",
SupplyLimit: htlctypes.SupplyLimit{
Limit: sdk.NewInt(2000000000000000),
TimeLimited: false,
TimeBasedLimit: sdk.ZeroInt(),
TimePeriod: time.Duration(0),
},
Active: true,
DeputyAddress: "iaa1z2sdef0ypat9lq7wsxrt7ue3uzdnzcsd34wsl4",
FixedFee: sdk.NewInt(20000),
MinSwapAmount: sdk.NewInt(20001),
MaxSwapAmount: sdk.NewInt(15000000000000),
MinBlockLock: 50,
MaxBlockLock: 34560,
},
},
}
}