Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: updated history module #353

Merged
merged 12 commits into from
Mar 11, 2022
2 changes: 1 addition & 1 deletion cmd/parse-genesis/parseGenesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewParseGenesisCmd(parseCfg *parse.Config) *cobra.Command {
return &cobra.Command{
Use: "parse-genesis [[module names]]",
Short: "Parse genesis file. To parse specific modules, input module names as arguments",
Example: "bdjuno parse-genesis auth bank consensus gov history staking",
Example: "bdjuno parse-genesis auth bank consensus gov staking",
PreRunE: parse.ReadConfig(parseCfg),
RunE: func(cmd *cobra.Command, args []string) error {
parseCtx, err := parse.GetParsingContext(parseCfg)
Expand Down
36 changes: 0 additions & 36 deletions database/history.go

This file was deleted.

29 changes: 29 additions & 0 deletions database/pricefeed.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,32 @@ WHERE token_price.timestamp <= excluded.timestamp`

return nil
}

// SaveTokenPricesHistory stores the given prices as historic ones
func (db *Db) SaveTokenPricesHistory(prices []types.TokenPrice) error {
if len(prices) == 0 {
return nil
}

query := `INSERT INTO token_price_history (unit_name, price, market_cap, timestamp) VALUES`
var param []interface{}

for i, ticker := range prices {
vi := i * 4
query += fmt.Sprintf("($%d,$%d,$%d,$%d),", vi+1, vi+2, vi+3, vi+4)
param = append(param, ticker.UnitName, ticker.Price, ticker.MarketCap, ticker.Timestamp)
}

query = query[:len(query)-1] // Remove trailing ","
query += `
ON CONFLICT ON CONSTRAINT unique_price_for_timestamp DO UPDATE
SET price = excluded.price,
market_cap = excluded.market_cap`

_, err := db.Sql.Exec(query, param...)
if err != nil {
return fmt.Errorf("error while storing tokens price history: %s", err)
}

return nil
}
138 changes: 138 additions & 0 deletions database/pricefeed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,141 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveTokenPrice() {
suite.Require().True(expected[i].Equals(row))
}
}

func (suite *DbTestSuite) TestBigDipperDb_SaveTokenPriceHistory() {
suite.insertToken("desmos")
suite.insertToken("atom")

// Save data
tickers := []types.TokenPrice{
types.NewTokenPrice(
"desmos",
100.01,
10,
time.Date(2020, 10, 10, 15, 00, 00, 000, time.UTC),
),
types.NewTokenPrice(
"desmos",
200.01,
20,
time.Date(2020, 10, 10, 15, 02, 00, 000, time.UTC),
),
types.NewTokenPrice(
"atom",
1,
20,
time.Date(2020, 10, 10, 15, 00, 00, 000, time.UTC),
),
types.NewTokenPrice(
"atom",
1,
20,
time.Date(2020, 10, 10, 15, 02, 00, 000, time.UTC),
),
}
err := suite.database.SaveTokenPricesHistory(tickers)
suite.Require().NoError(err)

// Verify data
expected := []dbtypes.TokenPriceRow{
dbtypes.NewTokenPriceRow(
"desmos",
100.01,
10,
time.Date(2020, 10, 10, 15, 00, 00, 000, time.UTC),
),
dbtypes.NewTokenPriceRow(
"desmos",
200.01,
20,
time.Date(2020, 10, 10, 15, 02, 00, 000, time.UTC),
),
dbtypes.NewTokenPriceRow(
"atom",
1,
20,
time.Date(2020, 10, 10, 15, 00, 00, 000, time.UTC),
),
dbtypes.NewTokenPriceRow(
"atom",
1,
20,
time.Date(2020, 10, 10, 15, 02, 00, 000, time.UTC),
),
}

var rows []dbtypes.TokenPriceRow
err = suite.database.Sqlx.Select(&rows, `SELECT * FROM token_price_history`)
suite.Require().NoError(err)

for i, row := range rows {
suite.Require().True(expected[i].Equals(row))
}

// Update data
tickers = []types.TokenPrice{
types.NewTokenPrice(
"desmos",
100.01,
10,
time.Date(2020, 10, 10, 15, 00, 00, 000, time.UTC),
),
types.NewTokenPrice(
"desmos",
300.01,
20,
time.Date(2020, 10, 10, 15, 02, 00, 000, time.UTC),
),
types.NewTokenPrice(
"atom",
1,
20,
time.Date(2020, 10, 10, 15, 00, 00, 000, time.UTC),
),
types.NewTokenPrice(
"atom",
10,
20,
time.Date(2020, 10, 10, 15, 02, 00, 000, time.UTC),
),
}
err = suite.database.SaveTokenPricesHistory(tickers)
suite.Require().NoError(err)

// Verify data
expected = []dbtypes.TokenPriceRow{
dbtypes.NewTokenPriceRow(
"desmos",
100.01,
10,
time.Date(2020, 10, 10, 15, 00, 00, 000, time.UTC),
),
dbtypes.NewTokenPriceRow(
"atom",
1,
20,
time.Date(2020, 10, 10, 15, 00, 00, 000, time.UTC),
),
dbtypes.NewTokenPriceRow(
"desmos",
300.01,
20,
time.Date(2020, 10, 10, 15, 02, 00, 000, time.UTC),
),

dbtypes.NewTokenPriceRow(
"atom",
10,
20,
time.Date(2020, 10, 10, 15, 02, 00, 000, time.UTC),
),
}

rows = []dbtypes.TokenPriceRow{}
err = suite.database.Sqlx.Select(&rows, `SELECT * FROM token_price_history ORDER BY timestamp`)
suite.Require().NoError(err)

for i, row := range rows {
suite.Require().True(expected[i].Equals(row))
}
}
42 changes: 0 additions & 42 deletions modules/history/module.go

This file was deleted.

14 changes: 0 additions & 14 deletions modules/history/utils_prices.go

This file was deleted.

7 changes: 0 additions & 7 deletions modules/pricefeed/expected_modules.go

This file was deleted.

2 changes: 1 addition & 1 deletion modules/pricefeed/handle_additional_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ func (m *Module) storeTokens() error {
return fmt.Errorf("error while storing token prices: %s", err)
}

return m.historyModule.UpdatePricesHistory(prices)
return nil
}
Loading