Skip to content

Commit 286b188

Browse files
committed
fix: actions sigs
1 parent 3eeca35 commit 286b188

File tree

3 files changed

+130
-59
lines changed

3 files changed

+130
-59
lines changed

actions.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ type OrderAction struct {
148148

149149
// ModifyAction represents a single order modification
150150
type ModifyAction struct {
151-
Type string `json:"type,omitempty" msgpack:"type,omitempty"`
152-
Oid any `json:"oid" msgpack:"oid"`
153-
Order OrderWire `json:"order" msgpack:"order"`
151+
Type string `json:"type,omitempty" msgpack:"type,omitempty"`
152+
Oid any `json:"oid" msgpack:"oid"`
153+
Order OrderWire `json:"order" msgpack:"order"`
154154
}
155155

156156
// BatchModifyAction represents multiple order modifications

info.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ type Info struct {
2020
clientOpts []ClientOpt
2121
}
2222

23-
func NewInfo(ctx context.Context, baseURL string, skipWS bool, meta *Meta, spotMeta *SpotMeta, opts ...InfoOpt) *Info {
23+
func NewInfo(
24+
ctx context.Context,
25+
baseURL string,
26+
skipWS bool,
27+
meta *Meta,
28+
spotMeta *SpotMeta,
29+
opts ...InfoOpt,
30+
) *Info {
2431
info := &Info{
2532
coinToAsset: make(map[string]int),
2633
nameToCoin: make(map[string]string),
@@ -232,7 +239,10 @@ func (i *Info) OpenOrders(ctx context.Context, address string) ([]OpenOrder, err
232239
return result, nil
233240
}
234241

235-
func (i *Info) FrontendOpenOrders(ctx context.Context, address string) ([]FrontendOpenOrder, error) {
242+
func (i *Info) FrontendOpenOrders(
243+
ctx context.Context,
244+
address string,
245+
) ([]FrontendOpenOrder, error) {
236246
resp, err := i.client.post(ctx, "/info", map[string]any{
237247
"type": "frontendOpenOrders",
238248
"user": address,

signing.go

Lines changed: 115 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,13 @@ import (
1717
"github.com/vmihailenco/msgpack/v5"
1818
)
1919

20-
// addressToBytes converts a hex address to bytes, matching Python's address_to_bytes
2120
func addressToBytes(address string) []byte {
2221
address = strings.TrimPrefix(address, "0x")
2322
bytes, _ := hex.DecodeString(address)
2423
return bytes
2524
}
2625

27-
// actionHash implements the same logic as Python's action_hash function
2826
func actionHash(action any, vaultAddress string, nonce int64, expiresAfter *int64) []byte {
29-
// Pack action using msgpack (like Python's msgpack.packb)
3027
var buf bytes.Buffer
3128
enc := msgpack.NewEncoder(&buf)
3229
enc.SetSortMapKeys(true)
@@ -71,7 +68,6 @@ func actionHash(action any, vaultAddress string, nonce int64, expiresAfter *int6
7168
return hash
7269
}
7370

74-
// constructPhantomAgent implements the same logic as Python's construct_phantom_agent
7571
func constructPhantomAgent(hash []byte, isMainnet bool) map[string]any {
7672
source := "b" // testnet
7773
if isMainnet {
@@ -83,7 +79,6 @@ func constructPhantomAgent(hash []byte, isMainnet bool) map[string]any {
8379
}
8480
}
8581

86-
// l1Payload implements the same logic as Python's l1_payload
8782
func l1Payload(phantomAgent map[string]any) apitypes.TypedData {
8883
chainId := math.HexOrDecimal256(*big.NewInt(1337))
8984
return apitypes.TypedData{
@@ -117,7 +112,6 @@ type SignatureResult struct {
117112
V int `json:"v"`
118113
}
119114

120-
// signInner implements the same logic as Python's sign_inner
121115
func signInner(
122116
privateKey *ecdsa.PrivateKey,
123117
typedData apitypes.TypedData,
@@ -155,7 +149,6 @@ func signInner(
155149
}, nil
156150
}
157151

158-
// SignL1Action implements the same logic as Python's sign_l1_action
159152
func SignL1Action(
160153
privateKey *ecdsa.PrivateKey,
161154
action any,
@@ -177,6 +170,12 @@ func SignL1Action(
177170
return signInner(privateKey, typedData)
178171
}
179172

173+
type signUsdClassTransferAction struct {
174+
Type string `msgpack:"type"`
175+
Amount float64 `msgpack:"amount"`
176+
ToPerp bool `msgpack:"toPerp"`
177+
}
178+
180179
// SignUsdClassTransferAction signs USD class transfer action
181180
func SignUsdClassTransferAction(
182181
privateKey *ecdsa.PrivateKey,
@@ -185,15 +184,22 @@ func SignUsdClassTransferAction(
185184
timestamp int64,
186185
isMainnet bool,
187186
) (SignatureResult, error) {
188-
action := map[string]any{
189-
"type": "usdClassTransfer",
190-
"amount": amount,
191-
"toPerp": toPerp,
187+
action := signUsdClassTransferAction{
188+
Type: "usdClassTransfer",
189+
Amount: amount,
190+
ToPerp: toPerp,
192191
}
193192

194193
return SignL1Action(privateKey, action, "", timestamp, nil, isMainnet)
195194
}
196195

196+
type signSpotTransferAction struct {
197+
Type string `msgpack:"type"`
198+
Amount float64 `msgpack:"amount"`
199+
Destination string `msgpack:"destination"`
200+
Token string `msgpack:"token"`
201+
}
202+
197203
// SignSpotTransferAction signs spot transfer action
198204
func SignSpotTransferAction(
199205
privateKey *ecdsa.PrivateKey,
@@ -202,16 +208,22 @@ func SignSpotTransferAction(
202208
timestamp int64,
203209
isMainnet bool,
204210
) (SignatureResult, error) {
205-
action := map[string]any{
206-
"type": "spotTransfer",
207-
"amount": amount,
208-
"destination": destination,
209-
"token": token,
211+
action := signSpotTransferAction{
212+
Type: "spotTransfer",
213+
Amount: amount,
214+
Destination: destination,
215+
Token: token,
210216
}
211217

212218
return SignL1Action(privateKey, action, "", timestamp, nil, isMainnet)
213219
}
214220

221+
type signUsdTransferAction struct {
222+
Type string `msgpack:"type"`
223+
Amount float64 `msgpack:"amount"`
224+
Destination string `msgpack:"destination"`
225+
}
226+
215227
// SignUsdTransferAction signs USD transfer action
216228
func SignUsdTransferAction(
217229
privateKey *ecdsa.PrivateKey,
@@ -220,15 +232,23 @@ func SignUsdTransferAction(
220232
timestamp int64,
221233
isMainnet bool,
222234
) (SignatureResult, error) {
223-
action := map[string]any{
224-
"type": "usdTransfer",
225-
"amount": amount,
226-
"destination": destination,
235+
action := signUsdTransferAction{
236+
Type: "usdTransfer",
237+
Amount: amount,
238+
Destination: destination,
227239
}
228240

229241
return SignL1Action(privateKey, action, "", timestamp, nil, isMainnet)
230242
}
231243

244+
type signPerpDexClassTransferAction struct {
245+
Type string `msgpack:"type"`
246+
Dex string `msgpack:"dex"`
247+
Token string `msgpack:"token"`
248+
Amount float64 `msgpack:"amount"`
249+
ToPerp bool `msgpack:"toPerp"`
250+
}
251+
232252
// SignPerpDexClassTransferAction signs perp dex class transfer action
233253
func SignPerpDexClassTransferAction(
234254
privateKey *ecdsa.PrivateKey,
@@ -238,17 +258,24 @@ func SignPerpDexClassTransferAction(
238258
timestamp int64,
239259
isMainnet bool,
240260
) (SignatureResult, error) {
241-
action := map[string]any{
242-
"type": "perpDexClassTransfer",
243-
"dex": dex,
244-
"token": token,
245-
"amount": amount,
246-
"toPerp": toPerp,
261+
action := signPerpDexClassTransferAction{
262+
Type: "perpDexClassTransfer",
263+
Dex: dex,
264+
Token: token,
265+
Amount: amount,
266+
ToPerp: toPerp,
247267
}
248268

249269
return SignL1Action(privateKey, action, "", timestamp, nil, isMainnet)
250270
}
251271

272+
type signTokenDelegateAction struct {
273+
Type string `msgpack:"type"`
274+
Token string `msgpack:"token"`
275+
Amount float64 `msgpack:"amount"`
276+
ValidatorAddress string `msgpack:"validatorAddress"`
277+
}
278+
252279
// SignTokenDelegateAction signs token delegate action
253280
func SignTokenDelegateAction(
254281
privateKey *ecdsa.PrivateKey,
@@ -258,16 +285,23 @@ func SignTokenDelegateAction(
258285
timestamp int64,
259286
isMainnet bool,
260287
) (SignatureResult, error) {
261-
action := map[string]any{
262-
"type": "tokenDelegate",
263-
"token": token,
264-
"amount": amount,
265-
"validatorAddress": validatorAddress,
288+
action := signTokenDelegateAction{
289+
Type: "tokenDelegate",
290+
Token: token,
291+
Amount: amount,
292+
ValidatorAddress: validatorAddress,
266293
}
267294

268295
return SignL1Action(privateKey, action, "", timestamp, nil, isMainnet)
269296
}
270297

298+
type signWithdrawFromBridgeAction struct {
299+
Type string `msgpack:"type"`
300+
Destination string `msgpack:"destination"`
301+
Amount float64 `msgpack:"amount"`
302+
Fee float64 `msgpack:"fee"`
303+
}
304+
271305
// SignWithdrawFromBridgeAction signs withdraw from bridge action
272306
func SignWithdrawFromBridgeAction(
273307
privateKey *ecdsa.PrivateKey,
@@ -276,32 +310,46 @@ func SignWithdrawFromBridgeAction(
276310
timestamp int64,
277311
isMainnet bool,
278312
) (SignatureResult, error) {
279-
action := map[string]any{
280-
"type": "withdrawFromBridge",
281-
"destination": destination,
282-
"amount": amount,
283-
"fee": fee,
313+
action := signWithdrawFromBridgeAction{
314+
Type: "withdrawFromBridge",
315+
Destination: destination,
316+
Amount: amount,
317+
Fee: fee,
284318
}
285319

286320
return SignL1Action(privateKey, action, "", timestamp, nil, isMainnet)
287321
}
288322

323+
type signAgent struct {
324+
Type string `msgpack:"type"`
325+
AgentAddress string `msgpack:"agentAddress"`
326+
AgentName string `msgpack:"agentName"`
327+
}
328+
289329
// SignAgent signs agent approval action
290330
func SignAgent(
291331
privateKey *ecdsa.PrivateKey,
292332
agentAddress, agentName string,
293333
timestamp int64,
294334
isMainnet bool,
295335
) (SignatureResult, error) {
296-
action := map[string]any{
297-
"type": "approveAgent",
298-
"agentAddress": agentAddress,
299-
"agentName": agentName,
336+
action := signAgent{
337+
Type: "approveAgent",
338+
AgentAddress: agentAddress,
339+
AgentName: agentName,
300340
}
301341

302342
return SignL1Action(privateKey, action, "", timestamp, nil, isMainnet)
303343
}
304344

345+
type signApproveBuilderFee struct {
346+
Type string `msgpack:"type"`
347+
// BuilderAddress is the address of the builder
348+
BuilderAddress string `msgpack:"builderAddress"`
349+
// MaxFeeRate is the maximum fee rate the user is willing to pay
350+
MaxFeeRate float64 `msgpack:"maxFeeRate"`
351+
}
352+
305353
// SignApproveBuilderFee signs approve builder fee action
306354
func SignApproveBuilderFee(
307355
privateKey *ecdsa.PrivateKey,
@@ -310,15 +358,21 @@ func SignApproveBuilderFee(
310358
timestamp int64,
311359
isMainnet bool,
312360
) (SignatureResult, error) {
313-
action := map[string]any{
314-
"type": "approveBuilderFee",
315-
"builderAddress": builderAddress,
316-
"maxFeeRate": maxFeeRate,
361+
action := signApproveBuilderFee{
362+
Type: "approveBuilderFee",
363+
BuilderAddress: builderAddress,
364+
MaxFeeRate: maxFeeRate,
317365
}
318366

319367
return SignL1Action(privateKey, action, "", timestamp, nil, isMainnet)
320368
}
321369

370+
type signConvertToMultiSigUserAction struct {
371+
Type string `msgpack:"type"`
372+
Signers []string `msgpack:"signers"`
373+
Threshold int `msgpack:"threshold"`
374+
}
375+
322376
// SignConvertToMultiSigUserAction signs convert to multi-sig user action
323377
func SignConvertToMultiSigUserAction(
324378
privateKey *ecdsa.PrivateKey,
@@ -327,15 +381,22 @@ func SignConvertToMultiSigUserAction(
327381
timestamp int64,
328382
isMainnet bool,
329383
) (SignatureResult, error) {
330-
action := map[string]any{
331-
"type": "convertToMultiSigUser",
332-
"signers": signers,
333-
"threshold": threshold,
384+
action := signConvertToMultiSigUserAction{
385+
Type: "convertToMultiSigUser",
386+
Signers: signers,
387+
Threshold: threshold,
334388
}
335389

336390
return SignL1Action(privateKey, action, "", timestamp, nil, isMainnet)
337391
}
338392

393+
type signMultiSigAction struct {
394+
Type string `msgpack:"type"`
395+
Action map[string]any `msgpack:"action"`
396+
Signers []string `msgpack:"signers"`
397+
Signatures []string `msgpack:"signatures"`
398+
}
399+
339400
// SignMultiSigAction signs multi-signature action
340401
func SignMultiSigAction(
341402
privateKey *ecdsa.PrivateKey,
@@ -345,17 +406,17 @@ func SignMultiSigAction(
345406
timestamp int64,
346407
isMainnet bool,
347408
) (SignatureResult, error) {
348-
action := map[string]any{
349-
"type": "multiSig",
350-
"action": innerAction,
351-
"signers": signers,
352-
"signatures": signatures,
409+
action := signMultiSigAction{
410+
Type: "multiSig",
411+
Action: innerAction,
412+
Signers: signers,
413+
Signatures: signatures,
353414
}
354415

355416
return SignL1Action(privateKey, action, "", timestamp, nil, isMainnet)
356417
}
357418

358-
// Utility function to convert float to USD integer representation
419+
// FloatToUsdInt converts float to USD integer representation
359420
func FloatToUsdInt(value float64) int {
360421
// Convert float USD to integer representation (assuming 6 decimals for USDC)
361422
return int(value * 1e6)

0 commit comments

Comments
 (0)