-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9a3c89d
commit e80bc22
Showing
9 changed files
with
299 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
--- | ||
name: calculator | ||
emoji: 🧮 | ||
help: Perform calculations such as reward and fee estimations | ||
sub_commands: | ||
- name: reward | ||
help: Calculate the PAC coins you can earn based on your validator stake | ||
result_template: | | ||
Approximately you earn {{.reward}} PAC reward, with {{.stake}} stake 🔒 on your validator in {{.days}} days ⏰ with {{.totalPower}} total power ⚡ of committee. | ||
> Note📝: This number is just an estimation. It will vary depending on your stake amount and total network power. | ||
args: | ||
- name: stake | ||
desc: The amount of stake in your validator | ||
input_box: Integer | ||
optional: false | ||
- name: days | ||
desc: "The number of days to calculate rewards for (range : 1-365)" | ||
input_box: Integer | ||
optional: false | ||
- name: fee | ||
help: Return the estimated transaction fee on the network | ||
result_template: | | ||
Sending {{.amount}} will cost {{.fee}} with current fee percentage. | ||
args: | ||
- name: amount | ||
desc: The amount of PAC coins to calculate fee for | ||
input_box: Integer | ||
optional: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package calculator | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/pagu-project/pagu/internal/testsuite" | ||
"github.com/pagu-project/pagu/pkg/client" | ||
"go.uber.org/mock/gomock" | ||
) | ||
|
||
type testData struct { | ||
*testsuite.TestSuite | ||
|
||
calculatorCmd *CalculatorCmd | ||
mockClientMgr *client.MockIManager | ||
} | ||
|
||
func setup(t *testing.T) *testData { | ||
t.Helper() | ||
|
||
ts := testsuite.NewTestSuite(t) | ||
ctrl := gomock.NewController(t) | ||
|
||
mockClientMgr := client.NewMockIManager(ctrl) | ||
|
||
calculatorCmd := NewCalculatorCmd(mockClientMgr) | ||
calculatorCmd.buildCalculatorCommand() | ||
|
||
return &testData{ | ||
TestSuite: ts, | ||
calculatorCmd: calculatorCmd, | ||
mockClientMgr: mockClientMgr, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,27 @@ | ||
package calculator | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/pagu-project/pagu/internal/engine/command" | ||
"github.com/pagu-project/pagu/internal/entity" | ||
"github.com/pagu-project/pagu/pkg/amount" | ||
) | ||
|
||
func (c *CalculatorCmd) calcFeeHandler( | ||
func (c *CalculatorCmd) feeHandler( | ||
_ *entity.User, | ||
cmd *command.Command, | ||
args map[string]string, | ||
) command.CommandResult { | ||
amt, err := amount.FromString(args["amount"]) | ||
if err != nil { | ||
return cmd.ErrorResult(errors.New("invalid amount param")) | ||
return cmd.RenderFailedTemplate("Invalid amount param") | ||
} | ||
|
||
fee, err := c.clientMgr.GetFee(amt.ToNanoPAC()) | ||
if err != nil { | ||
return cmd.ErrorResult(err) | ||
return cmd.RenderErrorTemplate(err) | ||
} | ||
|
||
feeAmount := amount.Amount(fee) | ||
|
||
return cmd.SuccessfulResultF("Sending %s will cost %s with current fee percentage."+ | ||
"\n> Note: Consider unbond and sortition transaction fee is 0 PAC always.", amt.String(), feeAmount.String()) | ||
return cmd.RenderResultTemplate("amount", amt, "fee", feeAmount) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package calculator | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/pagu-project/pagu/pkg/amount" | ||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/mock/gomock" | ||
) | ||
|
||
func TestFeeHandler(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
td := setup(t) | ||
cmd := td.calculatorCmd.subCmdFee | ||
|
||
t.Run("Invalid Amount Param", func(t *testing.T) { | ||
args := map[string]string{ | ||
"amount": "invalid", | ||
} | ||
|
||
result := td.calculatorCmd.feeHandler(nil, cmd, args) | ||
assert.False(t, result.Successful) | ||
assert.Contains(t, result.Message, "Invalid amount param") | ||
}) | ||
|
||
t.Run("Error from GetFee", func(t *testing.T) { | ||
args := map[string]string{ | ||
"amount": "10", | ||
} | ||
|
||
amt, _ := amount.FromString("10") | ||
td.mockClientMgr.EXPECT().GetFee(amt.ToNanoPAC()).Return(int64(0), errors.New("some error")) | ||
|
||
result := td.calculatorCmd.feeHandler(nil, cmd, args) | ||
assert.False(t, result.Successful) | ||
assert.Contains(t, result.Message, "some error") | ||
}) | ||
|
||
t.Run("Successful Fee Calculation", func(t *testing.T) { | ||
args := map[string]string{ | ||
"amount": "10", | ||
} | ||
|
||
amt, _ := amount.FromString("10") | ||
td.mockClientMgr.EXPECT().GetFee(amt.ToNanoPAC()).Return(int64(100), nil) | ||
|
||
result := td.calculatorCmd.feeHandler(nil, cmd, args) | ||
assert.True(t, result.Successful) | ||
assert.Contains(t, result.Message, | ||
fmt.Sprintf("Sending %v will cost %v with current fee percentage", amt, amount.Amount(int64(100)))) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.