Skip to content

Commit

Permalink
Merge pull request #63 from celenium-io/fix/round
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored Dec 2, 2023
2 parents 27e115e + ea9f061 commit a4c947a
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 44 deletions.
7 changes: 4 additions & 3 deletions cmd/api/gas/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync"

"github.com/celenium-io/celestia-indexer/cmd/api/bus"
"github.com/celenium-io/celestia-indexer/internal/currency"
"github.com/celenium-io/celestia-indexer/internal/storage"
"github.com/dipdup-io/workerpool"
sdk "github.com/dipdup-net/indexer-sdk/pkg/storage"
Expand Down Expand Up @@ -189,9 +190,9 @@ func (tracker *Tracker) computeMetrics() error {

tracker.mx.Lock()
{
tracker.gasState.Slow = slow.StringFixed(8)
tracker.gasState.Median = median.StringFixed(8)
tracker.gasState.Fast = fast.StringFixed(8)
tracker.gasState.Slow = currency.StringTia(slow)
tracker.gasState.Median = currency.StringTia(median)
tracker.gasState.Fast = currency.StringTia(fast)
}
tracker.mx.Unlock()

Expand Down
6 changes: 3 additions & 3 deletions cmd/api/gas/tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ func TestTracker_computeMetrics(t *testing.T) {
err := tracker.computeMetrics()
require.NoError(t, err)
state := tracker.State()
require.Equal(t, "2.00000000", state.Slow)
require.Equal(t, "3.00000000", state.Median)
require.Equal(t, "4.00000000", state.Fast)
require.Equal(t, "2.000000", state.Slow)
require.Equal(t, "3.000000", state.Median)
require.Equal(t, "4.000000", state.Fast)
})
}

Expand Down
15 changes: 0 additions & 15 deletions internal/consts/currency.go

This file was deleted.

25 changes: 25 additions & 0 deletions internal/currency/currency.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-FileCopyrightText: 2023 PK Lab AG <[email protected]>
// SPDX-License-Identifier: MIT

package currency

import "github.com/shopspring/decimal"

type Denom string

const (
Utia Denom = "utia"
Tia Denom = "tia"
)

const (
DefaultCurrency = "utia"
)

func StringTia(val decimal.Decimal) string {
return val.StringFixed(6)
}

func StringUtia(val decimal.Decimal) string {
return val.StringFixed(0)
}
43 changes: 43 additions & 0 deletions internal/currency/currency_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-FileCopyrightText: 2023 PK Lab AG <[email protected]>
// SPDX-License-Identifier: MIT

package currency

import (
"testing"

"github.com/shopspring/decimal"
"github.com/stretchr/testify/require"
)

func TestStringTia(t *testing.T) {
tests := []struct {
name string
val decimal.Decimal
want string
}{
{
name: "test 1",
val: decimal.RequireFromString("0.123456789"),
want: "0.123457",
}, {
name: "test 2",
val: decimal.RequireFromString("10000.123456789"),
want: "10000.123457",
}, {
name: "test 3",
val: decimal.RequireFromString("10000"),
want: "10000.000000",
}, {
name: "test 4",
val: decimal.RequireFromString("2"),
want: "2.000000",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := StringTia(tt.val)
require.Equal(t, tt.want, got)
})
}
}
12 changes: 6 additions & 6 deletions pkg/indexer/decode/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package decode

