Skip to content

Commit

Permalink
Merge pull request #199 from InjectiveLabs/dev
Browse files Browse the repository at this point in the history
feat/release changes to support chain upgrade v1.12
  • Loading branch information
aarmoa authored Jan 8, 2024
2 parents f196717 + d10fa53 commit b0f68ed
Show file tree
Hide file tree
Showing 300 changed files with 44,092 additions and 28,109 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: pre-commit
on:
pull_request:
push:
branches: [master, dev]

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-python@v4
- uses: actions/setup-go@v5
with:
go-version: 1.19
- run: go install golang.org/x/tools/cmd/goimports@latest
- run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s - -b $(go env GOPATH)/bin v1.49.0
- run: echo "PATH=$PATH:/home/runner/go/bin" >> $GITHUB_ENV
- uses: actions/checkout@v3
- uses: pre-commit/[email protected]
20 changes: 20 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: run-tests
on:
pull_request:
push:
branches: [ master, dev ]

jobs:
run-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 2
- uses: actions/setup-go@v5
with:
go-version: 1.19
- name: Run test and calculate coverage
run: make coverage
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.idea/
.chain_cookie
.exchange_cookie
coverage.out
21 changes: 21 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
exclude: |
(?x)^(
chain/.*|
exchange/.*|
proto/.*
)$
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: end-of-file-fixer
- id: check-yaml
- repo: https://github.com/dnephin/pre-commit-golang
rev: v0.5.0
hooks:
- id: go-fmt
- id: go-imports
- id: golangci-lint
args: [--timeout=15m]
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,4 @@ Copyright © 2020 - 2022 Injective Labs Inc. (https://injectivelabs.org/)
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ copy-exchange-client:
cp -r ../injective-indexer/api/gen/grpc/injective_derivative_exchange_rpc/pb exchange/derivative_exchange_rpc/pb
cp -r ../injective-indexer/api/gen/grpc/injective_portfolio_rpc/pb exchange/portfolio_rpc/pb

.PHONY: copy-exchange-client
.PHONY: copy-exchange-client tests coverage

copy-chain-types:
cp ../injective-core/injective-chain/types/*.go chain/types
rm -rf chain/types/*test.go rm -rf chain/types/*gw.go
cp ../injective-core/injective-chain/crypto/ethsecp256k1/*.go chain/crypto/ethsecp256k1
rm -rf chain/crypto/ethsecp256k1/*test.go rm -rf chain/crypto/ethsecp256k1/*gw.go
cp ../injective-core/injective-chain/modules/auction/types/*.go chain/auction/types
rm -rf chain/auction/types/*test.go rm -rf chain/auction/types/*gw.go
cp ../injective-core/injective-chain/modules/exchange/types/*.go chain/exchange/types
Expand All @@ -46,6 +48,13 @@ copy-chain-types:
rm -rf chain/wasmx/types/*test.go rm -rf chain/wasmx/types/*gw.go
cp ../injective-core/injective-chain/modules/tokenfactory/types/*.go chain/tokenfactory/types
rm -rf chain/tokenfactory/types/*test.go rm -rf chain/tokenfactory/types/*gw.go
cp ../injective-core/injective-chain/stream/types/*.go chain/stream/types

echo "👉 Replace injective-core/injective-chain/modules with sdk-go/chain"
echo "👉 Replace injective-core/injective-chain/types with sdk-go/chain/types"
echo "👉 Replace injective-core/injective-chain/crypto with sdk-go/chain/crypto"

tests:
go test -race ./client/... ./ethereum/...
coverage:
go test -race -coverprofile=coverage.out -covermode=atomic ./client/... ./ethereum/...
2 changes: 1 addition & 1 deletion NOTICE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ Copyright © 2020 - 2022 Injective Labs Inc. (https://injectivelabs.org/)
Originally released by Injective Labs Inc. under: <br />
Apache License <br />
Version 2.0, January 2004 <br />
http://www.apache.org/licenses/
http://www.apache.org/licenses/
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Injective Protocol Golang SDK 🌟

[![codecov](https://codecov.io/gh/InjectiveLabs/sdk-go/graph/badge.svg?token=XDGZV265EE)](https://codecov.io/gh/InjectiveLabs/sdk-go)

---

## 📚 Getting Started
Expand Down Expand Up @@ -65,4 +67,4 @@ Copyright © 2020 - 2022 Injective Labs Inc. (https://injectivelabs.org/)
Originally released by Injective Labs Inc. under: <br />
Apache License <br />
Version 2.0, January 2004 <br />
http://www.apache.org/licenses/
http://www.apache.org/licenses/
7 changes: 4 additions & 3 deletions chain/auction/types/auction.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions chain/auction/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions chain/auction/types/query.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions chain/auction/types/tx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions chain/crypto/ethsecp256k1/keys.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions chain/exchange/types/authz.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions chain/exchange/types/common_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,11 @@ func (m *OrderType) UnmarshalJSON(data []byte) error {
*m = OrderType(value)
return nil
}

// GetOrderIdentifier returns the cid of an order if it exists, otherwise returns the order hash
func GetOrderIdentifier(orderHash, cid string) any {
if cid != "" {
return cid
}
return common.HexToHash(orderHash)
}
21 changes: 18 additions & 3 deletions chain/exchange/types/common_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func (d *DerivativeLimitOrderDelta) OrderHash() common.Hash {
return d.Order.Hash()
}

func (d *DerivativeLimitOrderDelta) Cid() string {
return d.Order.Cid()
}

var AuctionSubaccountID = common.HexToHash("0x1111111111111111111111111111111111111111111111111111111111111111")
var ZeroSubaccountID = common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000")

Expand Down Expand Up @@ -138,6 +142,11 @@ func IsValidOrderHash(orderHash string) bool {
return IsHexHash(orderHash)
}

func IsValidCid(cid string) bool {
// Arbitrarily setting max length of cid to uuid length
return len(cid) <= 36
}

// IsHexHash verifies whether a string can represent a valid hex-encoded hash or not.
func IsHexHash(s string) bool {
if !isHexString(s) {
Expand Down Expand Up @@ -337,12 +346,18 @@ func HasDuplicatesCoin(slice []sdk.Coin) bool {
}

func HasDuplicatesOrder(slice []*OrderData) bool {
seen := make(map[string]struct{})
seenHashes := make(map[string]struct{})
seenCids := make(map[string]struct{})
for _, item := range slice {
if _, ok := seen[item.OrderHash]; ok {
hash, cid := item.GetOrderHash(), item.GetCid()
_, hashExists := seenHashes[hash]
_, cidExists := seenCids[cid]

if (hash != "" && hashExists) || (cid != "" && cidExists) {
return true
}
seen[item.OrderHash] = struct{}{}
seenHashes[hash] = struct{}{}
seenCids[cid] = struct{}{}
}
return false
}
28 changes: 21 additions & 7 deletions chain/exchange/types/derivative_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ import (
"github.com/ethereum/go-ethereum/common"
)

func NewMarketOrderForLiquidation(position *Position, positionSubaccountID common.Hash, liquidator sdk.AccAddress) *DerivativeMarketOrder {
func NewMarketOrderForLiquidation(
position *Position,
positionSubaccountID common.Hash,
liquidator sdk.AccAddress,
worstPrice sdk.Dec,
) *DerivativeMarketOrder {
var (
worstPrice sdk.Dec
orderType OrderType
orderType OrderType
)

// if long position, market sell order at price 0
// if short position, market buy order at price infinity
// if long position, market sell order
// if short position, market buy order
if position.IsLong {
worstPrice = sdk.ZeroDec()
orderType = OrderType_SELL
} else {
worstPrice = MaxOrderPrice
orderType = OrderType_BUY
}

Expand Down Expand Up @@ -447,10 +449,22 @@ func (o *DerivativeLimitOrder) IsConditional() bool {
return o.OrderType.IsConditional()
}

func (o *DerivativeLimitOrder) Cid() string {
return o.OrderInfo.GetCid()
}

func (o *DerivativeMarketOrder) Cid() string {
return o.OrderInfo.GetCid()
}

func (o *DerivativeOrder) SubaccountID() common.Hash {
return o.OrderInfo.SubaccountID()
}

func (o *DerivativeOrder) Cid() string {
return o.OrderInfo.GetCid()
}

func (o *DerivativeOrder) IsFromDefaultSubaccount() bool {
return o.OrderInfo.IsFromDefaultSubaccount()
}
Expand Down
4 changes: 4 additions & 0 deletions chain/exchange/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,8 @@ var (
ErrTooMuchOrderMargin = errors.Register(ModuleName, 93, "Order has too much margin")
ErrBadSubaccountNonce = errors.Register(ModuleName, 94, "Subaccount nonce is invalid")
ErrInsufficientFunds = errors.Register(ModuleName, 95, "insufficient funds")
ErrPostOnlyMode = errors.Register(ModuleName, 96, "exchange is in post-only mode")
ErrClientOrderIdAlreadyExists = errors.Register(ModuleName, 97, "client order id already exists")
ErrInvalidCid = errors.Register(ModuleName, 98, "client order id is invalid. Max length is 36 chars")
ErrInvalidEmergencySettle = errors.Register(ModuleName, 99, "market cannot be settled in emergency mode")
)
2 changes: 2 additions & 0 deletions chain/exchange/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"github.com/ethereum/go-ethereum/common"
)

// Event type and attribute constants

func (e *EventOrderFail) AddOrderFail(orderHash common.Hash, flag uint32) {
e.Hashes = append(e.Hashes, orderHash.Bytes())
e.Flags = append(e.Flags, flag)
Expand Down
7 changes: 4 additions & 3 deletions chain/exchange/types/events.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions chain/exchange/types/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ func (e ExecutionType) IsTaker() bool {

func (s MarketStatus) SupportsOrderCancellations() bool {
switch s {
case MarketStatus_Active, MarketStatus_Demolished, MarketStatus_Expired:
case MarketStatus_Active, MarketStatus_Demolished, MarketStatus_Expired, MarketStatus_Paused:
return true
case MarketStatus_Paused:
return false
default:
return false
}
Expand Down
Loading

0 comments on commit b0f68ed

Please sign in to comment.