mirror of
https://github.com/ditatompel/xmr-remote-nodes.git
synced 2025-01-08 05:52:10 +07:00
Fetch estimate fee has it own function
This commit is contained in:
parent
c6d48764a2
commit
33efa8fd31
1 changed files with 40 additions and 30 deletions
|
@ -227,41 +227,13 @@ func (p *proberClient) fetchNode(node monero.Node) (monero.Node, error) {
|
||||||
// time.Sleep(1 * time.Second)
|
// time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
// check fee
|
// check fee
|
||||||
rpcCheckFeeParam := []byte(`{"jsonrpc": "2.0","id": "0","method": "get_fee_estimate"}`)
|
fee, err := p.fetchFee(client, endpoint)
|
||||||
reqCheckFee, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(rpcCheckFeeParam))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return node, err
|
return node, err
|
||||||
}
|
}
|
||||||
reqCheckFee.Header.Set("Content-Type", "application/json; charset=UTF-8")
|
node.EstimateFee = fee
|
||||||
reqCheckFee.Header.Set("User-Agent", RPCUserAgent)
|
|
||||||
|
|
||||||
checkFee, err := client.Do(reqCheckFee)
|
|
||||||
if err != nil {
|
|
||||||
return node, err
|
|
||||||
}
|
|
||||||
defer checkFee.Body.Close()
|
|
||||||
|
|
||||||
if checkFee.StatusCode != 200 {
|
|
||||||
return node, fmt.Errorf("status code: %d", checkFee.StatusCode)
|
|
||||||
}
|
|
||||||
|
|
||||||
bodyCheckFee, err := io.ReadAll(checkFee.Body)
|
|
||||||
if err != nil {
|
|
||||||
return node, err
|
|
||||||
}
|
|
||||||
|
|
||||||
feeEstimate := struct {
|
|
||||||
Result struct {
|
|
||||||
Fee uint `json:"fee"`
|
|
||||||
} `json:"result"`
|
|
||||||
}{}
|
|
||||||
|
|
||||||
if err := json.Unmarshal(bodyCheckFee, &feeEstimate); err != nil {
|
|
||||||
return node, err
|
|
||||||
}
|
|
||||||
|
|
||||||
tookTime := time.Since(startTime).Seconds()
|
tookTime := time.Since(startTime).Seconds()
|
||||||
node.EstimateFee = feeEstimate.Result.Fee
|
|
||||||
|
|
||||||
slog.Info(fmt.Sprintf("[PROBE] Took %f seconds", tookTime))
|
slog.Info(fmt.Sprintf("[PROBE] Took %f seconds", tookTime))
|
||||||
if err := p.reportResult(node, tookTime); err != nil {
|
if err := p.reportResult(node, tookTime); err != nil {
|
||||||
|
@ -270,6 +242,44 @@ func (p *proberClient) fetchNode(node monero.Node) (monero.Node, error) {
|
||||||
return node, nil
|
return node, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get estimate fee from remote node
|
||||||
|
func (p *proberClient) fetchFee(client http.Client, endpoint string) (uint, error) {
|
||||||
|
rpcParam := []byte(`{"jsonrpc": "2.0","id": "0","method": "get_fee_estimate"}`)
|
||||||
|
req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(rpcParam))
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
|
||||||
|
req.Header.Set("User-Agent", RPCUserAgent)
|
||||||
|
|
||||||
|
res, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
if res.StatusCode != 200 {
|
||||||
|
return 0, fmt.Errorf("status code: %d", res.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := io.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
f := struct {
|
||||||
|
Result struct {
|
||||||
|
Fee uint `json:"fee"`
|
||||||
|
} `json:"result"`
|
||||||
|
}{}
|
||||||
|
|
||||||
|
if err := json.Unmarshal(body, &f); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return f.Result.Fee, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (p *proberClient) reportResult(node monero.Node, tookTime float64) error {
|
func (p *proberClient) reportResult(node monero.Node, tookTime float64) error {
|
||||||
jsonData, err := json.Marshal(monero.ProbeReport{
|
jsonData, err := json.Marshal(monero.ProbeReport{
|
||||||
TookTime: tookTime,
|
TookTime: tookTime,
|
||||||
|
|
Loading…
Reference in a new issue