diff --git a/util/testsuite/testsuite.go b/util/testsuite/testsuite.go index d566d0334..a94fb03d1 100644 --- a/util/testsuite/testsuite.go +++ b/util/testsuite/testsuite.go @@ -149,6 +149,10 @@ func (ts *TestSuite) RandAmount() int64 { return ts.RandInt64(100e9) } +func (ts *TestSuite) RandAmountInPAC() float64 { + return util.ChangeToCoin(ts.RandAmount()) +} + // RandBytes returns a slice of random bytes of the given length. func (ts *TestSuite) RandBytes(length int) []byte { buf := make([]byte, length) diff --git a/www/grpc/transaction_test.go b/www/grpc/transaction_test.go index d601b666c..c8ec4f1ea 100644 --- a/www/grpc/transaction_test.go +++ b/www/grpc/transaction_test.go @@ -2,6 +2,8 @@ package grpc import ( "context" + "fmt" + "strconv" "testing" "github.com/pactus-project/pactus/types/tx" @@ -122,14 +124,16 @@ func TestGetCalculateFee(t *testing.T) { td := setup(t, nil) conn, client := td.transactionClient(t) - amount := td.RandAmount() + amount := td.RandAmountInPAC() res, err := client.CalculateFee(context.Background(), &pactus.CalculateFeeRequest{ - Amount: util.ChangeToCoin(amount), + Amount: amount, PayloadType: pactus.PayloadType_TRANSFER_PAYLOAD, }) + + expected, _ := strconv.ParseFloat(fmt.Sprintf("%.9f", amount/10000), 64) assert.NoError(t, err) - assert.Equal(t, util.ChangeToCoin(amount/10000), res.Fee) + assert.LessOrEqual(t, res.Fee, expected) assert.Nil(t, conn.Close(), "Error closing connection") td.StopServer() @@ -140,13 +144,13 @@ func TestGetRawTransaction(t *testing.T) { conn, client := td.transactionClient(t) t.Run("Transfer", func(t *testing.T) { - amount := td.RandAmount() + amount := td.RandAmountInPAC() res, err := client.GetRawTransferTransaction(context.Background(), &pactus.GetRawTransferTransactionRequest{ Sender: td.RandAccAddress().String(), Receiver: td.RandAccAddress().String(), - Amount: util.ChangeToCoin(amount), + Amount: amount, Memo: td.RandString(32), }) assert.NoError(t, err) @@ -154,21 +158,21 @@ func TestGetRawTransaction(t *testing.T) { decodedTrx, _ := tx.FromBytes(res.RawTransaction) expectedLockTime := td.mockState.LastBlockHeight() - expectedFee := td.mockState.CalculateFee(amount, payload.TypeTransfer) + expectedFee := td.mockState.CalculateFee(util.CoinToChange(amount), payload.TypeTransfer) - assert.Equal(t, amount, decodedTrx.Payload().Value()) + assert.Equal(t, amount, util.ChangeToCoin(decodedTrx.Payload().Value())) assert.Equal(t, expectedLockTime, decodedTrx.LockTime()) assert.Equal(t, expectedFee, decodedTrx.Fee()) }) t.Run("Bond", func(t *testing.T) { - amount := td.RandAmount() + amount := td.RandAmountInPAC() res, err := client.GetRawBondTransaction(context.Background(), &pactus.GetRawBondTransactionRequest{ Sender: td.RandAccAddress().String(), Receiver: td.RandValAddress().String(), - Stake: amount, + Stake: util.CoinToChange(amount), PublicKey: "", Memo: td.RandString(32), }) @@ -177,9 +181,9 @@ func TestGetRawTransaction(t *testing.T) { decodedTrx, _ := tx.FromBytes(res.RawTransaction) expectedLockTime := td.mockState.LastBlockHeight() - expectedFee := td.mockState.CalculateFee(amount, payload.TypeBond) + expectedFee := td.mockState.CalculateFee(util.CoinToChange(amount), payload.TypeBond) - assert.Equal(t, amount, decodedTrx.Payload().Value()) + assert.Equal(t, amount, util.ChangeToCoin(decodedTrx.Payload().Value())) assert.Equal(t, expectedLockTime, decodedTrx.LockTime()) assert.Equal(t, expectedFee, decodedTrx.Fee()) }) @@ -202,13 +206,13 @@ func TestGetRawTransaction(t *testing.T) { }) t.Run("Withdraw", func(t *testing.T) { - amount := td.RandAmount() + amount := td.RandAmountInPAC() res, err := client.GetRawWithdrawTransaction(context.Background(), &pactus.GetRawWithdrawTransactionRequest{ ValidatorAddress: td.RandValAddress().String(), AccountAddress: td.RandAccAddress().String(), - Amount: util.ChangeToCoin(amount), + Amount: amount, Memo: td.RandString(32), }) @@ -217,9 +221,9 @@ func TestGetRawTransaction(t *testing.T) { decodedTrx, _ := tx.FromBytes(res.RawTransaction) expectedLockTime := td.mockState.LastBlockHeight() - expectedFee := td.mockState.CalculateFee(amount, payload.TypeWithdraw) + expectedFee := td.mockState.CalculateFee(util.CoinToChange(amount), payload.TypeWithdraw) - assert.Equal(t, amount, decodedTrx.Payload().Value()) + assert.Equal(t, amount, util.ChangeToCoin(decodedTrx.Payload().Value())) assert.Equal(t, expectedLockTime, decodedTrx.LockTime()) assert.Equal(t, expectedFee, decodedTrx.Fee()) }) @@ -233,27 +237,27 @@ func TestCalculateFee(t *testing.T) { conn, client := td.transactionClient(t) t.Run("Not fixed amount", func(t *testing.T) { - amount := td.RandAmount() + amount := td.RandAmountInPAC() res, err := client.CalculateFee(context.Background(), &pactus.CalculateFeeRequest{ - Amount: util.ChangeToCoin(amount), + Amount: amount, PayloadType: pactus.PayloadType_TRANSFER_PAYLOAD, FixedAmount: false, }) assert.NoError(t, err) - assert.Equal(t, res.Amount, util.ChangeToCoin(amount)) + assert.Equal(t, res.Amount, amount) }) t.Run("Fixed amount", func(t *testing.T) { - amount := td.RandAmount() + amount := td.RandAmountInPAC() res, err := client.CalculateFee(context.Background(), &pactus.CalculateFeeRequest{ - Amount: util.ChangeToCoin(amount), + Amount: amount, PayloadType: pactus.PayloadType_TRANSFER_PAYLOAD, FixedAmount: true, }) assert.NoError(t, err) - assert.LessOrEqual(t, util.CoinToChange(res.Amount+res.Fee), amount) + assert.LessOrEqual(t, res.Amount+res.Fee, amount) }) assert.Nil(t, conn.Close(), "Error closing connection")