Skip to content

Commit

Permalink
add nav event emit
Browse files Browse the repository at this point in the history
  • Loading branch information
nullpointer0x00 committed Aug 16, 2023
1 parent 6d34194 commit e7d7946
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 32 deletions.
34 changes: 18 additions & 16 deletions internal/handlers/bank_send_restriction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion x/marker/keeper/invariants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 8 additions & 5 deletions x/marker/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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)

Check failure on line 241 in x/marker/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / Build dbmigrate

undefined: types.NewEventSetNetAssetValue

Check failure on line 241 in x/marker/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / Build Linux

undefined: types.NewEventSetNetAssetValue

Check failure on line 241 in x/marker/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / Build OSX

undefined: types.NewEventSetNetAssetValue

Check failure on line 241 in x/marker/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / build-linux

undefined: types.NewEventSetNetAssetValue

Check failure on line 241 in x/marker/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / test-race (00)

undefined: types.NewEventSetNetAssetValue

Check failure on line 241 in x/marker/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / test-race (01)

undefined: types.NewEventSetNetAssetValue

Check failure on line 241 in x/marker/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / test-race (01)

undefined: types.NewEventSetNetAssetValue

Check failure on line 241 in x/marker/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / test-race (02)

undefined: types.NewEventSetNetAssetValue

Check failure on line 241 in x/marker/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / test-race (03)

undefined: types.NewEventSetNetAssetValue

Check failure on line 241 in x/marker/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (00)

undefined: types.NewEventSetNetAssetValue

Check failure on line 241 in x/marker/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (01)

undefined: types.NewEventSetNetAssetValue

Check failure on line 241 in x/marker/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (01)

undefined: types.NewEventSetNetAssetValue

Check failure on line 241 in x/marker/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (02)

undefined: types.NewEventSetNetAssetValue

Check failure on line 241 in x/marker/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (03)

undefined: types.NewEventSetNetAssetValue
return ctx.EventManager().EmitTypedEvent(markerActivateEvent)

}

// IterateNetAssetValues iterates net asset values for marker
Expand Down
12 changes: 6 additions & 6 deletions x/marker/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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")

Expand All @@ -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")

Expand Down
6 changes: 3 additions & 3 deletions x/marker/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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())
}
Expand Down
2 changes: 1 addition & 1 deletion x/marker/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions x/marker/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
}
}

0 comments on commit e7d7946

Please sign in to comment.