-
Notifications
You must be signed in to change notification settings - Fork 6
/
cmptimelockbalance.go
138 lines (124 loc) · 3.57 KB
/
cmptimelockbalance.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
134
135
136
137
138
// Copyright 2019 The fsn-go-sdk Authors
// This file is part of the fsn-go-sdk library.
//
// The fsn-go-sdk library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The fsn-go-sdk library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the fsn-go-sdk library. If not, see <http://www.gnu.org/licenses/>.
package extensions
import (
"context"
"fmt"
"math/big"
"time"
"github.com/fsn-dev/fsn-go-sdk/efsn/common"
"github.com/fsn-dev/fsn-go-sdk/efsn/tools"
clicommon "github.com/fsn-dev/fsn-go-sdk/fsn-cli/common"
"gopkg.in/urfave/cli.v1"
)
var CommandCmpTimeLockBalance = cli.Command{
Name: "cmptimelockbalance",
Aliases: []string{"cmptlb"},
Category: "online-extensions",
Usage: "cmp time lock balance",
ArgsUsage: "<assetID> <address> <value>",
Description: `
cmp time lock balance`,
Flags: []cli.Flag{
timeLockStartFlag,
timeLockEndFlag,
blockHeightFlag,
serverAddrFlag,
},
Action: cmptimelockbalance,
}
func cmptimelockbalance(ctx *cli.Context) error {
setLogger(ctx)
if len(ctx.Args()) != 3 {
cli.ShowCommandHelpAndExit(ctx, "cmptimelockbalance", 1)
}
client := dialServer(ctx)
defer client.Close()
assetID := clicommon.GetHashFromText("assetID", ctx.Args().Get(0))
address := clicommon.GetAddressFromText("address", ctx.Args().Get(1))
value := clicommon.GetBigIntFromText("value", ctx.Args().Get(2))
start := getUint64Time(ctx, timeLockStartFlag.Name, common.TimeLockNow)
end := getUint64Time(ctx, timeLockEndFlag.Name, common.TimeLockForever)
blockNr := clicommon.GetBlockNumberFromText(ctx.String(blockHeightFlag.Name))
if start == 0 {
start = uint64(time.Now().Unix())
}
if end == 0 {
end = common.TimeLockForever
}
timelock := common.NewTimeLock(&common.TimeLockItem{
StartTime: start,
EndTime: end,
Value: value,
})
if err := timelock.IsValid(); err != nil {
return err
}
balance, err := client.GetRawTimeLockBalance(context.Background(), assetID, address, blockNr)
if err != nil {
return err
}
cmp := balance.Cmp(timelock)
if cmp >= 0 {
fmt.Println("Has enough timelock balance of")
tools.MustPrintJSON(timelock)
return nil
}
diff := &common.TimeLock{}
x := start
for _, item := range balance.Items {
if x > end {
break
}
if item.EndTime < start {
continue
}
if item.StartTime > x { // hole
diff.Items = append(diff.Items, &common.TimeLockItem{
StartTime: x,
EndTime: common.MinUint64(item.StartTime-1, end),
Value: value,
})
}
if item.EndTime != common.TimeLockForever {
x = item.EndTime + 1
} else {
x = item.EndTime
}
if item.Value.Cmp(value) >= 0 {
continue
}
if item.StartTime > end {
break
}
// intersection
diff.Items = append(diff.Items, &common.TimeLockItem{
StartTime: common.MaxUint64(item.StartTime, start),
EndTime: common.MinUint64(item.EndTime, end),
Value: new(big.Int).Sub(value, item.Value),
})
}
if x < end { // last hole
diff.Items = append(diff.Items, &common.TimeLockItem{
StartTime: x,
EndTime: end,
Value: value,
})
}
fmt.Println("Not enough timelock balance, differnce is")
tools.MustPrintJSON(diff)
return nil
}