Skip to content

Commit

Permalink
fee suggestion for construction/metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoff Lee committed Dec 14, 2021
1 parent c7e2305 commit 96a7311
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 14 deletions.
20 changes: 14 additions & 6 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ type RosettaConfig struct {

// Offline defines if the server must be run in offline mode
Offline bool `mapstructure:"offline"`

// SuggestGas defines gas limit when calculate fee
SuggestGas int `mapstructure:"suggest-gas"`

// DefaultSuggestDenom defines the defult denom for fee suggestion
DefaultSuggestDenom string `mapstructure:"default-suggest-denom"`
}

// GRPCConfig defines configuration for the gRPC server.
Expand Down Expand Up @@ -292,12 +298,14 @@ func GetConfig(v *viper.Viper) Config {
EnableUnsafeCORS: v.GetBool("api.enabled-unsafe-cors"),
},
Rosetta: RosettaConfig{
Enable: v.GetBool("rosetta.enable"),
Address: v.GetString("rosetta.address"),
Blockchain: v.GetString("rosetta.blockchain"),
Network: v.GetString("rosetta.network"),
Retries: v.GetInt("rosetta.retries"),
Offline: v.GetBool("rosetta.offline"),
Enable: v.GetBool("rosetta.enable"),
Address: v.GetString("rosetta.address"),
Blockchain: v.GetString("rosetta.blockchain"),
Network: v.GetString("rosetta.network"),
Retries: v.GetInt("rosetta.retries"),
Offline: v.GetBool("rosetta.offline"),
SuggestGas: v.GetInt("rosetta.suggest-gas"),
DefaultSuggestDenom: v.GetString("rosetta.default-suggest-denom"),
},
GRPC: GRPCConfig{
Enable: v.GetBool("grpc.enable"),
Expand Down
19 changes: 19 additions & 0 deletions server/rosetta/client_online.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,25 @@ func (c *Client) ConstructionMetadataFromOptions(ctx context.Context, options ma
return nil, err
}

if constructionOptions.GasLimit <= 0 {
// set to default
constructionOptions.GasLimit = uint64(c.config.SuggestGas)
}
if constructionOptions.GasPrice == "" {
// set to default
denom := c.config.DefaultSuggestDenom
constructionOptions.GasPrice = c.config.SuggestPrices.AmountOf(denom).String() + denom
} else {
gasPrice, err := sdk.ParseDecCoin(constructionOptions.GasPrice)
if err != nil {
return nil, err
}
// check gasPrice is in the list
if !c.config.SuggestPrices.AmountOf(gasPrice.Denom).IsPositive() {
return nil, crgerrs.ErrBadArgument
}
}

signersData := make([]*SignerData, len(constructionOptions.ExpectedSigners))

for i, signer := range constructionOptions.ExpectedSigners {
Expand Down
22 changes: 22 additions & 0 deletions server/rosetta/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// configuration defaults constants
Expand All @@ -30,6 +31,8 @@ const (
DefaultNetwork = "network"
// DefaultOffline defines the default offline value
DefaultOffline = false
// DefaultSuggestGas defines the default gas limit for fee suggestion
DefaultSuggestGas = 20_000
)

// configuration flags
Expand Down Expand Up @@ -65,6 +68,12 @@ type Config struct {
Retries int
// Offline defines if the server must be run in offline mode
Offline bool
// SuggestGas defines the gas limit for fee suggestion
SuggestGas int
// DefaultSuggestDenom defines the default denom for fee suggestion
DefaultSuggestDenom string
// SuggestPrices defines the gas prices for fee suggestion
SuggestPrices sdk.DecCoins
// Codec overrides the default data and construction api client codecs
Codec *codec.ProtoCodec
// InterfaceRegistry overrides the default data and construction api interface registry
Expand Down Expand Up @@ -102,6 +111,19 @@ func (c *Config) validate() error {
if c.Offline {
return fmt.Errorf("offline mode is not supported for stargate implementation due to how sigv2 works")
}
if c.SuggestGas <= 0 {
c.SuggestGas = DefaultSuggestGas
}
found := false
for i := 0; i < c.SuggestPrices.Len(); i++ {
if c.SuggestPrices.GetDenomByIndex(i) == c.DefaultSuggestDenom {
found = true
break
}
}
if !found {
return fmt.Errorf("default suggest denom is not found in minimum-gas-prices")
}

// these are optional but it must be online
if c.GRPCEndpoint == "" {
Expand Down
19 changes: 18 additions & 1 deletion server/rosetta/lib/internal/service/construction.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/coinbase/rosetta-sdk-go/types"

"github.com/cosmos/cosmos-sdk/server/rosetta/lib/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// ConstructionCombine Combine creates a network-specific transaction from an unsigned transaction
Expand Down Expand Up @@ -69,8 +70,24 @@ func (on OnlineNetwork) ConstructionMetadata(ctx context.Context, request *types
return nil, errors.ToRosetta(err)
}

price, err := sdk.ParseDecCoin(metadata["gas_price"].(string))
if err != nil {
return nil, errors.ToRosetta(err)
}
gas := sdk.NewIntFromUint64(uint64(metadata["gas_limit"].(float64)))

suggestedFee := types.Amount{
Value: price.Amount.MulInt64(gas.Int64()).String(),
Currency: &(types.Currency{
Symbol: price.Denom,
Decimals: 0,
}),
/*metadata*/
}

return &types.ConstructionMetadataResponse{
Metadata: metadata,
Metadata: metadata,
SuggestedFee: []*types.Amount{&suggestedFee},
}, nil
}

Expand Down
24 changes: 17 additions & 7 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
crgserver "github.com/cosmos/cosmos-sdk/server/rosetta/lib/server"
"github.com/cosmos/cosmos-sdk/server/types"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdktypes "github.com/cosmos/cosmos-sdk/types"
)

// Tendermint full-node start flags
Expand Down Expand Up @@ -340,14 +341,23 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App
offlineMode = true
}

minGasPrices, err := sdktypes.ParseDecCoins(config.MinGasPrices)
if err != nil {
ctx.Logger.Error("failed to parse minimum-gas-prices: ", err)
return err
}

conf := &rosetta.Config{
Blockchain: config.Rosetta.Blockchain,
Network: config.Rosetta.Network,
TendermintRPC: ctx.Config.RPC.ListenAddress,
GRPCEndpoint: config.GRPC.Address,
Addr: config.Rosetta.Address,
Retries: config.Rosetta.Retries,
Offline: offlineMode,
Blockchain: config.Rosetta.Blockchain,
Network: config.Rosetta.Network,
TendermintRPC: ctx.Config.RPC.ListenAddress,
GRPCEndpoint: config.GRPC.Address,
Addr: config.Rosetta.Address,
Retries: config.Rosetta.Retries,
Offline: offlineMode,
SuggestGas: config.Rosetta.SuggestGas,
DefaultSuggestDenom: config.Rosetta.DefaultSuggestDenom,
SuggestPrices: minGasPrices.Sort(),
}
conf.WithCodec(clientCtx.InterfaceRegistry, clientCtx.Codec.(*codec.ProtoCodec))

Expand Down

0 comments on commit 96a7311

Please sign in to comment.