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 upgrade handler to add default navs for markers #1712

Merged
merged 16 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 11 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 @@ -70,6 +70,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* Bump cometbft to v0.34.29 (from v0.34.28) [PR 1649](https://github.com/provenance-io/provenance/pull/1649).
* Add genesis/init for Marker module send deny list addresses. [#1660](https://github.com/provenance-io/provenance/issues/1660)
* Add automatic changelog entries for dependabot. [#1674](https://github.com/provenance-io/provenance/issues/1674)
* Add upgrade handler to set net asset values to markers [PR 1712](https://github.com/provenance-io/provenance/pull/1712).
* Ensure IBC marker has matching supply [#1706](https://github.com/provenance-io/provenance/issues/1706).

### Bug Fixes
iramiller marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
48 changes: 48 additions & 0 deletions app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"github.com/provenance-io/provenance/x/exchange"
"github.com/provenance-io/provenance/x/hold"
ibchookstypes "github.com/provenance-io/provenance/x/ibchooks/types"
markertypes "github.com/provenance-io/provenance/x/marker/types"
msgfeetypes "github.com/provenance-io/provenance/x/msgfees/types"
oracletypes "github.com/provenance-io/provenance/x/oracle/types"
triggertypes "github.com/provenance-io/provenance/x/trigger/types"
Expand Down Expand Up @@ -114,6 +115,15 @@
removeInactiveValidatorDelegations(ctx, app)
setupICQ(ctx, app)
updateMaxSupply(ctx, app)

denomToNav := map[string]markertypes.NetAssetValue{
"usd.deposit": markertypes.NewNetAssetValue(sdk.NewInt64Coin(markertypes.UsdDenom, 1000), 1),
"tp13gc5xe8375msm7y6jhf752e5hcrjypmcpf9wldsjprhnpzlkhqaqawxujf.investment": markertypes.NewNetAssetValue(sdk.NewInt64Coin(markertypes.UsdDenom, 10), 1),
"tp1cxuqqyjjf5x66jvlmtvj3juppn370ev7rr5cja3ml65nzhxgvpkszfuvtw.investment": markertypes.NewNetAssetValue(sdk.NewInt64Coin(markertypes.UsdDenom, 10), 33600000),
"pm.sale.pool.7v2gsuvnudyfvuig50r3k3": markertypes.NewNetAssetValue(sdk.NewInt64Coin(markertypes.UsdDenom, 19026640), 1),
}
addMarkerNavs(ctx, app, denomToNav, markertypes.NewNetAssetValue(sdk.NewInt64Coin(markertypes.UsdDenom, int64(150)), 1))

iramiller marked this conversation as resolved.
Show resolved Hide resolved
setExchangeParams(ctx, app)

return vm, nil
Expand All @@ -134,6 +144,11 @@
removeInactiveValidatorDelegations(ctx, app)
setupICQ(ctx, app)
updateMaxSupply(ctx, app)

denomToNav := map[string]markertypes.NetAssetValue{
// TODO: Add custom mainnet values here
}
addMarkerNavs(ctx, app, denomToNav, markertypes.NewNetAssetValue(sdk.NewInt64Coin(markertypes.UsdDenom, int64(150)), 1))
setExchangeParams(ctx, app)

return vm, nil
Expand Down Expand Up @@ -346,6 +361,39 @@
ctx.Logger().Info("Done updating MaxSupply marker param")
}

// addMarkerNavs adds navs to existing markers, if denom is not in map it will default to $0.15 cents
func addMarkerNavs(ctx sdk.Context, app *App, denomToNav map[string]markertypes.NetAssetValue, defaultNav markertypes.NetAssetValue) {
ctx.Logger().Info("Adding marker net asset values")
for denom, nav := range denomToNav {
marker, err := app.MarkerKeeper.GetMarkerByDenom(ctx, denom)
if err != nil {
ctx.Logger().Error(fmt.Sprintf("unable to get marker %v: %v", denom, err))
continue
}
if err := app.MarkerKeeper.AddSetNetAssetValues(ctx, marker, []markertypes.NetAssetValue{nav}, "upgrade_handler"); err != nil {
ctx.Logger().Error(fmt.Sprintf("unable to set net asset value %v: %v", nav, err))
}

Check warning on line 375 in app/upgrades.go

View check run for this annotation

Codecov / codecov/patch

app/upgrades.go#L374-L375

Added lines #L374 - L375 were not covered by tests
}
app.MarkerKeeper.IterateMarkers(ctx, func(record markertypes.MarkerAccountI) bool {
var hasNav bool
err := app.MarkerKeeper.IterateNetAssetValues(ctx, record.GetAddress(), func(nav markertypes.NetAssetValue) bool {
hasNav = true
return true
})
if err != nil {
ctx.Logger().Error(fmt.Sprintf("unable iterate net asset values for marker %v: %v", record, err))
return false
}

Check warning on line 386 in app/upgrades.go

View check run for this annotation

Codecov / codecov/patch

app/upgrades.go#L384-L386

Added lines #L384 - L386 were not covered by tests
if !hasNav {
iramiller marked this conversation as resolved.
Show resolved Hide resolved
if err := app.MarkerKeeper.AddSetNetAssetValues(ctx, record, []markertypes.NetAssetValue{defaultNav}, "upgrade_handler"); err != nil {
ctx.Logger().Error(fmt.Sprintf("unable to set net asset value %v: %v", defaultNav, err))
}
}
return false
})
ctx.Logger().Info("Done adding marker net asset values")
}

// setExchangeParams sets exchange module's params to the defaults.
// TODO: Remove with the saffron handlers.
func setExchangeParams(ctx sdk.Context, app *App) {
Expand Down
82 changes: 81 additions & 1 deletion app/upgrades_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/provenance-io/provenance/x/exchange"
markertypes "github.com/provenance-io/provenance/x/marker/types"
msgfeetypes "github.com/provenance-io/provenance/x/msgfees/types"
)

Expand Down Expand Up @@ -427,6 +428,8 @@ func (s *UpgradeTestSuite) TestSaffronRC1() {
"INF Done updating ICQ params",
"INF Updating MaxSupply marker param",
"INF Done updating MaxSupply marker param",
"INF Adding marker net asset values",
"INF Done adding marker net asset values",
"INF Ensuring exchange module params are set.",
}

Expand All @@ -444,6 +447,8 @@ func (s *UpgradeTestSuite) TestSaffron() {
"INF Done updating ICQ params",
"INF Updating MaxSupply marker param",
"INF Done updating MaxSupply marker param",
"INF Adding marker net asset values",
"INF Done adding marker net asset values",
"INF Ensuring exchange module params are set.",
}

Expand Down Expand Up @@ -845,6 +850,82 @@ func (s *UpgradeTestSuite) TestSetAccountDataNameRecord() {
s.AssertLogContents(logOutput, expInLog, nil, true, "setAccountDataNameRecord")
}

func (s *UpgradeTestSuite) TestAddMarkerNavs() {
address1 := sdk.AccAddress("address1")
testcoin := markertypes.NewEmptyMarkerAccount("testcoin",
address1.String(),
[]markertypes.AccessGrant{})
testcoin.Supply = sdk.OneInt()
s.Require().NoError(s.app.MarkerKeeper.AddMarkerAccount(s.ctx, testcoin), "AddMarkerAccount() error")

testcoinInList := markertypes.NewEmptyMarkerAccount("testcoininlist",
address1.String(),
[]markertypes.AccessGrant{})
testcoinInList.Supply = sdk.OneInt()
s.Require().NoError(s.app.MarkerKeeper.AddMarkerAccount(s.ctx, testcoinInList), "AddMarkerAccount() error")

nosupplycoin := markertypes.NewEmptyMarkerAccount("nosupplycoin",
address1.String(),
[]markertypes.AccessGrant{})
s.Require().NoError(s.app.MarkerKeeper.AddMarkerAccount(s.ctx, nosupplycoin), "AddMarkerAccount() error")

hasnavcoin := markertypes.NewEmptyMarkerAccount("hasnavcoin",
address1.String(),
[]markertypes.AccessGrant{})
hasnavcoin.Supply = sdk.NewInt(100)
s.Require().NoError(s.app.MarkerKeeper.AddMarkerAccount(s.ctx, hasnavcoin), "AddMarkerAccount() error")
presentnav := markertypes.NewNetAssetValue(sdk.NewInt64Coin(markertypes.UsdDenom, int64(55)), uint64(100))
s.Require().NoError(s.app.MarkerKeeper.AddSetNetAssetValues(s.ctx, hasnavcoin, []markertypes.NetAssetValue{presentnav}, "test"))

addMarkerNavs(s.ctx, s.app, map[string]markertypes.NetAssetValue{
"testcoininlist": {Price: sdk.NewInt64Coin(markertypes.UsdDenom, int64(12345)), Volume: uint64(1)},
}, markertypes.NetAssetValue{Price: sdk.NewInt64Coin(markertypes.UsdDenom, int64(150)), Volume: uint64(1)})

tests := []struct {
name string
markerAddr sdk.AccAddress
expNav *markertypes.NetAssetValue
}{
{
name: "upgrade adds new default nav",
markerAddr: testcoin.GetAddress(),
expNav: &markertypes.NetAssetValue{Price: sdk.NewInt64Coin(markertypes.UsdDenom, int64(150)), Volume: uint64(1)},
},
{
name: "already has nav",
markerAddr: hasnavcoin.GetAddress(),
expNav: &markertypes.NetAssetValue{Price: sdk.NewInt64Coin(markertypes.UsdDenom, int64(55)), Volume: uint64(100)},
},
{
name: "nav add fails for coin",
markerAddr: nosupplycoin.GetAddress(),
expNav: nil,
},
{
name: "nav set from custom config",
markerAddr: testcoinInList.GetAddress(),
expNav: &markertypes.NetAssetValue{Price: sdk.NewInt64Coin(markertypes.UsdDenom, int64(12345)), Volume: uint64(1)},
},
}
for _, tc := range tests {
s.Run(tc.name, func() {
netAssetValues := []markertypes.NetAssetValue{}
err := s.app.MarkerKeeper.IterateNetAssetValues(s.ctx, tc.markerAddr, func(state markertypes.NetAssetValue) (stop bool) {
netAssetValues = append(netAssetValues, state)
return false
})
s.Require().NoError(err, "IterateNetAssetValues err")
if tc.expNav != nil {
s.Assert().Len(netAssetValues, 1, "Should be 1 nav set for testcoin")
s.Assert().Equal(tc.expNav.Price, netAssetValues[0].Price, "Net asset value price should equal default upgraded price")
s.Assert().Equal(tc.expNav.Volume, netAssetValues[0].Volume, "Net asset value volume should equal 1")
} else {
s.Assert().Len(netAssetValues, 0, "Marker not expected to have nav")
}
})
}
}

func (s *UpgradeTestSuite) TestSetExchangeParams() {
startMsg := "INF Ensuring exchange module params are set."
noopMsg := "INF Exchange module params are already defined."
Expand Down Expand Up @@ -894,7 +975,6 @@ func (s *UpgradeTestSuite) TestSetExchangeParams() {
expInLog: []string{startMsg, noopMsg, doneMsg},
},
}

for _, tc := range tests {
s.Run(tc.name, func() {
s.app.ExchangeKeeper.SetParams(s.ctx, tc.existingParams)
Expand Down
Loading