/
sendfunds.go
110 lines (96 loc) · 3.64 KB
/
sendfunds.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
package main
import (
"flag"
"fmt"
"os"
"time"
"github.com/mr-tron/base58"
"github.com/iotaledger/goshimmer/client/wallet"
"github.com/iotaledger/goshimmer/client/wallet/packages/address"
"github.com/iotaledger/goshimmer/client/wallet/packages/sendoptions"
"github.com/iotaledger/goshimmer/packages/ledgerstate"
)
func execSendFundsCommand(command *flag.FlagSet, cliWallet *wallet.Wallet) {
helpPtr := command.Bool("help", false, "show this help screen")
addressPtr := command.String("dest-addr", "", "destination address for the transfer")
amountPtr := command.Int64("amount", 0, "the amount of tokens that are supposed to be sent")
colorPtr := command.String("color", "IOTA", "(optional) color of the tokens to transfer")
timelockPtr := command.Int64("lock-until", 0, "(optional) unix timestamp until which time the sent funds are locked from spending")
fallbackAddressPtr := command.String("fallb-addr", "", "(optional) fallback address that can claim back the (unspent) sent funds after fallback deadline")
fallbackDeadlinePtr := command.Int64("fallb-deadline", 0, "(optional) unix timestamp after which only the fallback address can claim the funds back")
accessManaPledgeIDPtr := command.String("access-mana-id", "", "node ID to pledge access mana to")
consensusManaPledgeIDPtr := command.String("consensus-mana-id", "", "node ID to pledge consensus mana to")
err := command.Parse(os.Args[2:])
if err != nil {
panic(err)
}
if *helpPtr {
printUsage(command)
}
if *addressPtr == "" {
printUsage(command, "dest-addr has to be set")
}
if *amountPtr <= 0 {
printUsage(command, "amount has to be set and be bigger than 0")
}
if *colorPtr == "" {
printUsage(command, "color must be set")
}
destinationAddress, err := ledgerstate.AddressFromBase58EncodedString(*addressPtr)
if err != nil {
printUsage(command, err.Error())
return
}
var color ledgerstate.Color
switch *colorPtr {
case "IOTA":
color = ledgerstate.ColorIOTA
case "NEW":
color = ledgerstate.ColorMint
default:
colorBytes, parseErr := base58.Decode(*colorPtr)
if parseErr != nil {
printUsage(command, parseErr.Error())
}
color, _, parseErr = ledgerstate.ColorFromBytes(colorBytes)
if parseErr != nil {
printUsage(command, parseErr.Error())
}
}
options := []sendoptions.SendFundsOption{
sendoptions.Destination(address.Address{
AddressBytes: destinationAddress.Array(),
}, uint64(*amountPtr), color),
sendoptions.AccessManaPledgeID(*accessManaPledgeIDPtr),
sendoptions.ConsensusManaPledgeID(*consensusManaPledgeIDPtr),
}
nowis := time.Now()
if *timelockPtr > 0 {
timelock := time.Unix(*timelockPtr, 0)
if timelock.Before(nowis) {
printUsage(command, fmt.Sprintf("can't lock funds in the past: lock-until is %s", timelock.String()))
}
options = append(options, sendoptions.LockUntil(timelock))
}
if *fallbackAddressPtr != "" || *fallbackDeadlinePtr > 0 {
if !(*fallbackAddressPtr != "" && *fallbackDeadlinePtr > 0) {
printUsage(command, "please provide both fallb-addr and fallb-deadline arguments for conditional sending")
}
fAddy, aErr := ledgerstate.AddressFromBase58EncodedString(*fallbackAddressPtr)
if aErr != nil {
printUsage(command, fmt.Sprintf("wrong fallback address: %s", aErr.Error()))
}
fDeadline := time.Unix(*fallbackDeadlinePtr, 0)
if fDeadline.Before(nowis) {
printUsage(command, fmt.Sprintf("fallback deadline %s is in the past", fDeadline.String()))
}
options = append(options, sendoptions.Fallback(fAddy, fDeadline))
}
fmt.Println("Sending funds...")
_, err = cliWallet.SendFunds(options...)
if err != nil {
printUsage(command, err.Error())
}
fmt.Println()
fmt.Println("Sending funds ... [DONE]")
}