Skip to content

Commit

Permalink
Merge pull request #10128 from vegaprotocol/fix/join-team-api
Browse files Browse the repository at this point in the history
Wire JoinTeam in wallet
  • Loading branch information
ValentinTrinque authored Nov 20, 2023
2 parents eb2cd3e + 1dae145 commit cd3c418
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 39 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
- [10126](https://github.com/vegaprotocol/vega/issues/10126) - Account for invalid stop orders in batch, charge default gas.
- [10123](https://github.com/vegaprotocol/vega/issues/10123) - Ledger exports contain account types of "UNKNOWN" type
- [10132](https://github.com/vegaprotocol/vega/issues/10132) - Add mapping in `GraphQL` for update perps market proposal.
- [10125](https://github.com/vegaprotocol/vega/issues/10125) - Wire the `JoinTeam` command in the wallet.
- [10127](https://github.com/vegaprotocol/vega/issues/10127) - Untangle `ApplyReferralCode` and `JoinTeam` command verification.

## 0.73.0

Expand Down
13 changes: 2 additions & 11 deletions commands/apply_referral_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,12 @@ func checkApplyReferralCode(cmd *commandspb.ApplyReferralCode) Errors {
errs := NewErrors()

if cmd == nil {
return errs.FinalAddForProperty("join_team", ErrIsRequired)
return errs.FinalAddForProperty("apply_referral_code", ErrIsRequired)
}

if !IsVegaID(cmd.Id) {
errs.AddForProperty("join_team.team_id", ErrShouldBeAValidVegaID)
errs.AddForProperty("apply_referral_code.id", ErrShouldBeAValidVegaID)
}

return errs
}

func CheckJoinTeamReferralCode(cmd *commandspb.JoinTeam) error {
applyReferralCode := &commandspb.ApplyReferralCode{
Id: cmd.Id,
}
// This is not to be lazy, but the team ID is the same as the referral code used to create the referral set/team, so we need to ensure we
// always follow the same logic for checking the referral code
return checkApplyReferralCode(applyReferralCode).ErrorOrNil()
}
32 changes: 4 additions & 28 deletions commands/apply_referral_code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ import (
commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestApplyReferralCode(t *testing.T) {
t.Run("Joining team succeeds", testJoiningTeamSucceeds)
t.Run("Joining team with team ID fails", testApplyReferralCodeWithoutTeamIDFails)
t.Run("Applying referral code succeeds", testApplyReferralCodeSucceeds)
t.Run("Applying referral code with team ID fails", testApplyReferralCodeWithoutTeamIDFails)
}

func testJoiningTeamSucceeds(t *testing.T) {
func testApplyReferralCodeSucceeds(t *testing.T) {
err := checkApplyReferralCode(t, &commandspb.ApplyReferralCode{
Id: vgtest.RandomVegaID(),
})
Expand All @@ -45,7 +44,7 @@ func testApplyReferralCodeWithoutTeamIDFails(t *testing.T) {
Id: "",
})

assert.Contains(t, err.Get("join_team.team_id"), commands.ErrShouldBeAValidVegaID)
assert.Contains(t, err.Get("apply_referral_code.id"), commands.ErrShouldBeAValidVegaID)
}

func checkApplyReferralCode(t *testing.T, cmd *commandspb.ApplyReferralCode) commands.Errors {
Expand All @@ -60,26 +59,3 @@ func checkApplyReferralCode(t *testing.T, cmd *commandspb.ApplyReferralCode) com

return e
}

func TestJoinTeamCommand(t *testing.T) {
t.Run("Joining team succeeds", testJoinTeamCommandSucceeds)
t.Run("Joining team with invalid team ID fails", testJoinTeamCommandFails)
}

func testJoinTeamCommandSucceeds(t *testing.T) {
err := commands.CheckJoinTeamReferralCode(&commandspb.JoinTeam{
Id: vgtest.RandomVegaID(),
})

assert.Nil(t, err)
}

func testJoinTeamCommandFails(t *testing.T) {
err := commands.CheckJoinTeamReferralCode(&commandspb.JoinTeam{
Id: "",
})

var e commands.Errors
require.True(t, errors.As(err, &e))
assert.Contains(t, e.Get("join_team.team_id"), commands.ErrShouldBeAValidVegaID)
}
36 changes: 36 additions & 0 deletions commands/join_team.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package commands

import commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"

func CheckJoinTeam(cmd *commandspb.JoinTeam) error {
return checkJoinTeam(cmd).ErrorOrNil()
}

func checkJoinTeam(cmd *commandspb.JoinTeam) Errors {
errs := NewErrors()

if cmd == nil {
return errs.FinalAddForProperty("join_team", ErrIsRequired)
}

if !IsVegaID(cmd.Id) {
errs.AddForProperty("join_team.id", ErrShouldBeAValidVegaID)
}

return errs
}
61 changes: 61 additions & 0 deletions commands/join_team_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package commands_test

import (
"errors"
"testing"

"code.vegaprotocol.io/vega/commands"
vgtest "code.vegaprotocol.io/vega/libs/test"
commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"

"github.com/stretchr/testify/assert"
)

func TestJoinTeam(t *testing.T) {
t.Run("Joining team succeeds", testJoiningTeamSucceeds)
t.Run("Joining team with team ID fails", testJoinTeamWithoutTeamIDFails)
}

func testJoiningTeamSucceeds(t *testing.T) {
err := checkJoinTeam(t, &commandspb.JoinTeam{
Id: vgtest.RandomVegaID(),
})

assert.Empty(t, err)
}

func testJoinTeamWithoutTeamIDFails(t *testing.T) {
err := checkJoinTeam(t, &commandspb.JoinTeam{
Id: "",
})

assert.Contains(t, err.Get("join_team.id"), commands.ErrShouldBeAValidVegaID)
}

func checkJoinTeam(t *testing.T, cmd *commandspb.JoinTeam) commands.Errors {
t.Helper()

err := commands.CheckJoinTeam(cmd)

var e commands.Errors
if ok := errors.As(err, &e); !ok {
return commands.NewErrors()
}

return e
}
6 changes: 6 additions & 0 deletions wallet/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ func CheckSubmitTransactionRequest(req *walletpb.SubmitTransactionRequest) comma
cmdErr = commands.CheckApplyReferralCode(cmd.ApplyReferralCode)
case *walletpb.SubmitTransactionRequest_UpdateMarginMode:
cmdErr = commands.CheckUpdateMarginMode(cmd.UpdateMarginMode)
case *walletpb.SubmitTransactionRequest_JoinTeam:
cmdErr = commands.CheckJoinTeam(cmd.JoinTeam)
default:
errs.AddForProperty("input_data.command", commands.ErrIsNotSupported)
}
Expand Down Expand Up @@ -234,6 +236,10 @@ func WrapRequestCommandIntoInputData(data *commandspb.InputData, req *walletpb.S
data.Command = &commandspb.InputData_UpdateMarginMode{
UpdateMarginMode: req.GetUpdateMarginMode(),
}
case *walletpb.SubmitTransactionRequest_JoinTeam:
data.Command = &commandspb.InputData_JoinTeam{
JoinTeam: req.GetJoinTeam(),
}
default:
panic(fmt.Sprintf("command %T is not supported", cmd))
}
Expand Down

0 comments on commit cd3c418

Please sign in to comment.