Skip to content

Commit

Permalink
fix: use default client.toml, overwrite with provided values (#5945)
Browse files Browse the repository at this point in the history
* use default client.toml, overwrite with prov vals

* 1 to 2 height for twap
  • Loading branch information
czarcas7ic authored Aug 1, 2023
1 parent 93086d7 commit 1735d8b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
64 changes: 62 additions & 2 deletions cmd/osmosisd/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"path/filepath"
"reflect"
"strconv"
"text/template"

Expand All @@ -17,6 +18,15 @@ import (
tmcli "github.com/tendermint/tendermint/libs/cli"
)

// Default constants
const (
chainID = ""
keyringBackend = "os"
output = "text"
node = "tcp://localhost:26657"
broadcastMode = "sync"
)

type OsmosisCustomClient struct {
ChainID string `mapstructure:"chain-id" json:"chain-id"`
KeyringBackend string `mapstructure:"keyring-backend" json:"keyring-backend"`
Expand All @@ -31,6 +41,31 @@ type OsmosisCustomClient struct {
HumanReadableDenomsOutput bool `mapstructure:"human-readable-denoms-output" json:"human-readable-denoms-output"`
}

// defaultClientConfig returns the reference to ClientConfig with default values.
func defaultClientConfig() *OsmosisCustomClient {
return &OsmosisCustomClient{ChainID: chainID, KeyringBackend: keyringBackend, Output: output, Node: node, BroadcastMode: broadcastMode}
}

func (c *OsmosisCustomClient) SetChainID(chainID string) {
c.ChainID = chainID
}

func (c *OsmosisCustomClient) SetKeyringBackend(keyringBackend string) {
c.KeyringBackend = keyringBackend
}

func (c *OsmosisCustomClient) SetOutput(output string) {
c.Output = output
}

func (c *OsmosisCustomClient) SetNode(node string) {
c.Node = node
}

func (c *OsmosisCustomClient) SetBroadcastMode(broadcastMode string) {
c.BroadcastMode = broadcastMode
}

// Override sdk ConfigCmd func
func ConfigCmd() *cobra.Command {
cmd := &cobra.Command{
Expand Down Expand Up @@ -165,17 +200,42 @@ fees = "{{ .Fees }}"
`

// writeConfigToFile parses defaultConfigTemplate, renders config using the template and writes it to
// configFilePath.
// configFilePath. If nil is provided as config, the default config is used.
func writeConfigToFile(configFilePath string, config *OsmosisCustomClient) error {
var buffer bytes.Buffer
defaultOsmosisCustomClient := defaultClientConfig()

tmpl := template.New("clientConfigFileTemplate")
configTemplate, err := tmpl.Parse(defaultConfigTemplate)
if err != nil {
return err
}

if err := configTemplate.Execute(&buffer, config); err != nil {
// Loop through the fields of the provided config and replace values in the default client
if config != nil {
configValue := reflect.ValueOf(config).Elem()
defaultValue := reflect.ValueOf(defaultOsmosisCustomClient).Elem()

for i := 0; i < configValue.NumField(); i++ {
configField := configValue.Field(i)
defaultField := defaultValue.Field(i)

// Check if the field is a pointer type
if configField.Kind() == reflect.Ptr {
// If it's a pointer type, check if it's nil
if !configField.IsNil() {
defaultField.Set(configField.Elem())
}
} else {
// For non-pointer types, check if the value is the zero value
if !reflect.DeepEqual(configField.Interface(), reflect.Zero(configField.Type()).Interface()) {
defaultField.Set(configField)
}
}
}
}

if err := configTemplate.Execute(&buffer, defaultOsmosisCustomClient); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/osmosisd/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func InitCmd(mbm module.BasicManager, defaultNodeHome string) *cobra.Command {

tmcfg.WriteConfigFile(filepath.Join(config.RootDir, "config", "config.toml"), config)

err = writeConfigToFile(filepath.Join(config.RootDir, "config", "client.toml"), &OsmosisCustomClient{})
err = writeConfigToFile(filepath.Join(config.RootDir, "config", "client.toml"), nil)
if err != nil {
return errors.Wrap(err, "Failed to write client.toml file")
}
Expand Down
7 changes: 6 additions & 1 deletion tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ import (
cltypes "github.com/osmosis-labs/osmosis/v17/x/concentrated-liquidity/types"
)

var (
// minDecTolerance minimum tolerance for sdk.Dec, given its precision of 18.
minDecTolerance = sdk.MustNewDecFromStr("0.000000000000000001")
)

// TODO: Find more scalable way to do this
func (s *IntegrationTestSuite) TestAllE2E() {
// There appears to be an E2E quirk that requires a sleep here
Expand Down Expand Up @@ -1074,7 +1079,7 @@ func (s *IntegrationTestSuite) CreateConcentratedLiquidityPoolVoting_And_TWAP()
s.T().Log("creating first position")
chainANode.CreateConcentratedPosition(address1, "[-120000]", "40000", fmt.Sprintf("10000000%s,20000000%s", concentratedPool.GetToken0(), concentratedPool.GetToken1()), 0, 0, concentratedPool.GetId())
timeAfterPositionCreationBeforeSwap := chainANode.QueryLatestBlockTime()
chainA.WaitForNumHeights(1)
chainA.WaitForNumHeights(2)
firstPositionTwapBOverA, err := chainANode.QueryGeometricTwapToNow(concentratedPool.GetId(), concentratedPool.GetToken0(), concentratedPool.GetToken1(), timeAfterPositionCreationBeforeSwap)
s.Require().NoError(err)
s.Require().Equal(sdk.MustNewDecFromStr("0.5"), firstPositionTwapBOverA)
Expand Down

0 comments on commit 1735d8b

Please sign in to comment.