Skip to content

Commit dc1704f

Browse files
committed
cmd: Add subtract-fee flag
Used by: - oasis account transfer --paratime xyz - oasis account trnasfer --no-paratime - oasis account withdraw
1 parent 6329841 commit dc1704f

18 files changed

+227
-135
lines changed

cmd/account/account.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,27 @@ package account
22

33
import (
44
"github.com/spf13/cobra"
5+
flag "github.com/spf13/pflag"
56

67
"github.com/oasisprotocol/cli/cmd/account/show"
78
)
89

9-
var Cmd = &cobra.Command{
10-
Use: "account",
11-
Short: "Account operations",
12-
Aliases: []string{"a", "acc", "accounts"},
13-
}
10+
// SubtractFeeFlags is a force mode switch.
11+
var (
12+
subtractFee bool
13+
SubtractFeeFlags *flag.FlagSet
14+
15+
Cmd = &cobra.Command{
16+
Use: "account",
17+
Short: "Account operations",
18+
Aliases: []string{"a", "acc", "accounts"},
19+
}
20+
)
1421

1522
func init() {
23+
SubtractFeeFlags = flag.NewFlagSet("", flag.ContinueOnError)
24+
SubtractFeeFlags.BoolVar(&subtractFee, "subtract-fee", false, "subtract fee from the amount")
25+
1626
Cmd.AddCommand(allowCmd)
1727
Cmd.AddCommand(amendCommissionScheduleCmd)
1828
Cmd.AddCommand(burnCmd)

cmd/account/transfer.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ var transferCmd = &cobra.Command{
6060
var sigTx, meta interface{}
6161
switch npa.ParaTime {
6262
case nil:
63+
// Consensus layer transfer.
6364
common.CheckForceErr(common.CheckAddressIsConsensusCapable(cfg, toAddr.String()))
6465
if toEthAddr != nil {
6566
common.CheckForceErr(common.CheckAddressIsConsensusCapable(cfg, toEthAddr.Hex()))
@@ -69,16 +70,23 @@ var transferCmd = &cobra.Command{
6970
cobra.CheckErr("consensus layer only supports the native denomination")
7071
}
7172

72-
// Consensus layer transfer.
73-
amount, err := helpers.ParseConsensusDenomination(npa.Network, amount)
73+
amt, err := helpers.ParseConsensusDenomination(npa.Network, amount)
7474
cobra.CheckErr(err)
7575

7676
// Prepare transaction.
77-
tx := staking.NewTransferTx(0, nil, &staking.Transfer{
77+
innerTx := staking.Transfer{
7878
To: toAddr.ConsensusAddress(),
79-
Amount: *amount,
80-
})
81-
79+
Amount: *amt,
80+
}
81+
tx := staking.NewTransferTx(0, nil, &innerTx)
82+
if subtractFee {
83+
_, fee, err := common.PrepareConsensusTransaction(ctx, npa, acc.ConsensusSigner(), conn, tx)
84+
cobra.CheckErr(err)
85+
err = amt.Sub(fee)
86+
cobra.CheckErr(err)
87+
innerTx.Amount = *amt
88+
tx = staking.NewTransferTx(0, nil, &innerTx)
89+
}
8290
sigTx, err = common.SignConsensusTransaction(ctx, npa, acc, conn, tx)
8391
cobra.CheckErr(err)
8492
default:
@@ -87,11 +95,19 @@ var transferCmd = &cobra.Command{
8795
cobra.CheckErr(err)
8896

8997
// Prepare transaction.
90-
tx := accounts.NewTransferTx(nil, &accounts.Transfer{
98+
innerTx := accounts.Transfer{
9199
To: *toAddr,
92100
Amount: *amountBaseUnits,
93-
})
94-
101+
}
102+
tx := accounts.NewTransferTx(nil, &innerTx)
103+
if subtractFee {
104+
_, fee, _, err := common.PrepareParatimeTransaction(ctx, npa, acc, conn, tx)
105+
cobra.CheckErr(err)
106+
err = amountBaseUnits.Amount.Sub(fee)
107+
cobra.CheckErr(err)
108+
innerTx.Amount = *amountBaseUnits
109+
tx = accounts.NewTransferTx(nil, &innerTx)
110+
}
95111
txDetails := sdkSignature.TxDetails{OrigTo: toEthAddr}
96112
sigTx, meta, err = common.SignParaTimeTransaction(ctx, npa, acc, conn, tx, &txDetails)
97113
cobra.CheckErr(err)
@@ -102,6 +118,7 @@ var transferCmd = &cobra.Command{
102118
}
103119

104120
func init() {
121+
transferCmd.Flags().AddFlagSet(SubtractFeeFlags)
105122
transferCmd.Flags().AddFlagSet(common.SelectorFlags)
106123
transferCmd.Flags().AddFlagSet(common.RuntimeTxFlags)
107124
transferCmd.Flags().AddFlagSet(common.ForceFlag)

cmd/account/withdraw.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,21 @@ var withdrawCmd = &cobra.Command{
7575
cobra.CheckErr(err)
7676

7777
// Prepare transaction.
78-
tx := consensusaccounts.NewWithdrawTx(nil, &consensusaccounts.Withdraw{
78+
innerTx := consensusaccounts.Withdraw{
7979
To: toAddr,
8080
Amount: *amountBaseUnits,
81-
})
81+
}
82+
tx := consensusaccounts.NewWithdrawTx(nil, &innerTx)
8283

8384
acc := common.LoadAccount(cfg, npa.AccountName)
85+
if subtractFee {
86+
_, fee, _, err := common.PrepareParatimeTransaction(ctx, npa, acc, conn, tx)
87+
cobra.CheckErr(err)
88+
err = amountBaseUnits.Amount.Sub(fee)
89+
cobra.CheckErr(err)
90+
innerTx.Amount = *amountBaseUnits
91+
tx = consensusaccounts.NewWithdrawTx(nil, &innerTx)
92+
}
8493
sigTx, meta, err := common.SignParaTimeTransaction(ctx, npa, acc, conn, tx, nil)
8594
cobra.CheckErr(err)
8695

@@ -125,6 +134,7 @@ var withdrawCmd = &cobra.Command{
125134
}
126135

127136
func init() {
137+
withdrawCmd.Flags().AddFlagSet(SubtractFeeFlags)
128138
withdrawCmd.Flags().AddFlagSet(common.SelectorFlags)
129139
withdrawCmd.Flags().AddFlagSet(common.RuntimeTxFlags)
130140
withdrawCmd.Flags().AddFlagSet(common.ForceFlag)

0 commit comments

Comments
 (0)