Skip to content

Commit

Permalink
fix: check recipient for lp role (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnletey authored Sep 12, 2024
1 parent 865b1ce commit f2372b2
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 10 deletions.
1 change: 1 addition & 0 deletions .changelog/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
project_url = "https://github.com/noble-assets/halo"
Empty file added .changelog/unreleased/.gitkeep
Empty file.
3 changes: 3 additions & 0 deletions .changelog/v1.0.0/summary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*Aug 26, 2024*

Initial release of the `x/halo` module, allowing [USYC](https://usyc.hashnote.com) native issuance on Noble.
1 change: 1 addition & 0 deletions .changelog/v1.0.1/bug-fixes/8-recipient-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Ensure the recipient is a liquidity provider when trading to fiat. ([#8](https://github.com/noble-assets/halo/pull/8))
3 changes: 3 additions & 0 deletions .changelog/v1.0.1/summary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*Sep 12, 2024*

This is a consensus breaking patch to the `v1` release line.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# CHANGELOG

## v1.0.1

*Sep 12, 2024*

This is a consensus breaking patch to the `v1` release line.

### BUG FIXES

- Ensure the recipient is a liquidity provider when trading to fiat. ([#8](https://github.com/noble-assets/halo/pull/8))

## v1.0.0

*Aug 26, 2024*

Initial release of the `x/halo` module, allowing [USYC](https://usyc.hashnote.com) native issuance on Noble.

2 changes: 1 addition & 1 deletion x/halo/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (k msgServer) TradeToFiat(goCtx context.Context, msg *types.MsgTradeToFiat)
if err != nil {
return nil, errors.Wrapf(err, "unable to decode recipient address %s", msg.Recipient)
}
if !k.HasRole(ctx, signer, entitlements.ROLE_LIQUIDITY_PROVIDER) {
if !k.HasRole(ctx, recipient, entitlements.ROLE_LIQUIDITY_PROVIDER) {
return nil, types.ErrInvalidLiquidityProvider
}
if !msg.Amount.IsPositive() {
Expand Down
18 changes: 9 additions & 9 deletions x/halo/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -913,8 +913,8 @@ func TestTradeToFiat(t *testing.T) {
goCtx := sdk.WrapSDKContext(ctx)
server := keeper.NewMsgServer(k)

// ARRANGE: Generate an admin account.
admin := utils.TestAccount()
// ARRANGE: Generate an admin and recipient account.
admin, recipient := utils.TestAccount(), utils.TestAccount()

// ACT: Attempt to trade to fiat with an invalid signer address.
_, err := server.TradeToFiat(goCtx, &types.MsgTradeToFiat{
Expand All @@ -936,27 +936,27 @@ func TestTradeToFiat(t *testing.T) {
// ACT: Attempt to trade to fiat with an invalid recipient address.
_, err = server.TradeToFiat(goCtx, &types.MsgTradeToFiat{
Signer: admin.Address,
Recipient: admin.Invalid,
Recipient: recipient.Invalid,
})
// ASSERT: The action should've failed due to invalid recipient address.
require.ErrorContains(t, err, "unable to decode recipient address")

// ACT: Attempt to trade to fiat with invalid recipient permissions.
_, err = server.TradeToFiat(goCtx, &types.MsgTradeToFiat{
Signer: admin.Address,
Recipient: admin.Address,
Recipient: recipient.Address,
})
// ASSERT: The action should've failed due to invalid recipient permissions.
require.ErrorContains(t, err, types.ErrInvalidLiquidityProvider.Error())

// ARRANGE: Set liquidity provider in state.
k.SetUserRole(ctx, admin.Bytes, entitlements.ROLE_LIQUIDITY_PROVIDER, true)
k.SetUserRole(ctx, recipient.Bytes, entitlements.ROLE_LIQUIDITY_PROVIDER, true)

// ACT: Attempt to trade to fiat with insufficient funds.
_, err = server.TradeToFiat(goCtx, &types.MsgTradeToFiat{
Signer: admin.Address,
Amount: ONE,
Recipient: admin.Address,
Recipient: recipient.Address,
})
// ASSERT: The action should've failed due to insufficient funds.
require.ErrorContains(t, err, "insufficient funds")
Expand All @@ -968,7 +968,7 @@ func TestTradeToFiat(t *testing.T) {
_, err = server.TradeToFiat(goCtx, &types.MsgTradeToFiat{
Signer: admin.Address,
Amount: sdk.NewInt(-1_000_000),
Recipient: admin.Address,
Recipient: recipient.Address,
})
// ASSERT: The action should've failed due to invalid amount.
require.ErrorContains(t, err, "invalid amount")
Expand All @@ -977,11 +977,11 @@ func TestTradeToFiat(t *testing.T) {
_, err = server.TradeToFiat(goCtx, &types.MsgTradeToFiat{
Signer: admin.Address,
Amount: ONE,
Recipient: admin.Address,
Recipient: recipient.Address,
})
// ASSERT: The action should've succeeded.
require.NoError(t, err)
require.Equal(t, ONE, bank.Balances[admin.Address].AmountOf(k.Underlying))
require.Equal(t, ONE, bank.Balances[recipient.Address].AmountOf(k.Underlying))
require.True(t, bank.Balances[types.ModuleName].IsZero())
}

Expand Down

0 comments on commit f2372b2

Please sign in to comment.