From 1ceb00543bb1f53e0cc5edb64293404fa4d1a502 Mon Sep 17 00:00:00 2001 From: ditatompel Date: Mon, 6 May 2024 13:19:48 +0700 Subject: [PATCH] Adding majority net fee Note that because this app not caching "expensive" query from database, I use static majority data for frontend to reduce API call. --- .../src/routes/(front)/remote-nodes/+page.js | 2 +- .../routes/(front)/remote-nodes/+page.svelte | 40 +++++++++++++++++-- handler/response.go | 9 +++++ handler/routes.go | 1 + internal/repo/monero.go | 24 +++++++++++ 5 files changed, 72 insertions(+), 4 deletions(-) diff --git a/frontend/src/routes/(front)/remote-nodes/+page.js b/frontend/src/routes/(front)/remote-nodes/+page.js index 6656793..f9bb681 100644 --- a/frontend/src/routes/(front)/remote-nodes/+page.js +++ b/frontend/src/routes/(front)/remote-nodes/+page.js @@ -22,6 +22,6 @@ export async function load({ data }) { description: metaDefaults.description, image: metaDefaults.image } - } + }, }; } diff --git a/frontend/src/routes/(front)/remote-nodes/+page.svelte b/frontend/src/routes/(front)/remote-nodes/+page.svelte index 853ef35..0d8422d 100644 --- a/frontend/src/routes/(front)/remote-nodes/+page.svelte +++ b/frontend/src/routes/(front)/remote-nodes/+page.svelte @@ -34,6 +34,41 @@ handler.invalidate(); }; + /** + * Array containing network fees. + * For now, I use static data to reduce the amount of API calls. + * See the values from `/api/v1/fees` + * @type {{ nettype: string, estimate_fee: number }[]} + */ + const netFees = [ + { + nettype: 'mainnet', + estimate_fee: 20000 + }, + { + nettype: 'stagenet', + estimate_fee: 58000 + }, + { + nettype: 'testnet', + estimate_fee: 20000 + } + ]; + + /** @type {Object.} */ + let majorityFee = netFees.reduce( + /** + * @param {Object.} o + * @param {{ nettype: string, estimate_fee: number }} key + * @returns {Object.} + */ + (o, key) => ({ + ...o, + [key.nettype]: key.estimate_fee + }), + {} + ); + /** @type {number | undefined} */ let intervalId; let intervalValue = 0; @@ -274,11 +309,10 @@ /> - diff --git a/handler/response.go b/handler/response.go index c35e210..c8eaa8e 100644 --- a/handler/response.go +++ b/handler/response.go @@ -171,6 +171,15 @@ func AddNode(c *fiber.Ctx) error { }) } +func NetFee(c *fiber.Ctx) error { + moneroRepo := repo.NewMoneroRepo(database.GetDB()) + return c.JSON(fiber.Map{ + "status": "ok", + "message": "Success", + "data": moneroRepo.NetFee(), + }) +} + func GiveJob(c *fiber.Ctx) error { acceptTor := c.QueryInt("accept_tor", 0) diff --git a/handler/routes.go b/handler/routes.go index 164737b..b9d71df 100644 --- a/handler/routes.go +++ b/handler/routes.go @@ -16,6 +16,7 @@ func V1Api(app *fiber.App) { v1.Post("/prober", Prober) v1.Get("/nodes", MoneroNodes) v1.Post("/nodes", AddNode) + v1.Get("/fees", NetFee) v1.Get("/job", CheckProber, GiveJob) v1.Post("/job", CheckProber, ProcessJob) v1.Get("/crons", Crons) diff --git a/internal/repo/monero.go b/internal/repo/monero.go index 2429fd3..868b01d 100644 --- a/internal/repo/monero.go +++ b/internal/repo/monero.go @@ -20,6 +20,7 @@ type MoneroRepository interface { Nodes(q MoneroQueryParams) (MoneroNodes, error) GiveJob(acceptTor int) (MoneroNode, error) ProcessJob(report ProbeReport, proberId int64) error + NetFee() []NetFee } type MoneroRepo struct { @@ -307,3 +308,26 @@ func (repo *MoneroRepo) ProcessJob(report ProbeReport, proberId int64) error { return err } + +type NetFee struct { + Nettype string `json:"nettype" db:"nettype"` + EstimateFee uint `json:"estimate_fee" db:"estimate_fee"` + NodeCount int `json:"node_count" db:"node_count"` +} + +func (repo *MoneroRepo) NetFee() []NetFee { + netTypes := [3]string{"mainnet", "stagenet", "testnet"} + netFees := []NetFee{} + + for _, net := range netTypes { + fees := NetFee{} + err := repo.db.Get(&fees, `SELECT COUNT(id) AS node_count, nettype, estimate_fee FROM tbl_node WHERE nettype = ? GROUP BY estimate_fee ORDER BY node_count DESC LIMIT 1`, net) + if err != nil { + fmt.Println("WARN:", err.Error()) + continue + } + netFees = append(netFees, fees) + } + + return netFees +}