Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,8 @@ jobs:
args: --timeout=15m -v

# Optional: show only new issues if it's a pull request. The default value is `false`.
only-new-issues: false
only-new-issues: true

# Optional: if set to true then the all caching functionality will be complete disabled,
# takes precedence over all other caching options.
# skip-cache: true

# Optional: if set to true then the action don't cache or restore ~/go/pkg.
skip-cache: true

# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
# skip-build-cache: true
4 changes: 2 additions & 2 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@master
with:
fetch-depth: 2
- uses: actions/setup-go@v5
with:
go-version-file: "go.mod"
check-latest: true
- uses: pre-commit/action@v2.0.2
- uses: pre-commit/action@v3.0.1
38 changes: 26 additions & 12 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
run:
tests: false
skip-dirs:
- chain
- exchange
- injective_data
- proto

linters:
fast: false
enable:
Expand All @@ -31,18 +23,40 @@ linters:

linters-settings:
revive:
enableAllRules: true
enable-all-rules: true
rules:
- name: var-naming
arguments:
- ["ID"]
- name: add-constant
disabled: true
- name: line-length-limit
arguments:
- 140
gocritic:
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- diagnostic
- style
- opinionated
- experimental
disabled-checks:
- singleCaseSwitch
- deferInLoop
- hugeParam
- unnamedResult
issues:
exclude-rules:
- linters:
- revive
text: ALL_CAPS
exclude-dirs:
- chain
- exchange
- injective_data
- proto
- client/keyring/testdata
max-issues-per-linter: 0

run:
tests: false
14 changes: 13 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,16 @@ tests:
coverage:
go test -race -coverprofile=coverage.out -covermode=atomic ./client/... ./ethereum/...

.PHONY: copy-exchange-client tests coverage
lint: export GOPROXY=direct
lint:
golangci-lint run --timeout=15m -v --new-from-rev=dev

lint-last-commit: export GOPROXY=direct
lint-last-commit:
golangci-lint run --timeout=15m -v --new-from-rev=HEAD~

lint-master: export GOPROXY=direct
lint-master:
golangci-lint run --timeout=15m -v --new-from-rev=master

.PHONY: copy-exchange-client tests coverage lint lint-last-commit lint-master
78 changes: 38 additions & 40 deletions client/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,38 @@ import (
"sync/atomic"
"time"

permissionstypes "github.com/InjectiveLabs/sdk-go/chain/permissions/types"

sdkmath "cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/client/grpc/cmtservice"

distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"

"github.com/cosmos/cosmos-sdk/types/query"

"google.golang.org/grpc/credentials/insecure"

wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
exchangetypes "github.com/InjectiveLabs/sdk-go/chain/exchange/types"
chainstreamtypes "github.com/InjectiveLabs/sdk-go/chain/stream/types"
tokenfactorytypes "github.com/InjectiveLabs/sdk-go/chain/tokenfactory/types"
"github.com/InjectiveLabs/sdk-go/client/common"
log "github.com/InjectiveLabs/suplog"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
"github.com/cosmos/cosmos-sdk/client"
sdkclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/grpc/cmtservice"
"github.com/cosmos/cosmos-sdk/client/tx"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
authztypes "github.com/cosmos/cosmos-sdk/x/authz"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/cosmos/gogoproto/proto"
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
ibcclienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
ibcconnectiontypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
ibcchanneltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

ethcommon "github.com/ethereum/go-ethereum/common"
exchangetypes "github.com/InjectiveLabs/sdk-go/chain/exchange/types"
permissionstypes "github.com/InjectiveLabs/sdk-go/chain/permissions/types"
chainstreamtypes "github.com/InjectiveLabs/sdk-go/chain/stream/types"
tokenfactorytypes "github.com/InjectiveLabs/sdk-go/chain/tokenfactory/types"
"github.com/InjectiveLabs/sdk-go/client"
"github.com/InjectiveLabs/sdk-go/client/common"
)

