Skip to content

Commit

Permalink
Add scope navs for testnet to v1.19.0-rc1 upgrade handler (#2046)
Browse files Browse the repository at this point in the history
* start scope nav upgrade handler

* add reading of testnet nav file and adding of scope navs to rc1 upgrade handler

* add upgrade handler test

* move nav call before removing inactive validators, update tests

* fix tests

* add change log entry

* fix lint

* add proper block heights

* remove comment

* fix tests to match cvs

* change source to owner

* extract method and write test

* lint-fix
  • Loading branch information
nullpointer0x00 authored Jun 21, 2024
1 parent 01d73d5 commit b44780f
Show file tree
Hide file tree
Showing 7 changed files with 1,388 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* Update stargate queries for Attribute, Exchange, Marker, IBCRateLimit, Metadata, Msgfees, and Oracle modules [#1760](https://github.com/provenance-io/provenance/issues/1760).
* Update stargate queries for Quarantine and Sanction modules [#2016](https://github.com/provenance-io/provenance/pull/2016).
* Add the circuit breaker module [#2031](https://github.com/provenance-io/provenance/pull/2031).
* Add upgrade handler to set scope net asset values and update block height for pio-testnet-1 [#2046](https://github.com/provenance-io/provenance/pull/2046)

### Improvements

Expand Down
79 changes: 79 additions & 0 deletions app/scope_navs_updater.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package app

import (
"encoding/csv"
"os"
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"

metadatatypes "github.com/provenance-io/provenance/x/metadata/types"
)

type NetAssetValueWithHeight struct {
ScopeUUID string
NetAssetValue metadatatypes.NetAssetValue
Height uint64
}

// parseValueToUsdMills parses and converts cents amount into usd mills as int64 $1.24 = 1240
func parseValueToUsdMills(navStr string) (int64, error) {
navValue, err := strconv.ParseFloat(navStr, 64)
if err != nil {
return 0, err
}
return int64(navValue * 1000), nil
}

// ReadNetAssetValues reads a CSV file and parses its contents into a slice of NetAssetValueWithHeight
func ReadNetAssetValues(fileName string) ([]NetAssetValueWithHeight, error) {
file, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer file.Close()

reader := csv.NewReader(file)

// Skip the header line
if _, err := reader.Read(); err != nil {
return nil, err
}

records, err := reader.ReadAll()
if err != nil {
return nil, err
}

assets := make([]NetAssetValueWithHeight, 0, len(records))
for _, record := range records {
if len(record) < 3 {
continue
}

scopeUUID := record[0]

navInt64, err := parseValueToUsdMills(record[1])
if err != nil {
return nil, err
}

heightIndex := len(record) - 1
height, err := strconv.ParseUint(record[heightIndex], 10, 64)
if err != nil {
return nil, err
}

price := sdk.NewInt64Coin(metadatatypes.UsdDenom, navInt64)

asset := NetAssetValueWithHeight{
ScopeUUID: scopeUUID,
NetAssetValue: metadatatypes.NewNetAssetValue(price),
Height: height,
}

assets = append(assets, asset)
}

return assets, nil
}
60 changes: 60 additions & 0 deletions app/scope_navs_updater_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package app

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
metadatatypes "github.com/provenance-io/provenance/x/metadata/types"
"github.com/stretchr/testify/assert"
)

func TestReadNetAssetValues(t *testing.T) {
fileName := "upgrade_files/testnet_scope_navs.csv"

assets, err := ReadNetAssetValues(fileName)
assert.NoError(t, err, "Failed to read net asset values")
assert.Len(t, assets, 1101, "The number of assets should be 1101")

expectedFirst := NetAssetValueWithHeight{
ScopeUUID: "2f389a9f-873d-4920-85a6-7734f27e1738",
NetAssetValue: metadatatypes.NewNetAssetValue(sdk.NewInt64Coin(metadatatypes.UsdDenom, 398820670)),
Height: 23056719,
}
assert.Equal(t, expectedFirst.ScopeUUID, assets[0].ScopeUUID, "The first ScopeUUID should match")
assert.True(t, assets[0].NetAssetValue.Price.Equal(expectedFirst.NetAssetValue.Price), "The first NetAssetValue should match")
assert.Equal(t, expectedFirst.Height, assets[0].Height, "The first Height should match")

expectedLast := NetAssetValueWithHeight{
ScopeUUID: "65939db0-6d7a-42ef-9443-378304d33225",
NetAssetValue: metadatatypes.NewNetAssetValue(sdk.NewInt64Coin(metadatatypes.UsdDenom, 93661920)),
Height: 23056719,
}
assert.Equal(t, expectedLast.ScopeUUID, assets[len(assets)-1].ScopeUUID, "The last ScopeUUID should match")
assert.True(t, assets[len(assets)-1].NetAssetValue.Price.Equal(expectedLast.NetAssetValue.Price), "The last NetAssetValue should match")
assert.Equal(t, expectedLast.Height, assets[len(assets)-1].Height, "The last Height should match")
}

func TestParseValueToUsdMills(t *testing.T) {
tests := []struct {
input string
expectedOutput int64
expectError bool
}{
{"1.24", 1240, false},
{"0.99", 990, false},
{"100.5", 100500, false},
{"100.3456", 100345, false},
{"abc", 0, true},
{"", 0, true},
}

for _, test := range tests {
output, err := parseValueToUsdMills(test.input)
if test.expectError {
assert.Error(t, err, "Expected an error for input: %s", test.input)
} else {
assert.NoError(t, err, "Did not expect an error for input: %s", test.input)
assert.Equal(t, test.expectedOutput, output, "Expected output to be %d for input: %s", test.expectedOutput, test.input)
}
}
}
Loading

0 comments on commit b44780f

Please sign in to comment.