-
Notifications
You must be signed in to change notification settings - Fork 320
/
builder.go
135 lines (115 loc) · 3.13 KB
/
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
// Copyright (c) 2019 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package action
import (
"math/big"
"github.com/iotexproject/go-pkgs/crypto"
"github.com/iotexproject/iotex-core/pkg/version"
)
// Builder is used to build an action.
type Builder struct {
act AbstractAction
}
// SetVersion sets action's version.
func (b *Builder) SetVersion(v uint32) *Builder {
b.act.version = v
return b
}
// SetNonce sets action's nonce.
func (b *Builder) SetNonce(n uint64) *Builder {
b.act.nonce = n
return b
}
// SetSourcePublicKey sets action's source's public key.
func (b *Builder) SetSourcePublicKey(key crypto.PublicKey) *Builder {
b.act.srcPubkey = key
return b
}
// SetGasLimit sets action's gas limit.
func (b *Builder) SetGasLimit(l uint64) *Builder {
b.act.gasLimit = l
return b
}
// SetGasPrice sets action's gas price.
func (b *Builder) SetGasPrice(p *big.Int) *Builder {
if p == nil {
return b
}
b.act.gasPrice = &big.Int{}
b.act.gasPrice.Set(p)
return b
}
// SetGasPriceByBytes sets action's gas price from a byte slice source.
func (b *Builder) SetGasPriceByBytes(buf []byte) *Builder {
if len(buf) == 0 {
return b
}
b.act.gasPrice = &big.Int{}
b.act.gasPrice.SetBytes(buf)
return b
}
// Build builds a new action.
func (b *Builder) Build() AbstractAction {
if b.act.gasPrice == nil {
b.act.gasPrice = big.NewInt(0)
}
if b.act.version == 0 {
b.act.version = version.ProtocolVersion
}
return b.act
}
// EnvelopeBuilder is the builder to build Envelope.
type EnvelopeBuilder struct {
elp envelope
}
// SetVersion sets action's version.
func (b *EnvelopeBuilder) SetVersion(v uint32) *EnvelopeBuilder {
b.elp.version = v
return b
}
// SetNonce sets action's nonce.
func (b *EnvelopeBuilder) SetNonce(n uint64) *EnvelopeBuilder {
b.elp.nonce = n
return b
}
// SetGasLimit sets action's gas limit.
func (b *EnvelopeBuilder) SetGasLimit(l uint64) *EnvelopeBuilder {
b.elp.gasLimit = l
return b
}
// SetGasPrice sets action's gas price.
func (b *EnvelopeBuilder) SetGasPrice(p *big.Int) *EnvelopeBuilder {
if p == nil {
return b
}
b.elp.gasPrice = &big.Int{}
b.elp.gasPrice.Set(p)
return b
}
// SetGasPriceByBytes sets action's gas price from a byte slice source.
func (b *EnvelopeBuilder) SetGasPriceByBytes(buf []byte) *EnvelopeBuilder {
if len(buf) == 0 {
return b
}
b.elp.gasPrice = &big.Int{}
b.elp.gasPrice.SetBytes(buf)
return b
}
// SetAction sets the action payload for the Envelope Builder is building.
func (b *EnvelopeBuilder) SetAction(action actionPayload) *EnvelopeBuilder {
b.elp.payload = action
return b
}
// Build builds a new action.
func (b *EnvelopeBuilder) Build() Envelope {
if b.elp.gasPrice == nil {
b.elp.gasPrice = big.NewInt(0)
}
if b.elp.version == 0 {
b.elp.version = version.ProtocolVersion
}
return &b.elp
}