Skip to content

Commit

Permalink
test: add rand amount in pac for transaction tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amirvalhalla committed Mar 12, 2024
1 parent 5bb0de0 commit 6bcdfaf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
4 changes: 4 additions & 0 deletions util/testsuite/testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
46 changes: 25 additions & 21 deletions www/grpc/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package grpc

import (
"context"
"fmt"
"strconv"
"testing"

"github.com/pactus-project/pactus/types/tx"
Expand Down Expand Up @@ -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()
Expand All @@ -140,35 +144,35 @@ 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)
assert.NotEmpty(t, res.RawTransaction)

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),
})
Expand All @@ -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())
})
Expand All @@ -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),
})

Expand All @@ -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())
})
Expand All @@ -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")
Expand Down

0 comments on commit 6bcdfaf

Please sign in to comment.