Skip to content

Commit

Permalink
add new parameters to write scope msg, add command line flags for usd…
Browse files Browse the repository at this point in the history
… mils and volume, update tests
  • Loading branch information
nullpointer0x00 committed Jan 16, 2024
1 parent 539bdd4 commit 012eba4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
21 changes: 19 additions & 2 deletions x/metadata/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const (
FlagRequirePartyRollup = "require-party-rollup"
AddSwitch = "add"
RemoveSwitch = "remove"
FlagUsdMils = "usd-mils"
FlagVolume = "volume"
)

// NewTxCmd is the top-level command for Metadata CLI transactions.
Expand Down Expand Up @@ -134,7 +136,21 @@ func WriteScopeCmd() *cobra.Command {
requirePartyRollup,
)

msg := types.NewMsgWriteScopeRequest(scope, signers)
usdMils, err := cmd.Flags().GetUint64(FlagUsdMils)
if err != nil {
return fmt.Errorf("incorrect value for %s flag. Accepted: 0 or greater value Error: %w", FlagUsdMils, err)
}

Check warning on line 142 in x/metadata/client/cli/tx.go

View check run for this annotation

Codecov / codecov/patch

x/metadata/client/cli/tx.go#L141-L142

Added lines #L141 - L142 were not covered by tests

volume, err := cmd.Flags().GetUint64(FlagVolume)
if err != nil {
return fmt.Errorf("incorrect value for %s flag. Accepted: 0 or greater value Error: %w", FlagVolume, err)
}

Check warning on line 147 in x/metadata/client/cli/tx.go

View check run for this annotation

Codecov / codecov/patch

x/metadata/client/cli/tx.go#L146-L147

Added lines #L146 - L147 were not covered by tests

if usdMils > 0 && volume == 0 {
return fmt.Errorf("incorrect value for %s flag. Must be positive number if %s flag has been set to positive value", FlagVolume, FlagUsdMils)
}

Check warning on line 151 in x/metadata/client/cli/tx.go

View check run for this annotation

Codecov / codecov/patch

x/metadata/client/cli/tx.go#L150-L151

Added lines #L150 - L151 were not covered by tests

