Skip to content

Commit 80ecf25

Browse files
committed
wip: withdraw issue
1 parent d86938f commit 80ecf25

File tree

6 files changed

+64
-54
lines changed

6 files changed

+64
-54
lines changed

common/src/vault.rs

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use near_sdk::{
66
near, require, AccountId, Gas, Promise, PromiseOrValue,
77
};
88

9-
use crate::asset::{BorrowAsset, FungibleAsset};
9+
use crate::{
10+
asset::{BorrowAsset, FungibleAsset},
11+
supply::SupplyPosition,
12+
};
1013

1114
pub type TimestampNs = u64;
1215

@@ -508,13 +511,13 @@ impl IdleBalanceDelta {
508511
#[near(event_json(standard = "templar-vault"))]
509512
pub enum Event {
510513
#[event_version("1.0.0")]
511-
MintedShares { amount: U128, receiver: AccountId },
514+
IdleBalanceUpdated { prev: U128, delta: IdleBalanceDelta },
512515
#[event_version("1.0.0")]
513-
AllocationStarted { op_id: U64, remaining: U128 },
516+
PerformanceFeeAccrued { recipient: AccountId, shares: U128 },
514517
#[event_version("1.0.0")]
515-
IdleBalanceUpdated { prev: U128, delta: IdleBalanceDelta },
518+
LockChange { is_locked: bool, market_index: u32 },
516519

517-
// Allocation lifecycle (plan/request)
520+
// Allocation
518521
#[event_version("1.0.0")]
519522
AllocationRequestedQueue { op_id: U64, total: U128 },
520523
#[event_version("1.0.0")]
@@ -523,8 +526,8 @@ pub enum Event {
523526
total: U128,
524527
plan: Vec<(AccountId, U128)>,
525528
},
526-
527-
// Per-step planning and outcomes
529+
#[event_version("1.0.0")]
530+
AllocationStarted { op_id: U64, remaining: U128 },
528531
#[event_version("1.0.0")]
529532
AllocationStepPlanned {
530533
op_id: U64,
@@ -563,8 +566,6 @@ pub enum Event {
563566
refunded: U128,
564567
remaining_after: U128,
565568
},
566-
567-
// Completion and stop
568569
#[event_version("1.0.0")]
569570
AllocationCompleted { op_id: u64 },
570571
#[event_version("1.0.0")]
@@ -575,12 +576,6 @@ pub enum Event {
575576
reason: Option<String>,
576577
},
577578

578-
#[event_version("1.0.0")]
579-
PerformanceFeeAccrued { recipient: AccountId, shares: U128 },
580-
581-
#[event_version("1.0.0")]
582-
LockChange { is_locked: bool, market_index: u32 },
583-
584579
// Admin and configuration events
585580
#[event_version("1.0.0")]
586581
CuratorSet { account: AccountId },
@@ -594,7 +589,6 @@ pub enum Event {
594589
FeeRecipientSet { account: AccountId },
595590
#[event_version("1.0.0")]
596591
PerformanceFeeSet { fee: U128 },
597-
598592
#[event_version("1.0.0")]
599593
TimelockSet { seconds: U64 },
600594
#[event_version("1.0.0")]
@@ -606,34 +600,30 @@ pub enum Event {
606600
#[event_version("1.0.0")]
607601
MarketCreated { market: AccountId },
608602
#[event_version("1.0.0")]
603+
MarketEnabled { market: AccountId },
604+
#[event_version("1.0.0")]
605+
MarketRemovalSubmitted {
606+
market: AccountId,
607+
removable_at: U64,
608+
},
609+
#[event_version("1.0.0")]
610+
MarketRemovalRevoked { market: AccountId },
611+
#[event_version("1.0.0")]
609612
SupplyCapRaiseSubmitted {
610613
market: AccountId,
611614
new_cap: U128,
612615
valid_at_ns: u64,
613616
},
614617
#[event_version("1.0.0")]
615618
SupplyCapRaiseRevoked { market: AccountId },
616-
617619
#[event_version("1.0.0")]
618620
SupplyCapSet { market: AccountId, new_cap: U128 },
619-
#[event_version("1.0.0")]
620-
MarketEnabled { market: AccountId },
621-
#[event_version("1.0.0")]
622-
MarketAlreadyInWithdrawQueue { market: AccountId },
623-
#[event_version("1.0.0")]
624-
WithdrawQueueMarketAdded { market: AccountId },
621+
625622
#[event_version("1.0.0")]
626623
WithdrawDequeued { index: U64 },
627624
#[event_version("1.0.0")]
628625
WithdrawalParked { id: U64 },
629626
#[event_version("1.0.0")]
630-
MarketRemovalSubmitted {
631-
market: AccountId,
632-
removable_at: U64,
633-
},
634-
#[event_version("1.0.0")]
635-
MarketRemovalRevoked { market: AccountId },
636-
#[event_version("1.0.0")]
637627
WithdrawExecutionRequired { op_id: U64, market_index: u32 },
638628

639629
// User flows
@@ -672,19 +662,27 @@ pub enum Event {
672662

673663
// Withdrawal read diagnostics
674664
#[event_version("1.0.0")]
675-
WithdrawalPositionReadFailed {
665+
CreateWithdrawalFailed {
676666
op_id: U64,
677667
market: AccountId,
678668
index: u32,
679-
before: U128,
669+
need: U128,
680670
},
681671

682672
#[event_version("1.0.0")]
683-
CreateWithdrawalFailed {
673+
WithdrawalInflowMismatch {
684674
op_id: U64,
685675
market: AccountId,
686676
index: u32,
687-
need: U128,
677+
delta: U128,
678+
inflow: U128,
679+
},
680+
#[event_version("1.0.0")]
681+
WithdrawalOverpayCredited {
682+
op_id: U64,
683+
market: AccountId,
684+
index: u32,
685+
extra: U128,
688686
},
689687

690688
// Payout and stop diagnostics
@@ -720,27 +718,29 @@ pub enum Event {
720718
},
721719

722720
#[event_version("1.0.0")]
723-
WithdrawalPositionMissing {
721+
ReportedPosition {
724722
op_id: U64,
725723
market: AccountId,
726724
index: u32,
727-
before: U128,
725+
position: SupplyPosition,
728726
},
729727
#[event_version("1.0.0")]
730-
WithdrawalInflowMismatch {
728+
PositionReadFailed {
731729
op_id: U64,
732730
market: AccountId,
733731
index: u32,
734-
delta: U128,
735-
inflow: U128,
732+
before: U128,
736733
},
737734
#[event_version("1.0.0")]
738-
WithdrawalOverpayCredited {
735+
PositionMissing {
739736
op_id: U64,
740737
market: AccountId,
741738
index: u32,
742-
extra: U128,
739+
before: U128,
743740
},
741+
742+
#[event_version("1.0.0")]
743+
VaultBalance { amount: U128 },
744744
}
745745

746746
#[near(serializers = [borsh, serde])]

contract/vault/src/impl_callbacks.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ impl Contract {
197197
}
198198

199199
#[private]
200-
pub fn execute_withdraw_01_call_market_fetch_position(
200+
pub fn execute_withdraw_01_execute_withdraw_fetch_position(
201201
&mut self,
202202
#[callback_result] before_balance: Result<U128, PromiseError>,
203203
op_id: u64,
@@ -212,6 +212,11 @@ impl Contract {
212212
let principal = self.principal_of(&market);
213213
let before_balance = before_balance.unwrap_or(U128(self.idle_balance));
214214

215+
Event::VaultBalance {
216+
amount: before_balance,
217+
}
218+
.emit();
219+
215220
PromiseOrValue::Promise(
216221
ext_market::ext(market.clone())
217222
// NOTE: gas might be incorrect here
@@ -264,9 +269,18 @@ impl Contract {
264269
};
265270

266271
let reported_principal: u128 = match position {
267-
Ok(Some(position)) => position.get_deposit().total().into(),
272+
Ok(Some(position)) => {
273+
Event::ReportedPosition {
274+
op_id: op_id.into(),
275+
market: market.clone(),
276+
index: market_index,
277+
position: position.clone(),
278+
}
279+
.emit();
280+
position.get_deposit().total().into()
281+
}
268282
Ok(None) => {
269-
Event::WithdrawalPositionMissing {
283+
Event::PositionMissing {
270284
op_id: op_id.into(),
271285
market: market.clone(),
272286
index: market_index,
@@ -277,7 +291,7 @@ impl Contract {
277291
0
278292
}
279293
Err(_) => {
280-
Event::WithdrawalPositionReadFailed {
294+
Event::PositionReadFailed {
281295
op_id: op_id.into(),
282296
market: market.clone(),
283297
index: market_index,

contract/vault/src/impl_token_receiver.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,6 @@ impl Contract {
122122
self.mint(&Nep141Mint::new(shares, &sender_id))
123123
.unwrap_or_else(|_| env::panic_str("Failed to mint shares"));
124124

125-
Event::MintedShares {
126-
amount: shares.into(),
127-
receiver: sender_id.clone(),
128-
}
129-
.emit();
130-
131125
self.update_idle_balance(IdleBalanceDelta::Increase(accept.into()));
132126
self.last_total_assets = self.last_total_assets.saturating_add(accept);
133127

contract/vault/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ impl Contract {
282282
.unwrap_or_else(|| env::panic_str("pending vanished unexpectedly"));
283283
let owner = pending.owner.clone();
284284
let receiver = pending.receiver.clone();
285+
env::log_str(&format!("Executing withdrawal for {pending:?}"));
285286

286287
if pending.expected_assets == 0 {
287288
// Skip dust request to avoid wedging the queue
@@ -336,7 +337,7 @@ impl Contract {
336337
.then(
337338
Self::ext(env::current_account_id())
338339
.with_static_gas(EXECUTE_WITHDRAW_01_FETCH_POSITION_GAS)
339-
.execute_withdraw_01_call_market_fetch_position(
340+
.execute_withdraw_01_execute_withdraw_fetch_position(
340341
op_id.into(),
341342
market_index,
342343
batch_limit,
@@ -964,6 +965,7 @@ impl Contract {
964965
};
965966

966967
if remaining == 0 {
968+
// FIXME: event for coveredbyidle
967969
// Already fully covered by idle => payout
968970
self.pay(
969971
op_id,

contract/vault/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2103,7 +2103,7 @@ fn after_exec_withdraw_req_returns_promise(mut c: Contract) {
21032103
escrow_shares: 0,
21042104
});
21052105

2106-
let res = c.execute_withdraw_01_call_market_fetch_position(Ok(U128(1)), op_id, 0, None);
2106+
let res = c.execute_withdraw_01_execute_withdraw_fetch_position(Ok(U128(1)), op_id, 0, None);
21072107
match res {
21082108
PromiseOrValue::Promise(_) => {}
21092109
_ => panic!("Expected Promise to read supply position after exec"),

contract/vault/tests/happy_path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ async fn happy(#[future(awt)] worker: Worker<Sandbox>) {
107107
.await
108108
.expect("Failed to get withdrawing op id");
109109
vault
110-
.execute_market_withdrawal(&vault_curator, op_id, 0, None)
110+
.execute_market_withdrawal(&vault_curator, op_id, 0, Some(10))
111111
.await;
112112

113113
assert_eq!(

0 commit comments

Comments
 (0)