This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
main.go
140 lines (122 loc) · 3.53 KB
/
main.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
139
140
// Copyright (c) 2020-2022 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
// Transfer examples
//
package main
import (
"context"
"flag"
"fmt"
"os"
"strconv"
"blockwatch.cc/tzgo/codec"
"blockwatch.cc/tzgo/rpc"
"blockwatch.cc/tzgo/signer"
"blockwatch.cc/tzgo/tezos"
"github.com/echa/log"
)
var (
flags = flag.NewFlagSet("transfer", flag.ContinueOnError)
verbose bool
node string
key string
)
func init() {
flags.Usage = func() {}
flags.BoolVar(&verbose, "v", false, "be verbose")
flags.StringVar(&key, "key", "", "private key")
flags.StringVar(&node, "node", "https://rpc.ithaca.tzstats.com", "Tezos node URL")
}
func main() {
if err := flags.Parse(os.Args[1:]); err != nil {
if err == flag.ErrHelp {
fmt.Println("Usage: transfer [flags] <cmd> [sub-args]")
fmt.Println("\nFlags")
flags.PrintDefaults()
fmt.Println("\nTransaction Commands")
fmt.Printf(" transfer {<receiver> <amount>}+ transfer tez to single or multiple receiver(s)\n")
os.Exit(0)
}
fmt.Println("Error:", err)
return
}
if err := run(); err != nil {
fmt.Println("Error:", err)
}
}
func run() error {
n := flags.NArg()
if n < 1 {
return fmt.Errorf("Command required")
}
if key == "" {
return fmt.Errorf("Key required")
}
switch {
case verbose:
log.SetLevel(log.LevelTrace)
default:
log.SetLevel(log.LevelWarn)
}
rpc.UseLogger(log.Log)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c, err := rpc.NewClient(node, nil)
if err != nil {
return err
}
err = c.Init(ctx)
if err != nil {
return err
}
c.Listen()
switch cmd := flags.Arg(0); cmd {
case "transfer":
if n < 3 || (n-1)%2 == 1 {
return fmt.Errorf("Missing arguments")
}
return transfer(ctx, c)
default:
return fmt.Errorf("Unknown command %q", cmd)
}
}
func transfer(ctx context.Context, c *rpc.Client) error {
sk, err := tezos.ParsePrivateKey(key)
if err != nil {
return fmt.Errorf("Invalid private key %q: %v", key, err)
}
c.Signer = signer.NewFromKey(sk)
// construct batch operation
op := codec.NewOp().WithSource(sk.Address())
for i := 1; i < flags.NArg(); i += 2 {
fReceiver := flags.Arg(i)
fAmount := flags.Arg(i + 1)
recv, err := tezos.ParseAddress(fReceiver)
if err != nil {
return fmt.Errorf("Invalid receiver %q: %v", fReceiver, err)
}
amount, err := strconv.ParseInt(fAmount, 10, 64)
if err != nil {
return fmt.Errorf("Invalid amount %q: %v", fAmount, err)
}
op.WithTransfer(recv, amount)
}
// send operation with default options
rcpt, err := c.Send(ctx, op, nil)
if err != nil {
return err
}
total := rcpt.TotalCosts()
fmt.Println("Costs")
fmt.Printf(" Total %d\n", total.Fee+total.StorageBurn+total.AllocationBurn)
fmt.Printf(" Baker Fee %d\n", total.Fee)
fmt.Printf(" Storage burn %d\n", total.StorageBurn)
fmt.Printf(" Allocation burn %d\n", total.AllocationBurn)
fmt.Printf(" Gas used %d\n", total.GasUsed)
fmt.Printf(" Storage bytes %d\n", total.StorageUsed)
if !rcpt.IsSuccess() {
return fmt.Errorf("Transfer failed: %v", rcpt.Error())
}
fmt.Printf("Success.")
return nil
}