-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add scope navs for testnet to v1.19.0-rc1 upgrade handler (#2046)
* 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
1 parent
01d73d5
commit b44780f
Showing
7 changed files
with
1,388 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
Oops, something went wrong.