Skip to content

Commit 91ad28a

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 91ad28a

File tree

9 files changed

+195
-124
lines changed

9 files changed

+195
-124
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/allow.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ var allowCmd = &cobra.Command{
6565
}
6666

6767
func init() {
68+
allowCmd.Flags().AddFlagSet(SubtractFeeFlags)
6869
allowCmd.Flags().AddFlagSet(common.SelectorNAFlags)
6970
allowCmd.Flags().AddFlagSet(common.TxFlags)
7071
}

cmd/account/burn.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ var burnCmd = &cobra.Command{
5555
}
5656

5757
func init() {
58+
burnCmd.Flags().AddFlagSet(SubtractFeeFlags)
5859
burnCmd.Flags().AddFlagSet(common.SelectorNAFlags)
5960
burnCmd.Flags().AddFlagSet(common.TxFlags)
6061
}

cmd/account/delegate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ var delegateCmd = &cobra.Command{
124124
}
125125

126126
func init() {
127+
delegateCmd.Flags().AddFlagSet(SubtractFeeFlags)
127128
delegateCmd.Flags().AddFlagSet(common.SelectorFlags)
128129
delegateCmd.Flags().AddFlagSet(common.RuntimeTxFlags)
129130
}

cmd/account/deposit.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ var depositCmd = &cobra.Command{
117117
}
118118

119119
func init() {
120+
depositCmd.Flags().AddFlagSet(SubtractFeeFlags)
120121
depositCmd.Flags().AddFlagSet(common.SelectorFlags)
121122
depositCmd.Flags().AddFlagSet(common.RuntimeTxFlags)
122123
depositCmd.Flags().AddFlagSet(common.ForceFlag)

cmd/account/show/delegations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ func prettyPrintAccountBalanceAndDelegationsFrom(
168168
}
169169
_ = totalAmount.Add(&totalDebDelegationsAmount)
170170

171+
txt, _ := generalAccount.Balance.MarshalText()
172+
fmt.Printf("%s\n", txt)
171173
fmt.Fprintf(w, "%sTotal: ", prefix)
172174
fmt.Fprintf(w, "%s\n", helpers.FormatConsensusDenomination(network, *totalAmount))
173175
fmt.Fprintf(w, "%sAvailable: ", prefix)

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)