Skip to content

Commit

Permalink
.github: Add workflows and fix lints
Browse files Browse the repository at this point in the history
  • Loading branch information
joeykraut committed Dec 19, 2024
1 parent 6993b4c commit 5651a4b
Show file tree
Hide file tree
Showing 37 changed files with 332 additions and 210 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Lint

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
lint:
name: Lint and Format
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.21'
cache: true

- name: Install dependencies
run: go mod download

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
args: --timeout=5m

- name: Check formatting
run: |
if [ "$(gofmt -l . | wc -l)" -gt 0 ]; then
echo "The following files are not formatted correctly:"
gofmt -l .
exit 1
fi
27 changes: 27 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Test

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
test:
name: Run Tests
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.21'
cache: true

- name: Install dependencies
run: go mod download

- name: Run tests
run: go test -v ./...
35 changes: 13 additions & 22 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
linters:
enable:
- gofmt
- golint
- govet
- errcheck
- staticcheck
- gosimple
- staticcheck
- errcheck
- ineffassign
- unused
- typecheck
- misspell
- lll
- goimports
- revive
- gosec

linters-settings:
govet:
shadow: true
goimports:
local-prefixes: github.com/renegade-fi/golang-sdk

run:
deadline: 5m
tests: true
skip-dirs:
- vendor

issues:
exclude-rules:
- path: _test\.go
linters:
- errcheck

linters-settings:
govet:
check-shadowing: true
golint:
min-confidence: 0.8
gofmt:
simplify: true
lll:
line-length: 120
exclude-use-default: false
19 changes: 10 additions & 9 deletions client/api_types/api_external_match.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package api_types
// Package api_types provides API data structures for the Renegade SDK
package api_types //nolint:revive

