@@ -142,12 +142,39 @@ export type DecodedBitcoinVmWithdrawal = {
142142 } ;
143143} ;
144144
145+ export type DecodedHyperliquidVmWithdrawal = {
146+ vmType : "hyperliquid-vm" ;
147+ withdrawal : {
148+ txType : number ;
149+ parameters :
150+ | {
151+ type : "UsdSend" ;
152+ hyperliquidChain : string ;
153+ destination : string ;
154+ amount : string ;
155+ time : string ;
156+ }
157+ | {
158+ type : "SendAsset" ;
159+ hyperliquidChain : string ;
160+ destination : string ;
161+ sourceDex : string ;
162+ destinationDex : string ;
163+ token : string ;
164+ amount : string ;
165+ fromSubAccount : string ;
166+ nonce : string ;
167+ } ;
168+ } ;
169+ } ;
170+
145171type DecodedWithdrawal =
146172 | DecodedEthereumVmWithdrawal
147173 | DecodedSolanaVmWithdrawal
148174 | DecodedSuiVmWithdrawal
149175 | DecodedBitcoinVmWithdrawal
150- | DecodedTronVmWithdrawal ;
176+ | DecodedTronVmWithdrawal
177+ | DecodedHyperliquidVmWithdrawal ;
151178
152179export const encodeWithdrawal = (
153180 decodedWithdrawal : DecodedWithdrawal
@@ -247,6 +274,71 @@ export const encodeWithdrawal = (
247274 return "0x" + decodedWithdrawal . withdrawal . psbt ;
248275 }
249276
277+ case "hyperliquid-vm" : {
278+ const { txType, parameters } = decodedWithdrawal . withdrawal ;
279+
280+ let encodedParameters : string ;
281+ switch ( parameters . type ) {
282+ case "UsdSend" : {
283+ encodedParameters = encodeAbiParameters (
284+ parseAbiParameters ( [
285+ "(string hyperliquidChain, string destination, string amount, uint64 time)" ,
286+ ] ) ,
287+ [
288+ {
289+ hyperliquidChain : parameters . hyperliquidChain ,
290+ destination : parameters . destination ,
291+ amount : parameters . amount ,
292+ time : BigInt ( parameters . time ) ,
293+ } ,
294+ ]
295+ ) ;
296+
297+ break ;
298+ }
299+
300+ case "SendAsset" : {
301+ encodedParameters = encodeAbiParameters (
302+ parseAbiParameters ( [
303+ "(string hyperliquidChain, string destination, string sourceDex, string destinationDex, string token, string amount, string fromSubAccount, uint64 nonce)" ,
304+ ] ) ,
305+ [
306+ {
307+ hyperliquidChain : parameters . hyperliquidChain ,
308+ destination : parameters . destination ,
309+ sourceDex : parameters . sourceDex ,
310+ destinationDex : parameters . destinationDex ,
311+ token : parameters . token ,
312+ amount : parameters . amount ,
313+ fromSubAccount : parameters . fromSubAccount ,
314+ nonce : BigInt ( parameters . nonce ) ,
315+ } ,
316+ ]
317+ ) ;
318+
319+ break ;
320+ }
321+
322+ default : {
323+ throw new Error (
324+ `Unsupported Hyperliquid transaction type ${
325+ ( parameters as any ) . type
326+ } `
327+ ) ;
328+ }
329+ }
330+
331+ return encodeAbiParameters (
332+ parseAbiParameters ( [ "(uint8 txType, bytes parameters)" ] ) ,
333+ [
334+ {
335+ txType,
336+ parameters : encodedParameters as Hex ,
337+ } ,
338+ ]
339+ ) ;
340+ }
341+
250342 default :
251343 throw new Error ( "Unsupported vm type" ) ;
252344 }
@@ -366,6 +458,89 @@ export const decodeWithdrawal = (
366458 } ;
367459 }
368460
461+ case "hyperliquid-vm" : {
462+ const result = decodeAbiParameters (
463+ parseAbiParameters ( [ "(uint8 txType, bytes parameters)" ] ) ,
464+ encodedWithdrawal as Hex
465+ ) ;
466+
467+ const { txType, parameters } = result [ 0 ] ;
468+
469+ switch ( txType ) {
470+ case 0 : {
471+ // UsdSend
472+ const paramResult = decodeAbiParameters (
473+ parseAbiParameters ( [
474+ "(string hyperliquidChain, string destination, string amount, uint64 time)" ,
475+ ] ) ,
476+ parameters
477+ ) ;
478+
479+ const { hyperliquidChain, destination, amount, time } =
480+ paramResult [ 0 ] ;
481+
482+ return {
483+ vmType : "hyperliquid-vm" ,
484+ withdrawal : {
485+ txType : Number ( txType ) ,
486+ parameters : {
487+ type : "UsdSend" as const ,
488+ hyperliquidChain,
489+ destination,
490+ amount,
491+ time : time . toString ( ) ,
492+ } ,
493+ } ,
494+ } ;
495+ }
496+
497+ case 1 : {
498+ // SendAsset
499+ const paramResult = decodeAbiParameters (
500+ parseAbiParameters ( [
501+ "(string hyperliquidChain, string destination, string sourceDex, string destinationDex, string token, string amount, string fromSubAccount, uint64 nonce)" ,
502+ ] ) ,
503+ parameters
504+ ) ;
505+
506+ const {
507+ hyperliquidChain,
508+ destination,
509+ sourceDex,
510+ destinationDex,
511+ token,
512+ amount,
513+ fromSubAccount,
514+ nonce,
515+ } = paramResult [ 0 ] ;
516+
517+ return {
518+ vmType : "hyperliquid-vm" ,
519+ withdrawal : {
520+ txType : Number ( txType ) ,
521+ parameters : {
522+ type : "SendAsset" as const ,
523+ hyperliquidChain,
524+ destination,
525+ sourceDex,
526+ destinationDex,
527+ token,
528+ amount,
529+ fromSubAccount,
530+ nonce : nonce . toString ( ) ,
531+ } ,
532+ } ,
533+ } ;
534+ }
535+
536+ default : {
537+ throw new Error (
538+ `Unsupported Hyperliquid transaction type: ${ txType } `
539+ ) ;
540+ }
541+ }
542+ }
543+
369544 default :
370545 throw new Error ( "Unsupported vm type" ) ;
371546 }
@@ -480,6 +655,68 @@ export const getDecodedWithdrawalId = (
480655 return "0x" + sha256 . create ( ) . update ( encodedWithdrawal ) . hex ( ) ;
481656 }
482657
658+ case "hyperliquid-vm" : {
659+ const { parameters } = decodedWithdrawal . withdrawal ;
660+
661+ switch ( parameters . type ) {
662+ case "UsdSend" : {
663+ return hashStruct ( {
664+ types : {
665+ "HyperliquidTransaction:UsdSend" : [
666+ { name : "hyperliquidChain" , type : "string" } ,
667+ { name : "destination" , type : "string" } ,
668+ { name : "amount" , type : "string" } ,
669+ { name : "time" , type : "uint64" } ,
670+ ] ,
671+ } ,
672+ primaryType : "HyperliquidTransaction:UsdSend" ,
673+ data : {
674+ hyperliquidChain : parameters . hyperliquidChain ,
675+ destination : parameters . destination ,
676+ amount : parameters . amount ,
677+ time : parameters . time ,
678+ } ,
679+ } ) ;
680+ }
681+
682+ case "SendAsset" : {
683+ return hashStruct ( {
684+ types : {
685+ "HyperliquidTransaction:SendAsset" : [
686+ { name : "hyperliquidChain" , type : "string" } ,
687+ { name : "destination" , type : "string" } ,
688+ { name : "sourceDex" , type : "string" } ,
689+ { name : "destinationDex" , type : "string" } ,
690+ { name : "token" , type : "string" } ,
691+ { name : "amount" , type : "string" } ,
692+ { name : "fromSubAccount" , type : "string" } ,
693+ { name : "nonce" , type : "uint64" } ,
694+ ] ,
695+ } ,
696+ primaryType : "HyperliquidTransaction:SendAsset" ,
697+ data : {
698+ hyperliquidChain : parameters . hyperliquidChain ,
699+ destination : parameters . destination ,
700+ sourceDex : parameters . sourceDex ,
701+ destinationDex : parameters . destinationDex ,
702+ token : parameters . token ,
703+ amount : parameters . amount ,
704+ fromSubAccount : parameters . fromSubAccount ,
705+ nonce : parameters . nonce ,
706+ } ,
707+ } ) ;
708+ }
709+
710+ default : {
711+ throw new Error (
712+ `Unsupported Hyperliquid transaction type ${
713+ ( parameters as any ) . type
714+ } `
715+ ) ;
716+ }
717+ }
718+ }
719+
483720 default :
484721 throw new Error ( "Unsupported vm type" ) ;
485722 }
@@ -514,5 +751,39 @@ export const getDecodedWithdrawalCurrency = (
514751 case "sui-vm" : {
515752 return decodedWithdrawal . withdrawal . coinType ;
516753 }
754+
755+ case "hyperliquid-vm" : {
756+ const { parameters } = decodedWithdrawal . withdrawal ;
757+
758+ switch ( parameters . type ) {
759+ case "UsdSend" : {
760+ return getVmTypeNativeCurrency ( decodedWithdrawal . vmType ) ;
761+ }
762+
763+ case "SendAsset" : {
764+ const SPOT_USDC = "0x6d1e7cde53ba9467b783cb7c530ce054" ;
765+
766+ const tokenAddress = parameters . token . split ( ":" ) [ 1 ] . toLowerCase ( ) ;
767+ const tokenDex = parameters . sourceDex ;
768+ if ( tokenDex === "" && tokenAddress !== SPOT_USDC ) {
769+ throw new Error ( "Only USDC is supported as a Perps token" ) ;
770+ }
771+
772+ return tokenDex === "spot"
773+ ? tokenAddress . toLowerCase ( )
774+ : tokenDex === ""
775+ ? getVmTypeNativeCurrency ( decodedWithdrawal . vmType )
776+ : tokenAddress . toLowerCase ( ) +
777+ Buffer . from ( tokenDex , "ascii" ) . toString ( "hex" ) ;
778+ }
779+
780+ default :
781+ throw new Error (
782+ `Unsupported Hyperliquid transaction type ${
783+ ( parameters as any ) . type
784+ } `
785+ ) ;
786+ }
787+ }
517788 }
518789} ;
0 commit comments