@lit-protocol/vincent-ability-hyperliquid 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (143) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/CONTRIBUTING.md +5 -0
  3. package/README.md +922 -0
  4. package/dist/CHANGELOG.md +21 -0
  5. package/dist/CONTRIBUTING.md +5 -0
  6. package/dist/README.md +922 -0
  7. package/dist/package.json +35 -0
  8. package/dist/src/generated/lit-action.js +9 -0
  9. package/dist/src/generated/vincent-ability-metadata.json +3 -0
  10. package/dist/src/generated/vincent-bundled-ability.d.ts +483 -0
  11. package/dist/src/generated/vincent-bundled-ability.d.ts.map +1 -0
  12. package/dist/src/generated/vincent-bundled-ability.js +15 -0
  13. package/dist/src/generated/vincent-bundled-ability.js.map +1 -0
  14. package/dist/src/generated/vincent-bundled-ability.ts +13 -0
  15. package/dist/src/index.d.ts +13 -0
  16. package/dist/src/index.d.ts.map +1 -0
  17. package/dist/src/index.js +14 -0
  18. package/dist/src/index.js.map +1 -0
  19. package/dist/src/lib/ability-checks/cancel.d.ts +41 -0
  20. package/dist/src/lib/ability-checks/cancel.d.ts.map +1 -0
  21. package/dist/src/lib/ability-checks/cancel.js +58 -0
  22. package/dist/src/lib/ability-checks/cancel.js.map +1 -0
  23. package/dist/src/lib/ability-checks/deposit-usdc.d.ts +18 -0
  24. package/dist/src/lib/ability-checks/deposit-usdc.d.ts.map +1 -0
  25. package/dist/src/lib/ability-checks/deposit-usdc.js +40 -0
  26. package/dist/src/lib/ability-checks/deposit-usdc.js.map +1 -0
  27. package/dist/src/lib/ability-checks/hyperliquid-account-exists.d.ts +6 -0
  28. package/dist/src/lib/ability-checks/hyperliquid-account-exists.d.ts.map +1 -0
  29. package/dist/src/lib/ability-checks/hyperliquid-account-exists.js +22 -0
  30. package/dist/src/lib/ability-checks/hyperliquid-account-exists.js.map +1 -0
  31. package/dist/src/lib/ability-checks/index.d.ts +11 -0
  32. package/dist/src/lib/ability-checks/index.d.ts.map +1 -0
  33. package/dist/src/lib/ability-checks/index.js +25 -0
  34. package/dist/src/lib/ability-checks/index.js.map +1 -0
  35. package/dist/src/lib/ability-checks/is-builder-code-approved.d.ts +14 -0
  36. package/dist/src/lib/ability-checks/is-builder-code-approved.d.ts.map +1 -0
  37. package/dist/src/lib/ability-checks/is-builder-code-approved.js +29 -0
  38. package/dist/src/lib/ability-checks/is-builder-code-approved.js.map +1 -0
  39. package/dist/src/lib/ability-checks/perp.d.ts +23 -0
  40. package/dist/src/lib/ability-checks/perp.d.ts.map +1 -0
  41. package/dist/src/lib/ability-checks/perp.js +47 -0
  42. package/dist/src/lib/ability-checks/perp.js.map +1 -0
  43. package/dist/src/lib/ability-checks/send-perp-usdc.d.ts +26 -0
  44. package/dist/src/lib/ability-checks/send-perp-usdc.d.ts.map +1 -0
  45. package/dist/src/lib/ability-checks/send-perp-usdc.js +38 -0
  46. package/dist/src/lib/ability-checks/send-perp-usdc.js.map +1 -0
  47. package/dist/src/lib/ability-checks/send-spot-asset.d.ts +27 -0
  48. package/dist/src/lib/ability-checks/send-spot-asset.d.ts.map +1 -0
  49. package/dist/src/lib/ability-checks/send-spot-asset.js +59 -0
  50. package/dist/src/lib/ability-checks/send-spot-asset.js.map +1 -0
  51. package/dist/src/lib/ability-checks/spot.d.ts +24 -0
  52. package/dist/src/lib/ability-checks/spot.d.ts.map +1 -0
  53. package/dist/src/lib/ability-checks/spot.js +98 -0
  54. package/dist/src/lib/ability-checks/spot.js.map +1 -0
  55. package/dist/src/lib/ability-checks/transfer-usdc.d.ts +26 -0
  56. package/dist/src/lib/ability-checks/transfer-usdc.d.ts.map +1 -0
  57. package/dist/src/lib/ability-checks/transfer-usdc.js +64 -0
  58. package/dist/src/lib/ability-checks/transfer-usdc.js.map +1 -0
  59. package/dist/src/lib/ability-checks/withdraw-usdc.d.ts +25 -0
  60. package/dist/src/lib/ability-checks/withdraw-usdc.d.ts.map +1 -0
  61. package/dist/src/lib/ability-checks/withdraw-usdc.js +39 -0
  62. package/dist/src/lib/ability-checks/withdraw-usdc.js.map +1 -0
  63. package/dist/src/lib/ability-helpers/approve-builder-code.d.ts +22 -0
  64. package/dist/src/lib/ability-helpers/approve-builder-code.d.ts.map +1 -0
  65. package/dist/src/lib/ability-helpers/approve-builder-code.js +53 -0
  66. package/dist/src/lib/ability-helpers/approve-builder-code.js.map +1 -0
  67. package/dist/src/lib/ability-helpers/cancel-order/cancel-all-orders.d.ts +21 -0
  68. package/dist/src/lib/ability-helpers/cancel-order/cancel-all-orders.d.ts.map +1 -0
  69. package/dist/src/lib/ability-helpers/cancel-order/cancel-all-orders.js +69 -0
  70. package/dist/src/lib/ability-helpers/cancel-order/cancel-all-orders.js.map +1 -0
  71. package/dist/src/lib/ability-helpers/cancel-order/cancel-order.d.ts +25 -0
  72. package/dist/src/lib/ability-helpers/cancel-order/cancel-order.d.ts.map +1 -0
  73. package/dist/src/lib/ability-helpers/cancel-order/cancel-order.js +63 -0
  74. package/dist/src/lib/ability-helpers/cancel-order/cancel-order.js.map +1 -0
  75. package/dist/src/lib/ability-helpers/execute-perp-order.d.ts +65 -0
  76. package/dist/src/lib/ability-helpers/execute-perp-order.d.ts.map +1 -0
  77. package/dist/src/lib/ability-helpers/execute-perp-order.js +110 -0
  78. package/dist/src/lib/ability-helpers/execute-perp-order.js.map +1 -0
  79. package/dist/src/lib/ability-helpers/execute-spot-order.d.ts +50 -0
  80. package/dist/src/lib/ability-helpers/execute-spot-order.d.ts.map +1 -0
  81. package/dist/src/lib/ability-helpers/execute-spot-order.js +84 -0
  82. package/dist/src/lib/ability-helpers/execute-spot-order.js.map +1 -0
  83. package/dist/src/lib/ability-helpers/get-hyperliquid-chain-id.d.ts +3 -0
  84. package/dist/src/lib/ability-helpers/get-hyperliquid-chain-id.d.ts.map +1 -0
  85. package/dist/src/lib/ability-helpers/get-hyperliquid-chain-id.js +15 -0
  86. package/dist/src/lib/ability-helpers/get-hyperliquid-chain-id.js.map +1 -0
  87. package/dist/src/lib/ability-helpers/get-hyperliquid-nonce.d.ts +2 -0
  88. package/dist/src/lib/ability-helpers/get-hyperliquid-nonce.d.ts.map +1 -0
  89. package/dist/src/lib/ability-helpers/get-hyperliquid-nonce.js +10 -0
  90. package/dist/src/lib/ability-helpers/get-hyperliquid-nonce.js.map +1 -0
  91. package/dist/src/lib/ability-helpers/index.d.ts +12 -0
  92. package/dist/src/lib/ability-helpers/index.d.ts.map +1 -0
  93. package/dist/src/lib/ability-helpers/index.js +26 -0
  94. package/dist/src/lib/ability-helpers/index.js.map +1 -0
  95. package/dist/src/lib/ability-helpers/lit-action-pkp-ethers-wallet.d.ts +74 -0
  96. package/dist/src/lib/ability-helpers/lit-action-pkp-ethers-wallet.d.ts.map +1 -0
  97. package/dist/src/lib/ability-helpers/lit-action-pkp-ethers-wallet.js +192 -0
  98. package/dist/src/lib/ability-helpers/lit-action-pkp-ethers-wallet.js.map +1 -0
  99. package/dist/src/lib/ability-helpers/send-deposit-tx.d.ts +15 -0
  100. package/dist/src/lib/ability-helpers/send-deposit-tx.d.ts.map +1 -0
  101. package/dist/src/lib/ability-helpers/send-deposit-tx.js +82 -0
  102. package/dist/src/lib/ability-helpers/send-deposit-tx.js.map +1 -0
  103. package/dist/src/lib/ability-helpers/send-perp-usdc.d.ts +16 -0
  104. package/dist/src/lib/ability-helpers/send-perp-usdc.d.ts.map +1 -0
  105. package/dist/src/lib/ability-helpers/send-perp-usdc.js +49 -0
  106. package/dist/src/lib/ability-helpers/send-perp-usdc.js.map +1 -0
  107. package/dist/src/lib/ability-helpers/send-spot-asset.d.ts +17 -0
  108. package/dist/src/lib/ability-helpers/send-spot-asset.d.ts.map +1 -0
  109. package/dist/src/lib/ability-helpers/send-spot-asset.js +61 -0
  110. package/dist/src/lib/ability-helpers/send-spot-asset.js.map +1 -0
  111. package/dist/src/lib/ability-helpers/sign-tx.d.ts +3 -0
  112. package/dist/src/lib/ability-helpers/sign-tx.d.ts.map +1 -0
  113. package/dist/src/lib/ability-helpers/sign-tx.js +28 -0
  114. package/dist/src/lib/ability-helpers/sign-tx.js.map +1 -0
  115. package/dist/src/lib/ability-helpers/transfer-usdc-to.d.ts +16 -0
  116. package/dist/src/lib/ability-helpers/transfer-usdc-to.d.ts.map +1 -0
  117. package/dist/src/lib/ability-helpers/transfer-usdc-to.js +50 -0
  118. package/dist/src/lib/ability-helpers/transfer-usdc-to.js.map +1 -0
  119. package/dist/src/lib/ability-helpers/withdraw-usdc.d.ts +16 -0
  120. package/dist/src/lib/ability-helpers/withdraw-usdc.d.ts.map +1 -0
  121. package/dist/src/lib/ability-helpers/withdraw-usdc.js +49 -0
  122. package/dist/src/lib/ability-helpers/withdraw-usdc.js.map +1 -0
  123. package/dist/src/lib/constants.d.ts +3 -0
  124. package/dist/src/lib/constants.d.ts.map +1 -0
  125. package/dist/src/lib/constants.js +6 -0
  126. package/dist/src/lib/constants.js.map +1 -0
  127. package/dist/src/lib/lit-action.d.ts +2 -0
  128. package/dist/src/lib/lit-action.d.ts.map +1 -0
  129. package/dist/src/lib/lit-action.js +16 -0
  130. package/dist/src/lib/lit-action.js.map +1 -0
  131. package/dist/src/lib/schemas.d.ts +687 -0
  132. package/dist/src/lib/schemas.d.ts.map +1 -0
  133. package/dist/src/lib/schemas.js +233 -0
  134. package/dist/src/lib/schemas.js.map +1 -0
  135. package/dist/src/lib/types.d.ts +39 -0
  136. package/dist/src/lib/types.d.ts.map +1 -0
  137. package/dist/src/lib/types.js +45 -0
  138. package/dist/src/lib/types.js.map +1 -0
  139. package/dist/src/lib/vincent-ability.d.ts +480 -0
  140. package/dist/src/lib/vincent-ability.d.ts.map +1 -0
  141. package/dist/src/lib/vincent-ability.js +595 -0
  142. package/dist/src/lib/vincent-ability.js.map +1 -0
  143. package/package.json +34 -0
