Skip to content

Commit 5634400

Browse files
committed
Improve hyperliquid logic
1 parent 3f0af9a commit 5634400

File tree

2 files changed

+74
-83
lines changed

2 files changed

+74
-83
lines changed

packages/sdk/src/utils/executeSteps/signatureStep.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { AxiosRequestConfig } from 'axios'
99
import { LogLevel } from '../logger.js'
1010
import type { RelayClient } from '../../client.js'
1111
import type { SetStateData } from './index.js'
12-
import { sendUsd } from '../hyperliquid.js'
12+
import { postHyperliquidSignature } from '../hyperliquid.js'
1313

1414
/**
1515
* Handles the execution of a signature step item, including signing, posting, and validation.
@@ -68,8 +68,12 @@ export async function handleSignatureStepItem({
6868
}
6969
}
7070

71-
if (chain.id === 1337 && signature && step?.id === ('sign' as any)) {
72-
await sendUsd(client, signature, stepItem)
71+
if (
72+
chain.id === 1337 &&
73+
signature &&
74+
step?.id === ('hyperliquid-signature' as any)
75+
) {
76+
await postHyperliquidSignature(client, signature, stepItem)
7377
}
7478

7579
if (postData) {

packages/sdk/src/utils/hyperliquid.ts

Lines changed: 67 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,17 @@ import axios from 'axios'
44
import type { RelayClient } from '../client.js'
55
import { LogLevel } from './logger.js'
66

7-
function updateHyperliquidSignatureChainId(
8-
step: Execute['steps'][0],
9-
activeWalletChainId: number
10-
): Execute['steps'][0] {
11-
return {
12-
...step,
13-
items: step.items?.map((item) => ({
14-
...item,
15-
data: {
16-
...item.data,
17-
sign: {
18-
...item.data.sign,
19-
domain: {
20-
...item.data.sign.domain,
21-
chainId: activeWalletChainId
22-
}
23-
},
24-
...(item.data.post && {
25-
post: {
26-
...item.data.post,
27-
body: {
28-
...item.data.post.body,
29-
signatureChainId: activeWalletChainId
30-
}
31-
}
32-
})
33-
}
34-
}))
35-
}
36-
}
37-
387
function prepareHyperliquidSignatureStep(
398
step: Execute['steps'][0],
409
chainId: number
4110
) {
42-
const items = step?.items
43-
const amount = items?.[0]?.data?.action?.parameters?.amount
44-
const destination = items?.[0]?.data?.action?.parameters?.destination
11+
const stepItem = step?.items?.[0]
12+
const action = stepItem?.data?.action
13+
const eip712Types = stepItem?.data?.eip712Types
14+
const eip712PrimaryType = stepItem?.data?.eip712PrimaryType
15+
4516
const signatureStep = {
46-
id: 'sign' as any,
17+
id: 'hyperliquid-signature' as any,
4718
action: 'Confirm transaction in your wallet',
4819
description: `Sign a message to confirm the transaction`,
4920
kind: 'signature' as const,
@@ -60,27 +31,19 @@ function prepareHyperliquidSignatureStep(
6031
verifyingContract: '0x0000000000000000000000000000000000000000'
6132
},
6233
types: {
63-
'HyperliquidTransaction:UsdSend': [
64-
{ name: 'hyperliquidChain', type: 'string' },
65-
{ name: 'destination', type: 'string' },
66-
{ name: 'amount', type: 'string' },
67-
{ name: 'time', type: 'uint64' }
68-
],
34+
...eip712Types,
6935
EIP712Domain: [
7036
{ name: 'name', type: 'string' },
7137
{ name: 'version', type: 'string' },
7238
{ name: 'chainId', type: 'uint256' },
7339
{ name: 'verifyingContract', type: 'address' }
7440
]
7541
},
76-
primaryType: 'HyperliquidTransaction:UsdSend',
42+
primaryType: eip712PrimaryType,
7743
value: {
78-
type: 'usdSend',
79-
signatureChainId: `0x${chainId.toString(16)}`,
80-
hyperliquidChain: 'Mainnet',
81-
destination: destination?.toLowerCase(),
82-
amount,
83-
time: items?.[0]?.data?.nonce
44+
...action.parameters,
45+
type: action.type,
46+
signatureChainId: `0x${chainId.toString(16)}`
8447
}
8548
}
8649
},
@@ -97,28 +60,7 @@ function prepareHyperliquidSignatureStep(
9760
return signatureStep
9861
}
9962

100-
export function prepareHyperliquidSteps(
101-
steps: Execute['steps'],
102-
activeWalletChainId: number
103-
): Execute['steps'] {
104-
return steps.map((step) => {
105-
// Skip steps that have already been converted (id is set to 'sign' by prepareHyperliquidSignatureStep)
106-
if ((step.id as string) === 'sign') {
107-
return step
108-
}
109-
// Update signature steps to use the active wallet chain ID
110-
if (step.kind === 'signature') {
111-
return updateHyperliquidSignatureChainId(step, activeWalletChainId)
112-
}
113-
// Convert transaction steps to Hyperliquid signature steps
114-
if (step.kind === 'transaction') {
115-
return prepareHyperliquidSignatureStep(step, activeWalletChainId)
116-
}
117-
return step
118-
})
119-
}
120-
121-
export async function sendUsd(
63+
export async function postHyperliquidSignature(
12264
client: RelayClient,
12365
signature: string,
12466
stepItem: Execute['steps'][0]['items'][0]
@@ -129,23 +71,16 @@ export async function sendUsd(
12971
)
13072
const { r, s, v } = parseSignature(signature as `0x${string}`)
13173

132-
const nonce = stepItem?.data?.sign?.value?.time
74+
const action = stepItem?.data?.sign?.value
13375

13476
const res = await axios.post('https://api.hyperliquid.xyz/exchange', {
13577
signature: {
13678
r,
13779
s,
13880
v: Number(v ?? 0n)
13981
},
140-
nonce: nonce,
141-
action: {
142-
type: stepItem?.data?.sign?.value?.type,
143-
signatureChainId: `0x${stepItem?.data?.sign?.domain?.chainId?.toString(16)}`,
144-
hyperliquidChain: 'Mainnet',
145-
destination: stepItem?.data?.sign?.value?.destination?.toLowerCase(),
146-
amount: stepItem?.data?.sign?.value?.amount,
147-
time: nonce
148-
}
82+
nonce: action?.nonce,
83+
action
14984
})
15085
if (
15186
!res ||
@@ -161,3 +96,55 @@ export async function sendUsd(
16196
)
16297
return res.data
16398
}
99+
100+
function updateHyperliquidSignatureChainId(
101+
step: Execute['steps'][0],
102+
activeWalletChainId: number
103+
): Execute['steps'][0] {
104+
return {
105+
...step,
106+
items: step.items?.map((item) => ({
107+
...item,
108+
data: {
109+
...item.data,
110+
sign: {
111+
...item.data.sign,
112+
domain: {
113+
...item.data.sign.domain,
114+
chainId: activeWalletChainId
115+
}
116+
},
117+
...(item.data.post && {
118+
post: {
119+
...item.data.post,
120+
body: {
121+
...item.data.post.body,
122+
signatureChainId: activeWalletChainId
123+
}
124+
}
125+
})
126+
}
127+
}))
128+
}
129+
}
130+
131+
export function prepareHyperliquidSteps(
132+
steps: Execute['steps'],
133+
activeWalletChainId: number
134+
): Execute['steps'] {
135+
return steps.map((step) => {
136+
// Skip steps that have already been converted (id is set to 'sign' by prepareHyperliquidSignatureStep)
137+
if ((step.id as string) === 'hyperliquid-signature') {
138+
return step
139+
}
140+
// Update signature steps to use the active wallet chain ID
141+
if (step.kind === 'signature') {
142+
return updateHyperliquidSignatureChainId(step, activeWalletChainId)
143+
}
144+
// Convert transaction steps to Hyperliquid signature steps
145+
if (step.kind === 'transaction') {
146+
return prepareHyperliquidSignatureStep(step, activeWalletChainId)
147+
}
148+
return step
149+
})
150+
}

0 commit comments

Comments
 (0)