Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: using PAC instead of atomic units for external input/outputs #1161

Merged
merged 15 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/wallet/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ func buildAllAddressesCmd(parentCmd *cobra.Command) {

if *balanceOpt {
balance, _ := wlt.Balance(info.Address)
line += fmt.Sprintf("%v\t", util.ChangeToCoin(balance))
line += fmt.Sprintf("%v\t", util.ConvertCoinUnitToCoin(balance))
}

if *stakeOpt {
stake, _ := wlt.Stake(info.Address)
line += fmt.Sprintf("%v\t", util.ChangeToCoin(stake))
line += fmt.Sprintf("%v\t", util.ConvertCoinUnitToCoin(stake))
}

line += info.Label
Expand Down Expand Up @@ -124,7 +124,7 @@ func buildBalanceCmd(parentCmd *cobra.Command) {
cmd.FatalErrorCheck(err)

cmd.PrintInfoMsgf("balance: %v\tstake: %v",
util.ChangeToCoin(balance), util.ChangeToCoin(stake))
util.ConvertCoinUnitToCoin(balance), util.ConvertCoinUnitToCoin(stake))
}
}

Expand Down
22 changes: 11 additions & 11 deletions cmd/wallet/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func buildTransferTxCmd(parentCmd *cobra.Command) {
cmd.FatalErrorCheck(err)

opts := []wallet.TxOption{
wallet.OptionFee(util.CoinToChange(*feeOpt)),
wallet.OptionFee(util.ConvertCoinToCoinUnit(*feeOpt)),
wallet.OptionLockTime(uint32(*lockTimeOpt)),
wallet.OptionMemo(*memoOpt),
}
Expand All @@ -56,8 +56,8 @@ func buildTransferTxCmd(parentCmd *cobra.Command) {
cmd.PrintInfoMsgf("You are going to sign this \033[1mTransfer\033[0m transition:")
cmd.PrintInfoMsgf("From : %s", from)
cmd.PrintInfoMsgf("To : %s", to)
cmd.PrintInfoMsgf("Amount: %.9f", util.ChangeToCoin(amount))
cmd.PrintInfoMsgf("Fee : %.9f", util.ChangeToCoin(trx.Fee()))
cmd.PrintInfoMsgf("Amount: %.9f", util.ConvertCoinUnitToCoin(amount))
cmd.PrintInfoMsgf("Fee : %.9f", util.ConvertCoinUnitToCoin(trx.Fee()))

signAndPublishTx(w, trx, *noConfirmOpt, *passOpt)
}
Expand Down Expand Up @@ -86,7 +86,7 @@ func buildBondTxCmd(parentCmd *cobra.Command) {
cmd.FatalErrorCheck(err)

opts := []wallet.TxOption{
wallet.OptionFee(util.CoinToChange(*feeOpt)),
wallet.OptionFee(util.ConvertCoinToCoinUnit(*feeOpt)),
wallet.OptionLockTime(uint32(*lockTime)),
wallet.OptionMemo(*memoOpt),
}
Expand All @@ -98,8 +98,8 @@ func buildBondTxCmd(parentCmd *cobra.Command) {
cmd.PrintInfoMsgf("You are going to sign this \033[1mBond\033[0m transition:")
cmd.PrintInfoMsgf("Account : %s", from)
cmd.PrintInfoMsgf("Validator: %s", to)
cmd.PrintInfoMsgf("Stake : %.9f", util.ChangeToCoin(amount))
cmd.PrintInfoMsgf("Fee : %.9f", util.ChangeToCoin(trx.Fee()))
cmd.PrintInfoMsgf("Stake : %.9f", util.ConvertCoinUnitToCoin(amount))
cmd.PrintInfoMsgf("Fee : %.9f", util.ConvertCoinUnitToCoin(trx.Fee()))

signAndPublishTx(w, trx, *noConfirmOpt, *passOpt)
}
Expand All @@ -124,7 +124,7 @@ func buildUnbondTxCmd(parentCmd *cobra.Command) {
cmd.FatalErrorCheck(err)

opts := []wallet.TxOption{
wallet.OptionFee(util.CoinToChange(*feeOpt)),
wallet.OptionFee(util.ConvertCoinToCoinUnit(*feeOpt)),
wallet.OptionLockTime(uint32(*lockTime)),
wallet.OptionMemo(*memoOpt),
}
Expand All @@ -135,7 +135,7 @@ func buildUnbondTxCmd(parentCmd *cobra.Command) {
cmd.PrintLine()
cmd.PrintInfoMsgf("You are going to sign this \033[1mUnbond\033[0m transition:")
cmd.PrintInfoMsgf("Validator: %s", from)
cmd.PrintInfoMsgf("Fee : %.9f", util.ChangeToCoin(trx.Fee()))
cmd.PrintInfoMsgf("Fee : %.9f", util.ConvertCoinUnitToCoin(trx.Fee()))

signAndPublishTx(w, trx, *noConfirmOpt, *passOpt)
}
Expand Down Expand Up @@ -163,7 +163,7 @@ func buildWithdrawTxCmd(parentCmd *cobra.Command) {
cmd.FatalErrorCheck(err)

opts := []wallet.TxOption{
wallet.OptionFee(util.CoinToChange(*feeOpt)),
wallet.OptionFee(util.ConvertCoinToCoinUnit(*feeOpt)),
wallet.OptionLockTime(uint32(*lockTime)),
wallet.OptionMemo(*memoOpt),
}
Expand All @@ -175,8 +175,8 @@ func buildWithdrawTxCmd(parentCmd *cobra.Command) {
cmd.PrintInfoMsgf("You are going to sign this \033[1mWithdraw\033[0m transition:")
cmd.PrintInfoMsgf("Validator: %s", from)
cmd.PrintInfoMsgf("Account : %s", to)
cmd.PrintInfoMsgf("Amount : %.9f", util.ChangeToCoin(amount))
cmd.PrintInfoMsgf("Fee : %.9f", util.ChangeToCoin(trx.Fee()))
cmd.PrintInfoMsgf("Amount : %.9f", util.ConvertCoinUnitToCoin(amount))
cmd.PrintInfoMsgf("Fee : %.9f", util.ConvertCoinUnitToCoin(trx.Fee()))

signAndPublishTx(w, trx, *noConfirmOpt, *passOpt)
}
Expand Down
26 changes: 13 additions & 13 deletions util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func OS2IP(os []byte) *big.Int {
return new(big.Int).SetBytes(os)
}

// I2OSP converts a nonnegative integer to an octet string of a specified length.
// IS2OP converts a nonnegative integer to an octet string of a specified length.
amirvalhalla marked this conversation as resolved.
Show resolved Hide resolved
// https://datatracker.ietf.org/doc/html/rfc8017#section-4.1
func IS2OP(x *big.Int, xLen int) []byte {
if x.Sign() == -1 {
Expand All @@ -124,18 +124,18 @@ func IS2OP(x *big.Int, xLen int) []byte {
return x.FillBytes(buf)
}

const changeFactor = float64(1000000000)
const coinUnit = float64(1000000000)

// CoinToChange converts a coin amount to its corresponding change value.
// Example: CoinToChange(2.75) returns 2750000000.
func CoinToChange(coin float64) int64 {
return int64(coin * changeFactor)
// ConvertCoinToCoinUnit converts a coin amount to its corresponding change value.
// Example: ConvertCoinToCoinUnit(2.75) returns 2750000000.
func ConvertCoinToCoinUnit(coin float64) int64 {
return int64(coin * coinUnit)
}

// ChangeToCoin converts a change value to its corresponding coin amount.
// Example: ChangeToCoin(2750000000) returns 2.75.
func ChangeToCoin(change int64) float64 {
return float64(change) / changeFactor
// ConvertCoinUnitToCoin converts a change value to its corresponding coin amount.
amirvalhalla marked this conversation as resolved.
Show resolved Hide resolved
// Example: ConvertCoinUnitToCoin(2750000000) returns 2.75.
func ConvertCoinUnitToCoin(change int64) float64 {
return float64(change) / coinUnit
}

// StringToChange converts a string representation of a coin amount to its corresponding change value.
Expand All @@ -147,22 +147,22 @@ func StringToChange(amount string) (int64, error) {
return 0, err
}

return CoinToChange(coin), nil
return ConvertCoinToCoinUnit(coin), nil
}

// ChangeToStringWithTrailingZeros converts a change value to its string representation with trailing zeros.
// The returned string will have up to 9 decimal places.
// Example: ChangeToStringWithTrailingZeros(2750000000) returns "2.750000000".
func ChangeToStringWithTrailingZeros(change int64) string {
coin := ChangeToCoin(change)
coin := ConvertCoinUnitToCoin(change)

return strconv.FormatFloat(coin, 'f', 9, 64)
}

// ChangeToString converts a change value to its string representation.
// Example: ChangeToString(2750000000) returns "2.75".
func ChangeToString(change int64) string {
coin := ChangeToCoin(change)
coin := ConvertCoinUnitToCoin(change)

return strconv.FormatFloat(coin, 'f', -1, 64)
}
Expand Down
2 changes: 1 addition & 1 deletion util/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func TestCoinToChangeConversion(t *testing.T) {
if test.parsErr == nil {
assert.NoError(t, err)
assert.Equal(t, change, test.change)
assert.Equal(t, ChangeToCoin(change), test.coin)
assert.Equal(t, ConvertCoinUnitToCoin(change), test.coin)
assert.Equal(t, ChangeToStringWithTrailingZeros(change), test.str1)
assert.Equal(t, ChangeToString(change), test.str2)
} else {
Expand Down
6 changes: 5 additions & 1 deletion wallet/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/pactus-project/pactus/crypto/hash"
"github.com/pactus-project/pactus/types/tx"
"github.com/pactus-project/pactus/types/tx/payload"
"github.com/pactus-project/pactus/util"
pactus "github.com/pactus-project/pactus/www/grpc/gen/go"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
Expand Down Expand Up @@ -102,7 +103,10 @@ func (c *grpcClient) getTransaction(id tx.ID) (*pactus.GetTransactionResponse, e

func (c *grpcClient) getFee(amount int64, payloadType payload.Type) (int64, error) {
res, err := c.transactionClient.CalculateFee(c.ctx,
&pactus.CalculateFeeRequest{Amount: amount, PayloadType: pactus.PayloadType(payloadType)})
&pactus.CalculateFeeRequest{
Amount: util.ConvertCoinUnitToCoin(amount),
PayloadType: pactus.PayloadType(payloadType),
})
if err != nil {
return 0, err
}
Expand Down
16 changes: 8 additions & 8 deletions wallet/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func TestSigningTx(t *testing.T) {
lockTime := td.RandHeight()

opts := []TxOption{
OptionFee(util.CoinToChange(10)),
OptionFee(util.ConvertCoinToCoinUnit(10)),
OptionLockTime(lockTime),
OptionMemo("test"),
}
Expand All @@ -279,14 +279,14 @@ func TestMakeTransferTx(t *testing.T) {

t.Run("set parameters manually", func(t *testing.T) {
opts := []TxOption{
OptionFee(util.CoinToChange(10)),
OptionFee(util.ConvertCoinToCoinUnit(10)),
OptionLockTime(lockTime),
OptionMemo("test"),
}

trx, err := td.wallet.MakeTransferTx(sender, receiver.String(), amount, opts...)
assert.NoError(t, err)
assert.Equal(t, trx.Fee(), util.CoinToChange(10))
assert.Equal(t, trx.Fee(), util.ConvertCoinToCoinUnit(10))
assert.Equal(t, trx.LockTime(), lockTime)
assert.Equal(t, trx.Memo(), "test")
})
Expand Down Expand Up @@ -332,15 +332,15 @@ func TestMakeBondTx(t *testing.T) {
t.Run("set parameters manually", func(t *testing.T) {
lockTime := td.RandHeight()
opts := []TxOption{
OptionFee(util.CoinToChange(10)),
OptionFee(util.ConvertCoinToCoinUnit(10)),
OptionLockTime(lockTime),
OptionMemo("test"),
}

trx, err := td.wallet.MakeBondTx(sender, receiver.Address().String(),
receiver.PublicKey().String(), amount, opts...)
assert.NoError(t, err)
assert.Equal(t, trx.Fee(), util.CoinToChange(10))
assert.Equal(t, trx.Fee(), util.ConvertCoinToCoinUnit(10))
assert.Equal(t, trx.LockTime(), lockTime)
assert.Equal(t, trx.Memo(), "test")
})
Expand Down Expand Up @@ -454,7 +454,7 @@ func TestMakeUnbondTx(t *testing.T) {
t.Run("set parameters manually", func(t *testing.T) {
lockTime := td.RandHeight()
opts := []TxOption{
OptionFee(util.CoinToChange(10)),
OptionFee(util.ConvertCoinToCoinUnit(10)),
OptionLockTime(lockTime),
OptionMemo("test"),
}
Expand Down Expand Up @@ -504,14 +504,14 @@ func TestMakeWithdrawTx(t *testing.T) {
t.Run("set parameters manually", func(t *testing.T) {
lockTime := td.RandHeight()
opts := []TxOption{
OptionFee(util.CoinToChange(10)),
OptionFee(util.ConvertCoinToCoinUnit(10)),
OptionLockTime(lockTime),
OptionMemo("test"),
}

trx, err := td.wallet.MakeWithdrawTx(sender, receiver, amount, opts...)
assert.NoError(t, err)
assert.Equal(t, trx.Fee(), util.CoinToChange(10)) // Fee for unbond transaction is zero
assert.Equal(t, trx.Fee(), util.ConvertCoinToCoinUnit(10)) // Fee for unbond transaction is zero
assert.Equal(t, trx.LockTime(), lockTime)
assert.Equal(t, trx.Memo(), "test")
})
Expand Down
Loading
Loading