-
Notifications
You must be signed in to change notification settings - Fork 0
[core] Implement CrestVault
#2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
WalkthroughAdds a new core package implementing a multi-contract vault system (Vault, Accountant, Teller, Manager), supporting scripts, tests, mocks, Foundry config, TypeScript index fetcher, remappings, tooling configs, and three git submodules for dependencies. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Teller as CrestTeller
participant Vault as CrestVault
participant Accountant as CrestAccountant
participant Manager as CrestManager
participant HL as HyperliquidCore
rect #E8F4F1
User->>+Teller: deposit(assets, receiver)
Teller->>Accountant: previewDeposit / compute shares
Teller-->>Vault: enter(from, asset, amount, to, shares)
Vault-->>User: emit Enter / Deposit
end
rect #F3F3FF
Curator/Admin->>+Manager: allocate(spotIndex, perpIndex)
Manager->>Vault: request assets / transfer
Manager->>HL: bridge funds, place orders (spot/perp)
HL-->>Manager: order confirmations / indices
Manager-->>Vault: emit Allocated
end
rect #FFF7E6
Curator/Admin->>Manager: rebalance() / closeAllPositions()
Manager->>HL: close positions, retrieve assets
Manager-->>Vault: emit Rebalanced / PositionClosed
Manager->>Accountant: trigger rate update (optional)
end
Estimated code review effort🎯 5 (Critical) | ⏱️ ~120 minutes Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Free 📒 Files selected for processing (1)
Note 🎁 Summarized by CodeRabbit FreeYour organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Pro by visiting https://app.coderabbit.ai/login. Comment |
| class HyperliquidAPI { | ||
| async _requestInfo<T extends object>(request: { type: string }): Promise<T> { | ||
| const response = await fetch('https://api.hyperliquid.xyz/info', { | ||
| method: 'POST', | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| body: JSON.stringify(request), | ||
| }); | ||
| return response.json(); | ||
| } | ||
|
|
||
| async _getSpotIndex(symbol: string) { | ||
| const { tokens, universe } = await this._requestInfo<{ | ||
| tokens: { name: string; index: number }[]; | ||
| universe: { | ||
| index: number; | ||
| tokens: [tokenIndex: number, quoteTokenIndex: number]; | ||
| }[]; | ||
| }>({ type: 'spotMeta' }); | ||
|
|
||
| const token = tokens.find((token) => token.name === symbol); | ||
| if (token?.index === undefined) { | ||
| throw new Error(`Token ${symbol} not found`); | ||
| } | ||
| const spot = universe.find((asset) => asset.tokens[0] === token.index); | ||
| if (spot?.index === undefined) { | ||
| throw new Error(`Spot ${symbol} not found`); | ||
| } | ||
| return { | ||
| tokenIndex: token.index, | ||
| spotIndex: spot.index, | ||
| meta: { token, spot }, | ||
| }; | ||
| } | ||
|
|
||
| async _getPerpIndex(symbol: string) { | ||
| const { universe } = await this._requestInfo<{ | ||
| universe: { | ||
| szDecimals: number; | ||
| name: string; | ||
| maxLeverage: number; | ||
| marginTableId: number; | ||
| }[]; | ||
| }>({ type: 'meta' }); | ||
|
|
||
| const perpIndex = universe.findIndex((asset) => asset.name === symbol); | ||
| return { perpIndex, meta: universe[perpIndex] }; | ||
| } | ||
|
|
||
| async getIndexesBySymbol(symbol: string) { | ||
| const spot = await this._getSpotIndex(symbol); | ||
| const perp = await this._getPerpIndex(symbol); | ||
| return { | ||
| symbol, | ||
| tokenIndex: spot.tokenIndex, | ||
| spotIndex: spot.spotIndex, | ||
| perpIndex: perp.perpIndex, | ||
| }; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이거는 다른 모듈에다 가져다 써도 되겠다
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
allocate (vault 에 남아 있는 USDC 를 가지고 기존 포지션 확장) 및 rebalance (포지션 변경 체크) 시에 자산의 spot/perp index 넣어줘야 함!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
지금 쌓고 있는 데이터에 자산의 spot/perp index을 추가로 더 쌓아야 하나? 아니면 컨트랙트 안에서만 사용하는거야?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
spot/perp index 는 immutable (아마) 해서, 컨트랙트의 allocate/rebalance 호출하기 전에 가져오면 될 듯
CrestVaultCrestVault
- Fixed arithmetic overflow in P&L calculations by adding proper type casting - Changed minimum balance requirement from 200 to 50 USDT0 for better capital efficiency - Replaced simplified market maker comments with actual order placement - Updated outdated comment about closeAllPositions using placeholder amounts - All 27 tests now pass with proper rate limiting 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
Summary by CodeRabbit