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

cmd: Add subtract-fee flag #257

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 15 additions & 5 deletions cmd/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,27 @@ package account

import (
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"

"github.com/oasisprotocol/cli/cmd/account/show"
)

var Cmd = &cobra.Command{
Use: "account",
Short: "Account operations",
Aliases: []string{"a", "acc", "accounts"},
}
var (
subtractFee bool
// SubtractFeeFlags enables makes the provided amount also contain any transaction fees.
SubtractFeeFlags *flag.FlagSet

Cmd = &cobra.Command{
Use: "account",
Short: "Account operations",
Aliases: []string{"a", "acc", "accounts"},
}
)

func init() {
SubtractFeeFlags = flag.NewFlagSet("", flag.ContinueOnError)
SubtractFeeFlags.BoolVar(&subtractFee, "subtract-fee", false, "subtract fee from the amount")

Cmd.AddCommand(allowCmd)
Cmd.AddCommand(amendCommissionScheduleCmd)
Cmd.AddCommand(burnCmd)
Expand Down
42 changes: 31 additions & 11 deletions cmd/account/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/spf13/cobra"

"github.com/oasisprotocol/oasis-core/go/common/quantity"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection"
sdkSignature "github.com/oasisprotocol/oasis-sdk/client-sdk/go/crypto/signature"
Expand Down Expand Up @@ -60,6 +61,7 @@ var transferCmd = &cobra.Command{
var sigTx, meta interface{}
switch npa.ParaTime {
case nil:
// Consensus layer transfer.
common.CheckForceErr(common.CheckAddressIsConsensusCapable(cfg, toAddr.String()))
if toEthAddr != nil {
common.CheckForceErr(common.CheckAddressIsConsensusCapable(cfg, toEthAddr.Hex()))
Expand All @@ -69,29 +71,46 @@ var transferCmd = &cobra.Command{
cobra.CheckErr("consensus layer only supports the native denomination")
}

// Consensus layer transfer.
amount, err := helpers.ParseConsensusDenomination(npa.Network, amount)
amt, err := helpers.ParseConsensusDenomination(npa.Network, amount)
cobra.CheckErr(err)

// Prepare transaction.
tx := staking.NewTransferTx(0, nil, &staking.Transfer{
innerTx := staking.Transfer{
To: toAddr.ConsensusAddress(),
Amount: *amount,
})

Amount: *amt,
}
tx := staking.NewTransferTx(0, nil, &innerTx)
if subtractFee {
var fee *quantity.Quantity
_, fee, err = common.PrepareConsensusTransaction(ctx, npa, acc.ConsensusSigner(), conn, tx)
cobra.CheckErr(err)
err = amt.Sub(fee)
cobra.CheckErr(err)
innerTx.Amount = *amt
tx = staking.NewTransferTx(0, nil, &innerTx)
}
sigTx, err = common.SignConsensusTransaction(ctx, npa, acc, conn, tx)
cobra.CheckErr(err)
default:
// ParaTime transfer.
amountBaseUnits, err := helpers.ParseParaTimeDenomination(npa.ParaTime, amount, types.Denomination(denom))
amtBaseUnits, err := helpers.ParseParaTimeDenomination(npa.ParaTime, amount, types.Denomination(denom))
cobra.CheckErr(err)

// Prepare transaction.
tx := accounts.NewTransferTx(nil, &accounts.Transfer{
innerTx := accounts.Transfer{
To: *toAddr,
Amount: *amountBaseUnits,
})

Amount: *amtBaseUnits,
}
tx := accounts.NewTransferTx(nil, &innerTx)
if subtractFee {
var fee *quantity.Quantity
_, fee, _, err = common.PrepareParatimeTransaction(ctx, npa, acc, conn, tx)
cobra.CheckErr(err)
err = amtBaseUnits.Amount.Sub(fee)
cobra.CheckErr(err)
innerTx.Amount = *amtBaseUnits
tx = accounts.NewTransferTx(nil, &innerTx)
}
txDetails := sdkSignature.TxDetails{OrigTo: toEthAddr}
sigTx, meta, err = common.SignParaTimeTransaction(ctx, npa, acc, conn, tx, &txDetails)
cobra.CheckErr(err)
Expand All @@ -102,6 +121,7 @@ var transferCmd = &cobra.Command{
}

func init() {
transferCmd.Flags().AddFlagSet(SubtractFeeFlags)
transferCmd.Flags().AddFlagSet(common.SelectorFlags)
transferCmd.Flags().AddFlagSet(common.RuntimeTxFlags)
transferCmd.Flags().AddFlagSet(common.ForceFlag)
Expand Down
16 changes: 14 additions & 2 deletions cmd/account/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/spf13/cobra"

"github.com/oasisprotocol/oasis-core/go/common/quantity"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/client"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/helpers"
Expand Down Expand Up @@ -75,12 +76,22 @@ var withdrawCmd = &cobra.Command{
cobra.CheckErr(err)

// Prepare transaction.
tx := consensusaccounts.NewWithdrawTx(nil, &consensusaccounts.Withdraw{
innerTx := consensusaccounts.Withdraw{
To: toAddr,
Amount: *amountBaseUnits,
})
}
tx := consensusaccounts.NewWithdrawTx(nil, &innerTx)

acc := common.LoadAccount(cfg, npa.AccountName)
if subtractFee {
var fee *quantity.Quantity
_, fee, _, err = common.PrepareParatimeTransaction(ctx, npa, acc, conn, tx)
cobra.CheckErr(err)
err = amountBaseUnits.Amount.Sub(fee)
cobra.CheckErr(err)
innerTx.Amount = *amountBaseUnits
tx = consensusaccounts.NewWithdrawTx(nil, &innerTx)
}
sigTx, meta, err := common.SignParaTimeTransaction(ctx, npa, acc, conn, tx, nil)
cobra.CheckErr(err)

Expand Down Expand Up @@ -125,6 +136,7 @@ var withdrawCmd = &cobra.Command{
}

func init() {
withdrawCmd.Flags().AddFlagSet(SubtractFeeFlags)
withdrawCmd.Flags().AddFlagSet(common.SelectorFlags)
withdrawCmd.Flags().AddFlagSet(common.RuntimeTxFlags)
withdrawCmd.Flags().AddFlagSet(common.ForceFlag)
Expand Down
2 changes: 1 addition & 1 deletion cmd/common/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (
var (
// AccountFlag corresponds to the --account selector flag.
AccountFlag *flag.FlagSet
// SelectorFlags contains the common selector flags for network/ParaTime/wallet.
// SelectorFlags contains the common selector flags for network/ParaTime/account.
SelectorFlags *flag.FlagSet
// SelectorNPFlags contains the common selector flags for network/ParaTime.
SelectorNPFlags *flag.FlagSet
Expand Down
Loading
Loading