31
31
port uint64
32
32
maxRequestsPerHour uint
33
33
maxRequestsPerDay uint
34
+ bottom uint64
34
35
}
35
36
36
37
faucetServer struct {
39
40
mutex sync.Mutex
40
41
accountRequestList map [string ][]time.Time
41
42
addressRequestList map [string ][]time.Time
43
+ client * client.APIClient
42
44
}
43
45
)
44
46
@@ -50,7 +52,7 @@ const (
50
52
func initFaucetServerCmd () * cobra.Command {
51
53
cmd := & cobra.Command {
52
54
Use : "faucet" ,
53
- Short : `starts a faucet server on the current wallet` ,
55
+ Short : `starts a faucet server on the wallet` ,
54
56
Args : cobra .NoArgs ,
55
57
Run : runFaucetServerCmd ,
56
58
}
@@ -65,72 +67,117 @@ func runFaucetServerCmd(_ *cobra.Command, _ []string) {
65
67
glb .Assertf (glb .GetTagAlongFee () > 0 , "tag-along amount not specified" )
66
68
glb .Assertf (glb .GetTagAlongSequencerID () != nil , "tag-along sequencer not specified" )
67
69
68
- cfg := readFaucetServerConfigIn (viper .Sub ("faucet" ))
69
-
70
70
fct := & faucetServer {
71
- cfg : cfg ,
72
71
walletData : walletData ,
73
72
accountRequestList : make (map [string ][]time.Time ),
74
73
addressRequestList : make (map [string ][]time.Time ),
74
+ client : glb .GetClient (),
75
75
}
76
+ fct .readFaucetServerConfigIn ()
76
77
77
- clnt := glb .GetClient ()
78
- fct .displayFaucetConfig (clnt )
78
+ fct .displayFaucetConfig ()
79
79
80
- if cfg .fromChain {
81
- o , _ , _ , err := clnt .GetChainOutput (* glb .GetOwnSequencerID ())
80
+ if fct . cfg .fromChain {
81
+ o , _ , _ , err := fct . client .GetChainOutput (* glb .GetOwnSequencerID ())
82
82
glb .AssertNoError (err )
83
- glb .Assertf (o .Output .Amount () > ledger .L ().ID .MinimumAmountOnSequencer + cfg .amount ,
83
+ glb .Assertf (o .Output .Amount () > ledger .L ().ID .MinimumAmountOnSequencer + fct . cfg .amount ,
84
84
"not enough balance on own sequencer %s" , fct .walletData .Sequencer .String ())
85
85
} else {
86
- _ , _ , _ , err := clnt . GetOutputsForAmount (walletData .Account , cfg .amount + glb .GetTagAlongFee ())
86
+ _ , _ , _ , err := fct . client . GetOutputsForAmount (walletData .Account , fct . cfg .amount + glb .GetTagAlongFee ())
87
87
glb .AssertNoError (err )
88
88
}
89
89
fct .run ()
90
90
}
91
91
92
- func readFaucetServerConfigIn (sub * viper.Viper ) (ret faucetServerConfig ) {
93
- glb .Assertf (sub != nil , "faucet server configuration has not found" )
94
- ret .fromChain = ! sub .GetBool ("use_wallet" )
95
- ret .port = sub .GetUint64 ("port" )
96
- if ret .port == 0 {
97
- ret .port = defaultFaucetPort
92
+ func (fct * faucetServer ) readFaucetServerConfigIn () {
93
+ sub := viper .Sub ("faucet" )
94
+ glb .Assertf (sub != nil , "faucet server configuration is missing" )
95
+ fct .cfg .fromChain = ! sub .GetBool ("use_wallet_as_source" )
96
+ fct .cfg .port = sub .GetUint64 ("port" )
97
+ if fct .cfg .port == 0 {
98
+ fct .cfg .port = defaultFaucetPort
98
99
}
99
- ret .amount = sub .GetUint64 ("amount" )
100
- glb .Assertf (ret .amount >= minAmount , "amount must be greater than %s" , util .Th (minAmount ))
101
- if ret . maxRequestsPerHour = sub .GetUint ("max_requests_per_hour" ); ret .maxRequestsPerHour == 0 {
102
- ret .maxRequestsPerHour = 1
100
+ fct . cfg .amount = sub .GetUint64 ("amount" )
101
+ glb .Assertf (fct . cfg .amount >= minAmount , "amount must be greater than %s" , util .Th (minAmount ))
102
+ if fct . cfg . maxRequestsPerHour = sub .GetUint ("max_requests_per_hour" ); fct . cfg .maxRequestsPerHour == 0 {
103
+ fct . cfg .maxRequestsPerHour = 1
103
104
}
104
- if ret . maxRequestsPerDay = sub .GetUint ("max_requests_per_day" ); ret .maxRequestsPerDay == 0 {
105
- ret .maxRequestsPerDay = 1
105
+ if fct . cfg . maxRequestsPerDay = sub .GetUint ("max_requests_per_day" ); fct . cfg .maxRequestsPerDay == 0 {
106
+ fct . cfg .maxRequestsPerDay = 1
106
107
}
108
+ fct .cfg .bottom = sub .GetUint64 ("bottom" )
109
+ if fct .cfg .bottom < fct .absoluteBottom () {
110
+ fct .cfg .bottom = fct .absoluteBottom ()
111
+ }
112
+
113
+ err := fct .checkBottom ()
114
+ glb .AssertNoError (err )
107
115
return
108
116
}
109
117
110
- func (fct * faucetServer ) displayFaucetConfig (clnt * client.APIClient ) {
111
- walletbalance , lrbid , err := clnt .GetNonChainBalance (fct .walletData .Account )
118
+ func (fct * faucetServer ) absoluteBottom () uint64 {
119
+ if fct .cfg .fromChain {
120
+ return ledger .L ().ID .MinimumAmountOnSequencer + fct .cfg .amount
121
+ }
122
+ return fct .cfg .amount + glb .GetTagAlongFee ()
123
+ }
124
+
125
+ func (fct * faucetServer ) checkBottom () error {
126
+ abs := fct .absoluteBottom ()
127
+ if fct .cfg .fromChain {
128
+ o , _ , _ , err := fct .client .GetChainOutput (* glb .GetOwnSequencerID ())
129
+ if err != nil {
130
+ return err
131
+ }
132
+ if o .Output .Amount () < abs {
133
+ return fmt .Errorf ("not enough balance on own sequencer %s. Must be at least %s, got %s" ,
134
+ fct .walletData .Sequencer .String (), util .Th (abs ), util .Th (o .Output .Amount ()))
135
+ }
136
+ } else {
137
+ balance , _ , err := fct .client .GetNonChainBalance (fct .walletData .Account )
138
+ if err != nil {
139
+ return err
140
+ }
141
+ if balance < abs {
142
+ return fmt .Errorf ("not enough balance on source address %s. Must be at least %s, got %s" ,
143
+ fct .walletData .Account .String (), util .Th (abs ), util .Th (balance ))
144
+ }
145
+ }
146
+ return nil
147
+ }
148
+
149
+ func (fct * faucetServer ) displayFaucetConfig () {
150
+ walletBalance , lrbid , err := fct .client .GetNonChainBalance (fct .walletData .Account )
112
151
glb .AssertNoError (err )
113
152
glb .PrintLRB (lrbid )
114
153
115
154
glb .Infof ("faucet server configuration:" )
116
155
glb .Infof (" amount per request: %s" , util .Th (fct .cfg .amount ))
117
156
glb .Infof (" port: %d" , fct .cfg .port )
118
157
glb .Infof (" wallet address: %s" , fct .walletData .Account .String ())
119
- glb .Infof (" wallet balance: %s" , util .Th (walletbalance ))
158
+ glb .Infof (" wallet balance: %s" , util .Th (walletBalance ))
120
159
glb .Infof (" tag-along amount: %d" , glb .GetTagAlongFee ())
121
160
glb .Infof (" tag-along sequencer: %s" , glb .GetTagAlongSequencerID ().String ())
161
+ glb .Infof (" bottom: %s" , util .Th (fct .cfg .bottom ))
122
162
if fct .cfg .fromChain {
123
- chainOut , _ , _ , err := clnt .GetChainOutput (* fct .walletData .Sequencer )
163
+ chainOut , _ , _ , err := fct . client .GetChainOutput (* fct .walletData .Sequencer )
124
164
glb .AssertNoError (err )
125
165
glb .Infof (" funds will be drawn from: %s (balance %s)" , fct .walletData .Sequencer .String (), util .Th (chainOut .Output .Amount ()))
126
166
127
167
} else {
128
- glb .Infof (" funds will be drawn from: %s (balance %s)" , fct .walletData .Account .String (), util .Th (walletbalance ))
168
+ glb .Infof (" funds will be drawn from: %s (balance %s)" , fct .walletData .Account .String (), util .Th (walletBalance ))
129
169
}
130
170
glb .Infof (" maximum number of requests per hour: %d, per day: %d" , fct .cfg .maxRequestsPerHour , fct .cfg .maxRequestsPerDay )
131
171
}
132
172
133
173
func (fct * faucetServer ) handler (w http.ResponseWriter , r * http.Request ) {
174
+ err := fct .checkBottom ()
175
+ if err != nil {
176
+ glb .Infof ("error from checkBottom: %s" , err .Error ())
177
+ writeResponse (w , err .Error ())
178
+ return
179
+ }
180
+
134
181
targetStr , ok := r .URL .Query ()["addr" ]
135
182
if ! ok || len (targetStr ) != 1 {
136
183
writeResponse (w , "wrong parameter 'addr' in request 'get_funds'" )
0 commit comments