forked from iost-official/go-iost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem_ram.go
80 lines (73 loc) · 2.27 KB
/
system_ram.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
package iwallet
import (
"strconv"
"github.com/spf13/cobra"
)
var other string
var buyCmd = &cobra.Command{
Use: "ram-buy amount",
Aliases: []string{"buy"},
Short: "Buy ram from system",
Long: `Buy ram from system`,
Example: ` iwallet sys buy 100 --account test0
iwallet sys buy 100 --account test0 --ram_receiver test1`,
Args: func(cmd *cobra.Command, args []string) error {
if err := checkArgsNumber(cmd, args, "amount"); err != nil {
return err
}
if err := checkFloat(cmd, args[0], "amount"); err != nil {
return err
}
return checkAccount(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
if other == "" {
other = accountName
}
amount, _ := strconv.ParseFloat(args[0], 64)
return saveOrSendAction("ram.iost", "buy", accountName, other, amount)
},
}
var sellCmd = &cobra.Command{
Use: "ram-sell amount",
Aliases: []string{"sell"},
Short: "Sell unused ram to system",
Long: `Sell unused ram to system`,
Example: ` iwallet sys sell 100 --account test0
iwallet sys sell 100 --account test0 --token_receiver test1`,
Args: buyCmd.Args,
RunE: func(cmd *cobra.Command, args []string) error {
if other == "" {
other = accountName
}
amount, _ := strconv.ParseFloat(args[0], 64)
return saveOrSendAction("ram.iost", "sell", accountName, other, amount)
},
}
var rtransCmd = &cobra.Command{
Use: "ram-transfer receiver amount",
Aliases: []string{"ram-trans", "rtrans"},
Short: "Transfer ram",
Long: `Transfer ram`,
Example: ` iwallet sys ram-transfer test1 100 --account test0`,
Args: func(cmd *cobra.Command, args []string) error {
if err := checkArgsNumber(cmd, args, "receiver", "amount"); err != nil {
return err
}
if err := checkFloat(cmd, args[1], "amount"); err != nil {
return err
}
return checkAccount(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
amount, _ := strconv.ParseFloat(args[1], 64)
return saveOrSendAction("ram.iost", "lend", accountName, args[0], amount)
},
}
func init() {
systemCmd.AddCommand(buyCmd)
buyCmd.Flags().StringVarP(&other, "ram_receiver", "", "", "who gets the bought ram")
systemCmd.AddCommand(sellCmd)
sellCmd.Flags().StringVarP(&other, "token_receiver", "", "", "who gets the returned IOST after selling")
systemCmd.AddCommand(rtransCmd)
}