-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer.go
65 lines (52 loc) · 1.46 KB
/
transfer.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
package commands
import (
"fmt"
"github.com/nknorg/nkn/v2/common"
nknwallet "github.com/omani/nkn-wallet"
"github.com/spf13/cobra"
)
var transferCmd = &cobra.Command{
Use: "transfer",
Short: "Transfer funds to another NKN address",
PreRun: func(cmd *cobra.Command, args []string) {
cmd.MarkFlagRequired("index")
},
RunE: func(cmd *cobra.Command, args []string) error {
return runTransfer()
},
}
var (
to string
amount string
fee string
)
func init() {
rootCmd.AddCommand(transferCmd)
transferCmd.Flags().StringVar(&to, "to", "", "NKN Address of recipient.")
transferCmd.Flags().StringVar(&amount, "amount", "", "Amount of funds to transfer.")
transferCmd.Flags().StringVar(&fee, "fee", "", "Make miners happy by specifying an optional fee for the transaction.")
transferCmd.MarkFlagRequired("amount")
transferCmd.MarkFlagRequired("to")
}
func runTransfer() error {
_, err := common.ToScriptHash(to)
checkerr(err)
store, err := nknwallet.NewStore(path)
checkerr(err)
wallet, err := getWallet(store, index)
checkerr(err)
if amount == "all" {
a, err := wallet.Balance()
checkerr(err)
amount = a.String()
}
a, err := common.StringToFixed64(amount)
checkerr(err)
if a == 0 || amount == "0" {
cobra.CheckErr("Trying to send amount of 0. Aborting!")
}
txhash, err := wallet.Transfer(to, a.String(), nil)
checkerr(err)
fmt.Printf("Successfully sent %s NKN from %s to %s. txHash: %s\n", a, wallet.Address(), to, txhash)
return nil
}