Skip to content

Commit 472d41b

Browse files
committed
chore: optimizations
1 parent 57554d6 commit 472d41b

File tree

14 files changed

+150
-235
lines changed

14 files changed

+150
-235
lines changed

packages/apps/ability-hyperliquid/src/generated/lit-action.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"ipfsCid": "QmUpGqvnfLEthsyK9MYM59794SEKerPhe7Jv1fDLLggZTz"
2+
"ipfsCid": "QmbWiHwBJq3X59qeibfGmjBPVKj4ti6hFUxCvCnw1r84Ec"
33
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
export { bundledVincentAbility } from './generated/vincent-bundled-ability';
2+
3+
// Export enums and types for users
4+
export { HyperliquidAction, TimeInForce, OrderType } from './lib/types';

packages/apps/ability-hyperliquid/src/lib/ability-checks/deposit.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
import { ethers } from 'ethers';
22
import { ERC20_ABI } from '@lit-protocol/vincent-ability-sdk';
33

4-
import { ARBITRUM_USDC_ADDRESS, DepositPrechecksResult } from '../types';
4+
import {
5+
ARBITRUM_USDC_ADDRESS_MAINNET,
6+
ARBITRUM_USDC_ADDRESS_TESTNET,
7+
DepositPrechecksResult,
8+
} from '../types';
59

610
export const depositPrechecks = async ({
711
provider,
812
agentWalletPkpEthAddress,
913
depositAmount,
14+
useTestnet = false,
1015
}: {
1116
provider: ethers.providers.Provider;
1217
agentWalletPkpEthAddress: string;
1318
depositAmount: string;
19+
useTestnet?: boolean;
1420
}): Promise<DepositPrechecksResult> => {
15-
const usdcContract = new ethers.Contract(ARBITRUM_USDC_ADDRESS, ERC20_ABI, provider);
21+
const usdcAddress = useTestnet ? ARBITRUM_USDC_ADDRESS_TESTNET : ARBITRUM_USDC_ADDRESS_MAINNET;
22+
const usdcContract = new ethers.Contract(usdcAddress, ERC20_ABI, provider);
1623
const amountWei = ethers.utils.parseUnits(depositAmount, 6);
1724

1825
const balance = await usdcContract.balanceOf(agentWalletPkpEthAddress);

packages/apps/ability-hyperliquid/src/lib/ability-checks/perp.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,15 @@ export async function perpTradePrechecks(
4949
});
5050

