forked from pactus-project/pactus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtx_builder.go
135 lines (119 loc) · 2.62 KB
/
tx_builder.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
package wallet
import (
"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/crypto/bls"
"github.com/pactus-project/pactus/types/tx"
"github.com/pactus-project/pactus/types/tx/payload"
)
type TxOption func(builder *txBuilder) error
func OptionLockTime(lockTime uint32) func(builder *txBuilder) error {
return func(builder *txBuilder) error {
builder.lockTime = lockTime
return nil
}
}
func OptionFee(fee int64) func(builder *txBuilder) error {
return func(builder *txBuilder) error {
builder.fee = fee
return nil
}
}
func OptionMemo(memo string) func(builder *txBuilder) error {
return func(builder *txBuilder) error {
builder.memo = memo
return nil
}
}
type txBuilder struct {
client *grpcClient
from *crypto.Address
to *crypto.Address
pub *bls.PublicKey
typ payload.Type
lockTime uint32
amount int64
fee int64
memo string
}
func newTxBuilder(client *grpcClient, options ...TxOption) (*txBuilder, error) {
builder := &txBuilder{
client: client,
}
for _, op := range options {
err := op(builder)
if err != nil {
return nil, err
}
}
return builder, nil
}
func (m *txBuilder) setFromAddr(addr string) error {
from, err := crypto.AddressFromString(addr)
if err != nil {
return err
}
m.from = &from
return nil
}
func (m *txBuilder) setToAddress(addr string) error {
to, err := crypto.AddressFromString(addr)
if err != nil {
return err
}
m.to = &to
return nil
}
func (m *txBuilder) build() (*tx.Tx, error) {
err := m.setLockTime()
if err != nil {
return nil, err
}
err = m.setFee()
if err != nil {
return nil, err
}
var trx *tx.Tx
switch m.typ {
case payload.TypeTransfer:
trx = tx.NewTransferTx(m.lockTime, *m.from, *m.to, m.amount, m.fee, m.memo)
case payload.TypeBond:
pub := m.pub
val, _ := m.client.getValidator(*m.to)
if val != nil {
// validator exists
pub = nil
}
trx = tx.NewBondTx(m.lockTime, *m.from, *m.to, pub, m.amount, m.fee, m.memo)
case payload.TypeUnbond:
trx = tx.NewUnbondTx(m.lockTime, *m.from, m.memo)
case payload.TypeWithdraw:
trx = tx.NewWithdrawTx(m.lockTime, *m.from, *m.to, m.amount, m.fee, m.memo)
}
return trx, nil
}
func (m *txBuilder) setLockTime() error {
if m.lockTime == 0 {
if m.client == nil {
return ErrOffline
}
info, err := m.client.getBlockchainInfo()
if err != nil {
return err
}
m.lockTime = info.LastBlockHeight + 1
}
return nil
}
func (m *txBuilder) setFee() error {
if m.fee == 0 {
if m.client == nil {
return ErrOffline
}
fee, err := m.client.getFee(m.amount, m.typ)
if err != nil {
return err
}
m.fee = fee
}
return nil
}