From 9edf9d2168de0e1dadcd7b9829830ec3dd7e9482 Mon Sep 17 00:00:00 2001 From: laisolizq Date: Mon, 9 Nov 2020 17:01:35 +0100 Subject: [PATCH] API State RecommendedFee --- api/api.go | 6 +++++ api/state.go | 7 ++++++ api/state_test.go | 8 +++++++ db/historydb/historydb.go | 49 +++++++++++++++++++++++++++++++++++---- 4 files changed, 65 insertions(+), 5 deletions(-) diff --git a/api/api.go b/api/api.go index 98b64f7..c387408 100644 --- a/api/api.go +++ b/api/api.go @@ -10,6 +10,12 @@ import ( "github.com/hermeznetwork/hermez-node/db/statedb" ) +// TODO: Add correct values to constants +const ( + createAccountExtraFeePercentage float64 = 2 + createAccountInternalExtraFeePercentage float64 = 2.5 +) + // Status define status of the network type Status struct { Network Network `json:"network"` diff --git a/api/state.go b/api/state.go index 3232f9b..a76b3ae 100644 --- a/api/state.go +++ b/api/state.go @@ -142,5 +142,12 @@ func (a *API) UpdateMetrics() error { // UpdateRecommendedFee update Status.RecommendedFee information func (a *API) UpdateRecommendedFee() error { + feeExistingAccount, err := a.h.GetAvgTxFee() + if err != nil { + return err + } + a.status.RecommendedFee.ExistingAccount = feeExistingAccount + a.status.RecommendedFee.CreatesAccount = createAccountExtraFeePercentage * feeExistingAccount + a.status.RecommendedFee.CreatesAccountAndRegister = createAccountInternalExtraFeePercentage * feeExistingAccount return nil } diff --git a/api/state_test.go b/api/state_test.go index de3e3cc..6e78577 100644 --- a/api/state_test.go +++ b/api/state_test.go @@ -96,3 +96,11 @@ func TestUpdateMetrics(t *testing.T) { assert.Greater(t, api.status.Metrics.TotalBJJs, int64(0)) assert.Greater(t, api.status.Metrics.AvgTransactionFee, float64(0)) } + +func TestUpdateRecommendedFee(t *testing.T) { + err := api.UpdateRecommendedFee() + assert.NoError(t, err) + assert.Greater(t, api.status.RecommendedFee.ExistingAccount, float64(0)) + assert.Equal(t, api.status.RecommendedFee.CreatesAccount, api.status.RecommendedFee.ExistingAccount*createAccountExtraFeePercentage) + assert.Equal(t, api.status.RecommendedFee.CreatesAccountAndRegister, api.status.RecommendedFee.ExistingAccount*createAccountInternalExtraFeePercentage) +} diff --git a/db/historydb/historydb.go b/db/historydb/historydb.go index 4c15b47..1e537d2 100644 --- a/db/historydb/historydb.go +++ b/db/historydb/historydb.go @@ -1475,7 +1475,12 @@ func (hdb *HistoryDB) GetMetrics(lastBatchNum common.BatchNum) (*Metrics, error) } metrics.TransactionsPerSecond = float64(metricsTotals.TotalTransactions / (24 * 60 * 60)) - metrics.TransactionsPerBatch = float64(int64(metricsTotals.TotalTransactions) / int64(lastBatchNum-metricsTotals.FirstBatchNum)) + if (lastBatchNum - metricsTotals.FirstBatchNum) > 0 { + metrics.TransactionsPerBatch = float64(int64(metricsTotals.TotalTransactions) / + int64(lastBatchNum-metricsTotals.FirstBatchNum)) + } else { + metrics.TransactionsPerBatch = float64(0) + } err = meddler.QueryRow( hdb.db, metricsTotals, `SELECT COUNT(*) AS total_batches, @@ -1484,10 +1489,16 @@ func (hdb *HistoryDB) GetMetrics(lastBatchNum common.BatchNum) (*Metrics, error) if err != nil { return nil, err } - - metrics.BatchFrequency = float64((24 * 60 * 60) / metricsTotals.TotalBatches) - metrics.AvgTransactionFee = metricsTotals.TotalFeesUSD / float64(metricsTotals.TotalTransactions) - + if metricsTotals.TotalBatches > 0 { + metrics.BatchFrequency = float64((24 * 60 * 60) / metricsTotals.TotalBatches) + } else { + metrics.BatchFrequency = 0 + } + if metricsTotals.TotalTransactions > 0 { + metrics.AvgTransactionFee = metricsTotals.TotalFeesUSD / float64(metricsTotals.TotalTransactions) + } else { + metrics.AvgTransactionFee = 0 + } err = meddler.QueryRow( hdb.db, metrics, `SELECT COUNT(*) AS total_bjjs, COUNT(DISTINCT(bjj)) AS total_accounts FROM account;`) @@ -1497,3 +1508,31 @@ func (hdb *HistoryDB) GetMetrics(lastBatchNum common.BatchNum) (*Metrics, error) return metrics, nil } + +// GetAvgTxFee returns average transaction fee of the last 1h +func (hdb *HistoryDB) GetAvgTxFee() (float64, error) { + metricsTotals := &MetricsTotals{} + err := meddler.QueryRow( + hdb.db, metricsTotals, `SELECT COUNT(tx.*) as total_txs, MIN(tx.batch_num) as batch_num + FROM tx INNER JOIN block ON tx.eth_block_num = block.eth_block_num + WHERE block.timestamp >= NOW() - INTERVAL '1 HOURS';`) + if err != nil { + return 0, err + } + err = meddler.QueryRow( + hdb.db, metricsTotals, `SELECT COUNT(*) AS total_batches, + SUM(total_fees_usd) AS total_fees FROM batch + WHERE batch_num > $1;`, metricsTotals.FirstBatchNum) + if err != nil { + return 0, err + } + + var avgTransactionFee float64 + if metricsTotals.TotalTransactions > 0 { + avgTransactionFee = metricsTotals.TotalFeesUSD / float64(metricsTotals.TotalTransactions) + } else { + avgTransactionFee = 0 + } + + return avgTransactionFee, nil +}