type OrderbookType string
Expand All @@ -76,20 +71,21 @@ type ChainClient interface {
CanSignTransactions() bool
FromAddress() sdk.AccAddress
QueryClient() *grpc.ClientConn
ClientContext() client.Context
ClientContext() sdkclient.Context
// return account number and sequence without increasing sequence
GetAccNonce() (accNum uint64, accSeq uint64)

SimulateMsg(clientCtx client.Context, msgs ...sdk.Msg) (*txtypes.SimulateResponse, error)
SimulateMsg(clientCtx sdkclient.Context, msgs ...sdk.Msg) (*txtypes.SimulateResponse, error)
AsyncBroadcastMsg(msgs ...sdk.Msg) (*txtypes.BroadcastTxResponse, error)
SyncBroadcastMsg(msgs ...sdk.Msg) (*txtypes.BroadcastTxResponse, error)
BroadcastMsg(broadcastMode txtypes.BroadcastMode, msgs ...sdk.Msg) (*txtypes.BroadcastTxRequest, *txtypes.BroadcastTxResponse, error)

// Build signed tx with given accNum and accSeq, useful for offline siging
// If simulate is set to false, initialGas will be used
BuildSignedTx(clientCtx client.Context, accNum, accSeq, initialGas uint64, msg ...sdk.Msg) ([]byte, error)
BuildSignedTx(clientCtx sdkclient.Context, accNum, accSeq, initialGas uint64, gasPrice uint64, msg ...sdk.Msg) ([]byte, error)
SyncBroadcastSignedTx(tyBytes []byte) (*txtypes.BroadcastTxResponse, error)
AsyncBroadcastSignedTx(txBytes []byte) (*txtypes.BroadcastTxResponse, error)
BroadcastSignedTx(txBytes []byte, broadcastMode txtypes.BroadcastMode) (*txtypes.BroadcastTxResponse, error)
QueueBroadcastMsg(msgs ...sdk.Msg) error

// Bank Module
Expand Down Expand Up @@ -314,7 +310,7 @@ type ChainClient interface {
var _ ChainClient = &chainClient{}

type chainClient struct {
ctx client.Context
ctx sdkclient.Context
network common.Network
opts *common.ClientOptions
logger log.Logger
Expand Down Expand Up @@ -360,7 +356,7 @@ type chainClient struct {
}

func NewChainClient(
ctx client.Context,
ctx sdkclient.Context,
network common.Network,
options ...common.ClientOption,
) (ChainClient, error) {
Expand Down Expand Up @@ -518,7 +514,7 @@ func (c *chainClient) syncTimeoutHeight() {
// if the account number and/or the account sequence number are zero (not set),
// they will be queried for and set on the provided Factory. A new Factory with
// the updated fields will be returned.
func PrepareFactory(clientCtx client.Context, txf tx.Factory) (tx.Factory, error) {
func PrepareFactory(clientCtx sdkclient.Context, txf tx.Factory) (tx.Factory, error) {
from := clientCtx.GetFromAddress()

if err := txf.AccountRetriever().EnsureExists(clientCtx, from); err != nil {
Expand Down Expand Up @@ -559,7 +555,7 @@ func (c *chainClient) QueryClient() *grpc.ClientConn {
return c.conn
}

func (c *chainClient) ClientContext() client.Context {
func (c *chainClient) ClientContext() sdkclient.Context {
return c.ctx
}

Expand Down Expand Up @@ -704,7 +700,7 @@ func (c *chainClient) GetFeeDiscountInfo(ctx context.Context, account string) (*
return res, err
}

func (c *chainClient) SimulateMsg(clientCtx client.Context, msgs ...sdk.Msg) (*txtypes.SimulateResponse, error) {
func (c *chainClient) SimulateMsg(clientCtx sdkclient.Context, msgs ...sdk.Msg) (*txtypes.SimulateResponse, error) {
c.txFactory = c.txFactory.WithSequence(c.accSeq)
c.txFactory = c.txFactory.WithAccountNumber(c.accNum)
txf, err := PrepareFactory(clientCtx, c.txFactory)
Expand All @@ -731,12 +727,17 @@ func (c *chainClient) SimulateMsg(clientCtx client.Context, msgs ...sdk.Msg) (*t
return simRes, nil
}

func (c *chainClient) BuildSignedTx(clientCtx client.Context, accNum, accSeq, initialGas uint64, msgs ...sdk.Msg) ([]byte, error) {
txf := NewTxFactory(clientCtx).WithSequence(accSeq).WithAccountNumber(accNum).WithGas(initialGas)
func (c *chainClient) BuildSignedTx(clientCtx sdkclient.Context, accNum, accSeq, initialGas uint64, gasPrice uint64, msgs ...sdk.Msg) ([]byte, error) {
txf := NewTxFactory(clientCtx).WithSequence(accSeq).WithAccountNumber(accNum)
txf = txf.WithGas(initialGas)

gasPriceWithDenom := fmt.Sprintf("%d%s", gasPrice, client.InjDenom)
txf = txf.WithGasPrices(gasPriceWithDenom)

return c.buildSignedTx(clientCtx, txf, msgs...)
}

func (c *chainClient) buildSignedTx(clientCtx client.Context, txf tx.Factory, msgs ...sdk.Msg) ([]byte, error) {
func (c *chainClient) buildSignedTx(clientCtx sdkclient.Context, txf tx.Factory, msgs ...sdk.Msg) ([]byte, error) {
ctx := context.Background()
if clientCtx.Simulate {
simTxBytes, err := txf.BuildSimTx(msgs...)
Expand Down Expand Up @@ -781,14 +782,7 @@ func (c *chainClient) buildSignedTx(clientCtx client.Context, txf tx.Factory, ms
}

func (c *chainClient) SyncBroadcastSignedTx(txBytes []byte) (*txtypes.BroadcastTxResponse, error) {
req := txtypes.BroadcastTxRequest{
TxBytes: txBytes,
Mode: txtypes.BroadcastMode_BROADCAST_MODE_SYNC,
}

ctx := context.Background()

res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.txClient.BroadcastTx, &req)
res, err := c.BroadcastSignedTx(txBytes, txtypes.BroadcastMode_BROADCAST_MODE_SYNC)
if err != nil || res.TxResponse.Code != 0 {
return res, err
}
Expand All @@ -808,7 +802,7 @@ func (c *chainClient) SyncBroadcastSignedTx(txBytes []byte) (*txtypes.BroadcastT
case <-t.C:
resultTx, err := c.ctx.Client.Tx(awaitCtx, txHash, false)
if err != nil {
if errRes := client.CheckCometError(err, txBytes); errRes != nil {
if errRes := sdkclient.CheckCometError(err, txBytes); errRes != nil {
return &txtypes.BroadcastTxResponse{TxResponse: errRes}, err
}

Expand All @@ -828,9 +822,13 @@ func (c *chainClient) SyncBroadcastSignedTx(txBytes []byte) (*txtypes.BroadcastT
}

func (c *chainClient) AsyncBroadcastSignedTx(txBytes []byte) (*txtypes.BroadcastTxResponse, error) {
return c.BroadcastSignedTx(txBytes, txtypes.BroadcastMode_BROADCAST_MODE_ASYNC)
}

func (c *chainClient) BroadcastSignedTx(txBytes []byte, broadcastMode txtypes.BroadcastMode) (*txtypes.BroadcastTxResponse, error) {
req := txtypes.BroadcastTxRequest{
TxBytes: txBytes,
Mode: txtypes.BroadcastMode_BROADCAST_MODE_SYNC,
Mode: broadcastMode,
}

ctx := context.Background()
Expand All @@ -843,7 +841,7 @@ func (c *chainClient) AsyncBroadcastSignedTx(txBytes []byte) (*txtypes.Broadcast
}

func (c *chainClient) broadcastTx(
clientCtx client.Context,
clientCtx sdkclient.Context,
txf tx.Factory,
broadcastMode txtypes.BroadcastMode,
msgs ...sdk.Msg,
Expand Down Expand Up @@ -2689,7 +2687,7 @@ func (c *chainClient) SyncBroadcastMsg(msgs ...sdk.Msg) (*txtypes.BroadcastTxRes
case <-t.C:
resultTx, err := c.ctx.Client.Tx(awaitCtx, txHash, false)
if err != nil {
if errRes := client.CheckCometError(err, req.TxBytes); errRes != nil {
if errRes := sdkclient.CheckCometError(err, req.TxBytes); errRes != nil {
return &txtypes.BroadcastTxResponse{TxResponse: errRes}, err
}

Expand Down
6 changes: 5 additions & 1 deletion client/chain/chain_test_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (c *MockChainClient) BroadcastMsg(broadcastMode txtypes.BroadcastMode, msgs
return &txtypes.BroadcastTxRequest{}, &txtypes.BroadcastTxResponse{}, nil
}

func (c *MockChainClient) BuildSignedTx(clientCtx client.Context, accNum, accSeq, initialGas uint64, msg ...sdk.Msg) ([]byte, error) {
func (c *MockChainClient) BuildSignedTx(clientCtx client.Context, accNum, accSeq, initialGas uint64, gasPrice uint64, msg ...sdk.Msg) ([]byte, error) {
return []byte(nil), nil
}

Expand All @@ -89,6 +89,10 @@ func (c *MockChainClient) AsyncBroadcastSignedTx(txBytes []byte) (*txtypes.Broad
return &txtypes.BroadcastTxResponse{}, nil
}

func (c *MockChainClient) BroadcastSignedTx(txBytes []byte, broadcastMode txtypes.BroadcastMode) (*txtypes.BroadcastTxResponse, error) {
return &txtypes.BroadcastTxResponse{}, nil
}

func (c *MockChainClient) QueueBroadcastMsg(msgs ...sdk.Msg) error {
return nil
}
Expand Down
9 changes: 6 additions & 3 deletions client/core/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ func (spotMarket SpotMarket) PriceToChainFormat(humanReadableValue decimal.Decim
func (spotMarket SpotMarket) NotionalToChainFormat(humanReadableValue decimal.Decimal) sdkmath.LegacyDec {
decimals := spotMarket.QuoteToken.Decimals
chainFormattedValue := humanReadableValue.Mul(decimal.New(1, decimals))
valueInChainFormat, _ := sdkmath.LegacyNewDecFromStr(chainFormattedValue.String())
quantizedValue := chainFormattedValue.Ceil()
valueInChainFormat, _ := sdkmath.LegacyNewDecFromStr(quantizedValue.String())

return valueInChainFormat
}
Expand Down Expand Up @@ -129,7 +130,8 @@ func (derivativeMarket DerivativeMarket) CalculateMarginInChainFormat(humanReada
func (derivativeMarket DerivativeMarket) NotionalToChainFormat(humanReadableValue decimal.Decimal) sdkmath.LegacyDec {
decimals := derivativeMarket.QuoteToken.Decimals
chainFormattedValue := humanReadableValue.Mul(decimal.New(1, decimals))
valueInChainFormat, _ := sdkmath.LegacyNewDecFromStr(chainFormattedValue.String())
quantizedValue := chainFormattedValue.Ceil()
valueInChainFormat, _ := sdkmath.LegacyNewDecFromStr(quantizedValue.String())

return valueInChainFormat
}
Expand Down Expand Up @@ -225,7 +227,8 @@ func (market BinaryOptionMarket) CalculateMarginInChainFormat(humanReadableQuant
func (market BinaryOptionMarket) NotionalToChainFormat(humanReadableValue decimal.Decimal) sdkmath.LegacyDec {
decimals := market.QuoteToken.Decimals
chainFormattedValue := humanReadableValue.Mul(decimal.New(1, decimals))
valueInChainFormat, _ := sdkmath.LegacyNewDecFromStr(chainFormattedValue.String())
quantizedValue := chainFormattedValue.Ceil()
valueInChainFormat, _ := sdkmath.LegacyNewDecFromStr(quantizedValue.String())

return valueInChainFormat
}
Expand Down
Loading
Loading