Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/five-sloths-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@relayprotocol/relay-sdk': minor
'@relayprotocol/relay-kit-ui': minor
---

Integrate hyperliquid direct deposits
2 changes: 1 addition & 1 deletion demo/pages/ui/swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { adaptTronWallet } from '@relayprotocol/relay-tron-wallet-adapter'
import Head from 'next/head'
import { isTronWallet, TronWallet } from '@dynamic-labs/tron'

const WALLET_VM_TYPES = ['evm', 'bvm', 'svm', 'suivm', 'tvm'] as const
const WALLET_VM_TYPES = ['evm', 'bvm', 'svm', 'suivm', 'tvm', 'hypevm'] as const

const SwapWidgetPage: NextPage = () => {
useDynamicEvents('walletAdded', (newWallet) => {
Expand Down
16 changes: 4 additions & 12 deletions packages/sdk/src/utils/executeSteps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
import type { AxiosRequestConfig } from 'axios'
import { getClient, RelayClient } from '../../client.js'
import { LogLevel } from '../logger.js'
import { prepareHyperliquidSignatureStep } from '../../utils/index.js'
import { prepareHyperliquidSteps } from '../../utils/index.js'
import {
canBatchTransactions,
prepareBatchTransaction
Expand Down Expand Up @@ -124,18 +124,10 @@ export async function executeSteps(
}
}

// Check if Hyperliquid and if so, rewrite steps to become a signature step
if (
chainId === 1337 &&
json.steps[0] &&
(json.steps[0].id as any) !== 'sign'
) {
// Check if Hyperliquid and if so, prepare the steps for Hyperliquid signing
if (chainId === 1337) {
const activeWalletChainId = await wallet?.getChainId()
const signatureStep = prepareHyperliquidSignatureStep(
json.steps,
activeWalletChainId
)
json.steps = [signatureStep]
json.steps = prepareHyperliquidSteps(json.steps, activeWalletChainId)
}

// Update state on first call or recursion
Expand Down
11 changes: 8 additions & 3 deletions packages/sdk/src/utils/executeSteps/signatureStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { AxiosRequestConfig } from 'axios'
import { LogLevel } from '../logger.js'
import type { RelayClient } from '../../client.js'
import type { SetStateData } from './index.js'
import { sendUsd } from '../hyperliquid.js'
import { postHyperliquidSignature } from '../hyperliquid.js'

/**
* Handles the execution of a signature step item, including signing, posting, and validation.
Expand Down Expand Up @@ -57,6 +57,7 @@ export async function handleSignatureStepItem({
breakdown: json?.breakdown,
details: json?.details
})

signature = await wallet.handleSignMessageStep(stepItem, step)

if (signature) {
Expand All @@ -67,8 +68,12 @@ export async function handleSignatureStepItem({
}
}

if (chain.id === 1337 && signature) {
await sendUsd(client, signature, stepItem)
if (
chain.id === 1337 &&
signature &&
step?.id === ('hyperliquid-signature' as any)
) {
await postHyperliquidSignature(client, signature, stepItem)
}

if (postData) {
Expand Down
108 changes: 75 additions & 33 deletions packages/sdk/src/utils/hyperliquid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import axios from 'axios'
import type { RelayClient } from '../client.js'
import { LogLevel } from './logger.js'

export function prepareHyperliquidSignatureStep(
steps: Execute['steps'],
function prepareHyperliquidSignatureStep(
step: Execute['steps'][0],
chainId: number
) {
const items = steps[0]?.items
const amount = items[0]?.data?.action?.parameters?.amount
const destination = items[0]?.data?.action?.parameters?.destination
const stepItem = step?.items?.[0]
const action = stepItem?.data?.action
const eip712Types = stepItem?.data?.eip712Types
const eip712PrimaryType = stepItem?.data?.eip712PrimaryType

const signatureStep = {
id: 'sign' as any,
id: 'hyperliquid-signature' as any,
action: 'Confirm transaction in your wallet',
description: `Sign a message to confirm the transaction`,
kind: 'signature' as const,
Expand All @@ -29,44 +31,36 @@ export function prepareHyperliquidSignatureStep(
verifyingContract: '0x0000000000000000000000000000000000000000'
},
types: {
'HyperliquidTransaction:UsdSend': [
{ name: 'hyperliquidChain', type: 'string' },
{ name: 'destination', type: 'string' },
{ name: 'amount', type: 'string' },
{ name: 'time', type: 'uint64' }
],
...eip712Types,
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' }
]
},
primaryType: 'HyperliquidTransaction:UsdSend',
primaryType: eip712PrimaryType,
value: {
type: 'usdSend',
signatureChainId: `0x${chainId.toString(16)}`,
hyperliquidChain: 'Mainnet',
destination: destination?.toLowerCase(),
amount,
time: new Date().getTime()
...action.parameters,
type: action.type,
signatureChainId: `0x${chainId.toString(16)}`
}
}
},
check: {
endpoint: `/intents/status?requestId=${steps[0]?.requestId}`,
endpoint: `/intents/status?requestId=${step?.requestId}`,
method: 'GET'
}
}
],
requestId: steps[0]?.requestId,
depositAddress: steps[0]?.depositAddress
requestId: step?.requestId,
depositAddress: step?.depositAddress
}

return signatureStep
}

export async function sendUsd(
export async function postHyperliquidSignature(
client: RelayClient,
signature: string,
stepItem: Execute['steps'][0]['items'][0]
Expand All @@ -76,22 +70,18 @@ export async function sendUsd(
LogLevel.Verbose
)
const { r, s, v } = parseSignature(signature as `0x${string}`)
const currentTime = stepItem?.data?.sign?.value?.time ?? new Date().getTime()

const action = stepItem?.data?.sign?.value
const nonce = action?.nonce ?? action?.time

const res = await axios.post('https://api.hyperliquid.xyz/exchange', {
signature: {
r,
s,
v: Number(v ?? 0n)
},
nonce: currentTime,
action: {
type: stepItem?.data?.sign?.value?.type,
signatureChainId: `0x${stepItem?.data?.sign?.domain?.chainId?.toString(16)}`,
hyperliquidChain: 'Mainnet',
destination: stepItem?.data?.sign?.value?.destination?.toLowerCase(),
amount: stepItem?.data?.sign?.value?.amount,
time: currentTime
}
nonce,
action
})
if (
!res ||
Expand All @@ -107,3 +97,55 @@ export async function sendUsd(
)
return res.data
}

function updateHyperliquidSignatureChainId(
step: Execute['steps'][0],
activeWalletChainId: number
): Execute['steps'][0] {
return {
...step,
items: step.items?.map((item) => ({
...item,
data: {
...item.data,
sign: {
...item.data.sign,
domain: {
...item.data.sign.domain,
chainId: activeWalletChainId
}
},
...(item.data.post && {
post: {
...item.data.post,
body: {
...item.data.post.body,
signatureChainId: activeWalletChainId
}
}
})
}
}))
}
}

export function prepareHyperliquidSteps(
steps: Execute['steps'],
activeWalletChainId: number
): Execute['steps'] {
return steps.map((step) => {
// Skip steps that have already been converted (id is set to 'sign' by prepareHyperliquidSignatureStep)
if ((step.id as string) === 'hyperliquid-signature') {
return step
}
// Update signature steps to use the active wallet chain ID
if (step.kind === 'signature') {
return updateHyperliquidSignatureChainId(step, activeWalletChainId)
}
// Convert transaction steps to Hyperliquid signature steps
if (step.kind === 'transaction') {
return prepareHyperliquidSignatureStep(step, activeWalletChainId)
}
return step
})
}
2 changes: 1 addition & 1 deletion packages/sdk/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export {
} from './simulateContract.js'
export { safeStructuredClone } from './structuredClone.js'
export { repeatUntilOk } from './repeatUntilOk.js'
export { prepareHyperliquidSignatureStep } from './hyperliquid.js'
export { prepareHyperliquidSteps } from './hyperliquid.js'
export { isRelayApiUrl, getApiKeyHeader } from './apiKey.js'
3 changes: 1 addition & 2 deletions packages/ui/src/components/widgets/SwapWidgetRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,7 @@ const SwapWidgetRenderer: FC<SwapWidgetRendererProps> = ({
toToken.decimals
).toString(),
referrer: relayClient?.source ?? undefined,
useDepositAddress:
!fromChainWalletVMSupported || fromToken?.chainId === 1337,
useDepositAddress: !fromChainWalletVMSupported,
refundTo: fromToken?.chainId === 1337 ? address : undefined,
slippageTolerance: slippageTolerance,
topupGas: gasTopUpEnabled && gasTopUpRequired,
Expand Down