Skip to content

Commit 80f628b

Browse files
committed
cmd: Improve support for different denominations
1 parent bd37849 commit 80f628b

6 files changed

Lines changed: 46 additions & 21 deletions

File tree

cmd/account/delegate.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
sdkSignature "github.com/oasisprotocol/oasis-sdk/client-sdk/go/crypto/signature"
1313
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/helpers"
1414
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/consensusaccounts"
15-
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"
1615

1716
"github.com/oasisprotocol/cli/cmd/common"
1817
cliConfig "github.com/oasisprotocol/cli/config"
@@ -67,9 +66,7 @@ var delegateCmd = &cobra.Command{
6766
cobra.CheckErr(err)
6867
default:
6968
// ParaTime delegation.
70-
// TODO: This should actually query the ParaTime (or config) to check what the consensus
71-
// layer denomination is in the ParaTime. Assume NATIVE for now.
72-
amountBaseUnits, err := helpers.ParseParaTimeDenomination(npa.ParaTime, amount, types.NativeDenomination)
69+
amountBaseUnits, err := helpers.ParseParaTimeDenomination(npa.ParaTime, amount, npa.ConsensusDenomination())
7370
cobra.CheckErr(err)
7471

7572
// Prepare transaction.

cmd/account/deposit.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ var depositCmd = &cobra.Command{
6363
}
6464

6565
// Parse amount.
66-
// TODO: This should actually query the ParaTime (or config) to check what the consensus
67-
// layer denomination is in the ParaTime. Assume NATIVE for now.
68-
amountBaseUnits, err := helpers.ParseParaTimeDenomination(npa.ParaTime, amount, types.NativeDenomination)
66+
amountBaseUnits, err := helpers.ParseParaTimeDenomination(npa.ParaTime, amount, npa.ConsensusDenomination())
6967
cobra.CheckErr(err)
7068

7169
// Prepare transaction.

cmd/account/transfer.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,23 @@ import (
1717
)
1818

1919
var transferCmd = &cobra.Command{
20-
Use: "transfer <amount> <to>",
20+
Use: "transfer <amount> [<denom>] <to>",
2121
Short: "Transfer given amount of tokens",
2222
Aliases: []string{"t"},
23-
Args: cobra.ExactArgs(2),
23+
Args: cobra.RangeArgs(2, 3),
2424
Run: func(cmd *cobra.Command, args []string) {
2525
cfg := cliConfig.Global()
2626
npa := common.GetNPASelection(cfg)
2727
txCfg := common.GetTransactionConfig()
28-
amount, to := args[0], args[1]
28+
var amount, denom, to string
29+
switch len(args) {
30+
case 2:
31+
amount, to = args[0], args[1]
32+
case 3:
33+
amount, denom, to = args[0], args[1], args[2]
34+
default:
35+
cobra.CheckErr("unexpected number of arguments") // Should never happen.
36+
}
2937

3038
if npa.Account == nil {
3139
cobra.CheckErr("no accounts configured in your wallet")
@@ -57,6 +65,10 @@ var transferCmd = &cobra.Command{
5765
common.CheckForceErr(common.CheckAddressIsConsensusCapable(cfg, toEthAddr.Hex()))
5866
}
5967

68+
if denom != "" {
69+
cobra.CheckErr("consensus layer only supports the native denomination")
70+
}
71+
6072
// Consensus layer transfer.
6173
amount, err := helpers.ParseConsensusDenomination(npa.Network, amount)
6274
cobra.CheckErr(err)
@@ -71,9 +83,7 @@ var transferCmd = &cobra.Command{
7183
cobra.CheckErr(err)
7284
default:
7385
// ParaTime transfer.
74-
// TODO: This should actually query the ParaTime (or config) to check what the consensus
75-
// layer denomination is in the ParaTime. Assume NATIVE for now.
76-
amountBaseUnits, err := helpers.ParseParaTimeDenomination(npa.ParaTime, amount, types.NativeDenomination)
86+
amountBaseUnits, err := helpers.ParseParaTimeDenomination(npa.ParaTime, amount, types.Denomination(denom))
7787
cobra.CheckErr(err)
7888

7989
// Prepare transaction.

cmd/account/withdraw.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ var withdrawCmd = &cobra.Command{
7171
common.CheckForceErr(common.CheckAddressNotReserved(cfg, addrToCheck))
7272

7373
// Parse amount.
74-
// TODO: This should actually query the ParaTime (or config) to check what the consensus
75-
// layer denomination is in the ParaTime. Assume NATIVE for now.
76-
amountBaseUnits, err := helpers.ParseParaTimeDenomination(npa.ParaTime, amount, types.NativeDenomination)
74+
amountBaseUnits, err := helpers.ParseParaTimeDenomination(npa.ParaTime, amount, npa.ConsensusDenomination())
7775
cobra.CheckErr(err)
7876

7977
// Prepare transaction.

cmd/common/selector.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
flag "github.com/spf13/pflag"
88

99
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
10+
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"
1011

1112
cliConfig "github.com/oasisprotocol/cli/config"
1213
)
@@ -96,6 +97,21 @@ func (npa *NPASelection) PrettyPrintNetwork() (out string) {
9697
return
9798
}
9899

100+
// ConsensusDenomination returns the denomination used to represent the consensus layer token.
101+
func (npa *NPASelection) ConsensusDenomination() (denom types.Denomination) {
102+
if npa.ParaTime == nil {
103+
return types.NativeDenomination
104+
}
105+
106+
switch cfgDenom := npa.ParaTime.ConsensusDenomination; cfgDenom {
107+
case config.NativeDenominationKey:
108+
denom = types.NativeDenomination
109+
default:
110+
denom = types.Denomination(cfgDenom)
111+
}
112+
return
113+
}
114+
99115
func init() {
100116
AccountFlag = flag.NewFlagSet("", flag.ContinueOnError)
101117
AccountFlag.StringVar(&selectedAccount, "account", "", "explicitly set account to use")

cmd/common/transaction.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var (
3434
txNonce uint64
3535
txGasLimit uint64
3636
txGasPrice string
37+
txFeeDenom string
3738
txEncrypted bool
3839
txYes bool
3940
txUnsigned bool
@@ -116,6 +117,11 @@ func SignConsensusTransaction(
116117
}
117118
tx.Fee.Gas = consensusTx.Gas(txGasLimit)
118119

120+
// For sanity make sure no fee denomination has been specified.
121+
if txFeeDenom != "" {
122+
return nil, fmt.Errorf("consensus layer only supports the native denomination for paying fees")
123+
}
124+
119125
gasPrice := quantity.NewQuantity()
120126
if txGasPrice != "" {
121127
var err error
@@ -215,11 +221,12 @@ func SignParaTimeTransaction(
215221
tx.AuthInfo.Fee.Gas = txGasLimit
216222
}
217223

224+
feeDenom := types.Denomination(txFeeDenom)
225+
218226
gasPrice := &types.BaseUnits{}
219227
if txGasPrice != "" {
220-
// TODO: Support different denominations for gas fees.
221228
var err error
222-
gasPrice, err = helpers.ParseParaTimeDenomination(npa.ParaTime, txGasPrice, types.NativeDenomination)
229+
gasPrice, err = helpers.ParseParaTimeDenomination(npa.ParaTime, txGasPrice, feeDenom)
223230
if err != nil {
224231
return nil, nil, fmt.Errorf("bad gas price: %w", err)
225232
}
@@ -262,9 +269,7 @@ func SignParaTimeTransaction(
262269
return nil, nil, fmt.Errorf("failed to query minimum gas price: %w", err)
263270
}
264271

265-
// TODO: Support different denominations for gas fees.
266-
denom := types.NativeDenomination
267-
*gasPrice = types.NewBaseUnits(mgp[denom], denom)
272+
*gasPrice = types.NewBaseUnits(mgp[feeDenom], feeDenom)
268273
}
269274
}
270275

@@ -575,6 +580,7 @@ func init() {
575580
RuntimeTxFlags.Uint64Var(&txNonce, "nonce", invalidNonce, "override nonce to use")
576581
RuntimeTxFlags.Uint64Var(&txGasLimit, "gas-limit", invalidGasLimit, "override gas limit to use (disable estimation)")
577582
RuntimeTxFlags.StringVar(&txGasPrice, "gas-price", "", "override gas price to use")
583+
RuntimeTxFlags.StringVar(&txFeeDenom, "fee-denom", "", "override fee denomination (defaults to native)")
578584
RuntimeTxFlags.BoolVar(&txEncrypted, "encrypted", false, "encrypt transaction call data (requires online mode)")
579585
RuntimeTxFlags.AddFlagSet(YesFlag)
580586
RuntimeTxFlags.BoolVar(&txUnsigned, "unsigned", false, "do not sign transaction")

0 commit comments

Comments
 (0)