Skip to content

Commit

Permalink
Merge pull request #206 from oasisprotocol/kostko/feature/rpcs-and-de…
Browse files Browse the repository at this point in the history
…noms
  • Loading branch information
kostko committed Mar 8, 2024
2 parents 50f6c7c + 80f628b commit 5677cc0
Show file tree
Hide file tree
Showing 33 changed files with 309 additions and 90 deletions.
5 changes: 1 addition & 4 deletions cmd/account/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
sdkSignature "github.com/oasisprotocol/oasis-sdk/client-sdk/go/crypto/signature"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/helpers"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/consensusaccounts"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"

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

// Prepare transaction.
Expand Down
4 changes: 1 addition & 3 deletions cmd/account/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ var depositCmd = &cobra.Command{
}

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

// Prepare transaction.
Expand Down
22 changes: 16 additions & 6 deletions cmd/account/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,23 @@ import (
)

var transferCmd = &cobra.Command{
Use: "transfer <amount> <to>",
Use: "transfer <amount> [<denom>] <to>",
Short: "Transfer given amount of tokens",
Aliases: []string{"t"},
Args: cobra.ExactArgs(2),
Args: cobra.RangeArgs(2, 3),
Run: func(cmd *cobra.Command, args []string) {
cfg := cliConfig.Global()
npa := common.GetNPASelection(cfg)
txCfg := common.GetTransactionConfig()
amount, to := args[0], args[1]
var amount, denom, to string
switch len(args) {
case 2:
amount, to = args[0], args[1]
case 3:
amount, denom, to = args[0], args[1], args[2]
default:
cobra.CheckErr("unexpected number of arguments") // Should never happen.
}

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

if denom != "" {
cobra.CheckErr("consensus layer only supports the native denomination")
}

// Consensus layer transfer.
amount, err := helpers.ParseConsensusDenomination(npa.Network, amount)
cobra.CheckErr(err)
Expand All @@ -71,9 +83,7 @@ var transferCmd = &cobra.Command{
cobra.CheckErr(err)
default:
// ParaTime transfer.
// TODO: This should actually query the ParaTime (or config) to check what the consensus
// layer denomination is in the ParaTime. Assume NATIVE for now.
amountBaseUnits, err := helpers.ParseParaTimeDenomination(npa.ParaTime, amount, types.NativeDenomination)
amountBaseUnits, err := helpers.ParseParaTimeDenomination(npa.ParaTime, amount, types.Denomination(denom))
cobra.CheckErr(err)

// Prepare transaction.
Expand Down
4 changes: 1 addition & 3 deletions cmd/account/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ var withdrawCmd = &cobra.Command{
common.CheckForceErr(common.CheckAddressNotReserved(cfg, addrToCheck))

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

// Prepare transaction.
Expand Down
16 changes: 16 additions & 0 deletions cmd/common/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
flag "github.com/spf13/pflag"

"github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"

cliConfig "github.com/oasisprotocol/cli/config"
)
Expand Down Expand Up @@ -96,6 +97,21 @@ func (npa *NPASelection) PrettyPrintNetwork() (out string) {
return
}

// ConsensusDenomination returns the denomination used to represent the consensus layer token.
func (npa *NPASelection) ConsensusDenomination() (denom types.Denomination) {
if npa.ParaTime == nil {
return types.NativeDenomination
}

switch cfgDenom := npa.ParaTime.ConsensusDenomination; cfgDenom {
case config.NativeDenominationKey:
denom = types.NativeDenomination
default:
denom = types.Denomination(cfgDenom)
}
return
}

func init() {
AccountFlag = flag.NewFlagSet("", flag.ContinueOnError)
AccountFlag.StringVar(&selectedAccount, "account", "", "explicitly set account to use")
Expand Down
16 changes: 11 additions & 5 deletions cmd/common/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var (
txNonce uint64
txGasLimit uint64
txGasPrice string
txFeeDenom string
txEncrypted bool
txYes bool
txUnsigned bool
Expand Down Expand Up @@ -116,6 +117,11 @@ func SignConsensusTransaction(
}
tx.Fee.Gas = consensusTx.Gas(txGasLimit)

// For sanity make sure no fee denomination has been specified.
if txFeeDenom != "" {
return nil, fmt.Errorf("consensus layer only supports the native denomination for paying fees")
}

gasPrice := quantity.NewQuantity()
if txGasPrice != "" {
var err error
Expand Down Expand Up @@ -215,11 +221,12 @@ func SignParaTimeTransaction(
tx.AuthInfo.Fee.Gas = txGasLimit
}

feeDenom := types.Denomination(txFeeDenom)

gasPrice := &types.BaseUnits{}
if txGasPrice != "" {
// TODO: Support different denominations for gas fees.
var err error
gasPrice, err = helpers.ParseParaTimeDenomination(npa.ParaTime, txGasPrice, types.NativeDenomination)
gasPrice, err = helpers.ParseParaTimeDenomination(npa.ParaTime, txGasPrice, feeDenom)
if err != nil {
return nil, nil, fmt.Errorf("bad gas price: %w", err)
}
Expand Down Expand Up @@ -262,9 +269,7 @@ func SignParaTimeTransaction(
return nil, nil, fmt.Errorf("failed to query minimum gas price: %w", err)
}

// TODO: Support different denominations for gas fees.
denom := types.NativeDenomination
*gasPrice = types.NewBaseUnits(mgp[denom], denom)
*gasPrice = types.NewBaseUnits(mgp[feeDenom], feeDenom)
}
}

Expand Down Expand Up @@ -575,6 +580,7 @@ func init() {
RuntimeTxFlags.Uint64Var(&txNonce, "nonce", invalidNonce, "override nonce to use")
RuntimeTxFlags.Uint64Var(&txGasLimit, "gas-limit", invalidGasLimit, "override gas limit to use (disable estimation)")
RuntimeTxFlags.StringVar(&txGasPrice, "gas-price", "", "override gas price to use")
RuntimeTxFlags.StringVar(&txFeeDenom, "fee-denom", "", "override fee denomination (defaults to native)")
RuntimeTxFlags.BoolVar(&txEncrypted, "encrypted", false, "encrypt transaction call data (requires online mode)")
RuntimeTxFlags.AddFlagSet(YesFlag)
RuntimeTxFlags.BoolVar(&txUnsigned, "unsigned", false, "do not sign transaction")
Expand Down
1 change: 1 addition & 0 deletions cmd/paratime/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ var addCmd = &cobra.Command{
Decimals: answers.Decimals,
},
}
pt.ConsensusDenomination = config.NativeDenominationKey // TODO: Make this configurable.

err = net.ParaTimes.Add(name, &pt)
cobra.CheckErr(err)
Expand Down
39 changes: 37 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,19 +192,54 @@ func (cfg *Config) migrateNetworks() bool {
var changes bool
for name, net := range cfg.Networks.All {
defaultCfg, knownDefault := Default.Networks.All[name]
oldChainContexts, knownOld := OldNetworks[name]
oldNetwork, knownOld := OldNetworks[name]
if !knownDefault || !knownOld {
continue
}

for _, oldChainContext := range oldChainContexts {
// Chain contexts.
for _, oldChainContext := range oldNetwork.ChainContexts {
if net.ChainContext == oldChainContext {
// If old chain context is known, replace with default.
net.ChainContext = defaultCfg.ChainContext
changes = true
break
}
}

// RPCs.
for _, oldRPC := range oldNetwork.RPCs {
if net.RPC == oldRPC {
// If old RPC is known, replace with default.
net.RPC = defaultCfg.RPC
changes = true
break
}
}

// Migrate consensus denominations for known paratimes.
for ptName, pt := range net.ParaTimes.All {
ptCfg, knownPt := defaultCfg.ParaTimes.All[ptName]
if !knownPt {
continue
}
if pt.ConsensusDenomination == ptCfg.ConsensusDenomination {
continue
}

pt.ConsensusDenomination = ptCfg.ConsensusDenomination
changes = true
}

// Add new paratimes.
for ptName, pt := range defaultCfg.ParaTimes.All {
if _, ok := net.ParaTimes.All[ptName]; ok {
continue
}

net.ParaTimes.All[ptName] = pt
changes = true
}
}

return changes
Expand Down
24 changes: 21 additions & 3 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,33 @@ var Default = Config{
Networks: config.DefaultNetworks,
}

// OldNetwork contains information about an old version of a known network configuration.
type OldNetwork struct {
// ChainContexts are the known chain contexts.
ChainContexts []string
// RPCs are the known RPC endpoint addresses.
RPCs []string
}

// OldNetworks contains information about old versions (e.g. chain contexts) of known networks so
// they can be automatically migrated.
var OldNetworks = map[string][]string{
var OldNetworks = map[string]*OldNetwork{
// Mainnet.
"mainnet": {
"b11b369e0da5bb230b220127f5e7b242d385ef8c6f54906243f30af63c815535", // oasis-3
ChainContexts: []string{
"b11b369e0da5bb230b220127f5e7b242d385ef8c6f54906243f30af63c815535", // oasis-3
},
RPCs: []string{
"grpc.oasis.dev:443", // Deprecated in 2024.
},
},
// Testnet.
"testnet": {
"50304f98ddb656620ea817cc1446c401752a05a249b36c9b90dba4616829977a", // 2022-03-03
ChainContexts: []string{
"50304f98ddb656620ea817cc1446c401752a05a249b36c9b90dba4616829977a", // 2022-03-03
},
RPCs: []string{
"testnet.grpc.oasis.dev:443", // Deprecated in 2024.
},
},
}
4 changes: 2 additions & 2 deletions examples/account/config/cli.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ default = 'testnet'
[networks.mainnet]
chain_context = 'b11b369e0da5bb230b220127f5e7b242d385ef8c6f54906243f30af63c815535'
description = ''
rpc = 'grpc.oasis.dev:443'
rpc = 'grpc.oasis.io:443'

[networks.mainnet.denomination]
decimals = 9
Expand Down Expand Up @@ -43,7 +43,7 @@ symbol = 'ROSE'
[networks.testnet]
chain_context = '50304f98ddb656620ea817cc1446c401752a05a249b36c9b90dba4616829977a'
description = ''
rpc = 'testnet.grpc.oasis.dev:443'
rpc = 'testnet.grpc.oasis.io:443'

[networks.testnet.denomination]
decimals = 9
Expand Down
4 changes: 2 additions & 2 deletions examples/addressbook/config/cli.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ default = 'mainnet'
[networks.mainnet]
chain_context = 'b11b369e0da5bb230b220127f5e7b242d385ef8c6f54906243f30af63c815535'
description = ''
rpc = 'grpc.oasis.dev:443'
rpc = 'grpc.oasis.io:443'

[networks.mainnet.denomination]
decimals = 9
Expand Down Expand Up @@ -43,7 +43,7 @@ symbol = 'ROSE'
[networks.testnet]
chain_context = '50304f98ddb656620ea817cc1446c401752a05a249b36c9b90dba4616829977a'
description = ''
rpc = 'testnet.grpc.oasis.dev:443'
rpc = 'testnet.grpc.oasis.io:443'

[networks.testnet.denomination]
decimals = 9
Expand Down
Loading

0 comments on commit 5677cc0

Please sign in to comment.