Skip to content

Commit

Permalink
[CORE-656] add test cases, metrics, and comments to sending module (#625
Browse files Browse the repository at this point in the history
)

* add test cases, metrics, and comments to sending module
  • Loading branch information
tqin7 authored Oct 18, 2023
1 parent b899c35 commit aa445b9
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 12 deletions.
2 changes: 2 additions & 0 deletions protocol/lib/metrics/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,10 @@ const (
Transfer = "transfer"
ProcessDepositToSubaccount = "process_deposit_to_subaccount"
ProcessWithdrawFromSubaccount = "process_withdraw_from_subaccount"
SendFromModuleToAccount = "send_from_module_to_account"
AssetId = "asset_id"
SenderAddress = "sender_address"
SenderModuleName = "sender_module_name"
SenderSubaccount = "sender_subaccount"
RecipientAddress = "recipient_address"
RecipientSubaccount = "recipient_subaccount"
Expand Down
19 changes: 18 additions & 1 deletion protocol/x/sending/keeper/msg_server_create_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"context"

"cosmossdk.io/errors"
gometrics "github.com/armon/go-metrics"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/dydxprotocol/v4-chain/protocol/lib/metrics"
"github.com/dydxprotocol/v4-chain/protocol/x/sending/types"
)

// CreateTransfer initiates a transfer from sender (an `x/subaccounts` subaccount)
// to a recipient (an `x/subaccounts` subaccount).
func (k msgServer) CreateTransfer(
goCtx context.Context,
msg *types.MsgCreateTransfer,
Expand Down Expand Up @@ -97,7 +100,7 @@ func (k msgServer) WithdrawFromSubaccount(
return &types.MsgWithdrawFromSubaccountResponse{}, nil
}

// SendFromModuleToAccount sends a coin from a module to an account.
// SendFromModuleToAccount sends coins from a module to an account.
func (k msgServer) SendFromModuleToAccount(
goCtx context.Context,
msg *types.MsgSendFromModuleToAccount,
Expand All @@ -113,8 +116,22 @@ func (k msgServer) SendFromModuleToAccount(
ctx := sdk.UnwrapSDKContext(goCtx)

if err := k.Keeper.SendFromModuleToAccount(ctx, msg); err != nil {
telemetry.IncrCounterWithLabels(
[]string{types.ModuleName, metrics.SendFromModuleToAccount, metrics.Error},
1,
[]gometrics.Label{
metrics.GetLabelForStringValue(metrics.SenderModuleName, msg.SenderModuleName),
},
)
return nil, err
}
telemetry.IncrCounterWithLabels(
[]string{types.ModuleName, metrics.SendFromModuleToAccount, metrics.Success},
1,
[]gometrics.Label{
metrics.GetLabelForStringValue(metrics.SenderModuleName, msg.SenderModuleName),
},
)

return &types.MsgSendFromModuleToAccountResponse{}, nil
}
1 change: 1 addition & 0 deletions protocol/x/sending/keeper/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ func (k Keeper) GenerateWithdrawEvent(withdraw *types.MsgWithdrawFromSubaccount)
)
}

// SendFromModuleToAccount sends coins from a module account to an `x/bank` recipient.
func (k Keeper) SendFromModuleToAccount(
ctx sdk.Context,
msg *types.MsgSendFromModuleToAccount,
Expand Down
5 changes: 5 additions & 0 deletions protocol/x/sending/keeper/transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,11 @@ func TestSendFromModuleToAccount(t *testing.T) {
balanceToSend: 100,
recipientAddress: authtypes.NewModuleAddress(testModuleName).String(),
},
"Success - send 0 amount": {
initialModuleBalance: 700,
balanceToSend: 0,
recipientAddress: authtypes.NewModuleAddress(testModuleName).String(),
},
"Error - insufficient fund": {
initialModuleBalance: 100,
balanceToSend: 101,
Expand Down
21 changes: 10 additions & 11 deletions protocol/x/sending/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ import errorsmod "cosmossdk.io/errors"

// x/sending module sentinel errors
var (
ErrSenderSameAsRecipient = errorsmod.Register(ModuleName, 1, "Sender is the same as recipient")
ErrInvalidTransferAmount = errorsmod.Register(ModuleName, 2, "Invalid transfer amount")
ErrDuplicatedTransfer = errorsmod.Register(ModuleName, 3, "Duplicated transfer")
ErrTransferNotFound = errorsmod.Register(ModuleName, 4, "Transfer not found")
ErrMissingFields = errorsmod.Register(ModuleName, 5, "Transfer does not contain all required fields")
ErrInvalidAccountAddress = errorsmod.Register(ModuleName, 6, "Account address is invalid")
ErrEmptyModuleName = errorsmod.Register(ModuleName, 7, "Module name is empty")
ErrInvalidAuthority = errorsmod.Register(ModuleName, 8, "Authority is invalid")
ErrKeeperMethodsNotImplemented = errorsmod.Register(
ErrSenderSameAsRecipient = errorsmod.Register(ModuleName, 1, "Sender is the same as recipient")
ErrInvalidTransferAmount = errorsmod.Register(ModuleName, 2, "Invalid transfer amount")
ErrDuplicatedTransfer = errorsmod.Register(ModuleName, 3, "Duplicated transfer")
ErrTransferNotFound = errorsmod.Register(ModuleName, 4, "Transfer not found")
ErrMissingFields = errorsmod.Register(
ModuleName,
1100,
"Sending module keeper method not implemented",
5,
"Transfer does not contain all required fields",
)
ErrInvalidAccountAddress = errorsmod.Register(ModuleName, 6, "Account address is invalid")
ErrEmptyModuleName = errorsmod.Register(ModuleName, 7, "Module name is empty")
ErrInvalidAuthority = errorsmod.Register(ModuleName, 8, "Authority is invalid")
ErrNonUsdcAssetTransferNotImplemented = errorsmod.Register(
ModuleName,
1101,
Expand Down

0 comments on commit aa445b9

Please sign in to comment.