Skip to content
This repository has been archived by the owner on Nov 28, 2023. It is now read-only.

Commit

Permalink
move starkex into repo to avoid ambiguous import
Browse files Browse the repository at this point in the history
  • Loading branch information
fardream committed May 25, 2022
1 parent b413eaf commit e763d21
Show file tree
Hide file tree
Showing 19 changed files with 3,258 additions and 23 deletions.
2 changes: 1 addition & 1 deletion create_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"net/http"

"github.com/yanue/starkex"
"github.com/fardream/go-dydx/starkex"
)

// CreateOrderRequest is the post payload to create a new order
Expand Down
2 changes: 1 addition & 1 deletion dydx-cli/cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ func (c *cancelCmd) do(*cobra.Command, []string) {
case c.orderId != "":
printOrPanic(getOrPanic(client.CancelOrder(ctx, c.orderId)).CancelOrder)
default:
orPanic(fmt.Errorf("missing market, or client id, or order id"))
orPanic(fmt.Errorf("missing client id, order id, or cancel all"))
}
}
17 changes: 13 additions & 4 deletions dydx-cli/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package main
import (
"context"
"crypto/rand"
"encoding/json"
"fmt"
"math/big"
"os"
"time"

"github.com/fardream/go-dydx"
"github.com/foonetic/dydxgo/util"
"github.com/spf13/cobra"
)

Expand All @@ -26,6 +27,7 @@ type sendCmd struct {
limitfee string
postonly bool
positionId int64
outputFile string
}

func newSendCmd() *sendCmd {
Expand All @@ -36,7 +38,7 @@ func newSendCmd() *sendCmd {
Long: "send an order to dydx.exchange",
},
commonFields: commonFields{},
duration: duration(time.Second * 60),
duration: duration(time.Minute * 15),
}