5151
// Check if user has any cross margin value or margin summary
52-
const crossMarginSummary = perpState.crossMarginSummary;
53-
if (!crossMarginSummary) {
52+
if (!perpState.crossMarginSummary) {
5453
return {
5554
success: false,
5655
reason: 'No perp account found. Please transfer funds to perp account first.',
5756
assetId,
5857
};
5958
}
6059

61-
const accountValue = parseFloat(crossMarginSummary.accountValue);
60+
const accountValue = parseFloat(perpState.crossMarginSummary.accountValue);
6261
if (accountValue <= 0) {
6362
return {
6463
success: false,

packages/apps/ability-hyperliquid/src/lib/ability-helpers/execute-perp-order.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ export interface PerpTradeParams {
1313
price: string;
1414
size: string;
1515
isLong: boolean; // true for long, false for short
16+
/**
17+
* Reduce-only flag. If true, order will only reduce existing position.
18+
* Use this to close positions - the order will only fill up to the position size.
19+
* @default false
20+
*/
21+
reduceOnly?: boolean;
1622
/**
1723
* Order type configuration.
1824
* - For limit orders: specify { type: 'limit', tif: 'Gtc' | 'Ioc' | 'Alo' }
@@ -56,7 +62,6 @@ export async function executePerpOrder({
5662

5763
// Create PKP wallet
5864
const pkpWallet = new LitActionPkpEthersWallet(pkpPublicKey);
59-
const pkpAddress = await pkpWallet.getAddress();
6065

6166
// Set leverage if specified
6267
if (params.leverage) {
@@ -127,7 +132,7 @@ export async function executePerpOrder({
127132
b: params.isLong, // true for long (buy), false for short (sell)
128133
p: params.price,
129134
s: params.size,
130-
r: false, // reduce only (false for opening positions)
135+
r: params.reduceOnly ?? false, // reduce only (true to close positions, false for opening)
131136
t: orderTypeField,
132137
},
133138
],

packages/apps/ability-hyperliquid/src/lib/schemas.ts

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ export const actionTypeSchema = z.enum([
66
'transferToPerp',
77
'spotBuy',
88
'spotSell',
9-
'spotCancelOrder',
10-
'spotCancelAll',
119
'perpLong',
1210
'perpShort',
13-
'perpCancelOrder',
14-
'perpCancelAll',
11+
'cancelOrder',
12+
'cancelAllOrdersForSymbol',
1513
]);
1614

1715
export const depositParamsSchema = z.object({
@@ -51,12 +49,12 @@ export const spotTradeParamsSchema = z.object({
5149
),
5250
});
5351

54-
export const spotCancelOrderParamsSchema = z.object({
52+
export const cancelOrderParamsSchema = z.object({
5553
symbol: z.string().describe('Trading pair symbol (e.g., "PURR/USDC")'),
5654
orderId: z.number().describe('Order ID to cancel'),
5755
});
5856

59-
export const spotCancelAllParamsSchema = z.object({
57+
export const cancelAllOrdersForSymbolParamsSchema = z.object({
6058
symbol: z.string().describe('Trading pair symbol to cancel all orders for (e.g., "PURR/USDC")'),
6159
});
6260

@@ -66,22 +64,19 @@ export const perpTradeParamsSchema = z.object({
6664
size: z.string().describe('Order size in base asset'),
6765
leverage: z.number().min(1).max(50).optional().describe('Leverage multiplier (default: 2x)'),
6866
isCross: z.boolean().optional().describe('Cross leverage (default: true)'),
67+
reduceOnly: z
68+
.boolean()
69+
.optional()
70+
.describe(
71+
'Reduce-only: if true, order will only reduce existing position and cannot increase it (default: false). Use this to close positions without worrying about exact size.',
72+
),
6973
orderType: orderTypeSchema
7074
.optional()
7175
.describe(
7276
'Order type: { type: "limit", tif: "Gtc" | "Ioc" | "Alo" } or { type: "market" }. Default: { type: "limit", tif: "Gtc" }',
7377
),
7478
});
7579

76-
export const perpCancelOrderParamsSchema = z.object({
77-
symbol: z.string().describe('Perpetual symbol (e.g., "ETH")'),
78-
orderId: z.number().describe('Order ID to cancel'),
79-
});
80-
81-
export const perpCancelAllParamsSchema = z.object({
82-
symbol: z.string().describe('Perpetual symbol to cancel all orders for (e.g., "ETH")'),
83-
});
84-
8580
export const abilityParamsSchema = z
8681
.object({
8782
action: actionTypeSchema.describe('Action type to execute'),
@@ -105,26 +100,18 @@ export const abilityParamsSchema = z
105100
spot: spotTradeParamsSchema
106101
.optional()
107102
.describe('Spot trading parameters (required for spotBuy/spotSell)'),
108-
// Spot cancel order params
109-
spotCancelOrder: spotCancelOrderParamsSchema
110-
.optional()
111-
.describe('Cancel specific spot order (required for spotCancelOrder action)'),
112-
// Spot cancel all params
113-
spotCancelAll: spotCancelAllParamsSchema
114-
.optional()
115-
.describe('Cancel all spot orders for a symbol (required for spotCancelAll action)'),
116103
// Perp trading params
117104
perp: perpTradeParamsSchema
118105
.optional()
119106
.describe('Perpetual trading parameters (required for perpLong/perpShort)'),
120-
// Perp cancel order params
121-
perpCancelOrder: perpCancelOrderParamsSchema
107+
// Cancel order params
108+
cancelOrder: cancelOrderParamsSchema
122109
.optional()
123-
.describe('Cancel specific perp order (required for perpCancelOrder action)'),
124-
// Perp cancel all params
125-
perpCancelAll: perpCancelAllParamsSchema
110+
.describe('Cancel specific order (required for cancelOrder action)'),
111+
// Cancel all orders for symbol params
112+
cancelAllOrdersForSymbol: cancelAllOrdersForSymbolParamsSchema
126113
.optional()
127-
.describe('Cancel all perp orders for a symbol (required for perpCancelAll action)'),
114+
.describe('Cancel all orders for a symbol (required for cancelAllOrdersForSymbol action)'),
128115
// Arbitrum RPC URL
129116
arbitrumRpcUrl: z.string().optional().describe('Arbitrum RPC URL (required for precheck)'),
130117
})
@@ -134,11 +121,9 @@ export const abilityParamsSchema = z
134121
if (data.action === 'transferToSpot' || data.action === 'transferToPerp')
135122
return !!data.transfer;
136123
if (data.action === 'spotBuy' || data.action === 'spotSell') return !!data.spot;
137-
if (data.action === 'spotCancelOrder') return !!data.spotCancelOrder;
138-
if (data.action === 'spotCancelAll') return !!data.spotCancelAll;
139124
if (data.action === 'perpLong' || data.action === 'perpShort') return !!data.perp;
140-
if (data.action === 'perpCancelOrder') return !!data.perpCancelOrder;
141-
if (data.action === 'perpCancelAll') return !!data.perpCancelAll;
125+
if (data.action === 'cancelOrder') return !!data.cancelOrder;
126+
if (data.action === 'cancelAllOrdersForSymbol') return !!data.cancelAllOrdersForSymbol;
142127
return false;
143128
},
144129
{
@@ -182,6 +167,4 @@ export const executeSuccessSchema = z.object({
182167
.any()
183168
.optional()
184169
.describe('Cancel result (for spotCancelOrder/spotCancelAll action)'),
185-
openOrders: z.array(z.any()).optional().describe('Open orders after execution'),
186-
positions: z.any().optional().describe('Positions after execution'),
187170
});

packages/apps/ability-hyperliquid/src/lib/types.ts

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
11
/**
2-
* Bridge contract address on Arbitrum Mainnet
3-
* Minimum deposit: 5 USDC + 1 USDC fee = 6 USDC total
2+
* Action types for Hyperliquid ability
43
*/
5-
export const HYPERLIQUID_BRIDGE_ADDRESS_MAINNET = '0x2Df1c51E09aECF9cacB7bc98cB1742757f163dF7';
4+
export enum HyperliquidAction {
5+
DEPOSIT = 'deposit',
6+
TRANSFER_TO_SPOT = 'transferToSpot',
7+
TRANSFER_TO_PERP = 'transferToPerp',
8+
SPOT_BUY = 'spotBuy',
9+
SPOT_SELL = 'spotSell',
10+
PERP_LONG = 'perpLong',
11+
PERP_SHORT = 'perpShort',
12+
CANCEL_ORDER = 'cancelOrder',
13+
CANCEL_ALL_ORDERS_FOR_SYMBOL = 'cancelAllOrdersForSymbol',
14+
}
615

716
/**
8-
* Bridge contract address on Arbitrum Sepolia (Testnet)
17+
* Time-in-force options for limit orders
918
*/
10-
export const HYPERLIQUID_BRIDGE_ADDRESS_TESTNET = '0xEdB6e5c456B7ccA2EB1e5c7007f4Ab80426CD20F';
19+
export enum TimeInForce {
20+
/** Good Till Canceled - order stays active until filled or manually canceled */
21+
GTC = 'Gtc',
22+
/** Immediate Or Cancel - order fills immediately or cancels */
23+
IOC = 'Ioc',
24+
/** Add Liquidity Only - order only fills if it adds liquidity (maker only) */
25+
ALO = 'Alo',
26+
}
1127

1228
/**
13-
* USDC address on Arbitrum Mainnet (standard ERC20)
29+
* Order type for trades
1430
*/
31+
export enum OrderType {
32+
LIMIT = 'limit',
33+
MARKET = 'market',
34+
}
35+
36+
export const HYPERLIQUID_BRIDGE_ADDRESS_MAINNET = '0x2Df1c51E09aECF9cacB7bc98cB1742757f163dF7';
37+
38+
export const HYPERLIQUID_BRIDGE_ADDRESS_TESTNET = '0xEdB6e5c456B7ccA2EB1e5c7007f4Ab80426CD20F';
39+
1540
export const ARBITRUM_USDC_ADDRESS_MAINNET = '0xaf88d065e77c8cC2239327C5EDb3A432268e5831';
1641

17-
/**
18-
* USDC address on Arbitrum Sepolia Testnet (standard ERC20)
19-
*/
2042
export const ARBITRUM_USDC_ADDRESS_TESTNET = '0x75faf114eafb1BDbe2F0316DF893fd58CE46AA4d';
2143

22-
// Legacy exports for backwards compatibility
23-
export const HYPERLIQUID_BRIDGE_ADDRESS = HYPERLIQUID_BRIDGE_ADDRESS_MAINNET;
24-
export const ARBITRUM_USDC_ADDRESS = ARBITRUM_USDC_ADDRESS_MAINNET;
25-
2644
export type DepositPrechecksResult = DepositPrechecksResultSuccess | DepositPrechecksResultFailure;
2745

2846
export interface DepositPrechecksResultSuccess {

0 commit comments

Comments
 (0)