package/README.md ADDED
@@ -0,0 +1,922 @@
1
+ <!-- omit from toc -->
2
+
3
+ # Vincent Ability Hyperliquid
4
+
5
+ A Vincent Ability for securely interacting with the Hyperliquid API.
6
+
7
+ - [Vincent Ability Hyperliquid](#vincent-ability-hyperliquid)
8
+ - [Testing the Ability](#testing-the-ability)
9
+ - [Prerequisites](#prerequisites)
10
+ - [Create the `.env` file](#create-the-env-file)
11
+ - [Fund the Agent Wallet PKP with USDC](#fund-the-agent-wallet-pkp-with-usdc)
12
+ - [Get the Agent Wallet PKP ETH address](#get-the-agent-wallet-pkp-eth-address)
13
+ - [Funding on Hyperliquid Testnet](#funding-on-hyperliquid-testnet)
14
+ - [Funding on Hyperliquid Mainnet](#funding-on-hyperliquid-mainnet)
15
+ - [Verify the Agent Wallet PKP USDC Balance](#verify-the-agent-wallet-pkp-usdc-balance)
16
+ - [Balance E2E Tests](#balance-e2e-tests)
17
+ - [Deposit E2E Tests](#deposit-e2e-tests)
18
+ - [Depositing for Hyperliquid Mainnet](#depositing-for-hyperliquid-mainnet)
19
+ - [Transfer to Spot E2E Tests](#transfer-to-spot-e2e-tests)
20
+ - [Spot Buy E2E Tests](#spot-buy-e2e-tests)
21
+ - [Spot Sell E2E Tests](#spot-sell-e2e-tests)
22
+ - [Cancel Order E2E Tests](#cancel-order-e2e-tests)
23
+ - [Cancel All Orders E2E Tests](#cancel-all-orders-e2e-tests)
24
+ - [Send Spot Asset E2E Tests](#send-spot-asset-e2e-tests)
25
+ - [Trade History E2E Tests](#trade-history-e2e-tests)
26
+ - [Open Orders E2E Tests](#open-orders-e2e-tests)
27
+ - [Transfer to Perp E2E Tests](#transfer-to-perp-e2e-tests)
28
+ - [Perp Long E2E Tests](#perp-long-e2e-tests)
29
+ - [Perp Short E2E Tests](#perp-short-e2e-tests)
30
+ - [Send Perp USDC E2E Tests](#send-perp-usdc-e2e-tests)
31
+ - [Withdraw E2E Tests](#withdraw-e2e-tests)
32
+
33
+ # Testing the Ability
34
+
35
+ In the [test/e2e](./test/e2e/) directory, you can find several E2E tests for the various Hyperliquid functions this Ability supports. You can run these tests against the Hyperliquid mainnet, using assets with real world value, or you can execute against the Hyperliquid testnet, using test assets.
36
+
37
+ The E2E tests for this Ability are not expected to be ran concurrently. This is because each test suite relies on a balance being available for it to execute. The expected order of execution is:
38
+
39
+ 1. [Balance E2E Tests](#running-the-balance-e2e-tests) - A sanity check to ensure the Agent Wallet PKP has a balance of USDC on the Hyperliquid network you're running against.
40
+ 2. [Deposit E2E Tests](#running-the-deposit-e2e-tests) - Deposits USDC into the Agent Wallet PKP's Hyperliquid mainnet portfolio.
41
+ - This test will **not** run if you set the Ability parameter `useTestnet` to `true`. This is because deposits via a bridge contract are only supported on Hyperliquid mainnet. You can get test USDC on Hyperliquid testnet by following [this process](#funding-on-hyperliquid-testnet).
42
+ 3. [Transfer to Spot E2E Tests](#running-the-transfer-to-spot-e2e-tests) - Transfers USDC from the Agent Wallet PKP's Hyperliquid Perp balance to it's Hyperliquid spot balance. The PKP must have at least a Perp balance of `USDC_TRANSFER_AMOUNT` USDC.
43
+ 4. [Spot Buy E2E Tests](#running-the-spot-buy-e2e-tests) - Places a Spot Buy order for `SPOT_BUY_AMOUNT_USDC` USDC of the token specified by `TOKEN_OUT_NAME` using USDC.
44
+ 5. [Spot Sell E2E Tests](#running-the-spot-sell-e2e-tests) - Places a Spot Sell order for `SPOT_SELL_AMOUNT_USDC` of the token specified by `TOKEN_TO_SELL`.
45
+ 6. [Open Orders E2E Tests](#running-the-open-orders-e2e-tests) - Fetches all open orders (Spot and Perp) for the Agent Wallet PKP, and logs them to the console.
46
+ 7. [Cancel Order E2E Tests](#running-the-cancel-order-e2e-tests) - Cancels a Spot order for the order ID specified by `ORDER_ID_TO_CANCEL`.
47
+ 8. [Cancel All Orders E2E Tests](#running-the-cancel-all-orders-e2e-tests) - Cancels all open Spot orders for the market specified by `TRADING_PAIR`.
48
+ 9. [Send Spot Asset E2E Tests](#running-the-send-spot-asset-e2e-tests) - Sends a spot asset (USDC or other tokens) from the Agent Wallet PKP's Hyperliquid spot balance to another Hyperliquid spot account. The PKP must have at least a Spot balance of `SPOT_ASSET_SEND_AMOUNT` USDC.
49
+ 10. [Trade History E2E Tests](#running-the-trade-history-e2e-tests) - Fetches the Spot and Perp trade history of the Agent Wallet PKP on Hyperliquid mainnet or testnet.
50
+ 11. [Transfer to Perp E2E Tests](#running-the-transfer-to-perp-e2e-tests) - Transfers USDC from the Agent Wallet PKP's Hyperliquid spot balance to it's Hyperliquid Perp balance. The PKP must have at least a Spot balance of `USDC_TRANSFER_AMOUNT` USDC.
51
+ 12. [Perp Long E2E Tests](#running-the-perp-long-e2e-tests) - Opens a long position for the token specified by `PERP_SYMBOL` using USDC. The Agent Wallet PKP must have at least a Perp balance of `PERP_LONG_USD_NOTIONAL` USDC.
52
+ 13. [Perp Short E2E Tests](#running-the-perp-short-e2e-tests) - Opens a short position for the token specified by `PERP_SYMBOL` using USDC. The Agent Wallet PKP must have at least a Perp balance of `PERP_SHORT_USD_NOTIONAL` USDC.
53
+ 14. [Send Perp USDC E2E Tests](#running-the-send-perp-usdc-e2e-tests) - Sends USDC from the Agent Wallet PKP's Hyperliquid Perp balance to another Hyperliquid perp account. The PKP must have at least a Perp balance of `USDC_SEND_AMOUNT` USDC.
54
+ 15. [Withdraw E2E Tests](#running-the-withdraw-e2e-tests) - Withdraws USDC from the Agent Wallet PKP's Hyperliquid Perp balance to the PKP's ETH address on Arbitrum. The PKP must have at least a Perp balance of `WITHDRAW_AMOUNT_USDC` USDC.
55
+
56
+ The E2E tests by default expected the Agent Wallet PKP to have a Perp or Spot balance of at least `15` USDC. Each test has a `const` like `USDC_TRANSFER_AMOUNT` (for the transfer E2E tests) that configures how much of the USDC balance is used to run the tests. There are order amount minimums associated with each market on Hyperliquid. So the minimum amount accepted by Hyperliquid to Spot Buy on the market `BTC/USDC` is not the same as the minimum amount accepted by Hyperliquid to Spot Buy on the market `ETH/USDC`.
57
+
58
+ Additionally, the same markets are not available on both Hyperliquid mainnet and testnet. So you may need to adjust the `consts` like `TOKEN_OUT_NAME` and/or `TRADING_PAIR` that configure what market the order is being executed on for tests like the Spot Buy, Spot Sell, Perp Long, Perp Short.
59
+
60
+ You can view which markets are available on [Hyperliquid mainnet](https://app.hyperliquid.xyz/trade) or [the testnet](https://app.hyperliquid-testnet.xyz/trade) by clicking the market drop down in the top left of the UI.
61
+
62
+ ## Prerequisites
63
+
64
+ ### Create the `.env` file
65
+
66
+ Copy the [.env.example](./.env.example) file to a new file called `.env` and populate the environment variables.
67
+
68
+ The wallet for `TEST_FUNDER_PRIVATE_KEY` is expected to have Lit test tokens in order to fund the rest of the test wallets. You can use the [Lit Testnet Faucet](https://chronicle-yellowstone-faucet.getlit.dev/) to fund this wallet.
69
+
70
+ ### Fund the Agent Wallet PKP with USDC
71
+
72
+ #### Get the Agent Wallet PKP ETH address
73
+
74
+ You can get the Agent Wallet PKP ETH address by running the following command:
75
+
76
+ ```
77
+ pnpx nx run ability-hyperliquid:test-e2e packages/apps/ability-hyperliquid/test/e2e/balance.spec.ts
78
+ ```
79
+
80
+ After running the above command, you'll see multiple logs with the Agent Wallet PKP ETH address:
81
+
82
+ ```
83
+ [Perp Balance] Agent Wallet PKP ETH Address: 0x17f51B528A0eA0ea19ABa0F343Dc9beED0FCc428
84
+ ```
85
+
86
+ #### Funding on Hyperliquid Testnet
87
+
88
+ To get test USDC on Hyperliquid testnet, you need to deposit a minimum of `5` USDC into Hyperliquid mainnet. Afterwards you can claim `1000` test USDC on Hyperliquid testnet ([docs](https://hyperliquid.gitbook.io/hyperliquid-docs/onboarding/testnet-faucet)).
89
+
90
+ In the Lit Bitwarden, there's is a wallet titled: `Vincent Ability Hyperliquid E2E Test USDC Funder` that has made a `5` USDC deposit into Hyperliquid mainnet, and has a balance of test USDC on Hyperliquid testnet. You can add the private key to your browser wallet of choice (e.g. MetaMask), and then send test USDC to the Agent Wallet PKP ETH address on this screen: https://app.hyperliquid-testnet.xyz/portfolio
91
+
92
+ #### Funding on Hyperliquid Mainnet
93
+
94
+ To make a deposit into Hyperliquid mainnet, you need to send a minimum of `5` USDC from the Agent Wallet PKP, to the [Hyperliquid bridge contract](https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/bridge2#deposit) that's deployed on Arbitrum mainnet.
95
+
96
+ To do this, you can excute the [Deposit E2E test](#running-the-deposit-e2e-tests), which will send the `5` USDC from the Agent Wallet PKP to the Hyperliquid bridge contract, giving the Agent Wallet a balance of `5` USDC on Hyperliquid mainnet.
97
+
98
+ ### Verify the Agent Wallet PKP USDC Balance
99
+
100
+ Run the following command to log the balances of the Agent Wallet PKP on Hyperliquid network you're running against:
101
+
102
+ ```
103
+ pnpx nx run ability-hyperliquid:test-e2e packages/apps/ability-hyperliquid/test/e2e/balance.spec.ts
104
+ ```
105
+
106
+ Depending on whether you sent from the `Vincent Ability Hyperliquid E2E Test USDC Funder`'s `spot` or `perp` balance, one of the following logs will show the balance increase:
107
+
108
+ Spot balance:
109
+
110
+ ```
111
+ [Spot Balance] Full spot state: {
112
+ balances: [
113
+ {
114
+ coin: 'USDC',
115
+ token: 0,
116
+ total: '0.0',
117
+ hold: '0.0',
118
+ entryNtl: '0.0'
119
+ }
120
+ ]
121
+ }
122
+ ```
123
+
124
+ Perp balance:
125
+
126
+ ```
127
+ [Perp Balance] Full perp state: {
128
+ marginSummary: {
129
+ accountValue: '25.0',
130
+ totalNtlPos: '0.0',
131
+ totalRawUsd: '25.0',
132
+ totalMarginUsed: '0.0'
133
+ },
134
+ crossMarginSummary: {
135
+ accountValue: '25.0',
136
+ totalNtlPos: '0.0',
137
+ totalRawUsd: '25.0',
138
+ totalMarginUsed: '0.0'
139
+ },
140
+ crossMaintenanceMarginUsed: '0.0',
141
+ withdrawable: '25.0',
142
+ assetPositions: [],
143
+ time: 1762827649707
144
+ }
145
+ ```
146
+
147
+ In this case a `25` USDC transfer was made from the `Vincent Ability Hyperliquid E2E Test USDC Funder`'s `perp` balance to the Agent Wallet PKP's `perp` balance.
148
+
149
+ ## Balance E2E Tests
150
+
151
+ This test will work on both Hyperliquid mainnet and testnet.
152
+
153
+ This test will log the Spot and Perp balances of the Agent Wallet PKP on Hyperliquid mainnet or testnet.
154
+
155
+ ```
156
+ pnpx nx run ability-hyperliquid:test-e2e packages/apps/ability-hyperliquid/test/e2e/balance.spec.ts
157
+ ```
158
+
159
+ ## Deposit E2E Tests
160
+
161
+ The deposit action for the Ability will **not** run if you set the Ability parameter `useTestnet` to `true`. This is because deposits via a bridge contract are only supported on Hyperliquid mainnet.
162
+
163
+ To get test USDC on Hyperliquid testnet, you need to follow [this process](#funding-on-hyperliquid-testnet).
164
+
165
+ ### Depositing for Hyperliquid Mainnet
166
+
167
+ Running this E2E test will attempt to deposit `5` USDC from Arbitrum mainnet into the Hyperliquid bridge contract, giving the Agent Wallet a balance of `5` USDC on Hyperliquid mainnet.
168
+
169
+ In order for this to be successful, the Agent Wallet PKP must have at least 5 `USDC` and native ETH on Arbitrum mainnet. You can check the balance of the Agent Wallet PKP on Arbitrum mainnet by running [the balance test](#verify-the-agent-wallet-pkp-usdc-balance), or checking the Arbitrum mainnet block explorer.
170
+
171
+ ```
172
+ pnpx nx run ability-hyperliquid:test-e2e packages/apps/ability-hyperliquid/test/e2e/balance.spec.ts
173
+ ```
174
+
175
+ ## Transfer to Spot E2E Tests
176
+
177
+ This test will work on both Hyperliquid mainnet and testnet.
178
+
179
+ In order for this to be successful, the Agent Wallet PKP must have at least a Perp balance of `USDC_TRANSFER_AMOUNT` USDC. You can check the balance of the Agent Wallet PKP on Hyperliquid mainnet or testnet by running [the balance test](#verify-the-agent-wallet-pkp-usdc-balance).
180
+
181
+ ```
182
+ pnpx nx run ability-hyperliquid:test-e2e packages/apps/ability-hyperliquid/test/e2e/transfer/to-spot.spec.ts
183
+ ```
184
+
185
+ You should see the following logs indicating the transfer from the Perp balance to the Spot balance was successful:
186
+
187
+ ```
188
+ [Spot] Clearinghouse state after transfer {
189
+ balances: [
190
+ {
191
+ coin: 'USDC',
192
+ token: 0,
193
+ total: '15.0',
194
+ hold: '0.0',
195
+ entryNtl: '0.0'
196
+ }
197
+ ]
198
+ }
199
+ [Spot] Found 1 token balance(s):
200
+ - USDC: total=15.0, hold=0.0, available=15.000000
201
+ [Spot] Initial balance: 0.0
202
+ [Spot] Final balance: 15.0
203
+ [Spot] Expected increase: 15
204
+ [Spot] Actual increase: 15
205
+ ```
206
+
207
+ ## Spot Buy E2E Tests
208
+
209
+ This test will work on both Hyperliquid mainnet and testnet.
210
+
211
+ In order for this to be successful, the Agent Wallet PKP must have at least a Perp balance of `USDC_TRANSFER_AMOUNT` USDC. You can check the balance of the Agent Wallet PKP on Hyperliquid mainnet or testnet by running [the balance test](#verify-the-agent-wallet-pkp-usdc-balance).
212
+
213
+ This test will put in an order to buy **up to, but not over** `SPOT_BUY_AMOUNT_USDC` USDC of the token specified by `TOKEN_OUT_NAME` using USDC.
214
+
215
+ ```
216
+ pnpx nx run ability-hyperliquid:test-e2e packages/apps/ability-hyperliquid/test/e2e/spot/buy.spec.ts
217
+ ```
218
+
219
+ You should see the following logs indicating the Spot Buy was successful:
220
+
221
+ ```
222
+ [Spot Buy Execute] {
223
+ success: true,
224
+ result: {
225
+ action: 'spotBuy',
226
+ orderResult: {
227
+ status: 'ok',
228
+ response: {
229
+ type: 'order',
230
+ data: {
231
+ statuses: [
232
+ {
233
+ filled: { totalSz: '3.0', avgPx: '5.1085', oid: 42815357657 }
234
+ }
235
+ ]
236
+ }
237
+ }
238
+ }
239
+ },
240
+ context: {
241
+ delegation: {
242
+ delegateeAddress: '0xb50aBA1E265B52067aF97401C25C39efF57Fe83b',
243
+ delegatorPkpInfo: {
244
+ tokenId: '64398341638522492941822855531388288999933789608071521616433988119257635428447',
245
+ ethAddress: '0x17f51B528A0eA0ea19ABa0F343Dc9beED0FCc428',
246
+ publicKey: '0x04576960d83a4eaf042e585c1cf90035b869b087631312719fa2b96fecde30705dbf80711fc30f6435e6a0739d1f47201320563a7f51357c146421c96355f6cbe3'
247
+ }
248
+ },
249
+ abilityIpfsCid: 'QmeB5SM3UxcFYKYNffhM4VMWXSnFoZPAH8iLAXTgYPPupp',
250
+ appId: 47700028661,
251
+ appVersion: 4,
252
+ policiesContext: { allow: true, evaluatedPolicies: [], allowedPolicies: {} }
253
+ }
254
+ }
255
+ ```
256
+
257
+ ## Spot Sell E2E Tests
258
+
259
+ This test will work on both Hyperliquid mainnet and testnet.
260
+
261
+ In order for this to be successful, the Agent Wallet PKP must have at least a Spot balance of `SPOT_SELL_AMOUNT_USDC` `TOKEN_TO_SELL`. You can check the balance of the Agent Wallet PKP on Hyperliquid mainnet or testnet by running [the balance test](#verify-the-agent-wallet-pkp-usdc-balance).
262
+
263
+ This test will put in an order to sell `SPOT_SELL_AMOUNT_USDC` of `TOKEN_TO_SELL`.
264
+
265
+ ```
266
+ pnpx nx run ability-hyperliquid:test-e2e packages/apps/ability-hyperliquid/test/e2e/spot/sell.spec.ts
267
+ ```
268
+
269
+ You should see the following logs indicating the Spot Sell was successful:
270
+
271
+ ```
272
+ [Spot Sell Execute] {
273
+ success: true,
274
+ result: {
275
+ action: 'spotSell',
276
+ orderResult: {
277
+ status: 'ok',
278
+ response: {
279
+ type: 'order',
280
+ data: {
281
+ statuses: [
282
+ {
283
+ filled: { totalSz: '2.0', avgPx: '5.058', oid: 42815741000 }
284
+ }
285
+ ]
286
+ }
287
+ }
288
+ }
289
+ },
290
+ context: {
291
+ delegation: {
292
+ delegateeAddress: '0xb50aBA1E265B52067aF97401C25C39efF57Fe83b',
293
+ delegatorPkpInfo: {
294
+ tokenId: '64398341638522492941822855531388288999933789608071521616433988119257635428447',
295
+ ethAddress: '0x17f51B528A0eA0ea19ABa0F343Dc9beED0FCc428',
296
+ publicKey: '0x04576960d83a4eaf042e585c1cf90035b869b087631312719fa2b96fecde30705dbf80711fc30f6435e6a0739d1f47201320563a7f51357c146421c96355f6cbe3'
297
+ }
298
+ },
299
+ abilityIpfsCid: 'QmeB5SM3UxcFYKYNffhM4VMWXSnFoZPAH8iLAXTgYPPupp',
300
+ appId: 47700028661,
301
+ appVersion: 4,
302
+ policiesContext: { allow: true, evaluatedPolicies: [], allowedPolicies: {} }
303
+ }
304
+ }
305
+ ```
306
+
307
+ ## Cancel Order E2E Tests
308
+
309
+ This test will work on both Hyperliquid mainnet and testnet.
310
+
311
+ For this test to be successful, you must set the `ORDER_ID_TO_CANCEL` `const` to the order ID of an unfulfilled Spot order on the market specified by `TRADING_PAIR`. You can obtain an order ID by checking the orders of the Agent Wallet PKP on Hyperliquid mainnet or testnet by running [Open Orders](#running-the-open-orders-e2e-tests) test.
312
+
313
+ ```
314
+ pnpx nx run ability-hyperliquid:test-e2e packages/apps/ability-hyperliquid/test/e2e/spot/cancel-order.spec.ts
315
+ ```
316
+
317
+ You should see the following logs indicating the Cancel Order was successful:
318
+
319
+ ```
320
+ [should run execute to cancel order] executeResult {
321
+ success: true,
322
+ result: {
323
+ action: 'cancelOrder',
324
+ cancelResult: {
325
+ status: 'ok',
326
+ response: { type: 'cancel', data: { statuses: [ 'success' ] } }
327
+ }
328
+ },
329
+ context: {
330
+ delegation: {
331
+ delegateeAddress: '0xb50aBA1E265B52067aF97401C25C39efF57Fe83b',
332
+ delegatorPkpInfo: {
333
+ tokenId: '64398341638522492941822855531388288999933789608071521616433988119257635428447',
334
+ ethAddress: '0x17f51B528A0eA0ea19ABa0F343Dc9beED0FCc428',
335
+ publicKey: '0x04576960d83a4eaf042e585c1cf90035b869b087631312719fa2b96fecde30705dbf80711fc30f6435e6a0739d1f47201320563a7f51357c146421c96355f6cbe3'
336
+ }
337
+ },
338
+ abilityIpfsCid: 'QmeB5SM3UxcFYKYNffhM4VMWXSnFoZPAH8iLAXTgYPPupp',
339
+ appId: 47700028661,
340
+ appVersion: 4,
341
+ policiesContext: { allow: true, evaluatedPolicies: [], allowedPolicies: {} }
342
+ }
343
+ }
344
+ ```
345
+
346
+ ## Cancel All Orders E2E Tests
347
+
348
+ This test will work on both Hyperliquid mainnet and testnet.
349
+
350
+ This test simply cancels all open Spot orders for the market specified by `TRADING_PAIR`.
351
+
352
+ ```
353
+ pnpx nx run ability-hyperliquid:test-e2e packages/apps/ability-hyperliquid/test/e2e/spot/cancel-all-orders.spec.ts
354
+ ```
355
+
356
+ You should see the following logs indicating the Cancel All Orders was successful:
357
+
358
+ ```
359
+ [should run execute to cancel all orders] executeResult {
360
+ success: true,
361
+ result: { action: 'cancelAllOrdersForSymbol' },
362
+ context: {
363
+ delegation: {
364
+ delegateeAddress: '0xb50aBA1E265B52067aF97401C25C39efF57Fe83b',
365
+ delegatorPkpInfo: {
366
+ tokenId: '64398341638522492941822855531388288999933789608071521616433988119257635428447',
367
+ ethAddress: '0x17f51B528A0eA0ea19ABa0F343Dc9beED0FCc428',
368
+ publicKey: '0x04576960d83a4eaf042e585c1cf90035b869b087631312719fa2b96fecde30705dbf80711fc30f6435e6a0739d1f47201320563a7f51357c146421c96355f6cbe3'
369
+ }
370
+ },
371
+ abilityIpfsCid: 'QmeB5SM3UxcFYKYNffhM4VMWXSnFoZPAH8iLAXTgYPPupp',
372
+ appId: 47700028661,
373
+ appVersion: 4,
374
+ policiesContext: { allow: true, evaluatedPolicies: [], allowedPolicies: {} }
375
+ }
376
+ }
377
+ ```
378
+
379
+ ## Send Spot Asset E2E Tests
380
+
381
+ This test will work on both Hyperliquid mainnet and testnet.
382
+
383
+ This test will send a spot asset (USDC or other tokens) from the Agent Wallet PKP's Hyperliquid spot balance to another Hyperliquid spot account. The PKP must have at least a Spot balance of `SEND_AMOUNT` of `TOKEN`.
384
+
385
+ ```
386
+ pnpx nx run ability-hyperliquid:test-e2e packages/apps/ability-hyperliquid/test/e2e/spot/send-spot-asset.spec.ts
387
+ ```
388
+
389
+ You should see the following logs indicating the Send Spot Asset was successful:
390
+
391
+ ```
392
+ [should execute send spot asset of 1.0 USDC] {
393
+ success: true,
394
+ result: {
395
+ action: 'sendSpotAsset',
396
+ sendResult: { status: 'ok', response: { type: 'default' } }
397
+ },
398
+ context: {
399
+ delegation: {
400
+ delegateeAddress: '0xb50aBA1E265B52067aF97401C25C39efF57Fe83b',
401
+ delegatorPkpInfo: {
402
+ tokenId: '64398341638522492941822855531388288999933789608071521616433988119257635428447',
403
+ ethAddress: '0x17f51B528A0eA0ea19ABa0F343Dc9beED0FCc428',
404
+ publicKey: '0x04576960d83a4eaf042e585c1cf90035b869b087631312719fa2b96fecde30705dbf80711fc30f6435e6a0739d1f47201320563a7f51357c146421c96355f6cbe3'
405
+ }
406
+ },
407
+ abilityIpfsCid: 'QmQtGCkrvTJsLvvLvZWFhtrschv7N3SDC3nMzFQY95pkt1',
408
+ appId: 47700028661,
409
+ appVersion: 19,
410
+ policiesContext: { allow: true, evaluatedPolicies: [], allowedPolicies: {} }
411
+ }
412
+ }
413
+ ```
414
+
415
+ ## Trade History E2E Tests
416
+
417
+ This test will work on both Hyperliquid mainnet and testnet.
418
+
419
+ This test will log the Spot and Perp trade history of the Agent Wallet PKP on Hyperliquid mainnet or testnet.
420
+
421
+ ```
422
+ pnpx nx run ability-hyperliquid:test-e2e packages/apps/ability-hyperliquid/test/e2e/tradeHistory.spec.ts
423
+ ```
424
+
425
+ You should see something similar to the following logs indicating fetching the trade history was successful:
426
+
427
+ ```
428
+ [Trade History] Full user fills response: [
429
+ {
430
+ coin: 'PURR/USDC',
431
+ px: '5.1085',
432
+ sz: '2.0',
433
+ side: 'B',
434
+ time: 1762838203560,
435
+ startPosition: '0.0',
436
+ dir: 'Buy',
437
+ closedPnl: '0.0',
438
+ hash: '0x122de8de1eb2e5d013a7041b7585bb01140000c3b9b604a2b5f69430ddb6bfba',
439
+ oid: 42819254595,
440
+ crossed: true,
441
+ fee: '0.0014',
442
+ tid: 681613753058186,
443
+ feeToken: 'PURR',
444
+ twapId: null
445
+ },
446
+ {
447
+ coin: 'PURR/USDC',
448
+ px: '5.058',
449
+ sz: '2.0',
450
+ side: 'A',
451
+ time: 1762834095134,
452
+ startPosition: '2.9979',
453
+ dir: 'Sell',
454
+ closedPnl: '-0.1081569',
455
+ hash: '0x8739e4d855acc84f88b3041b752870011200fcbdf0afe7212b02902b14a0a23a',
456
+ oid: 42815741000,
457
+ crossed: true,
458
+ fee: '0.0070812',
459
+ tid: 799015822074073,
460
+ feeToken: 'USDC',
461
+ twapId: null
462
+ },
463
+ ]
464
+ [Ledger Updates] Full ledger updates: [
465
+ {
466
+ time: 1762827527721,
467
+ hash: '0x977d7afdb352d8bf98f7041b74935c01090092e34e55f7913b4626507256b2aa',
468
+ delta: {
469
+ type: 'send',
470
+ user: '0x7787794d6f3d2f71ba02d51aec2265aa09d86cb9',
471
+ destination: '0x17f51b528a0ea0ea19aba0f343dc9beed0fcc428',
472
+ sourceDex: '',
473
+ destinationDex: '',
474
+ token: 'USDC',
475
+ amount: '10.0',
476
+ usdcValue: '10.0',
477
+ fee: '1.0',
478
+ nativeTokenFee: '0.0',
479
+ nonce: 1762826770389,
480
+ feeToken: 'USDC'
481
+ }
482
+ },
483
+ {
484
+ time: 1762828781566,
485
+ hash: '0x380fc68b72b15b563989041b74afc7011800de710db47a28dbd871de31b53540',
486
+ delta: { type: 'accountClassTransfer', usdc: '5.0', toPerp: false }
487
+ },
488
+ {
489
+ time: 1762829284703,
490
+ hash: '0xe95b6753f2bc447fead5041b74bb2b0124007f398dbf63518d2412a6b1b01e6a',
491
+ delta: {
492
+ type: 'send',
493
+ user: '0x7787794d6f3d2f71ba02d51aec2265aa09d86cb9',
494
+ destination: '0x17f51b528a0ea0ea19aba0f343dc9beed0fcc428',
495
+ sourceDex: '',
496
+ destinationDex: '',
497
+ token: 'USDC',
498
+ amount: '15.0',
499
+ usdcValue: '15.0',
500
+ fee: '0.0',
501
+ nativeTokenFee: '0.0',
502
+ nonce: 1762829270081,
503
+ feeToken: ''
504
+ }
505
+ },
506
+ {
507
+ time: 1762829463681,
508
+ hash: '0x6e2e9fb3e792d9306fa8041b74bf3a011700b7998295f80211f74b06a696b31b',
509
+ delta: { type: 'accountClassTransfer', usdc: '5.0', toPerp: false }
510
+ },
511
+ {
512
+ time: 1762829518878,
513
+ hash: '0xee4c491f1b9b2213efc6041b74c0870116006104b69e40e59214f471da9efbfe',
514
+ delta: { type: 'accountClassTransfer', usdc: '5.0', toPerp: false }
515
+ },
516
+ {
517
+ time: 1762831062233,
518
+ hash: '0x890873912fa569428a82041b74e39d0111008b76caa888142cd11ee3eea9432d',
519
+ delta: { type: 'accountClassTransfer', usdc: '10.0', toPerp: false }
520
+ },
521
+ {
522
+ time: 1762833525002,
523
+ hash: '0xe3be8948bcd12990e538041b751b6c011100a12e57d448628787349b7bd5037b',
524
+ delta: {
525
+ type: 'send',
526
+ user: '0x7787794d6f3d2f71ba02d51aec2265aa09d86cb9',
527
+ destination: '0x17f51b528a0ea0ea19aba0f343dc9beed0fcc428',
528
+ sourceDex: '',
529
+ destinationDex: '',
530
+ token: 'USDC',
531
+ amount: '50.0',
532
+ usdcValue: '50.0',
533
+ fee: '0.0',
534
+ nativeTokenFee: '0.0',
535
+ nonce: 1762833515003,
536
+ feeToken: ''
537
+ }
538
+ },
539
+ {
540
+ time: 1762833584557,
541
+ hash: '0xdf1f3d7824e587f6e098041b751cc2010c00555dbfe8a6c882e7e8cae3e961e1',
542
+ delta: { type: 'accountClassTransfer', usdc: '5.0', toPerp: false }
543
+ }
544
+ ]
545
+ ```
546
+
547
+ ## Open Orders E2E Tests
548
+
549
+ This test will work on both Hyperliquid mainnet and testnet.
550
+
551
+ Running this test will fetch all open orders (Spot and Perp) for the Agent Wallet PKP, and log them to the console. This test suite doesn't actually assert anything, it's just a convenience function to view the open orders of the Agent Wallet PKP.
552
+
553
+ ```
554
+ pnpx nx run ability-hyperliquid:test-e2e packages/apps/ability-hyperliquid/test/e2e/open-orders.spec.ts
555
+ ```
556
+
557
+ You should see something similar to the following logs indicating there are open orders for the Agent Wallet PKP:
558
+
559
+ ```
560
+ [Open Orders] Full open orders response: [
561
+ {
562
+ coin: 'PURR/USDC',
563
+ side: 'A',
564
+ limitPx: '20.0',
565
+ sz: '1.0',
566
+ oid: 42821777260,
567
+ timestamp: 1762840974157,
568
+ origSz: '1.0'
569
+ }
570
+ ]
571
+ ```
572
+
573
+ ## Transfer to Perp E2E Tests
574
+
575
+ This test will work on both Hyperliquid mainnet and testnet.
576
+
577
+ This test will transfer USDC from the Agent Wallet PKP's Hyperliquid spot balance to it's Hyperliquid Perp balance. The PKP must have at least a Spot balance of `USDC_TRANSFER_AMOUNT` USDC.
578
+
579
+ ```
580
+ pnpx nx run ability-hyperliquid:test-e2e packages/apps/ability-hyperliquid/test/e2e/transfer/to-perp.spec.ts
581
+ ```
582
+
583
+ You should see the following logs indicating the transfer from the Spot balance to the Perp balance was successful:
584
+
585
+ ```
586
+ [Perps] Clearinghouse state after transfer {
587
+ marginSummary: {
588
+ accountValue: '54.0',
589
+ totalNtlPos: '0.0',
590
+ totalRawUsd: '54.0',
591
+ totalMarginUsed: '0.0'
592
+ },
593
+ crossMarginSummary: {
594
+ accountValue: '54.0',
595
+ totalNtlPos: '0.0',
596
+ totalRawUsd: '54.0',
597
+ totalMarginUsed: '0.0'
598
+ },
599
+ crossMaintenanceMarginUsed: '0.0',
600
+ withdrawable: '54.0',
601
+ assetPositions: [],
602
+ time: 1762842914141
603
+ }
604
+ [Perps] Initial balance: 45.0
605
+ [Perps] Final balance: 54.0
606
+ [Perps] Expected increase: 9
607
+ [Perps] Actual increase: 9
608
+ ```
609
+
610
+ ## Perp Long E2E Tests
611
+
612
+ This test will work on both Hyperliquid mainnet and testnet.
613
+
614
+ This test will open a long position for the token specified by `PERP_SYMBOL` using USDC. The Agent Wallet PKP must have at least a Perp balance of `PERP_LONG_USD_NOTIONAL` USDC.
615
+
616
+ Note: opening a short position while the Agent Wallet PKP has a long position will result in the long position being decreased or cancelled out.
617
+
618
+ ```
619
+ pnpx nx run ability-hyperliquid:test-e2e packages/apps/ability-hyperliquid/test/e2e/perp/long.spec.ts
620
+ ```
621
+
622
+ You should see the following logs indicating the Perp Long was successful:
623
+
624
+ ```
625
+ [Perp Long] State after long {
626
+ marginSummary: {
627
+ accountValue: '53.98844',
628
+ totalNtlPos: '13.2408',
629
+ totalRawUsd: '40.74764',
630
+ totalMarginUsed: '6.6204'
631
+ },
632
+ crossMarginSummary: {
633
+ accountValue: '53.98844',
634
+ totalNtlPos: '13.2408',
635
+ totalRawUsd: '40.74764',
636
+ totalMarginUsed: '6.6204'
637
+ },
638
+ crossMaintenanceMarginUsed: '0.66204',
639
+ withdrawable: '47.36804',
640
+ assetPositions: [
641
+ {
642
+ type: 'oneWay',
643
+ position: {
644
+ coin: 'SOL',
645
+ szi: '0.08',
646
+ leverage: { type: 'cross', value: 2 },
647
+ entryPx: '165.58',
648
+ positionValue: '13.2408',
649
+ unrealizedPnl: '-0.0056',
650
+ returnOnEquity: '-0.0008455127',
651
+ liquidationPx: null,
652
+ marginUsed: '6.6204',
653
+ maxLeverage: 10,
654
+ cumFunding: { allTime: '0.0', sinceOpen: '0.0', sinceChange: '0.0' }
655
+ }
656
+ }
657
+ ],
658
+ time: 1762843035929
659
+ }
660
+ [Perp Long] Initial position size: 0
661
+ [Perp Long] Final position size: 0.08
662
+ [Perp Long] Position value: 13.2408
663
+ [Perp Long] Unrealized PnL: -0.0056
664
+ [Perp Long] Initial account value: 54.0
665
+ [Perp Long] Final account value: 53.98844
666
+ ```
667
+
668
+ Note: you can pass the `reduceOnly` flag when calling `execute` to close a short position without worrying about the exact size:
669
+
670
+ ```ts
671
+ const executeResult = await hyperliquidAbilityClient.execute(
672
+ {
673
+ action: HyperliquidAction.PERP_LONG,
674
+ useTestnet: USE_TESTNET,
675
+ perp: {
676
+ symbol: PERP_SYMBOL,
677
+ price: longPrice,
678
+ size: longSize,
679
+ leverage: LEVERAGE,
680
+ isCross: IS_CROSS,
681
+ reduceOnly: true, // <---
682
+ orderType: { type: OrderType.MARKET },
683
+ },
684
+ },
685
+ {
686
+ delegatorPkpEthAddress: agentPkpInfo.ethAddress,
687
+ },
688
+ );
689
+ ```
690
+
691
+ Note: if you open a position using `OrderType.MARKET`, your order is likely to be immediately filled. If you want to see your open position, use the [Balance E2E Tests](#balance-e2e-tests) to check the Perp position of the Agent Wallet PKP:
692
+
693
+ ```
694
+ [Perp Balance] Full perp state: {
695
+ marginSummary: {
696
+ accountValue: '54.025312',
697
+ totalNtlPos: '36.322',
698
+ totalRawUsd: '90.347312',
699
+ totalMarginUsed: '18.161'
700
+ },
701
+ crossMarginSummary: {
702
+ accountValue: '54.025312',
703
+ totalNtlPos: '36.322',
704
+ totalRawUsd: '90.347312',
705
+ totalMarginUsed: '18.161'
706
+ },
707
+ crossMaintenanceMarginUsed: '1.8161',
708
+ withdrawable: '35.864312',
709
+ assetPositions: [
710
+ {
711
+ type: 'oneWay',
712
+ position: {
713
+ coin: 'SOL',
714
+ szi: '-0.22',
715
+ leverage: { type: 'cross', value: 2 },
716
+ entryPx: '165.38',
717
+ positionValue: '36.322',
718
+ unrealizedPnl: '0.0616',
719
+ returnOnEquity: '0.003386141',
720
+ liquidationPx: '391.1139047619',
721
+ marginUsed: '18.161',
722
+ maxLeverage: 10,
723
+ cumFunding: { allTime: '0.0', sinceOpen: '0.0', sinceChange: '0.0' }
724
+ }
725
+ }
726
+ ],
727
+ time: 1762844136785
728
+ }
729
+ ```
730
+
731
+ ## Perp Short E2E Tests
732
+
733
+ This test will work on both Hyperliquid mainnet and testnet.
734
+
735
+ This test will open short position for the token specified by `PERP_SYMBOL` using USDC. The Agent Wallet PKP must have at least a Perp balance of `PERP_SHORT_USD_NOTIONAL` USDC.
736
+
737
+ Note: opening a short position while the Agent Wallet PKP has a long position will result in the long position being decreased or cancelled out.
738
+
739
+ ```
740
+ pnpx nx run ability-hyperliquid:test-e2e packages/apps/ability-hyperliquid/test/e2e/perp/short.spec.ts
741
+ ```
742
+
743
+ You should see the following logs indicating the Perp Short was successful:
744
+
745
+ ```
746
+ [Perp Short] State after short {
747
+ marginSummary: {
748
+ accountValue: '53.978594',
749
+ totalNtlPos: '3.3096',
750
+ totalRawUsd: '57.288194',
751
+ totalMarginUsed: '1.6548'
752
+ },
753
+ crossMarginSummary: {
754
+ accountValue: '53.978594',
755
+ totalNtlPos: '3.3096',
756
+ totalRawUsd: '57.288194',
757
+ totalMarginUsed: '1.6548'
758
+ },
759
+ crossMaintenanceMarginUsed: '0.16548',
760
+ withdrawable: '52.323794',
761
+ assetPositions: [
762
+ {
763
+ type: 'oneWay',
764
+ position: {
765
+ coin: 'SOL',
766
+ szi: '-0.02',
767
+ leverage: { type: 'cross', value: 2 },
768
+ entryPx: '165.48',
769
+ positionValue: '3.3096',
770
+ unrealizedPnl: '0.0',
771
+ returnOnEquity: '0.0',
772
+ liquidationPx: '2728.0092380952',
773
+ marginUsed: '1.6548',
774
+ maxLeverage: 10,
775
+ cumFunding: { allTime: '0.0', sinceOpen: '0.0', sinceChange: '0.0' }
776
+ }
777
+ }
778
+ ],
779
+ time: 1762843337806
780
+ }
781
+ [Perp Short] Initial position size: 0.08
782
+ [Perp Short] Final position size: -0.02
783
+ [Perp Short] Position value: 3.3096
784
+ [Perp Short] Unrealized PnL: 0.0
785
+ [Perp Short] Initial account value: 53.99324
786
+ [Perp Short] Final account value: 53.978594
787
+ ```
788
+
789
+ Note: you can pass the `reduceOnly` flag when calling `execute` to close a long position without worrying about the exact size:
790
+
791
+ ```ts
792
+ const executeResult = await hyperliquidAbilityClient.execute(
793
+ {
794
+ action: HyperliquidAction.PERP_SHORT,
795
+ useTestnet: USE_TESTNET,
796
+ perp: {
797
+ symbol: PERP_SYMBOL,
798
+ price: shortPrice,
799
+ size: shortSize,
800
+ leverage: LEVERAGE,
801
+ isCross: IS_CROSS,
802
+ reduceOnly: true, // <---
803
+ orderType: { type: OrderType.MARKET },
804
+ },
805
+ },
806
+ {
807
+ delegatorPkpEthAddress: agentPkpInfo.ethAddress,
808
+ },
809
+ );
810
+ ```
811
+
812
+ Note: if you open a position using `OrderType.MARKET`, your order is likely to be immediately filled. If you want to see your open position, use the [Balance E2E Tests](#balance-e2e-tests) to check the Perp position of the Agent Wallet PKP:
813
+
814
+ ```
815
+ [Perp Balance] Full perp state: {
816
+ marginSummary: {
817
+ accountValue: '54.025312',
818
+ totalNtlPos: '36.322',
819
+ totalRawUsd: '90.347312',
820
+ totalMarginUsed: '18.161'
821
+ },
822
+ crossMarginSummary: {
823
+ accountValue: '54.025312',
824
+ totalNtlPos: '36.322',
825
+ totalRawUsd: '90.347312',
826
+ totalMarginUsed: '18.161'
827
+ },
828
+ crossMaintenanceMarginUsed: '1.8161',
829
+ withdrawable: '35.864312',
830
+ assetPositions: [
831
+ {
832
+ type: 'oneWay',
833
+ position: {
834
+ coin: 'SOL',
835
+ szi: '-0.22',
836
+ leverage: { type: 'cross', value: 2 },
837
+ entryPx: '165.38',
838
+ positionValue: '36.322',
839
+ unrealizedPnl: '0.0616',
840
+ returnOnEquity: '0.003386141',
841
+ liquidationPx: '391.1139047619',
842
+ marginUsed: '18.161',
843
+ maxLeverage: 10,
844
+ cumFunding: { allTime: '0.0', sinceOpen: '0.0', sinceChange: '0.0' }
845
+ }
846
+ }
847
+ ],
848
+ time: 1762844136785
849
+ }
850
+ ```
851
+
852
+ ## Send Perp USDC E2E Tests
853
+
854
+ This test will work on both Hyperliquid mainnet and testnet.
855
+
856
+ This test will send USDC from the Agent Wallet PKP's Hyperliquid Perp balance to another Hyperliquid perp account. The PKP must have at least a Perp balance of `SEND_AMOUNT_USDC` USDC.
857
+
858
+ ```
859
+ pnpx nx run ability-hyperliquid:test-e2e packages/apps/ability-hyperliquid/test/e2e/perp/send-perp-usdc.spec.ts
860
+ ```
861
+
862
+ You should see the following logs indicating the Send Perp USDC was successful:
863
+
864
+ ```
865
+ [should execute send perp USDC of 1.0 USDC] {
866
+ success: true,
867
+ result: {
868
+ action: 'sendPerpUsdc',
869
+ sendResult: { status: 'ok', response: { type: 'default' } }
870
+ },
871
+ context: {
872
+ delegation: {
873
+ delegateeAddress: '0xb50aBA1E265B52067aF97401C25C39efF57Fe83b',
874
+ delegatorPkpInfo: {
875
+ tokenId: '64398341638522492941822855531388288999933789608071521616433988119257635428447',
876
+ ethAddress: '0x17f51B528A0eA0ea19ABa0F343Dc9beED0FCc428',
877
+ publicKey: '0x04576960d83a4eaf042e585c1cf90035b869b087631312719fa2b96fecde30705dbf80711fc30f6435e6a0739d1f47201320563a7f51357c146421c96355f6cbe3'
878
+ }
879
+ },
880
+ abilityIpfsCid: 'QmcKrDBzYPhBoe1CTAqrKSNcpZpDRsSuAt3PCUG5ZAWWXN',
881
+ appId: 47700028661,
882
+ appVersion: 18,
883
+ policiesContext: { allow: true, evaluatedPolicies: [], allowedPolicies: {} }
884
+ }
885
+ }
886
+ ```
887
+
888
+ ## Withdraw E2E Tests
889
+
890
+ This test will work on both Hyperliquid mainnet and testnet.
891
+
892
+ This test will withdraw USDC from the Agent Wallet PKP's Hyperliquid Perp balance to the PKP's ETH address on Arbitrum. The PKP must have at least a Perp balance of `WITHDRAW_AMOUNT_USDC` USDC.
893
+
894
+ ```
895
+ pnpx nx run ability-hyperliquid:test-e2e packages/apps/ability-hyperliquid/test/e2e/withdraw.spec.ts
896
+ ```
897
+
898
+ You should see the following logs indicating the Withdraw was successful:
899
+
900
+ ```
901
+ [should execute withdrawal of 5.0 USDC to Arbitrum] {
902
+ success: true,
903
+ result: {
904
+ action: 'withdraw',
905
+ withdrawResult: { status: 'ok', response: { type: 'default' } }
906
+ },
907
+ context: {
908
+ delegation: {
909
+ delegateeAddress: '0xb50aBA1E265B52067aF97401C25C39efF57Fe83b',
910
+ delegatorPkpInfo: {
911
+ tokenId: '64398341638522492941822855531388288999933789608071521616433988119257635428447',
912
+ ethAddress: '0x17f51B528A0eA0ea19ABa0F343Dc9beED0FCc428',
913
+ publicKey: '0x04576960d83a4eaf042e585c1cf90035b869b087631312719fa2b96fecde30705dbf80711fc30f6435e6a0739d1f47201320563a7f51357c146421c96355f6cbe3'
914
+ }
915
+ },
916
+ abilityIpfsCid: 'QmQtGCkrvTJsLvvLvZWFhtrschv7N3SDC3nMzFQY95pkt1',
917
+ appId: 47700028661,
918
+ appVersion: 19,
919
+ policiesContext: { allow: true, evaluatedPolicies: [], allowedPolicies: {} }
920
+ }
921
+ }
922
+ ```