Skip to content

Commit f96aada

Browse files
authored
Merge pull request #257 from oasisprotocol/matevz/feature/subtract-fee
cmd: Add subtract-fee flag
2 parents 6329841 + 2c49a09 commit f96aada

19 files changed

+245
-139
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+
var (
11+
subtractFee bool
12+
// SubtractFeeFlags enables makes the provided amount also contain any transaction fees.
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: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/spf13/cobra"
77

8+
"github.com/oasisprotocol/oasis-core/go/common/quantity"
89
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
910
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection"
1011
sdkSignature "github.com/oasisprotocol/oasis-sdk/client-sdk/go/crypto/signature"
@@ -60,6 +61,7 @@ var transferCmd = &cobra.Command{
6061
var sigTx, meta interface{}
6162
switch npa.ParaTime {
6263
case nil:
64+
// Consensus layer transfer.
6365
common.CheckForceErr(common.CheckAddressIsConsensusCapable(cfg, toAddr.String()))
6466
if toEthAddr != nil {
6567
common.CheckForceErr(common.CheckAddressIsConsensusCapable(cfg, toEthAddr.Hex()))
@@ -69,29 +71,46 @@ var transferCmd = &cobra.Command{
6971
cobra.CheckErr("consensus layer only supports the native denomination")
7072
}
7173

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

7677
// Prepare transaction.
77-
tx := staking.NewTransferTx(0, nil, &staking.Transfer{
78+
innerTx := staking.Transfer{
7879
To: toAddr.ConsensusAddress(),
79-
Amount: *amount,
80-
})
81-
80+
Amount: *amt,
81+
}
82+
tx := staking.NewTransferTx(0, nil, &innerTx)
83+
if subtractFee {
84+
var fee *quantity.Quantity
85+
_, fee, err = common.PrepareConsensusTransaction(ctx, npa, acc.ConsensusSigner(), conn, tx)
86+
cobra.CheckErr(err)
87+
err = amt.Sub(fee)
88+
cobra.CheckErr(err)
89+
innerTx.Amount = *amt
90+
tx = staking.NewTransferTx(0, nil, &innerTx)
91+
}
8292
sigTx, err = common.SignConsensusTransaction(ctx, npa, acc, conn, tx)
8393
cobra.CheckErr(err)
8494
default:
8595
// ParaTime transfer.
86-
amountBaseUnits, err := helpers.ParseParaTimeDenomination(npa.ParaTime, amount, types.Denomination(denom))
96+
amtBaseUnits, err := helpers.ParseParaTimeDenomination(npa.ParaTime, amount, types.Denomination(denom))
8797
cobra.CheckErr(err)
8898

8999
// Prepare transaction.
90-
tx := accounts.NewTransferTx(nil, &accounts.Transfer{
100+
innerTx := accounts.Transfer{
91101
To: *toAddr,
92-
Amount: *amountBaseUnits,
93-
})
94-
102+
Amount: *amtBaseUnits,
103+
}
104+
tx := accounts.NewTransferTx(nil, &innerTx)
105+
if subtractFee {
106+
var fee *quantity.Quantity
107+
_, fee, _, err = common.PrepareParatimeTransaction(ctx, npa, acc, conn, tx)
108+
cobra.CheckErr(err)
109+
err = amtBaseUnits.Amount.Sub(fee)
110+
cobra.CheckErr(err)
111+
innerTx.Amount = *amtBaseUnits
112+
tx = accounts.NewTransferTx(nil, &innerTx)
113+
}
95114
txDetails := sdkSignature.TxDetails{OrigTo: toEthAddr}
96115
sigTx, meta, err = common.SignParaTimeTransaction(ctx, npa, acc, conn, tx, &txDetails)
97116
cobra.CheckErr(err)
@@ -102,6 +121,7 @@ var transferCmd = &cobra.Command{
102121
}
103122

104123
func init() {
124+
transferCmd.Flags().AddFlagSet(SubtractFeeFlags)
105125
transferCmd.Flags().AddFlagSet(common.SelectorFlags)
106126
transferCmd.Flags().AddFlagSet(common.RuntimeTxFlags)
107127
transferCmd.Flags().AddFlagSet(common.ForceFlag)

cmd/account/withdraw.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
ethCommon "github.com/ethereum/go-ethereum/common"
88
"github.com/spf13/cobra"
99

10+
"github.com/oasisprotocol/oasis-core/go/common/quantity"
1011
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/client"
1112
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection"
1213
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/helpers"
@@ -75,12 +76,22 @@ var withdrawCmd = &cobra.Command{
7576
cobra.CheckErr(err)
7677

7778
// Prepare transaction.
78-
tx := consensusaccounts.NewWithdrawTx(nil, &consensusaccounts.Withdraw{
79+
innerTx := consensusaccounts.Withdraw{
7980
To: toAddr,
8081
Amount: *amountBaseUnits,
81-
})
82+
}
83+
tx := consensusaccounts.NewWithdrawTx(nil, &innerTx)
8284

8385
acc := common.LoadAccount(cfg, npa.AccountName)
86+
if subtractFee {
87+
var fee *quantity.Quantity
88+
_, fee, _, err = common.PrepareParatimeTransaction(ctx, npa, acc, conn, tx)
89+
cobra.CheckErr(err)
90+
err = amountBaseUnits.Amount.Sub(fee)
91+
cobra.CheckErr(err)
92+
innerTx.Amount = *amountBaseUnits
93+
tx = consensusaccounts.NewWithdrawTx(nil, &innerTx)
94+
}
8495
sigTx, meta, err := common.SignParaTimeTransaction(ctx, npa, acc, conn, tx, nil)
8596
cobra.CheckErr(err)
8697

@@ -125,6 +136,7 @@ var withdrawCmd = &cobra.Command{
125136
}
126137

127138
func init() {
139+
withdrawCmd.Flags().AddFlagSet(SubtractFeeFlags)
128140
withdrawCmd.Flags().AddFlagSet(common.SelectorFlags)
129141
withdrawCmd.Flags().AddFlagSet(common.RuntimeTxFlags)
130142
withdrawCmd.Flags().AddFlagSet(common.ForceFlag)

cmd/common/selector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var (
2525
var (
2626
// AccountFlag corresponds to the --account selector flag.
2727
AccountFlag *flag.FlagSet
28-
// SelectorFlags contains the common selector flags for network/ParaTime/wallet.
28+
// SelectorFlags contains the common selector flags for network/ParaTime/account.
2929
SelectorFlags *flag.FlagSet
3030
// SelectorNPFlags contains the common selector flags for network/ParaTime.
3131
SelectorNPFlags *flag.FlagSet

0 commit comments

Comments
 (0)