Skip to content
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as hyperliquid from '@nktkas/hyperliquid';
import { approveBuilderFee, SuccessResponse } from '@nktkas/hyperliquid/api/exchange';
import { LitActionPkpEthersWallet } from './lit-action-pkp-ethers-wallet';
import { DEFAULT_BUILDER } from '../constants';

export async function approveBuilderFeeAction({
transport,
pkpPublicKey,
useTestnet = false,
}: {
transport: hyperliquid.HttpTransport;
pkpPublicKey: string;
useTestnet?: boolean;
}): Promise<SuccessResponse> {
const pkpWallet = new LitActionPkpEthersWallet(pkpPublicKey);

const response = await Lit.Actions.runOnce(
{ waitForResponse: true, name: 'HyperLiquidApproveBuilderFee' },
async () => {
return JSON.stringify(
await approveBuilderFee(
{ transport, wallet: pkpWallet },
{ builder: DEFAULT_BUILDER.address, maxFeeRate: DEFAULT_BUILDER.maxFeeRate },
),
);
},
);

return JSON.parse(response) as SuccessResponse;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { signL1Action } from '@nktkas/hyperliquid/signing';
import { bigIntReplacer } from '@lit-protocol/vincent-ability-sdk';

import { LitActionPkpEthersWallet } from './lit-action-pkp-ethers-wallet';
import { DEFAULT_BUILDER } from '../constants';

export type TimeInForce = 'Gtc' | 'Ioc' | 'Alo';

Expand Down Expand Up @@ -140,6 +141,8 @@ export async function executePerpOrder({
? { limit: { tif: 'FrontendMarket' } }
: { limit: { tif: orderType.tif } };

const builderConfig = DEFAULT_BUILDER;

// Construct order action
const orderAction = parser(OrderRequest.entries.action)({
type: 'order',
Expand All @@ -151,6 +154,10 @@ export async function executePerpOrder({
s: params.size,
r: params.reduceOnly ?? false, // reduce only (true to close positions, false for opening)
t: orderTypeField,
builder: {
b: builderConfig.address,
f: builderConfig.fee,
},
},
],
grouping: 'na',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { signL1Action } from '@nktkas/hyperliquid/signing';
import { bigIntReplacer } from '@lit-protocol/vincent-ability-sdk';

import { LitActionPkpEthersWallet } from './lit-action-pkp-ethers-wallet';
import { DEFAULT_BUILDER } from '../constants';

export type TimeInForce = 'Gtc' | 'Ioc' | 'Alo';

Expand Down Expand Up @@ -77,6 +78,8 @@ export async function executeSpotOrder({
? { limit: { tif: 'FrontendMarket' } }
: { limit: { tif: orderType.tif } };

const builderConfig = DEFAULT_BUILDER;

// Construct order action
const orderAction = parser(OrderRequest.entries.action)({
type: 'order',
Expand All @@ -88,6 +91,10 @@ export async function executeSpotOrder({
s: params.size,
r: false, // reduce only (false for spot)
t: orderTypeField,
builder: {
b: builderConfig.address,
f: builderConfig.fee,
},
},
],
grouping: 'na',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export { sendDepositTx } from './send-deposit-tx';
export { transferUsdcTo } from './transfer-usdc-to';
export { executeSpotOrder } from './execute-spot-order';
export { executePerpOrder } from './execute-perp-order';
export { approveBuilderFeeAction } from './approve-builder-fee';
6 changes: 6 additions & 0 deletions packages/apps/ability-hyperliquid/src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const DEFAULT_BUILDER = {
address: '0x132Db5f531Ba38628F1640683354229daE04E8e6',
/** 0.05% expressed in 0.1 bps units (500 * 0.0001% = 0.05%). */
fee: 500,
maxFeeRate: '0.05%',
};
6 changes: 6 additions & 0 deletions packages/apps/ability-hyperliquid/src/lib/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const actionTypeSchema = z.enum([
'perpShort',
'cancelOrder',
'cancelAllOrdersForSymbol',
'approveBuilder',
]);

export const depositParamsSchema = z.object({
Expand Down Expand Up @@ -153,6 +154,7 @@ export const abilityParamsSchema = z
if (data.action === 'perpLong' || data.action === 'perpShort') return !!data.perp;
if (data.action === 'cancelOrder') return !!data.cancelOrder;
if (data.action === 'cancelAllOrdersForSymbol') return !!data.cancelAllOrdersForSymbol;
if (data.action === 'approveBuilder') return true;
return false;
},
{
Expand Down Expand Up @@ -196,4 +198,8 @@ export const executeSuccessSchema = z.object({
.any()
.optional()
.describe('Cancel result (for spotCancelOrder/spotCancelAll action)'),
builderApproval: z
.any()
.optional()
.describe('Builder approval result (for approveBuilder action)'),
});
1 change: 1 addition & 0 deletions packages/apps/ability-hyperliquid/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export enum HyperliquidAction {
PERP_SHORT = 'perpShort',
CANCEL_ORDER = 'cancelOrder',
CANCEL_ALL_ORDERS_FOR_SYMBOL = 'cancelAllOrdersForSymbol',
APPROVE_BUILDER = 'approveBuilder',
}

/**
Expand Down
18 changes: 18 additions & 0 deletions packages/apps/ability-hyperliquid/src/lib/vincent-ability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
cancelOrder,
cancelAllOrdersForSymbol,
executePerpOrder,
approveBuilderFeeAction,
} from './ability-helpers';
import {
depositPrechecks,
Expand Down Expand Up @@ -227,6 +228,10 @@ export const vincentAbility = createVincentAbility({
return succeed({ action });
}

case HyperliquidAction.APPROVE_BUILDER: {
return succeed({ action });
}

default:
return fail({ action, reason: `Unknown action: ${action}` });
}
Expand Down Expand Up @@ -441,6 +446,19 @@ export const vincentAbility = createVincentAbility({
});
}

case HyperliquidAction.APPROVE_BUILDER: {
const result = await approveBuilderFeeAction({
transport,
pkpPublicKey: delegatorPkpInfo.publicKey,
useTestnet,
});

return succeed({
action,
builderApproval: result,
});
}

default:
return fail({ action, reason: `Unknown action: ${action}` });
}
Expand Down