c.setupCommonFields(c.Command)
Expand All @@ -57,6 +59,8 @@ func newSendCmd() *sendCmd {
c.Flags().BoolVar(&c.postonly, "post-only", false, "post only")
c.Flags().Int64Var(&c.positionId, "position-id", 0, "position id")
c.MarkFlagRequired("position-id")
c.Flags().StringVarP(&c.outputFile, "output", "o", "", "output result to this file")
c.MarkFlagFilename("output")
c.Run = c.do

return c
Expand All @@ -77,7 +81,12 @@ func (c *sendCmd) do(*cobra.Command, []string) {

expiration := dydx.GetIsoDateStr(now.Add((time.Duration)(c.duration)))

order := dydx.NewCreateOrderRequest(c.market, c.side, c.orderType, fmt.Sprintf("%f", c.size), fmt.Sprintf("%f", c.size), c.clientId, c.tif, expiration, c.limitfee, c.postonly)
order := dydx.NewCreateOrderRequest(c.market, c.side, c.orderType, fmt.Sprintf("%f", c.size), fmt.Sprintf("%f", c.price), c.clientId, c.tif, expiration, c.limitfee, c.postonly)

printOrPanic(util.GetOrPanic(client.NewOrder(ctx, order, c.positionId)).Order)
printOrPanic(order)
result := getOrPanic(client.NewOrder(ctx, order, c.positionId)).Order
printOrPanic(result)
if c.outputFile != "" {
os.WriteFile(c.outputFile, getOrPanic(json.MarshalIndent(result, "", " ")), 0o666)
}
}
13 changes: 4 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,19 @@ require (
github.com/google/go-querystring v1.1.0
github.com/google/uuid v1.3.0
github.com/gorilla/websocket v1.5.0
github.com/huandu/xstrings v1.3.2
github.com/miguelmota/go-solidity-sha3 v0.1.1
github.com/shopspring/decimal v1.3.1
github.com/spf13/cobra v1.4.1-0.20220518005708-5b11656e45a6
github.com/yanue/starkex v0.0.0-20211122094927-61a9aa6b8d97
go.uber.org/zap v1.21.0
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898
)

require (
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/ethereum/go-ethereum v1.10.17 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/miguelmota/go-solidity-sha3 v0.1.1 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
)

replace github.com/btcsuite/btcd v0.20.1-beta => github.com/btcsuite/btcd v0.22.1
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/bmizerany/pat v0.0.0-20170815010413-6226ea591a40/go.mod h1:8rLXio+WjiTceGBHIoTvn60HIbs7Hm7bcHjyrSqYB9c=
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
github.com/btcsuite/btcd v0.22.1 h1:CnwP9LM/M9xuRrGSCGeMVs9iv09uMqwsVX7EeIpgV2c=
github.com/btcsuite/btcd/btcec/v2 v2.1.2/go.mod h1:ctjw4H1kknNJmRN4iP1R7bTQ+v3GJkZBd6mui8ZsAZE=
github.com/btcsuite/btcd/btcec/v2 v2.2.0 h1:fzn1qaOt32TuLjFlkzYSsBC35Q3KUjT1SwPxiMSCF5k=
github.com/btcsuite/btcd/btcec/v2 v2.2.0/go.mod h1:U7MHm051Al6XmscBQ0BoNydpOTsFAn707034b5nY8zU=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.0/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U=
github.com/c-bata/go-prompt v0.2.2/go.mod h1:VzqtzE2ksDBcdln8G7mk2RX9QyGjH+OVqOCSiVIqS34=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/cp v0.1.0/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW+s=
Expand All @@ -75,9 +71,7 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckarep/golang-set v1.8.0/go.mod h1:5nI87KwE7wgsBU1F4GKAw2Qod7p5kyS383rP6+o6qqo=
github.com/decred/dcrd/crypto/blake256 v1.0.0 h1:/8DMNYp9SGi5f0w7uCm6d6M4OU2rGFK09Y2A4Xv7EE0=
github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1m5sE92cU+pd5Mcc=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs=
github.com/deepmap/oapi-codegen v1.6.0/go.mod h1:ryDa9AgbELGeB+YEXE1dR53yAjHwFvE9iAUlWl9Al3M=
github.com/deepmap/oapi-codegen v1.8.2/go.mod h1:YLgSKSDv/bZQB7N4ws6luhozi3cEdRktEqrX88CvjIw=
Expand Down Expand Up @@ -337,8 +331,6 @@ github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPU
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/willf/bitset v1.1.3/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4=
github.com/xlab/treeprint v0.0.0-20180616005107-d6fb6747feb6/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg=
github.com/yanue/starkex v0.0.0-20211122094927-61a9aa6b8d97 h1:2J/o24Zad7R0J+F/OqMazZHoXJSIpMMBN8PksR8bm4s=
github.com/yanue/starkex v0.0.0-20211122094927-61a9aa6b8d97/go.mod h1:Owc+hA2CxIsKGVgX6n/KIJtyXxJoAGNcHO+8iyL0J6A=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
Expand Down
21 changes: 21 additions & 0 deletions starkex/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 yanue

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
78 changes: 78 additions & 0 deletions starkex/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# stark key authentication library, signature generator for dydx exchange

for the following operations:

- Place an order
- Withdraw funds

> link : https://docs.dydx.exchange/#authentication
### Installation

```
go get github.com/yanue/starkex
```

### demos

#### order sign demo

```
const MOCK_PRIVATE_KEY = "58c7d5a90b1776bde86ebac077e053ed85b0f7164f53b080304a531947f46e3"
param := OrderSignParam{
NetworkId: NETWORK_ID_ROPSTEN,
Market: "ETH-USD",
Side: "BUY",
PositionId: 12345,
HumanSize: "145.0005",
HumanPrice: "350.00067",
LimitFee: "0.125",
ClientId: "This is an ID that the client came up with to describe this order",
Expiration: "2020-09-17T04:15:55.028Z",
}
sign, err := OrderSign(MOCK_PRIVATE_KEY, param)
// 00cecbe513ecdbf782cd02b2a5efb03e58d5f63d15f2b840e9bc0029af04e8dd0090b822b16f50b2120e4ea9852b340f7936ff6069d02acca02f2ed03029ace5
fmt.Println("sign,err", sign, err)
```

#### withdraw sign demo

```
const MOCK_PRIVATE_KEY = "58c7d5a90b1776bde86ebac077e053ed85b0f7164f53b080304a531947f46e3"
param := WithdrawSignParam{
NetworkId: NETWORK_ID_ROPSTEN,
PositionId: 12345,
HumanAmount: "49.478023",
ClientId: "This is an ID that the client came up with to describe this withdrawal",
Expiration: "2020-09-17T04:15:55.028Z",
}
sign, err := WithdrawSign(MOCK_PRIVATE_KEY, param)
// 05e48c33f8205a5359c95f1bd7385c1c1f587e338a514298c07634c0b6c952ba0687d6980502a5d7fa84ef6fdc00104db22c43c7fb83e88ca84f19faa9ee3de1
fmt.Println("sign,err", sign, err)
```

#### transfer sign demo (fast_withdraw)

```
const MOCK_PRIVATE_KEY = "58c7d5a90b1776bde86ebac077e053ed85b0f7164f53b080304a531947f46e3"
param := TransferSignParam{
NetworkId: NETWORK_ID_MAINNET,
CreditAmount: "1",
DebitAmount: "2",
SenderPositionId: 12345,
ReceiverPositionId: 67890,
ReceiverPublicKey: "04a9ecd28a67407c3cff8937f329ca24fd631b1d9ca2b9f2df47c7ebf72bf0b0",
ReceiverAddress: "0x1234567890123456789012345678901234567890",
Expiration: "2020-09-17T04:15:55.028Z",
ClientId: "This is an ID that the client came up with to describe this transfer",
}
sign, err := TransferSign(MOCK_PRIVATE_KEY, param)
// 0278aeb361938d4c377950487bb770fc9464bf5352e19117c03243efad4e10a302bb3983e05676c7952caa4acdc1a83426d5c8cb8c56d7f6c477cfdafd37718a
fmt.Println("sign,err", sign, err)
```

### inspired by

> https://github.com/dydxprotocol/dydx-v3-python
> https://github.com/starkware-libs/starkex-resources

0 comments on commit e763d21

Please sign in to comment.