@@ -5,7 +5,7 @@ import type {
55 SignatureStepItem
66} from '../types/index.js'
77import { pollUntilHasData , pollUntilOk } from './pollApi.js'
8- import { axios } from '../utils/index.js'
8+ import { axios , prepareHyperliquidSignatureStep } from '../utils/index.js'
99import type { AxiosRequestConfig } from 'axios'
1010import { getClient } from '../client.js'
1111import { LogLevel } from '../utils/logger.js'
@@ -14,6 +14,7 @@ import {
1414 canBatchTransactions ,
1515 prepareBatchTransaction
1616} from './prepareBatchTransaction.js'
17+ import { sendUsd } from './hyperliquid.js'
1718
1819export type SetStateData = Pick <
1920 Execute ,
@@ -91,6 +92,20 @@ export async function executeSteps(
9192 }
9293 }
9394
95+ //Check if Hyperliquid and if so, rewrite steps to become a signature step
96+ if (
97+ chainId === 1337 &&
98+ json . steps [ 0 ] &&
99+ ( json . steps [ 0 ] . id as any ) !== 'sign'
100+ ) {
101+ const activeWalletChainId = await wallet ?. getChainId ( )
102+ const signatureStep = prepareHyperliquidSignatureStep (
103+ json . steps ,
104+ activeWalletChainId
105+ )
106+ json . steps = [ signatureStep ]
107+ }
108+
94109 // Update state on first call or recursion
95110 setState ( {
96111 steps : [ ...json ?. steps ] ,
@@ -345,6 +360,11 @@ export async function executeSteps(
345360 }
346361 }
347362
363+ //Special Logic for Hyperliquid to send signature
364+ if ( chainId === 1337 && signature ) {
365+ await sendUsd ( client , signature , stepItem )
366+ }
367+
348368 if ( postData ) {
349369 client . log ( [ 'Execute Steps: Posting order' ] , LogLevel . Verbose )
350370 stepItem . progressState = 'posting'
@@ -405,86 +425,6 @@ export async function executeSteps(
405425 break
406426 }
407427
408- // If check, poll check until validated
409- if ( stepItem ?. check ) {
410- stepItem . progressState = 'validating'
411- setState ( {
412- steps : [ ...json . steps ] ,
413- fees : { ...json ?. fees } ,
414- breakdown : json ?. breakdown ,
415- details : json ?. details
416- } )
417- stepItem . isValidatingSignature = true
418- setState ( {
419- steps : [ ...json ?. steps ] ,
420- fees : { ...json ?. fees } ,
421- breakdown : json ?. breakdown ,
422- details : json ?. details
423- } )
424-
425- await pollUntilOk (
426- {
427- url : `${ request . baseURL } ${ stepItem ?. check . endpoint } ` ,
428- method : stepItem ?. check . method ,
429- headers
430- } ,
431- ( res ) => {
432- client . log (
433- [
434- `Execute Steps: Polling execute status to check if indexed` ,
435- res
436- ] ,
437- LogLevel . Verbose
438- )
439-
440- //set status
441- if (
442- res ?. data ?. status === 'success' &&
443- res ?. data ?. txHashes
444- ) {
445- const chainTxHashes : NonNullable <
446- Execute [ 'steps' ] [ 0 ] [ 'items' ]
447- > [ 0 ] [ 'txHashes' ] = res . data ?. txHashes ?. map (
448- ( hash : string ) => {
449- return {
450- txHash : hash ,
451- chainId :
452- res . data . destinationChainId ?? chain ?. id
453- }
454- }
455- )
456-
457- if ( res ?. data ?. inTxHashes ) {
458- const chainInTxHashes : NonNullable <
459- Execute [ 'steps' ] [ 0 ] [ 'items' ]
460- > [ 0 ] [ 'txHashes' ] = res . data ?. inTxHashes ?. map (
461- ( hash : string ) => {
462- return {
463- txHash : hash ,
464- chainId : chain ?. id ?? res . data . originChainId
465- }
466- }
467- )
468- stepItem . internalTxHashes = chainInTxHashes
469- }
470- stepItem . txHashes = chainTxHashes
471-
472- return true
473- } else if ( res ?. data ?. status === 'failure' ) {
474- throw Error (
475- res ?. data ?. details || 'Transaction failed'
476- )
477- } else if ( res ?. data ?. status === 'delayed' ) {
478- stepItem . progressState = 'validating_delayed'
479- }
480- return false
481- } ,
482- maximumAttempts ,
483- 0 ,
484- pollingInterval
485- )
486- }
487-
488428 if ( res . status > 299 || res . status < 200 ) throw res . data
489429
490430 if ( res . data . results ) {
@@ -509,6 +449,87 @@ export async function executeSteps(
509449 }
510450 }
511451
452+ // If check, poll check until validated
453+ if ( stepItem ?. check ) {
454+ stepItem . progressState = 'validating'
455+ setState ( {
456+ steps : [ ...json . steps ] ,
457+ fees : { ...json ?. fees } ,
458+ breakdown : json ?. breakdown ,
459+ details : json ?. details
460+ } )
461+ stepItem . isValidatingSignature = true
462+ setState ( {
463+ steps : [ ...json ?. steps ] ,
464+ fees : { ...json ?. fees } ,
465+ breakdown : json ?. breakdown ,
466+ details : json ?. details
467+ } )
468+
469+ const headers = {
470+ 'Content-Type' : 'application/json'
471+ }
472+
473+ await pollUntilOk (
474+ {
475+ url : `${ request . baseURL } ${ stepItem ?. check . endpoint } ` ,
476+ method : stepItem ?. check . method ,
477+ headers
478+ } ,
479+ ( res ) => {
480+ client . log (
481+ [
482+ `Execute Steps: Polling execute status to check if indexed` ,
483+ res
484+ ] ,
485+ LogLevel . Verbose
486+ )
487+
488+ //set status
489+ if (
490+ res ?. data ?. status === 'success' &&
491+ res ?. data ?. txHashes
492+ ) {
493+ const chainTxHashes : NonNullable <
494+ Execute [ 'steps' ] [ 0 ] [ 'items' ]
495+ > [ 0 ] [ 'txHashes' ] = res . data ?. txHashes ?. map (
496+ ( hash : string ) => {
497+ return {
498+ txHash : hash ,
499+ chainId : res . data . destinationChainId ?? chain ?. id
500+ }
501+ }
502+ )
503+
504+ if ( res ?. data ?. inTxHashes ) {
505+ const chainInTxHashes : NonNullable <
506+ Execute [ 'steps' ] [ 0 ] [ 'items' ]
507+ > [ 0 ] [ 'txHashes' ] = res . data ?. inTxHashes ?. map (
508+ ( hash : string ) => {
509+ return {
510+ txHash : hash ,
511+ chainId : chain ?. id ?? res . data . originChainId
512+ }
513+ }
514+ )
515+ stepItem . internalTxHashes = chainInTxHashes
516+ }
517+ stepItem . txHashes = chainTxHashes
518+
519+ return true
520+ } else if ( res ?. data ?. status === 'failure' ) {
521+ throw Error ( res ?. data ?. details || 'Transaction failed' )
522+ } else if ( res ?. data ?. status === 'delayed' ) {
523+ stepItem . progressState = 'validating_delayed'
524+ }
525+ return false
526+ } ,
527+ maximumAttempts ,
528+ 0 ,
529+ pollingInterval
530+ )
531+ }
532+
512533 break
513534 }
514535
0 commit comments