Skip to content

Commit

Permalink
fix: ensure round existence (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-luca authored Aug 7, 2024
1 parent 98a4948 commit c68b7bd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
12 changes: 10 additions & 2 deletions x/halo/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ func (k *Keeper) mintCoins(ctx sdk.Context, recipient sdk.AccAddress, coins sdk.

// depositFor is an internal helper function to deposit.
func (k *Keeper) depositFor(ctx sdk.Context, signer sdk.AccAddress, recipient sdk.AccAddress, underlying sdk.Int) (amount sdk.Int, err error) {
round, _ := k.GetRound(ctx, k.GetLastRoundId(ctx))
lastRoundId := k.GetLastRoundId(ctx)
round, found := k.GetRound(ctx, lastRoundId)
if !found {
return sdk.Int{}, fmt.Errorf("round %d not found", lastRoundId)
}
amount = underlying.QuoRaw(10000).MulRaw(10000)
amount = amount.MulRaw(100000000).Quo(round.Answer)

Expand All @@ -147,7 +151,11 @@ func (k *Keeper) depositFor(ctx sdk.Context, signer sdk.AccAddress, recipient sd

// withdrawTo is an internal helper function to withdraw.
func (k *Keeper) withdrawTo(ctx sdk.Context, signer sdk.AccAddress, recipient sdk.AccAddress, amount sdk.Int) (underlying sdk.Int, err error) {
round, _ := k.GetRound(ctx, k.GetLastRoundId(ctx))
lastRoundId := k.GetLastRoundId(ctx)
round, found := k.GetRound(ctx, lastRoundId)
if !found {
return sdk.Int{}, fmt.Errorf("round %d not found", lastRoundId)
}
underlying = amount.Mul(round.Answer).QuoRaw(100000000)
underlying = underlying.QuoRaw(10000).MulRaw(10000)

Expand Down
23 changes: 23 additions & 0 deletions x/halo/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ func TestDepositFor(t *testing.T) {
// ARRANGE: Assign the international feeder role to recipient.
k.SetUserRole(ctx, recipient.Bytes, entitlements.ROLE_INTERNATIONAL_FEEDER, true)

// ACT: Attempt to deposit for with non-existing round.
_, err = server.DepositFor(goCtx, &types.MsgDepositFor{
Signer: user.Address,
Recipient: recipient.Address,
Amount: amount,
})
// ASSERT: The action should've failed due to non-existing round.
require.ErrorContains(t, err, "round 0 not found")

// ARRANGE: Report Ethereum Round #229.
// https://etherscan.io/tx/0xcff68ffc6f79afadf835f559f8a51ed7092bc679d2a4f34cd153ef321d6bc8ec
k.SetRound(ctx, 229, aggregator.RoundData{
Expand Down Expand Up @@ -390,6 +399,17 @@ func TestWithdrawTo(t *testing.T) {

// ARRANGE: Set user withdrawal nonce in state.
k.SetNonce(ctx, recipient.Bytes, 10)

// ACT: Attempt to withdraw to with non-existing last round.
_, err = server.WithdrawTo(goCtx, &types.MsgWithdrawTo{
Signer: user.Address,
Recipient: recipient.Address,
Amount: amount,
Signature: signature,
})
// ASSERT: The action should've failed due to non-existing last round.
require.ErrorContains(t, err, "round 0 not found")

// ARRANGE: Report Ethereum Round #139.
// https://etherscan.io/tx/0x9095266d81856a28b80c4500228ab994197652fc4ad1c05cd4345d1454fccfd7
k.SetRound(ctx, 139, aggregator.RoundData{
Expand All @@ -401,6 +421,9 @@ func TestWithdrawTo(t *testing.T) {
})
k.SetLastRoundId(ctx, 139)

// ARRANGE: Set user withdrawal nonce in state.
k.SetNonce(ctx, recipient.Bytes, 10)

// ACT: Attempt to withdraw to with insufficient funds.
_, err = server.WithdrawTo(goCtx, &types.MsgWithdrawTo{
Signer: user.Address,
Expand Down

0 comments on commit c68b7bd

Please sign in to comment.