import (
"errors"
Expand All @@ -7,7 +8,7 @@ import (

// ApiExternalOrder is an order from outside of the darkpool, generated by a client
// requesting an external match
type ApiExternalOrder struct {
type ApiExternalOrder struct { //nolint:revive
// The mint (erc20 address) of the base asset
// As a hex string
BaseMint string `json:"base_mint"`
Expand All @@ -26,7 +27,7 @@ type ApiExternalOrder struct {
}

// ApiExternalOrderBuilder helps construct ApiExternalOrder with validation
type ApiExternalOrderBuilder struct {
type ApiExternalOrderBuilder struct { //nolint:revive
order ApiExternalOrder
}

Expand Down Expand Up @@ -98,13 +99,13 @@ func (b *ApiExternalOrderBuilder) Build() (*ApiExternalOrder, error) {
}

// ApiExternalAssetTransfer represents a single transfer between the external client and darkpool
type ApiExternalAssetTransfer struct {
type ApiExternalAssetTransfer struct { //nolint:revive
Mint string `json:"mint"`
Amount Amount `json:"amount"`
}

// ApiExternalQuote is a quote from the relayer for an external order
type ApiExternalQuote struct {
type ApiExternalQuote struct { //nolint:revive
Order ApiExternalOrder `json:"order"`
MatchResult ApiExternalMatchResult `json:"match_result"`
Fees ApiFee `json:"fees"`
Expand All @@ -117,13 +118,13 @@ type ApiExternalQuote struct {
// ApiSignedQuote is a quote from the relayer, signed with the relayer's admin API key
// This allows a client to submit an authorized quote to the relayer and receive back an
// assembled settlement transaction at the quoted price
type ApiSignedQuote struct {
type ApiSignedQuote struct { //nolint:revive
Quote ApiExternalQuote `json:"quote"`
Signature string `json:"signature"`
}

// ApiExternalMatchBundle contains a match and a transaction that the client can submit on-chain
type ApiExternalMatchBundle struct {
type ApiExternalMatchBundle struct { //nolint:revive
MatchResult ApiExternalMatchResult `json:"match_result"`
Fees ApiFee `json:"fees"`
Receive ApiExternalAssetTransfer `json:"receive"`
Expand All @@ -132,7 +133,7 @@ type ApiExternalMatchBundle struct {
}

// ApiExternalMatchResult is the result of a request to generate an external match
type ApiExternalMatchResult struct {
type ApiExternalMatchResult struct { //nolint:revive
QuoteMint string `json:"quote_mint"`
BaseMint string `json:"base_mint"`
QuoteAmount Amount `json:"quote_amount"`
Expand All @@ -141,7 +142,7 @@ type ApiExternalMatchResult struct {
}

// ApiSettlementTransaction is an EVM transaction parameterization for settling an external match
type ApiSettlementTransaction struct {
type ApiSettlementTransaction struct { //nolint:revive
Type string `json:"type"`
To string `json:"to"`
Data string `json:"data"`
Expand Down
4 changes: 2 additions & 2 deletions client/api_types/api_order_book.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package api_types
package api_types //nolint:revive

// ApiToken is a token available on the exchange
type ApiToken struct {
type ApiToken struct { //nolint:revive
// The mint (erc20 address) of the token
Address string `json:"address"`
// The symbol of the token
Expand Down
37 changes: 27 additions & 10 deletions client/api_types/api_wallet.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package api_types
// Package api_types provides API data structures for the Renegade SDK
package api_types //nolint:revive

import (
"crypto/ecdsa"
Expand All @@ -14,25 +15,31 @@ import (
// The number of u32 limbs in the serialized form of a secret share
const secretShareLimbCount = 8 // 256 bits

// Amount is a big.Int marshalled and unmarshalled as a rust-compatible string
type Amount big.Int

// NewAmount creates a new Amount from an int64
func NewAmount(i int64) Amount {
return Amount(*big.NewInt(i))
}

// IsZero returns true if the amount is zero
func (a *Amount) IsZero() bool {
return (*big.Int)(a).Sign() == 0
}

// String returns the string representation of the amount
func (a *Amount) String() string {
return (*big.Int)(a).String()
}

// MarshalJSON marshals the amount to a JSON string
func (a Amount) MarshalJSON() ([]byte, error) {
s := a.String()
return []byte(s), nil
}

// SetString sets the amount from a string
func (a *Amount) SetString(s string, base int) error {
i, ok := new(big.Int).SetString(s, base)
if !ok {
Expand All @@ -42,31 +49,37 @@ func (a *Amount) SetString(s string, base int) error {
return nil
}

// UnmarshalJSON unmarshals the amount from a JSON string
func (a *Amount) UnmarshalJSON(b []byte) error {
s := string(b)
return a.SetString(s, 10)
}

// Add adds two amounts
func (a Amount) Add(b Amount) Amount {
sum := new(big.Int).Add((*big.Int)(&a), (*big.Int)(&b))
return Amount(*sum)
}

// Sub subtracts two amounts
func (a Amount) Sub(b Amount) Amount {
diff := new(big.Int).Sub((*big.Int)(&a), (*big.Int)(&b))
return Amount(*diff)
}

// Mul multiplies two amounts
func (a Amount) Mul(b Amount) Amount {
prod := new(big.Int).Mul((*big.Int)(&a), (*big.Int)(&b))
return Amount(*prod)
}

// Div divides two amounts
func (a Amount) Div(b Amount) Amount {
quot := new(big.Int).Div((*big.Int)(&a), (*big.Int)(&b))
return Amount(*quot)
}

// Cmp compares two amounts
func (a Amount) Cmp(b Amount) int {
return (*big.Int)(&a).Cmp((*big.Int)(&b))
}
Expand Down Expand Up @@ -101,9 +114,9 @@ func orderSideToScalar(side string) (wallet.Scalar, error) {
}

// ApiOrder is an order in a Renegade wallet
type ApiOrder struct {
type ApiOrder struct { //nolint:revive
// The id of the order
Id uuid.UUID `json:"id"`
Id uuid.UUID `json:"id"` //nolint:revive
// The mint (erc20 address) of the base asset
// As a hex string
BaseMint string `json:"base_mint"`
Expand Down Expand Up @@ -162,7 +175,7 @@ func (a *ApiOrder) ToOrder(o *wallet.Order) error {
}

// ApiBalance is a balance in a Renegade wallet
type ApiBalance struct {
type ApiBalance struct { //nolint:revive
// The mint (erc20 address) of the asset
Mint string `json:"mint"`
// The amount of the asset
Expand Down Expand Up @@ -201,17 +214,18 @@ func (a *ApiBalance) ToBalance(b *wallet.Balance) error {

// ApiFee is a fee in the Renegade system, due on a match, balance, etc
// Contains both a relayer fee and a protocol fee
type ApiFee struct {
type ApiFee struct { //nolint:revive
RelayerFee Amount `json:"relayer_fee"`
ProtocolFee Amount `json:"protocol_fee"`
}

// Total returns the total fee
func (f *ApiFee) Total() Amount {
return f.RelayerFee.Add(f.ProtocolFee)
}

// ApiPublicKeychain is a public keychain in the Renegade system
type ApiPublicKeychain struct {
type ApiPublicKeychain struct { //nolint:revive
// The public root key of the wallet
// As a hex string
PkRoot string `json:"pk_root"`
Expand All @@ -220,13 +234,15 @@ type ApiPublicKeychain struct {
PkMatch string `json:"pk_match"`
}

// FromPublicKeychain converts a wallet.PublicKeychain to an ApiPublicKeychain
func (a *ApiPublicKeychain) FromPublicKeychain(pk *wallet.PublicKeychain) error {
a.PkRoot = pk.PkRoot.ToHexString()
a.PkMatch = pk.PkMatch.ToHexString()

return nil
}

// ToPublicKeychain converts an ApiPublicKeychain to a wallet.PublicKeychain
func (a *ApiPublicKeychain) ToPublicKeychain() (*wallet.PublicKeychain, error) {
pkRoot, err := new(wallet.PublicSigningKey).FromHexString(a.PkRoot)
if err != nil {
Expand All @@ -244,7 +260,7 @@ func (a *ApiPublicKeychain) ToPublicKeychain() (*wallet.PublicKeychain, error) {
}

// ApiPrivateKeychain represents a private keychain for the API wallet
type ApiPrivateKeychain struct {
type ApiPrivateKeychain struct { //nolint:revive
// The private root key of the wallet
// As a hex string, optional
SkRoot *string `json:"sk_root,omitempty"`
Expand Down Expand Up @@ -302,7 +318,7 @@ func (a *ApiPrivateKeychain) ToPrivateKeychain() (*wallet.PrivateKeychain, error
}

// ApiKeychain represents a keychain API type that maintains all keys as hex strings
type ApiKeychain struct {
type ApiKeychain struct { //nolint:revive
// The public keychain
PublicKeys ApiPublicKeychain `json:"public_keys"`
// The private keychain
Expand Down Expand Up @@ -346,9 +362,9 @@ func (a *ApiKeychain) ToKeychain() (*wallet.Keychain, error) {
}

// ApiWallet is a wallet in the Renegade system
type ApiWallet struct {
type ApiWallet struct { //nolint: revive
// Identifier
Id uuid.UUID `json:"id"`
Id uuid.UUID `json:"id"` //nolint: revive
// The orders maintained by this wallet
Orders []ApiOrder `json:"orders"`
// The balances maintained by the wallet to cover orders
Expand All @@ -370,6 +386,7 @@ type ApiWallet struct {
Blinder [secretShareLimbCount]uint32 `json:"blinder"`
}

// FromWallet converts a wallet.Wallet to an ApiWallet
func (a *ApiWallet) FromWallet(w *wallet.Wallet) (*ApiWallet, error) {
a.Id = w.Id

Expand Down
5 changes: 3 additions & 2 deletions client/api_types/api_wallet_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package api_types
package api_types //nolint:revive

import (
"crypto/ecdsa"
"crypto/rand"
"testing"

"github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/renegade-fi/golang-sdk/wallet"
"github.com/stretchr/testify/assert"

"github.com/renegade-fi/golang-sdk/wallet"
)

func TestApiWalletConversion(t *testing.T) {
Expand Down
Loading

0 comments on commit 5651a4b

Please sign in to comment.