#!/usr/bin/env npx tsx // Approve Builder Fee - Required before using open-broker import { getClient } from '../core/client.js'; import { OPEN_BROKER_BUILDER_ADDRESS } from '../core/config.js'; import { parseArgs } from '../core/utils.js'; function printUsage() { console.log(` Open Broker - Approve Builder Fee ================================= Approve the open-broker builder to receive fees on your trades. This is a ONE-TIME setup required before using open-broker. IMPORTANT: - Must be signed by your MAIN wallet, not an API wallet - Sub-accounts cannot approve builder fees - The approval persists until you revoke it Usage: npx tsx scripts/setup/approve-builder.ts [options] Options: --max-fee Maximum fee rate to approve (default: "0.1%") --check Only check current approval status, don't approve --builder Custom builder address (default: open-broker) Examples: # Check current approval status npx tsx scripts/setup/approve-builder.ts --check # Approve with default max fee (0.1% = 10 bps) npx tsx scripts/setup/approve-builder.ts # Approve with custom max fee npx tsx scripts/setup/approve-builder.ts --max-fee "0.05%" Builder Fee Info: - Open Broker charges 1 bps (0.01%) on trades by default - This fee supports continued development - You approve a MAX fee - actual fee per trade may be lower - Fee is only charged on successful fills `); } async function main() { const args = parseArgs(process.argv.slice(2)); if (args.help) { printUsage(); process.exit(0); } const maxFee = (args['max-fee'] as string) || '0.1%'; const checkOnly = args.check as boolean; const customBuilder = args.builder as string | undefined; const client = getClient(); if (args.verbose) { client.verbose = true; } console.log('Open Broker - Builder Fee Setup'); console.log('===============================\n'); const builderAddress = customBuilder || OPEN_BROKER_BUILDER_ADDRESS; console.log('Account Configuration'); console.log('---------------------'); console.log(`Trading Account: ${client.address}`); console.log(`Signing Wallet: ${client.walletAddress}`); console.log(`Is API Wallet: ${client.isApiWallet ? 'YES' : 'NO'}`); console.log(`Builder Address: ${builderAddress}`); console.log(`Default Fee: ${client.builderFeeBps} bps`); // Check for API wallet - can't approve if (client.isApiWallet) { console.log(` ERROR: Cannot approve builder fee with an API wallet. You are using an API wallet (signing wallet differs from trading account). Builder fee approval must be signed by the MAIN wallet that owns the account. To fix this: 1. Set HYPERLIQUID_PRIVATE_KEY to your main wallet's private key 2. Remove HYPERLIQUID_ACCOUNT_ADDRESS (or set it to match the main wallet) 3. Run this script again After approval, you can switch back to using your API wallet for trading. `); process.exit(1); } // Check for sub-accounts (indicates this is a master account) console.log('\nChecking account type...'); const subAccounts = await client.getSubAccounts(); if (subAccounts.length === 0) { // Could be a master account with no subs, or a sub-account // We'll proceed and let the API reject if it's a sub-account console.log('Account type: Main account (no sub-accounts)'); } else { console.log(`Account type: Main account with ${subAccounts.length} sub-account(s)`); for (const sub of subAccounts) { console.log(` - ${sub.name}: ${sub.subAccountUser}`); } } // Check current approval status console.log('\nChecking builder fee approval...'); const currentApproval = await client.getMaxBuilderFee(client.address, builderAddress); if (currentApproval) { console.log(`Current approval: ${currentApproval}`); console.log('\nāœ… Builder fee already approved!'); if (checkOnly) { return; } // Check if we need to increase the approval const currentPct = parseFloat(currentApproval.replace('%', '')); const requestedPct = parseFloat(maxFee.replace('%', '')); if (requestedPct <= currentPct) { console.log(`Requested max fee (${maxFee}) is within current approval.`); console.log('No action needed.'); return; } console.log(`\nIncreasing approval from ${currentApproval} to ${maxFee}...`); } else { console.log('Current approval: None'); if (checkOnly) { console.log(` āŒ Builder fee NOT approved! You need to approve the builder fee before using open-broker. Run without --check to approve: npx tsx scripts/setup/approve-builder.ts `); process.exit(1); } console.log(`\nApproving builder fee (max: ${maxFee})...`); } // Perform the approval const result = await client.approveBuilderFee(maxFee, builderAddress); if (result.status === 'ok') { console.log('\nāœ… Builder fee approved successfully!'); console.log(`\nYou can now use open-broker for trading.`); console.log(`Builder will receive up to ${maxFee} on your trades.`); console.log(`Default fee charged: ${client.builderFeeBps} bps (0.0${client.builderFeeBps}%)`); } else { console.log('\nāŒ Failed to approve builder fee'); console.log(`Error: ${result.response}`); if (String(result.response).includes('sub')) { console.log(` This appears to be a sub-account. Sub-accounts cannot approve builder fees. Please use the MAIN account (master wallet) to approve builder fees. `); } process.exit(1); } } main().catch((error) => { console.error('Error:', error); process.exit(1); });