Skip to content

Commit 93688dc

Browse files
Merge pull request #4 from Logarithm-Labs/msg-builders
Add EIP712 order building method
2 parents ecb9bf9 + b1a55c2 commit 93688dc

File tree

4 files changed

+86
-10
lines changed

4 files changed

+86
-10
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package hyperliquid
2+
3+
import (
4+
"math"
5+
"testing"
6+
)
7+
8+
func GetEmptyExchangeAPI() *ExchangeAPI {
9+
exchangeAPI := NewExchangeAPI(true)
10+
if GLOBAL_DEBUG {
11+
exchangeAPI.SetDebugActive()
12+
}
13+
return exchangeAPI
14+
}
15+
16+
func TestExchangeAPI_BuildOrder(t *testing.T) {
17+
exchangeAPI := GetEmptyExchangeAPI()
18+
// input params
19+
coin := "ETH"
20+
size := 0.1
21+
price := 2500.0
22+
23+
isBuy := IsBuy(size)
24+
orderType := OrderType{
25+
Limit: &LimitOrderType{
26+
Tif: TifIoc,
27+
},
28+
}
29+
orderRequest := OrderRequest{
30+
Coin: coin,
31+
IsBuy: isBuy,
32+
Sz: math.Abs(size),
33+
LimitPx: price,
34+
OrderType: orderType,
35+
ReduceOnly: false,
36+
}
37+
res, err := exchangeAPI.BuildOrderEIP712(orderRequest, GroupingNa)
38+
if err != nil {
39+
t.Errorf("BuildOrder() error = %v", err)
40+
}
41+
t.Logf("BuildOrder() = %+v", res)
42+
}

hyperliquid/exchange_service.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package hyperliquid
33
import (
44
"fmt"
55
"math"
6+
7+
"github.com/ethereum/go-ethereum/signer/core/apitypes"
68
)
79

810
// IExchangeAPI is an interface for the /exchange service.
@@ -308,3 +310,24 @@ func (api *ExchangeAPI) getChainParams() (string, string) {
308310
}
309311
return "0x66eee", "Testnet"
310312
}
313+
314+
// Build bulk orders EIP712 message
315+
func (api *ExchangeAPI) BuildBulkOrdersEIP712(requests []OrderRequest, grouping Grouping) (apitypes.TypedData, error) {
316+
var wires []OrderWire
317+
for _, req := range requests {
318+
wires = append(wires, OrderRequestToWire(req, api.meta))
319+
}
320+
timestamp := GetNonce()
321+
action := OrderWiresToOrderAction(wires, grouping)
322+
srequest, err := api.BuildEIP712Message(action, timestamp)
323+
if err != nil {
324+
api.debug("Error building EIP712 message: %s", err)
325+
return apitypes.TypedData{}, err
326+
}
327+
return SignRequestToEIP712TypedData(srequest), nil
328+
}
329+
330+
// Build order EIP712 message
331+
func (api *ExchangeAPI) BuildOrderEIP712(request OrderRequest, grouping Grouping) (apitypes.TypedData, error) {
332+
return api.BuildBulkOrdersEIP712([]OrderRequest{request}, grouping)
333+
}

hyperliquid/exchange_signing.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,19 @@ func (api *ExchangeAPI) SignUserSignableAction(action any, payloadTypes []apityp
3434
}
3535

3636
func (api *ExchangeAPI) SignL1Action(action any, timestamp uint64) (byte, [32]byte, [32]byte, error) {
37-
hash, err := buildActionHash(action, "", timestamp)
37+
srequest, err := api.BuildEIP712Message(action, timestamp)
3838
if err != nil {
39-
api.debug("Error building action hash: %s", err)
39+
api.debug("Error building EIP712 message: %s", err)
4040
return 0, [32]byte{}, [32]byte{}, err
4141
}
42+
return api.Sign(srequest)
43+
}
44+
45+
func (api *ExchangeAPI) BuildEIP712Message(action any, timestamp uint64) (*SignRequest, error) {
46+
hash, err := buildActionHash(action, "", timestamp)
47+
if err != nil {
48+
return nil, err
49+
}
4250
message := buildMessage(hash.Bytes(), api.IsMainnet())
4351
srequest := &SignRequest{
4452
DomainName: "Exchange",
@@ -56,7 +64,7 @@ func (api *ExchangeAPI) SignL1Action(action any, timestamp uint64) (byte, [32]by
5664
DTypeMsg: message,
5765
IsMainNet: api.IsMainnet(),
5866
}
59-
return api.Sign(srequest)
67+
return srequest, nil
6068
}
6169

6270
func (api *ExchangeAPI) SignWithdrawAction(action WithdrawAction) (byte, [32]byte, [32]byte, error) {

hyperliquid/signature.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,7 @@ func NewSigner(manager *PKeyManager) Signer {
6464
}
6565

6666
func (signer *Signer) Sign(request *SignRequest) (byte, [32]byte, [32]byte, error) {
67-
typedData := apitypes.TypedData{
68-
Domain: request.GetDomain(),
69-
Types: request.GetTypes(),
70-
PrimaryType: request.PrimaryType,
71-
Message: request.DTypeMsg,
72-
}
73-
return signer.signInternal(typedData)
67+
return signer.signInternal(SignRequestToEIP712TypedData(request))
7468
}
7569

7670
// signInternal signs the typed data and returns the signature in VRS format
@@ -89,6 +83,15 @@ func (signer *Signer) signInternal(message apitypes.TypedData) (byte, [32]byte,
8983
return SignatureToVRS(signature)
9084
}
9185

86+
func SignRequestToEIP712TypedData(request *SignRequest) apitypes.TypedData {
87+
return apitypes.TypedData{
88+
Domain: request.GetDomain(),
89+
Types: request.GetTypes(),
90+
PrimaryType: request.PrimaryType,
91+
Message: request.DTypeMsg,
92+
}
93+
}
94+
9295
func SignatureToVRS(sig []byte) (byte, [32]byte, [32]byte, error) {
9396
var v byte
9497
var r [32]byte

0 commit comments

Comments
 (0)