Skip to content

Commit

Permalink
swap: correction of swapout in CheckPremiumAmount
Browse files Browse the repository at this point in the history
Corrected where to get premium limit at swapout in
CheckPremiumAmount and OnSwapOutRequestReceived.
Also, unit tests for CheckPremiumAmount have been added.
  • Loading branch information
YusukeShimizu committed Feb 1, 2024
1 parent 9c34c47 commit d8b683d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
2 changes: 1 addition & 1 deletion swap/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (v *CheckPremiumAmount) Execute(services *SwapServices, swap *SwapData) Eve
} else if swap.SwapOutAgreement != nil {
if swap.SwapOutAgreement.Premium > swap.SwapOutRequest.PremiumLimit {
return swap.HandleError(fmt.Errorf("premium amt too high: %d, limit : %d",
swap.SwapOutAgreement.Premium, swap.SwapInRequest.PremiumLimit))
swap.SwapOutAgreement.Premium, swap.SwapOutRequest.PremiumLimit))
}
return v.next.Execute(services, swap)
}
Expand Down
73 changes: 73 additions & 0 deletions swap/actions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package swap

import (
"testing"

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

func TestCheckPremiumAmount_Execute(t *testing.T) {
t.Parallel()
next := &NoOpAction{}
v := CheckPremiumAmount{
next: next,
}
tests := map[string]struct {
swap *SwapData
want EventType
}{
"swap in premium amount is within limit": {
swap: &SwapData{
SwapInAgreement: &SwapInAgreementMessage{
Premium: 100,
},
SwapInRequest: &SwapInRequestMessage{
PremiumLimit: 200,
},
},
want: NoOp,
},
"swap in premium amount exceeds limit": {
swap: &SwapData{
SwapInAgreement: &SwapInAgreementMessage{
Premium: 300,
},
SwapInRequest: &SwapInRequestMessage{
PremiumLimit: 200,
},
},
want: Event_ActionFailed,
},
"swap out premium amount is within limit": {
swap: &SwapData{
SwapOutAgreement: &SwapOutAgreementMessage{
Premium: 100,
},
SwapOutRequest: &SwapOutRequestMessage{
PremiumLimit: 200,
},
},
want: NoOp,
},
"swap out premium amount exceeds limit": {
swap: &SwapData{
SwapOutAgreement: &SwapOutAgreementMessage{
Premium: 300,
},
SwapOutRequest: &SwapOutRequestMessage{
PremiumLimit: 200,
},
},
want: Event_ActionFailed,
},
}

for name, tt := range tests {
tt := tt
t.Run(name, func(t *testing.T) {
t.Parallel()
got := v.Execute(nil, tt.swap)
assert.Equal(t, tt.want, got, "Event type should match")
})
}
}
2 changes: 1 addition & 1 deletion swap/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ func (s *SwapService) OnSwapInRequestReceived(swapId *SwapId, peerId string, mes

// OnSwapInRequestReceived creates a new swap-out process and sends the event to the swap statemachine
func (s *SwapService) OnSwapOutRequestReceived(swapId *SwapId, peerId string, message *SwapOutRequestMessage) error {
premium := ComputePremium(message.Amount, s.swapServices.policy.GetSwapInPremiumRatePPM())
premium := ComputePremium(message.Amount, s.swapServices.policy.GetSwapOutPremiumRatePPM())
if premium > message.PremiumLimit {
err := fmt.Errorf("unacceptable premium: %d, limit: %d", premium, message.PremiumLimit)
msg := fmt.Sprintf("from the %s peer: %s", s.swapServices.lightning.Implementation(), err.Error())
Expand Down

0 comments on commit d8b683d

Please sign in to comment.