import (
"github.com/celenium-io/celestia-indexer/internal/consts"
"github.com/celenium-io/celestia-indexer/internal/currency"
"github.com/celenium-io/celestia-indexer/pkg/types"
"github.com/celestiaorg/celestia-app/app"
"github.com/celestiaorg/celestia-app/app/encoding"
Expand Down Expand Up @@ -103,9 +103,9 @@ func decodeFee(authInfo tx.AuthInfo) (decimal.Decimal, error) {
return decimal.Zero, errors.Errorf("found fee in %d currencies", len(amount))
}

fee, ok := getFeeInDenom(amount, consts.Utia)
fee, ok := getFeeInDenom(amount, currency.Utia)
if !ok {
if fee, ok = getFeeInDenom(amount, consts.Tia); !ok {
if fee, ok = getFeeInDenom(amount, currency.Tia); !ok {
// TODO stop indexer if tx is not in failed status
return decimal.Zero, errors.New("couldn't find fee amount in utia or in tia denom")
}
Expand All @@ -114,17 +114,17 @@ func decodeFee(authInfo tx.AuthInfo) (decimal.Decimal, error) {
return fee, nil
}

func getFeeInDenom(amount cosmosTypes.Coins, denom consts.Denom) (decimal.Decimal, bool) {
func getFeeInDenom(amount cosmosTypes.Coins, denom currency.Denom) (decimal.Decimal, bool) {
ok, utiaCoin := amount.Find(string(denom))
if !ok {
return decimal.Zero, false
}

switch denom {
case consts.Utia:
case currency.Utia:
fee := decimal.NewFromBigInt(utiaCoin.Amount.BigInt(), 0)
return fee, true
case consts.Tia:
case currency.Tia:
fee := decimal.NewFromBigInt(utiaCoin.Amount.BigInt(), 6)
return fee, true
default:
Expand Down
13 changes: 7 additions & 6 deletions pkg/indexer/decode/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
package decode

import (
"github.com/celenium-io/celestia-indexer/internal/consts"
"testing"

"github.com/celenium-io/celestia-indexer/internal/currency"
"github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx"
cosmosStakingTypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"testing"

testsuite "github.com/celenium-io/celestia-indexer/internal/test_suite"
nodeTypes "github.com/celenium-io/celestia-indexer/pkg/types"
Expand Down Expand Up @@ -172,7 +173,7 @@ func TestGetFeeInDenom(t *testing.T) {
testCases := []struct {
desc string
amount types.Coins
denom consts.Denom
denom currency.Denom
expectedFee decimal.Decimal
expectedOk bool
}{
Expand All @@ -181,7 +182,7 @@ func TestGetFeeInDenom(t *testing.T) {
amount: types.Coins{
types.NewCoin("utia", types.NewInt(1000)),
},
denom: consts.Utia,
denom: currency.Utia,
expectedFee: decimal.NewFromInt(1000),
expectedOk: true,
},
Expand All @@ -190,7 +191,7 @@ func TestGetFeeInDenom(t *testing.T) {
amount: types.Coins{
types.NewCoin("tia", types.NewInt(5000000)),
},
denom: consts.Tia,
denom: currency.Tia,
expectedFee: decimal.NewFromInt(5000000).Shift(6),
expectedOk: true,
},
Expand All @@ -199,7 +200,7 @@ func TestGetFeeInDenom(t *testing.T) {
amount: types.Coins{
types.NewCoin("unknown", types.NewInt(1000)),
},
denom: consts.Utia,
denom: currency.Utia,
expectedFee: decimal.Zero,
expectedOk: false,
},
Expand Down
6 changes: 3 additions & 3 deletions pkg/indexer/parser/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package parser

import (
"github.com/celenium-io/celestia-indexer/internal/consts"
"github.com/celenium-io/celestia-indexer/internal/currency"
"github.com/celenium-io/celestia-indexer/internal/storage"
"github.com/celenium-io/celestia-indexer/pkg/indexer/decode"
pkgTypes "github.com/celenium-io/celestia-indexer/pkg/types"
Expand Down Expand Up @@ -32,7 +32,7 @@ func parseCoinSpent(data map[string]any, height pkgTypes.Level) (*storage.Addres
Height: height,
LastHeight: height,
Balance: storage.Balance{
Currency: consts.DefaultCurrency,
Currency: currency.DefaultCurrency,
Total: decimal.Zero,
},
}
Expand Down Expand Up @@ -65,7 +65,7 @@ func parseCoinReceived(data map[string]any, height pkgTypes.Level) (*storage.Add
Height: height,
LastHeight: height,
Balance: storage.Balance{
Currency: consts.DefaultCurrency,
Currency: currency.DefaultCurrency,
Total: decimal.Zero,
},
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/indexer/parser/balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package parser
import (
"testing"

"github.com/celenium-io/celestia-indexer/internal/consts"
"github.com/celenium-io/celestia-indexer/internal/currency"
"github.com/celenium-io/celestia-indexer/internal/storage"
pkgTypes "github.com/celenium-io/celestia-indexer/pkg/types"
"github.com/shopspring/decimal"
Expand Down Expand Up @@ -39,7 +39,7 @@ func Test_parseCoinSpent(t *testing.T) {
Address: testAddress,
Hash: testHashAddress,
Balance: storage.Balance{
Currency: consts.DefaultCurrency,
Currency: currency.DefaultCurrency,
Total: decimal.RequireFromString("-123"),
},
},
Expand All @@ -56,7 +56,7 @@ func Test_parseCoinSpent(t *testing.T) {
Address: testAddress,
Hash: testHashAddress,
Balance: storage.Balance{
Currency: consts.DefaultCurrency,
Currency: currency.DefaultCurrency,
Total: decimal.Zero,
},
},
Expand Down
6 changes: 3 additions & 3 deletions pkg/indexer/rollback/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package rollback
import (
"context"

"github.com/celenium-io/celestia-indexer/internal/consts"
"github.com/celenium-io/celestia-indexer/internal/currency"
"github.com/celenium-io/celestia-indexer/internal/storage"
"github.com/celenium-io/celestia-indexer/internal/storage/types"
"github.com/celenium-io/celestia-indexer/pkg/indexer/decode"
Expand Down Expand Up @@ -103,7 +103,7 @@ func coinSpent(data map[string]any) (*storage.Address, error) {
return nil, errors.Wrapf(err, "decode spender: %s", coinSpent.Spender)
}
balance := storage.Balance{
Currency: consts.DefaultCurrency,
Currency: currency.DefaultCurrency,
Total: decimal.Zero,
}
if coinSpent.Amount != nil {
Expand All @@ -129,7 +129,7 @@ func coinReceived(data map[string]any) (*storage.Address, error) {
}

balance := storage.Balance{
Currency: consts.DefaultCurrency,
Currency: currency.DefaultCurrency,
Total: decimal.Zero,
}
if coinReceived.Amount != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/indexer/rollback/balances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"context"
"testing"

"github.com/celenium-io/celestia-indexer/internal/consts"
"github.com/celenium-io/celestia-indexer/internal/currency"
"github.com/celenium-io/celestia-indexer/internal/storage"
"github.com/celenium-io/celestia-indexer/internal/storage/mock"
"github.com/celenium-io/celestia-indexer/internal/storage/types"
Expand Down Expand Up @@ -189,7 +189,7 @@ func Test_getBalanceUpdates(t *testing.T) {
Address: testAddress,
Hash: testHashAddress,
Balance: storage.Balance{
Currency: consts.DefaultCurrency,
Currency: currency.DefaultCurrency,
Total: decimal.RequireFromString("100"),
},
LastHeight: 100,
Expand Down

0 comments on commit a4c947a

Please sign in to comment.