@@ -4,15 +4,17 @@ import axios from 'axios'
44import type { RelayClient } from '../client.js'
55import { LogLevel } from './logger.js'
66
7- export function prepareHyperliquidSignatureStep (
8- steps : Execute [ 'steps' ] ,
7+ function prepareHyperliquidSignatureStep (
8+ step : Execute [ 'steps' ] [ 0 ] ,
99 chainId : number
1010) {
11- const items = steps [ 0 ] ?. items
12- const amount = items [ 0 ] ?. data ?. action ?. parameters ?. amount
13- 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+
1416 const signatureStep = {
15- id : 'sign ' as any ,
17+ id : 'hyperliquid-signature ' as any ,
1618 action : 'Confirm transaction in your wallet' ,
1719 description : `Sign a message to confirm the transaction` ,
1820 kind : 'signature' as const ,
@@ -29,44 +31,36 @@ export function prepareHyperliquidSignatureStep(
2931 verifyingContract : '0x0000000000000000000000000000000000000000'
3032 } ,
3133 types : {
32- 'HyperliquidTransaction:UsdSend' : [
33- { name : 'hyperliquidChain' , type : 'string' } ,
34- { name : 'destination' , type : 'string' } ,
35- { name : 'amount' , type : 'string' } ,
36- { name : 'time' , type : 'uint64' }
37- ] ,
34+ ...eip712Types ,
3835 EIP712Domain : [
3936 { name : 'name' , type : 'string' } ,
4037 { name : 'version' , type : 'string' } ,
4138 { name : 'chainId' , type : 'uint256' } ,
4239 { name : 'verifyingContract' , type : 'address' }
4340 ]
4441 } ,
45- primaryType : 'HyperliquidTransaction:UsdSend' ,
42+ primaryType : eip712PrimaryType ,
4643 value : {
47- type : 'usdSend' ,
48- signatureChainId : `0x${ chainId . toString ( 16 ) } ` ,
49- hyperliquidChain : 'Mainnet' ,
50- destination : destination ?. toLowerCase ( ) ,
51- amount,
52- time : new Date ( ) . getTime ( )
44+ ...action . parameters ,
45+ type : action . type ,
46+ signatureChainId : `0x${ chainId . toString ( 16 ) } `
5347 }
5448 }
5549 } ,
5650 check : {
57- endpoint : `/intents/status?requestId=${ steps [ 0 ] ?. requestId } ` ,
51+ endpoint : `/intents/status?requestId=${ step ?. requestId } ` ,
5852 method : 'GET'
5953 }
6054 }
6155 ] ,
62- requestId : steps [ 0 ] ?. requestId ,
63- depositAddress : steps [ 0 ] ?. depositAddress
56+ requestId : step ?. requestId ,
57+ depositAddress : step ?. depositAddress
6458 }
6559
6660 return signatureStep
6761}
6862
69- export async function sendUsd (
63+ export async function postHyperliquidSignature (
7064 client : RelayClient ,
7165 signature : string ,
7266 stepItem : Execute [ 'steps' ] [ 0 ] [ 'items' ] [ 0 ]
@@ -76,22 +70,18 @@ export async function sendUsd(
7670 LogLevel . Verbose
7771 )
7872 const { r, s, v } = parseSignature ( signature as `0x${string } `)
79- const currentTime = stepItem ?. data ?. sign ?. value ?. time ?? new Date ( ) . getTime ( )
73+
74+ const action = stepItem ?. data ?. sign ?. value
75+ const nonce = action ?. nonce ?? action ?. time
76+
8077 const res = await axios . post ( 'https://api.hyperliquid.xyz/exchange' , {
8178 signature : {
8279 r,
8380 s,
8481 v : Number ( v ?? 0n )
8582 } ,
86- nonce : currentTime ,
87- action : {
88- type : stepItem ?. data ?. sign ?. value ?. type ,
89- signatureChainId : `0x${ stepItem ?. data ?. sign ?. domain ?. chainId ?. toString ( 16 ) } ` ,
90- hyperliquidChain : 'Mainnet' ,
91- destination : stepItem ?. data ?. sign ?. value ?. destination ?. toLowerCase ( ) ,
92- amount : stepItem ?. data ?. sign ?. value ?. amount ,
93- time : currentTime
94- }
83+ nonce,
84+ action
9585 } )
9686 if (
9787 ! res ||
@@ -107,3 +97,55 @@ export async function sendUsd(
10797 )
10898 return res . data
10999}
100+
101+ function updateHyperliquidSignatureChainId (
102+ step : Execute [ 'steps' ] [ 0 ] ,
103+ activeWalletChainId : number
104+ ) : Execute [ 'steps' ] [ 0 ] {
105+ return {
106+ ...step ,
107+ items : step . items ?. map ( ( item ) => ( {
108+ ...item ,
109+ data : {
110+ ...item . data ,
111+ sign : {
112+ ...item . data . sign ,
113+ domain : {
114+ ...item . data . sign . domain ,
115+ chainId : activeWalletChainId
116+ }
117+ } ,
118+ ...( item . data . post && {
119+ post : {
120+ ...item . data . post ,
121+ body : {
122+ ...item . data . post . body ,
123+ signatureChainId : activeWalletChainId
124+ }
125+ }
126+ } )
127+ }
128+ } ) )
129+ }
130+ }
131+
132+ export function prepareHyperliquidSteps (
133+ steps : Execute [ 'steps' ] ,
134+ activeWalletChainId : number
135+ ) : Execute [ 'steps' ] {
136+ return steps . map ( ( step ) => {
137+ // Skip steps that have already been converted (id is set to 'sign' by prepareHyperliquidSignatureStep)
138+ if ( ( step . id as string ) === 'hyperliquid-signature' ) {
139+ return step
140+ }
141+ // Update signature steps to use the active wallet chain ID
142+ if ( step . kind === 'signature' ) {
143+ return updateHyperliquidSignatureChainId ( step , activeWalletChainId )
144+ }
145+ // Convert transaction steps to Hyperliquid signature steps
146+ if ( step . kind === 'transaction' ) {
147+ return prepareHyperliquidSignatureStep ( step , activeWalletChainId )
148+ }
149+ return step
150+ } )
151+ }
0 commit comments