Skip to content

Commit 26d0177

Browse files
authored
Initial implementation of payback loan (#3793)
* init * update * init * update * update oa type check * remove unneeded * fix tests * fix tests * small refactoring * fix * update * update * add equity param * add comment * Try to fix the clearinghouseState response struct * fix price calculation * fix compile * fix calculations * fix bug * bug fix * fix bugs * fix bugs regarding pricing * check spot buy size * clippy * try to debug * Revert "try to debug" This reverts commit ab18312. * fix position size
1 parent 54b89c6 commit 26d0177

File tree

13 files changed

+47225
-839
lines changed

13 files changed

+47225
-839
lines changed

tee-worker/omni-executor/Cargo.lock

Lines changed: 180 additions & 181 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tee-worker/omni-executor/executor-storage/src/loan_record.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ pub struct LoanRecord {
2121
pub usdc_loaned: String,
2222
pub spot_sell_cloid: String,
2323
pub hedge_open_cloid: String,
24+
/// Actual position size that was opened for this loan (filled or partially filled)
25+
/// This should be set after the hedge order completes in request_loan
26+
pub position_size: String,
2427
}
2528

2629
pub struct LoanRecordStorage {

tee-worker/omni-executor/heima/authentication/src/auth_token.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ mod tests {
112112
use super::*;
113113
use chrono::{Days, Utc};
114114
use executor_primitives::{utils::hex::hex_encode, Identity, Web2IdentityType};
115-
use parity_scale_codec::Encode;
116115
use rsa::{pkcs1::EncodeRsaPrivateKey, RsaPrivateKey};
117116

118117
#[derive(PartialEq, Debug, Serialize, Deserialize)]

tee-worker/omni-executor/hyperliquid/src/corewriter.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,57 @@ pub fn encode_usd_class_transfer_action(ntl: u64, to_perp: bool) -> Vec<u8> {
120120
pub fn build_usd_class_transfer_to_perp(ntl: u64) -> Vec<u8> {
121121
encode_usd_class_transfer_action(ntl, true)
122122
}
123+
124+
pub fn build_usd_class_transfer_to_spot(ntl: u64) -> Vec<u8> {
125+
encode_usd_class_transfer_action(ntl, false)
126+
}
127+
128+
pub fn build_perp_close_order(asset_id: u32, size: u64, price: u64, cloid: u128) -> Vec<u8> {
129+
// Close position by selling (is_buy = false) with reduce_only = true
130+
// Note: For long positions, we need to sell to close
131+
// The size should match the position size we want to close
132+
encode_limit_order_action_with_reduce_only(asset_id, false, price, size, cloid, true)
133+
}
134+
135+
pub fn build_spot_buy_order(asset_id: u32, size: u64, price: u64, cloid: u128) -> Vec<u8> {
136+
let is_buy = true;
137+
encode_limit_order_action(asset_id, is_buy, price, size, cloid)
138+
}
139+
140+
pub fn build_cancel_order_by_cloid(asset_id: u32, cloid: u128) -> Vec<u8> {
141+
let encoded =
142+
ethabi::encode(&[ethabi::Token::Uint(asset_id.into()), ethabi::Token::Uint(cloid.into())]);
143+
144+
let mut data = Vec::new();
145+
data.push(0x01); // version
146+
data.extend_from_slice(&[0x00, 0x00, 0x0b]); // action_id = 11 (cancel order by cloid)
147+
data.extend_from_slice(&encoded);
148+
data
149+
}
150+
151+
fn encode_limit_order_action_with_reduce_only(
152+
asset: u32,
153+
is_buy: bool,
154+
limit_px: u64,
155+
sz: u64,
156+
cloid: u128,
157+
reduce_only: bool,
158+
) -> Vec<u8> {
159+
let encoded_tif: u8 = 2; // Gtc
160+
161+
let encoded = ethabi::encode(&[
162+
ethabi::Token::Uint(asset.into()),
163+
ethabi::Token::Bool(is_buy),
164+
ethabi::Token::Uint(limit_px.into()),
165+
ethabi::Token::Uint(sz.into()),
166+
ethabi::Token::Bool(reduce_only),
167+
ethabi::Token::Uint(encoded_tif.into()),
168+
ethabi::Token::Uint(cloid.into()),
169+
]);
170+
171+
let mut data = Vec::new();
172+
data.push(0x01); // version
173+
data.extend_from_slice(&[0x00, 0x00, 0x01]); // action_id = 1
174+
data.extend_from_slice(&encoded);
175+
data
176+
}

0 commit comments

Comments
 (0)