API State RecommendedFee
laisolizq laisolizq@gmail.com
Mon, 09 Nov 2020 17:01:35 +0100
4 files changed,
65 insertions(+),
5 deletions(-)
M
api/api.go
→
api/api.go
@@ -10,6 +10,12 @@ "github.com/hermeznetwork/hermez-node/db/l2db"
"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"`
M
api/state.go
→
api/state.go
@@ -142,5 +142,12 @@ // Recommended fee
// 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 }
M
api/state_test.go
→
api/state_test.go
@@ -96,3 +96,11 @@ assert.Greater(t, api.status.Metrics.TotalAccounts, int64(0))
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) +}
M
db/historydb/historydb.go
→
db/historydb/historydb.go
@@ -1475,7 +1475,12 @@ return nil, err
} 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 @@ WHERE batch_num > $1;`, metricsTotals.FirstBatchNum)
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 @@ }
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 +}