Merge pull request #216 from hermeznetwork/feature/batch-total-fee

Calulate total collected batch fee and fix merkleproof api format
This commit is contained in:
Eduard S
2020-10-22 14:28:49 +02:00
committed by GitHub
6 changed files with 125 additions and 49 deletions

View File

@@ -1,6 +1,8 @@
package historydb
import (
"math"
"math/big"
"os"
"testing"
@@ -109,6 +111,34 @@ func TestBatches(t *testing.T) {
fetchedLastL1TxsNum, err = historyDB.GetLastL1TxsNum()
assert.NoError(t, err)
assert.Equal(t, *batches[nBatches-1].ForgeL1TxsNum, *fetchedLastL1TxsNum)
// Test total fee
// Generate fake tokens
const nTokens = 5
tokens := test.GenTokens(nTokens, blocks)
err = historyDB.AddTokens(tokens)
assert.NoError(t, err)
feeBatch := batches[0]
feeBatch.BatchNum = 9999
feeBatch.CollectedFees = make(map[common.TokenID]*big.Int)
var total float64
for i, token := range tokens {
value := 3.019237 * float64(i)
assert.NoError(t, historyDB.UpdateTokenValue(token.Symbol, value))
bigAmount := big.NewInt(345000000)
feeBatch.CollectedFees[token.TokenID] = bigAmount
f := new(big.Float).SetInt(bigAmount)
amount, _ := f.Float64()
total += value * (amount / math.Pow(10, float64(token.Decimals)))
}
err = historyDB.AddBatch(&feeBatch)
assert.NoError(t, err)
fetchedBatches, err = historyDB.GetBatches(feeBatch.BatchNum-1, feeBatch.BatchNum+1)
assert.NoError(t, err)
for _, fetchedBatch := range fetchedBatches {
if fetchedBatch.BatchNum == feeBatch.BatchNum {
assert.Equal(t, total, *fetchedBatch.TotalFeesUSD)
}
}
}
func TestBids(t *testing.T) {