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

Add scope navs for testnet to v1.19.0-rc1 upgrade handler #2046

Merged
merged 15 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
nullpointer0x00 marked this conversation as resolved.
Show resolved Hide resolved

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
Loading