Skip to content

Commit

Permalink
Merge pull request #7 from xpladev/my/refactor-x
Browse files Browse the repository at this point in the history
refactor: remove unused functions and add some comments
  • Loading branch information
Moonyongjung committed Sep 8, 2023
2 parents 218a4ad + 3cf51c7 commit 4107ffb
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 387 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Xpla.go is a Golang SDK for writing applications that interact with the Xpla blo

|xpla.go|XPLA|Note|
|:---:|:---:|:---:|
|v0.1.1|[v1.2.3](https://github.com/xpladev/xpla/tree/v1.2.3)||
|v0.1.1-v0.1.2|[v1.2.3](https://github.com/xpladev/xpla/tree/v1.2.3)||
|v0.1.0|[v1.2.2](https://github.com/xpladev/xpla/tree/v1.2.2)||


Expand Down
154 changes: 29 additions & 125 deletions client/xplaclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ func (xplac *xplaClient) WithOptions(
}

// List of core modules.
// If new modules are implemented, regist ModuleExternal structure with
// receiver method in externalCoreModule.
type externalCoreModule struct {
auth.AuthExternal
authz.AuthzExternal
Expand All @@ -114,7 +116,7 @@ type externalCoreModule struct {
wasm.WasmExternal
}

// Update xpla client if data in the client are changed.
// Update xpla client if data in the xplaClient are changed.
func (xplac *xplaClient) UpdateXplacInCoreModule() provider.XplaClient {
xplac.externalCoreModule = externalCoreModule{
auth.NewAuthExternal(xplac),
Expand Down Expand Up @@ -302,127 +304,29 @@ func (xplac *xplaClient) WithErr(err error) provider.XplaClient {
return xplac.UpdateXplacInCoreModule()
}

// Get chain ID
func (xplac *xplaClient) GetChainId() string {
return xplac.chainId
}

// Get private key
func (xplac *xplaClient) GetPrivateKey() key.PrivateKey {
return xplac.opts.PrivateKey
}

// Get encoding configuration
func (xplac *xplaClient) GetEncoding() paramsapp.EncodingConfig {
return xplac.encodingConfig
}

// Get xpla client context
func (xplac *xplaClient) GetContext() context.Context {
return xplac.context
}

// Get LCD URL
func (xplac *xplaClient) GetLcdURL() string {
return xplac.opts.LcdURL
}

// Get GRPC URL to query or broadcast tx
func (xplac *xplaClient) GetGrpcUrl() string {
return xplac.opts.GrpcURL
}

// Get GRPC client connector
func (xplac *xplaClient) GetGrpcClient() grpc1.ClientConn {
return xplac.grpc
}

// Get RPC URL of tendermint core
func (xplac *xplaClient) GetRpc() string {
return xplac.opts.RpcURL
}

// Get RPC URL for evm module
func (xplac *xplaClient) GetEvmRpc() string {
return xplac.opts.EvmRpcURL
}

// Get broadcast mode
func (xplac *xplaClient) GetBroadcastMode() string {
return xplac.opts.BroadcastMode
}

// Get account number
func (xplac *xplaClient) GetAccountNumber() string {
return xplac.opts.AccountNumber
}

// Get account sequence
func (xplac *xplaClient) GetSequence() string {
return xplac.opts.Sequence
}

// Get gas limit
func (xplac *xplaClient) GetGasLimit() string {
return xplac.opts.GasLimit
}

// Get Gas price
func (xplac *xplaClient) GetGasPrice() string {
return xplac.opts.GasPrice
}

// Get Gas adjustment
func (xplac *xplaClient) GetGasAdjustment() string {
return xplac.opts.GasAdjustment
}

// Get fee amount
func (xplac *xplaClient) GetFeeAmount() string {
return xplac.opts.FeeAmount
}

// Get transaction sign mode
func (xplac *xplaClient) GetSignMode() signing.SignMode {
return xplac.opts.SignMode
}

// Get fee granter
func (xplac *xplaClient) GetFeeGranter() sdk.AccAddress {
return xplac.opts.FeeGranter
}

// Get timeout block height
func (xplac *xplaClient) GetTimeoutHeight() string {
return xplac.opts.TimeoutHeight
}

// Get pagination
func (xplac *xplaClient) GetPagination() *query.PageRequest {
return core.PageRequest
}

// Get output document name
func (xplac *xplaClient) GetOutputDocument() string {
return xplac.opts.OutputDocument
}

// Get module name
func (xplac *xplaClient) GetModule() string {
return xplac.module
}

// Get message type of modules
func (xplac *xplaClient) GetMsgType() string {
return xplac.msgType
}

// Get message
func (xplac *xplaClient) GetMsg() interface{} {
return xplac.msg
}

// Get error
func (xplac *xplaClient) GetErr() error {
return xplac.err
}
// Get parameters of the xpla client
func (xplac *xplaClient) GetChainId() string { return xplac.chainId }
func (xplac *xplaClient) GetPrivateKey() key.PrivateKey { return xplac.opts.PrivateKey }
func (xplac *xplaClient) GetEncoding() paramsapp.EncodingConfig { return xplac.encodingConfig }
func (xplac *xplaClient) GetContext() context.Context { return xplac.context }
func (xplac *xplaClient) GetLcdURL() string { return xplac.opts.LcdURL }
func (xplac *xplaClient) GetGrpcUrl() string { return xplac.opts.GrpcURL }
func (xplac *xplaClient) GetGrpcClient() grpc1.ClientConn { return xplac.grpc }
func (xplac *xplaClient) GetRpc() string { return xplac.opts.RpcURL }
func (xplac *xplaClient) GetEvmRpc() string { return xplac.opts.EvmRpcURL }
func (xplac *xplaClient) GetBroadcastMode() string { return xplac.opts.BroadcastMode }
func (xplac *xplaClient) GetAccountNumber() string { return xplac.opts.AccountNumber }
func (xplac *xplaClient) GetSequence() string { return xplac.opts.Sequence }
func (xplac *xplaClient) GetGasLimit() string { return xplac.opts.GasLimit }
func (xplac *xplaClient) GetGasPrice() string { return xplac.opts.GasPrice }
func (xplac *xplaClient) GetGasAdjustment() string { return xplac.opts.GasAdjustment }
func (xplac *xplaClient) GetFeeAmount() string { return xplac.opts.FeeAmount }
func (xplac *xplaClient) GetSignMode() signing.SignMode { return xplac.opts.SignMode }
func (xplac *xplaClient) GetFeeGranter() sdk.AccAddress { return xplac.opts.FeeGranter }
func (xplac *xplaClient) GetTimeoutHeight() string { return xplac.opts.TimeoutHeight }
func (xplac *xplaClient) GetPagination() *query.PageRequest { return core.PageRequest }
func (xplac *xplaClient) GetOutputDocument() string { return xplac.opts.OutputDocument }
func (xplac *xplaClient) GetModule() string { return xplac.module }
func (xplac *xplaClient) GetMsgType() string { return xplac.msgType }
func (xplac *xplaClient) GetMsg() interface{} { return xplac.msg }
func (xplac *xplaClient) GetErr() error { return xplac.err }
47 changes: 24 additions & 23 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,32 @@ func init() {

// Set core controller only once as singleton, and get core controller.
func Controller() *coreController {
once.Do(
func() {
cc = NewCoreController(
auth.NewCoreModule(),
authz.NewCoreModule(),
bank.NewCoreModule(),
base.NewCoreModule(),
crisis.NewCoreModule(),
distribution.NewCoreModule(),
evidence.NewCoreModule(),
evm.NewCoreModule(),
feegrant.NewCoreModule(),
gov.NewCoreModule(),
ibc.NewCoreModule(),
mint.NewCoreModule(),
params.NewCoreModule(),
reward.NewCoreModule(),
slashing.NewCoreModule(),
staking.NewCoreModule(),
upgrade.NewCoreModule(),
wasm.NewCoreModule(),
)
})
once.Do(func() {
cc = NewCoreController(
auth.NewCoreModule(),
authz.NewCoreModule(),
bank.NewCoreModule(),
base.NewCoreModule(),
crisis.NewCoreModule(),
distribution.NewCoreModule(),
evidence.NewCoreModule(),
evm.NewCoreModule(),
feegrant.NewCoreModule(),
gov.NewCoreModule(),
ibc.NewCoreModule(),
mint.NewCoreModule(),
params.NewCoreModule(),
reward.NewCoreModule(),
slashing.NewCoreModule(),
staking.NewCoreModule(),
upgrade.NewCoreModule(),
wasm.NewCoreModule(),
)
})
return cc
}

// Register routing info of core modules in the hash map.
func NewCoreController(coreModules ...core.CoreModule) *coreController {
m := make(map[string]core.CoreModule)
for _, coreModule := range coreModules {
Expand All @@ -77,6 +77,7 @@ func NewCoreController(coreModules ...core.CoreModule) *coreController {
}
}

// Get info of each module by its name.
func (c coreController) Get(moduleName string) core.CoreModule {
return c.cores[moduleName]
}
7 changes: 7 additions & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ package core
import cmclient "github.com/cosmos/cosmos-sdk/client"

// The standard form for a module in the core package.
// Every modules are enrolled to the controller by using this interface.
type CoreModule interface {
// Name of a core module must not be duplicated previous names.
Name() string

// Routed transaction messages are built in the TxBuilder of Cosmos-SDK.
NewTxRouter(cmclient.TxBuilder, string, interface{}) (cmclient.TxBuilder, error)

// Route query requests by gRPC or HTTP.
// Queries are returned with string type regardless of communication protocol.
NewQueryRouter(QueryClient) (string, error)
}
40 changes: 39 additions & 1 deletion provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,35 @@ import (
)

// The standard form of XPLA client is interface type.
// XplaClient is endpoint in order to access xpla.go from external packages.
// If new modules are implemeted, external functions that are used to send tx or query state should be
// enrolled in XplaClient interface.
//
// e.g. - enroll bank module
//
// type TxMsgProvider interface {
// ...
// BankSend(types.BankSendMsg) XplaClient
// ...
// }
//
// type QueryMsgProvider interface {
// ...
// BankBalances(types.BankBalancesMsg) XplaClient
// DenomMetadata(...types.DenomMetadataMsg) XplaClient
// Total(...types.TotalMsg) XplaClient
// ...
// }
//
// The return type of these methods must be always the XplaClient because the client uses mehod chaining.
//
// e.g. - create and sign transaction
//
// txbytes, err := xplac.BankSend(bankSendMsg).CreateAndSignTx()
//
// e.g. - query
//
// res, err := xplac.BankBalances(bankBalancesMsg).Query()
type XplaClient interface {
WithProvider
GetProvider
Expand All @@ -29,7 +58,7 @@ type XplaClient interface {
HelperProvider
}

// Optional parameters of xpla client.
// Optional parameters of client.xplaClient.
type Options struct {
PrivateKey key.PrivateKey
AccountNumber string
Expand All @@ -50,6 +79,7 @@ type Options struct {
OutputDocument string
}

// Methods set params of client.xplaClient.
type WithProvider interface {
UpdateXplacInCoreModule() XplaClient
WithOptions(Options) XplaClient
Expand Down Expand Up @@ -79,6 +109,7 @@ type WithProvider interface {
WithErr(err error) XplaClient
}

// Methods get params of client.xplaClient.
type GetProvider interface {
GetChainId() string
GetPrivateKey() key.PrivateKey
Expand Down Expand Up @@ -107,6 +138,7 @@ type GetProvider interface {
GetErr() error
}

// Methods handle transaction.
type TxProvider interface {
CreateAndSignTx() ([]byte, error)
CreateUnsignedTx() ([]byte, error)
Expand All @@ -117,21 +149,25 @@ type TxProvider interface {
ValidateSignatures(types.ValidateSignaturesMsg) (string, error)
}

// Method handles query functions.
type QueryProvider interface {
Query() (string, error)
}

// Methods handle functions of broadcasting.
type BroadcastProvider interface {
Broadcast([]byte) (*types.TxRes, error)
BroadcastBlock([]byte) (*types.TxRes, error)
BroadcastAsync([]byte) (*types.TxRes, error)
}

// Methods get information from XPLA chain.
type InfoRequestProvider interface {
LoadAccount(sdk.AccAddress) (authtypes.AccountI, error)
Simulate(cmclient.TxBuilder) (*sdktx.SimulateResponse, error)
}

// Methods are external functions of each module for sending transaction.
type TxMsgProvider interface {
// authz
AuthzGrant(types.AuthzGrantMsg) XplaClient
Expand Down Expand Up @@ -195,6 +231,7 @@ type TxMsgProvider interface {
Migrate(types.MigrateMsg) XplaClient
}

// Methods are external functions of each module for querying.
type QueryMsgProvider interface {
// auth
AuthParams() XplaClient
Expand Down Expand Up @@ -339,6 +376,7 @@ type QueryMsgProvider interface {
LibwasmvmVersion() XplaClient
}

// Method of helper.
type HelperProvider interface {
EncodedTxbytesToJsonTx([]byte) ([]byte, error)
}
3 changes: 3 additions & 0 deletions provider/provider_helper.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package provider

// Reset XplaClient.
// Remove recorded all parameters.
func ResetXplac(xplac XplaClient) XplaClient {
return ResetModuleAndMsgXplac(xplac).
WithOptions(Options{}).
WithErr(nil)
}

// Reset XplaClient with removing module name and message.
func ResetModuleAndMsgXplac(xplac XplaClient) XplaClient {
return xplac.
WithModule("").
Expand Down

0 comments on commit 4107ffb

Please sign in to comment.