From e7d79464bdf8325742b2db31a08b883fb26c1b2c Mon Sep 17 00:00:00 2001 From: Carlton N Hanna Date: Wed, 16 Aug 2023 09:22:29 -0600 Subject: [PATCH] add nav event emit --- .../handlers/bank_send_restriction_test.go | 34 ++++++++++--------- x/marker/keeper/invariants_test.go | 2 +- x/marker/keeper/keeper.go | 13 ++++--- x/marker/keeper/keeper_test.go | 12 +++---- x/marker/keeper/msg_server.go | 6 ++-- x/marker/keeper/msg_server_test.go | 2 +- x/marker/types/events.go | 11 ++++++ 7 files changed, 48 insertions(+), 32 deletions(-) diff --git a/internal/handlers/bank_send_restriction_test.go b/internal/handlers/bank_send_restriction_test.go index 7239ac585a..289e7b8b60 100644 --- a/internal/handlers/bank_send_restriction_test.go +++ b/internal/handlers/bank_send_restriction_test.go @@ -47,33 +47,35 @@ func TestBankSend(tt *testing.T) { require.NoError(tt, app.AttributeKeeper.SetAttribute(ctx, attrtypes.NewAttribute("some.kyc.provenance.io", acct3.Address, attrtypes.AttributeType_Bytes, []byte{}, nil), addr1)) nrMarkerDenom := "nonrestrictedmarker" - nrMarkerAcct := authtypes.NewBaseAccount(types.MustGetMarkerAddress(nrMarkerDenom), nil, 200, 0) - require.NoError(tt, app.MarkerKeeper.SetNetAssetValue(ctx, nrMarkerAcct.GetAddress(), markertypes.NetAssetValue{ + nrMarkerBaseAcct := authtypes.NewBaseAccount(types.MustGetMarkerAddress(nrMarkerDenom), nil, 200, 0) + nrMarkerAcct := markertypes.NewMarkerAccount(nrMarkerBaseAcct, sdk.NewInt64Coin(nrMarkerDenom, 10_000), addr1, []markertypes.AccessGrant{{Address: acct1.Address, + Permissions: []markertypes.Access{markertypes.Access_Withdraw}}}, markertypes.StatusProposed, markertypes.MarkerType_Coin, true, true, false, []string{}) + require.NoError(tt, app.MarkerKeeper.SetNetAssetValue(ctx, nrMarkerAcct, markertypes.NetAssetValue{ Value: sdk.NewInt64Coin(pioconfig.GetProvenanceConfig().BondDenom, 1), Volume: 1, - }), "SetNetAssetValue failed to create nav for marker") - require.NoError(tt, app.MarkerKeeper.AddFinalizeAndActivateMarker(ctx, markertypes.NewMarkerAccount(nrMarkerAcct, sdk.NewInt64Coin(nrMarkerDenom, 10_000), addr1, []markertypes.AccessGrant{{Address: acct1.Address, - Permissions: []markertypes.Access{markertypes.Access_Withdraw}}}, markertypes.StatusProposed, markertypes.MarkerType_Coin, true, true, false, []string{})), + }, "test"), "SetNetAssetValue failed to create nav for marker") + require.NoError(tt, app.MarkerKeeper.AddFinalizeAndActivateMarker(ctx, nrMarkerAcct), "AddFinalizeAndActivateMarker failed to create marker") restrictedMarkerDenom := "restrictedmarker" - rMarkerAcct := authtypes.NewBaseAccount(types.MustGetMarkerAddress(restrictedMarkerDenom), nil, 300, 0) - - require.NoError(tt, app.MarkerKeeper.SetNetAssetValue(ctx, rMarkerAcct.GetAddress(), markertypes.NetAssetValue{ + rMarkerBaseAcct := authtypes.NewBaseAccount(types.MustGetMarkerAddress(restrictedMarkerDenom), nil, 300, 0) + rMarkerAcct := markertypes.NewMarkerAccount(rMarkerBaseAcct, sdk.NewInt64Coin(restrictedMarkerDenom, 10_000), addr1, []markertypes.AccessGrant{{Address: acct1.Address, + Permissions: []markertypes.Access{markertypes.Access_Withdraw, markertypes.Access_Transfer}}}, markertypes.StatusProposed, markertypes.MarkerType_RestrictedCoin, true, true, false, []string{}) + require.NoError(tt, app.MarkerKeeper.SetNetAssetValue(ctx, rMarkerAcct, markertypes.NetAssetValue{ Value: sdk.NewInt64Coin(pioconfig.GetProvenanceConfig().BondDenom, 1), Volume: 1, - }), "SetNetAssetValue failed to create nav for marker") - require.NoError(tt, app.MarkerKeeper.AddFinalizeAndActivateMarker(ctx, markertypes.NewMarkerAccount(rMarkerAcct, sdk.NewInt64Coin(restrictedMarkerDenom, 10_000), addr1, []markertypes.AccessGrant{{Address: acct1.Address, - Permissions: []markertypes.Access{markertypes.Access_Withdraw, markertypes.Access_Transfer}}}, markertypes.StatusProposed, markertypes.MarkerType_RestrictedCoin, true, true, false, []string{})), "AddFinalizeAndActivateMarker failed to create marker") + }, "test"), "SetNetAssetValue failed to create nav for marker") + require.NoError(tt, app.MarkerKeeper.AddFinalizeAndActivateMarker(ctx, rMarkerAcct), "AddFinalizeAndActivateMarker failed to create marker") restrictedAttrMarkerDenom := "restrictedmarkerattr" - raMarkerAcct := authtypes.NewBaseAccount(types.MustGetMarkerAddress(restrictedAttrMarkerDenom), nil, 400, 0) - require.NoError(tt, app.MarkerKeeper.SetNetAssetValue(ctx, raMarkerAcct.GetAddress(), markertypes.NetAssetValue{ + raMarkerBaseAcct := authtypes.NewBaseAccount(types.MustGetMarkerAddress(restrictedAttrMarkerDenom), nil, 400, 0) + raMarkerAcct := markertypes.NewMarkerAccount(raMarkerBaseAcct, sdk.NewInt64Coin(restrictedAttrMarkerDenom, 10_000), addr1, []markertypes.AccessGrant{{Address: acct1.Address, + Permissions: []markertypes.Access{markertypes.Access_Withdraw, markertypes.Access_Transfer}}}, markertypes.StatusProposed, markertypes.MarkerType_RestrictedCoin, true, true, false, []string{"some.kyc.provenance.io"}) + require.NoError(tt, app.MarkerKeeper.SetNetAssetValue(ctx, raMarkerAcct, markertypes.NetAssetValue{ Value: sdk.NewInt64Coin(pioconfig.GetProvenanceConfig().BondDenom, 1), Volume: 1, - }), "SetNetAssetValue failed to create nav for marker") - require.NoError(tt, app.MarkerKeeper.AddFinalizeAndActivateMarker(ctx, markertypes.NewMarkerAccount(raMarkerAcct, sdk.NewInt64Coin(restrictedAttrMarkerDenom, 10_000), addr1, []markertypes.AccessGrant{{Address: acct1.Address, - Permissions: []markertypes.Access{markertypes.Access_Withdraw, markertypes.Access_Transfer}}}, markertypes.StatusProposed, markertypes.MarkerType_RestrictedCoin, true, true, false, []string{"some.kyc.provenance.io"})), "AddFinalizeAndActivateMarker failed to create marker") + }, "test"), "SetNetAssetValue failed to create nav for marker") + require.NoError(tt, app.MarkerKeeper.AddFinalizeAndActivateMarker(ctx, raMarkerAcct), "AddFinalizeAndActivateMarker failed to create marker") // Check both account balances before we begin. addr1beforeBalance := app.BankKeeper.GetAllBalances(ctx, addr1).String() diff --git a/x/marker/keeper/invariants_test.go b/x/marker/keeper/invariants_test.go index b4aa50a573..84072fa596 100644 --- a/x/marker/keeper/invariants_test.go +++ b/x/marker/keeper/invariants_test.go @@ -37,7 +37,7 @@ func TestMarkerInvariant(t *testing.T) { require.NoError(t, mac.SetManager(user)) require.NoError(t, mac.SetSupply(sdk.NewCoin(mac.Denom, sdk.OneInt()))) require.NoError(t, app.MarkerKeeper.AddMarkerAccount(ctx, mac)) - require.NoError(t, app.MarkerKeeper.SetNetAssetValue(ctx, mac.GetAddress(), types.NewNetAssetValue(sdk.NewInt64Coin(types.UsdDenom, 1), 1))) + require.NoError(t, app.MarkerKeeper.SetNetAssetValue(ctx, mac, types.NewNetAssetValue(sdk.NewInt64Coin(types.UsdDenom, 1), 1), "test")) // Initial, invariant should pass _, isBroken := invariantChecks(ctx) diff --git a/x/marker/keeper/keeper.go b/x/marker/keeper/keeper.go index 3d5f6715d0..33f001e40a 100644 --- a/x/marker/keeper/keeper.go +++ b/x/marker/keeper/keeper.go @@ -211,7 +211,7 @@ func (k Keeper) RemoveSendDeny(ctx sdk.Context, markerAddr, senderAddr sdk.AccAd store.Delete(types.DenySendKey(markerAddr, senderAddr)) } -func (k Keeper) AddSetNetAssetValues(ctx sdk.Context, marker types.MarkerAccountI, netAssetValues []types.NetAssetValue) error { +func (k Keeper) AddSetNetAssetValues(ctx sdk.Context, marker types.MarkerAccountI, netAssetValues []types.NetAssetValue, source string) error { for _, nav := range netAssetValues { if nav.Value.Denom == marker.GetDenom() { return fmt.Errorf("net asset value denom cannot match marker denom %q", marker.GetDenom()) @@ -224,20 +224,23 @@ func (k Keeper) AddSetNetAssetValues(ctx sdk.Context, marker types.MarkerAccount } nav.UpdatedBlockHeight = uint64(ctx.BlockHeight()) - if err := k.SetNetAssetValue(ctx, marker.GetAddress(), nav); err != nil { + if err := k.SetNetAssetValue(ctx, marker, nav, source); err != nil { return fmt.Errorf("cannot set net asset value %v : %v", nav, err.Error()) } } return nil } -func (k Keeper) SetNetAssetValue(ctx sdk.Context, markerAddr sdk.AccAddress, netAssetValue types.NetAssetValue) error { +func (k Keeper) SetNetAssetValue(ctx sdk.Context, marker types.MarkerAccountI, netAssetValue types.NetAssetValue, source string) error { bz, err := k.cdc.Marshal(&netAssetValue) if err != nil { return err } - ctx.KVStore(k.storeKey).Set(types.NetAssetValueKey(markerAddr, netAssetValue.Value.Denom), bz) - return nil + ctx.KVStore(k.storeKey).Set(types.NetAssetValueKey(marker.GetAddress(), netAssetValue.Value.Denom), bz) + + markerActivateEvent := types.NewEventSetNetAssetValue(marker.GetDenom(), netAssetValue.Value, netAssetValue.Volume, source) + return ctx.EventManager().EmitTypedEvent(markerActivateEvent) + } // IterateNetAssetValues iterates net asset values for marker diff --git a/x/marker/keeper/keeper_test.go b/x/marker/keeper/keeper_test.go index 1d4649f6f8..f4468ef5a8 100644 --- a/x/marker/keeper/keeper_test.go +++ b/x/marker/keeper/keeper_test.go @@ -182,7 +182,7 @@ func TestAccountKeeperManageAccess(t *testing.T) { require.NoError(t, mac.SetManager(user1)) require.NoError(t, mac.SetSupply(sdk.NewCoin(mac.Denom, sdk.OneInt()))) require.NoError(t, app.MarkerKeeper.AddMarkerAccount(ctx, mac)) - require.NoError(t, app.MarkerKeeper.SetNetAssetValue(ctx, mac.GetAddress(), types.NewNetAssetValue(sdk.NewInt64Coin(types.UsdDenom, 1), 1))) + require.NoError(t, app.MarkerKeeper.SetNetAssetValue(ctx, mac, types.NewNetAssetValue(sdk.NewInt64Coin(types.UsdDenom, 1), 1), "test")) // Initial, should not have access m, err := app.MarkerKeeper.GetMarkerByDenom(ctx, "testcoin") @@ -330,7 +330,7 @@ func TestAccountKeeperMintBurnCoins(t *testing.T) { require.NoError(t, mac.SetSupply(sdk.NewCoin("testcoin", sdk.NewInt(1000)))) require.NoError(t, app.MarkerKeeper.AddMarkerAccount(ctx, mac)) - require.NoError(t, app.MarkerKeeper.SetNetAssetValue(ctx, mac.GetAddress(), types.NewNetAssetValue(sdk.NewInt64Coin(types.UsdDenom, 1), 1))) + require.NoError(t, app.MarkerKeeper.SetNetAssetValue(ctx, mac, types.NewNetAssetValue(sdk.NewInt64Coin(types.UsdDenom, 1), 1), "test")) // Should not fail for a non-active/finalized coin, must be able to adjust supply amount to match any existing require.NoError(t, app.MarkerKeeper.MintCoin(ctx, user, sdk.NewInt64Coin("testcoin", 100))) @@ -492,7 +492,7 @@ func TestAccountInsufficientExisting(t *testing.T) { require.NoError(t, mac.SetSupply(sdk.NewCoin("testcoin", sdk.NewInt(1000)))) require.NoError(t, app.MarkerKeeper.AddMarkerAccount(ctx, mac)) - require.NoError(t, app.MarkerKeeper.SetNetAssetValue(ctx, mac.GetAddress(), types.NewNetAssetValue(sdk.NewInt64Coin(types.UsdDenom, 1), 1))) + require.NoError(t, app.MarkerKeeper.SetNetAssetValue(ctx, mac, types.NewNetAssetValue(sdk.NewInt64Coin(types.UsdDenom, 1), 1), "test")) // insufficient supply to cover existing require.Error(t, app.MarkerKeeper.FinalizeMarker(ctx, user, "testcoin")) @@ -530,7 +530,7 @@ func TestAccountImplictControl(t *testing.T) { require.NoError(t, mac.SetSupply(sdk.NewCoin("testcoin", sdk.NewInt(1000)))) require.NoError(t, app.MarkerKeeper.AddMarkerAccount(ctx, mac)) - require.NoError(t, app.MarkerKeeper.SetNetAssetValue(ctx, mac.GetAddress(), types.NewNetAssetValue(sdk.NewInt64Coin(types.UsdDenom, 1), 1))) + require.NoError(t, app.MarkerKeeper.SetNetAssetValue(ctx, mac, types.NewNetAssetValue(sdk.NewInt64Coin(types.UsdDenom, 1), 1), "test")) // Moves to finalized, mints required supply, moves to active status. require.NoError(t, app.MarkerKeeper.FinalizeMarker(ctx, user, "testcoin")) @@ -626,7 +626,7 @@ func TestForceTransfer(t *testing.T) { false, []string{}, ) - require.NoError(t, app.MarkerKeeper.SetNetAssetValue(ctx, noForceMac.GetAddress(), types.NewNetAssetValue(sdk.NewInt64Coin(types.UsdDenom, 1), 1))) + require.NoError(t, app.MarkerKeeper.SetNetAssetValue(ctx, noForceMac, types.NewNetAssetValue(sdk.NewInt64Coin(types.UsdDenom, 1), 1), "test")) require.NoError(t, app.MarkerKeeper.AddFinalizeAndActivateMarker(ctx, noForceMac), "AddFinalizeAndActivateMarker without force transfer") @@ -647,7 +647,7 @@ func TestForceTransfer(t *testing.T) { true, []string{}, ) - require.NoError(t, app.MarkerKeeper.SetNetAssetValue(ctx, wForceMac.GetAddress(), types.NewNetAssetValue(sdk.NewInt64Coin(types.UsdDenom, 1), 1))) + require.NoError(t, app.MarkerKeeper.SetNetAssetValue(ctx, wForceMac, types.NewNetAssetValue(sdk.NewInt64Coin(types.UsdDenom, 1), 1), "test")) require.NoError(t, app.MarkerKeeper.AddFinalizeAndActivateMarker(ctx, wForceMac), "AddFinalizeAndActivateMarker with force transfer") diff --git a/x/marker/keeper/msg_server.go b/x/marker/keeper/msg_server.go index 228d107d7e..e5be34c4fd 100644 --- a/x/marker/keeper/msg_server.go +++ b/x/marker/keeper/msg_server.go @@ -114,7 +114,7 @@ func (k msgServer) AddMarker(goCtx context.Context, msg *types.MsgAddMarkerReque } nav := types.NewNetAssetValue(sdk.NewInt64Coin(types.UsdDenom, int64(msg.UsdCents)), msg.Volume) - err = k.AddSetNetAssetValues(ctx, ma, []types.NetAssetValue{nav}) + err = k.AddSetNetAssetValues(ctx, ma, []types.NetAssetValue{nav}, "test") if err != nil { return nil, sdkerrors.ErrInvalidRequest.Wrap(err.Error()) } @@ -601,7 +601,7 @@ func (k msgServer) AddFinalizeActivateMarker(goCtx context.Context, msg *types.M normalizedReqAttrs, ) - err = k.AddSetNetAssetValues(ctx, ma, []types.NetAssetValue{types.NewNetAssetValue(sdk.NewInt64Coin(types.UsdDenom, int64(msg.UsdCents)), msg.Volume)}) + err = k.AddSetNetAssetValues(ctx, ma, []types.NetAssetValue{types.NewNetAssetValue(sdk.NewInt64Coin(types.UsdDenom, int64(msg.UsdCents)), msg.Volume)}, "test") if err != nil { return nil, sdkerrors.ErrInvalidRequest.Wrap(err.Error()) } @@ -837,7 +837,7 @@ func (k msgServer) AddNetAssetValue(goCtx context.Context, msg *types.MsgAddNetA } } - err = k.AddSetNetAssetValues(ctx, marker, msg.NetAssetValues) + err = k.AddSetNetAssetValues(ctx, marker, msg.NetAssetValues, "test") if err != nil { return nil, sdkerrors.ErrInvalidRequest.Wrap(err.Error()) } diff --git a/x/marker/keeper/msg_server_test.go b/x/marker/keeper/msg_server_test.go index 734effd8ec..e8871873ad 100644 --- a/x/marker/keeper/msg_server_test.go +++ b/x/marker/keeper/msg_server_test.go @@ -74,7 +74,7 @@ func (s *MsgServerTestSuite) TestMsgFinalizeMarkerRequest() { ) validMarker.Supply = sdk.NewInt(1) s.Require().NoError(s.app.MarkerKeeper.AddMarkerAccount(s.ctx, validMarker)) - s.Require().NoError(s.app.MarkerKeeper.SetNetAssetValue(s.ctx, validMarker.GetAddress(), types.NetAssetValue{Value: sdk.NewInt64Coin(types.UsdDenom, 1), Volume: 1})) + s.Require().NoError(s.app.MarkerKeeper.SetNetAssetValue(s.ctx, validMarker, types.NetAssetValue{Value: sdk.NewInt64Coin(types.UsdDenom, 1), Volume: 1}, "test")) testCases := []struct { name string diff --git a/x/marker/types/events.go b/x/marker/types/events.go index 6d0ee6e676..1e45051abc 100644 --- a/x/marker/types/events.go +++ b/x/marker/types/events.go @@ -2,7 +2,9 @@ package types import ( "fmt" + "strconv" + sdk "github.com/cosmos/cosmos-sdk/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" ) @@ -169,3 +171,12 @@ func NewEventMarkerSetDenomMetadata(metadata banktypes.Metadata, administrator s Administrator: administrator, } } + +func EventEventSetNetAssetValue(denom string, price sdk.Coin, volume uint64, source string) *EventSetNetAssetValue { + return &EventSetNetAssetValue{ + Denom: denom, + Price: price.String(), + Volume: strconv.FormatUint(volume, 10), + Source: source, + } +}