msg := types.NewMsgWriteScopeRequest(scope, signers, usdMils, volume)
err = msg.ValidateBasic()
if err != nil {
return err
Expand All @@ -147,7 +163,8 @@ func WriteScopeCmd() *cobra.Command {
cmd.Flags().Bool(FlagRequirePartyRollup, false, "Indicates party rollup is required in this scope")
addSignersFlagToCmd(cmd)
flags.AddTxFlagsToCmd(cmd)

cmd.Flags().Uint64(FlagUsdMils, 0, "Indicates the net asset value of scope in usd mils, i.e. 1234 = $1.234")
cmd.Flags().Uint64(FlagVolume, 0, "Indicates the volume of the net asset value")
return cmd
}

Expand Down
4 changes: 2 additions & 2 deletions x/metadata/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (s *MetadataHandlerTestSuite) TestAddAndDeleteScopeDataAccess() {
},
{
"setup test with new scope",
types.NewMsgWriteScopeRequest(*scope, []string{s.user1}),
types.NewMsgWriteScopeRequest(*scope, []string{s.user1}, 0, 0),
[]string{s.user1},
"",
},
Expand Down Expand Up @@ -277,7 +277,7 @@ func (s *MetadataHandlerTestSuite) TestAddAndDeleteScopeOwners() {
},
{
"setup test with new scope",
types.NewMsgWriteScopeRequest(*scope, []string{s.user1}),
types.NewMsgWriteScopeRequest(*scope, []string{s.user1}, 0, 0),
[]string{s.user1},
"",
},
Expand Down
4 changes: 3 additions & 1 deletion x/metadata/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,12 @@ func stringsToAccAddresses(strings []string) []sdk.AccAddress {
// ------------------ MsgWriteScopeRequest ------------------

// NewMsgWriteScopeRequest creates a new msg instance
func NewMsgWriteScopeRequest(scope Scope, signers []string) *MsgWriteScopeRequest {
func NewMsgWriteScopeRequest(scope Scope, signers []string, usdMils, volume uint64) *MsgWriteScopeRequest {
return &MsgWriteScopeRequest{
Scope: scope,
Signers: signers,
UsdMils: usdMils,
Volume: volume,
}
}

Expand Down
8 changes: 5 additions & 3 deletions x/metadata/types/msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func TestWriteScopeRoute(t *testing.T) {
DataAccess: []string{"data_accessor"},
ValueOwnerAddress: "value_owner",
}
var msg = NewMsgWriteScopeRequest(*scope, []string{})
var msg = NewMsgWriteScopeRequest(*scope, []string{}, 0, 0)

require.Equal(t, sdk.MsgTypeURL(msg), "/provenance.metadata.v1.MsgWriteScopeRequest")
expectedYaml := `scope:
Expand All @@ -245,14 +245,16 @@ func TestWriteScopeRoute(t *testing.T) {
signers: []
scope_uuid: ""
spec_uuid: ""
usdmils: 0
volume: 0
`
bz, err := yaml.Marshal(msg)
require.NoError(t, err, "yaml.Marshal(msg)")
assert.Equal(t, expectedYaml, string(bz), "scope as yaml")

bz, err = ModuleCdc.MarshalJSON(msg)
require.NoError(t, err, "ModuleCdc.MarshalJSON(msg)")
assert.Equal(t, "{\"scope\":{\"scope_id\":\"scope1qzxcpvj6czy5g354dews3nlruxjsahhnsp\",\"specification_id\":\"scopespec1qs30c9axgrw5669ft0kffe6h9gysfe58v3\",\"owners\":[{\"address\":\"data_owner\",\"role\":\"PARTY_TYPE_OWNER\",\"optional\":false}],\"data_access\":[\"data_accessor\"],\"value_owner_address\":\"value_owner\",\"require_party_rollup\":false},\"signers\":[],\"scope_uuid\":\"\",\"spec_uuid\":\"\"}", string(bz))
assert.Equal(t, "{\"scope\":{\"scope_id\":\"scope1qzxcpvj6czy5g354dews3nlruxjsahhnsp\",\"specification_id\":\"scopespec1qs30c9axgrw5669ft0kffe6h9gysfe58v3\",\"owners\":[{\"address\":\"data_owner\",\"role\":\"PARTY_TYPE_OWNER\",\"optional\":false}],\"data_access\":[\"data_accessor\"],\"value_owner_address\":\"value_owner\",\"require_party_rollup\":false},\"signers\":[],\"scope_uuid\":\"\",\"spec_uuid\":\"\",\"usd_mils\":\"0\",\"volume\":\"0\"}", string(bz))
}

func TestWriteScopeValidation(t *testing.T) {
Expand All @@ -263,7 +265,7 @@ func TestWriteScopeValidation(t *testing.T) {
DataAccess: []string{"data_accessor"},
ValueOwnerAddress: "value_owner",
}
var msg = NewMsgWriteScopeRequest(*scope, []string{"invalid"})
var msg = NewMsgWriteScopeRequest(*scope, []string{"invalid"}, 0, 0)
err := msg.ValidateBasic()
require.EqualError(t, err, "invalid scope owners: invalid party address [data_owner]: decoding bech32 failed: invalid separator index -1")
require.Panics(t, func() { msg.GetSigners() }, "panics due to invalid addresses")
Expand Down
6 changes: 5 additions & 1 deletion x/metadata/wasm/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ type WriteScope struct {
Scope Scope `json:"scope"`
// The signers' addresses.
Signers []string `json:"signers"`
// UsdCents used to initialize the net asset value of scope
UsdMils uint64 `json:"usd_mils,omitempty"`
// Volume for the net asset value of scope
Volume uint64 `json:"volume,omitempty"`
}

// Encoder returns a smart contract message encoder for the metadata module.
Expand Down Expand Up @@ -59,7 +63,7 @@ func (params *WriteScope) Encode() ([]sdk.Msg, error) {
return nil, err
}

msg := types.NewMsgWriteScopeRequest(*scope, params.Signers)
msg := types.NewMsgWriteScopeRequest(*scope, params.Signers, params.UsdMils, params.Volume)

return []sdk.Msg{msg}, nil
}

0 comments on commit 012eba4

Please sign in to comment.