Narwallets / meta-pool

NEAR protocol Meta Staking Pool, tokenized stake, liquid unstake, stNEAR, $META
Other
43 stars 16 forks source link

Cross-contract liquid unstake #15

Closed TrevorJTClarke closed 2 years ago

TrevorJTClarke commented 2 years ago

I have implemented ability for my contract to manage staking with metapool. It is actually quite cumbersome, so I propose an easy fix to make this straightforward for others to integrate.

Problem:

  1. You need to get your stake balance to know what your balance is to liquid unstake (not a problem IMO)
  2. You need to understand what amount is available so you can compute min_expected_near as it is required here: https://github.com/Narwallets/meta-pool/blob/master/metapool/src/lib.rs#L553-L557, for now I am using: https://github.com/Narwallets/meta-pool/blob/master/metapool/src/lib.rs#L534 but more callbacks makes expensive
  3. Once I know both balance and min_expected_near I can do the final call to liquid_unstake

My problem is more with efficiency, if I was able to get the min_expected_near of the total staked during the balance call, id be able to compute a ratio to calculate all other liquid unstake amounts.

Solution:

  1. Add min_expected_near_total to the response of https://github.com/Narwallets/meta-pool/blob/master/metapool/src/owner.rs#L163
  2. Add liquid_unstake_fee to the same response ^^, which allows for any other amount of st_near to be computed for accurate NEAR transfer (I want 5 NEAR, how much st_near does that actually mean?)
fercargo commented 2 years ago

Hi Trevor! Thanks! this is a very detailed issue

Notes:

  1. if you are willing to pay current liquid-unstake fees (0.3-3%) you can send zero in min_expected_near, you can just send zero. The idea for min_expected_near is that you don't pay more fee that the one you're willing to pay. For example, stNEAR price is 1.1, you lliquid-unstake 100 stNEAR, and you're willing to pay 1%. So you say: 100 stNEAR->110 NEAR, 1% fee = 0.11, so min_expected_near = 105 - 0.11 = 104.89 you don't need to send a value computed from data in the contract, if you are willing to pay current liquid-unstake fees (0.3-3%) you can send zero in min_expected_near

  2. pub fn get_near_amount_sell_stnear will return you the NEAR amount you'll receive, fees included, it's like simulating a liquid-unstake.

  3. re: Add min_expected_near_total to the response of https://github.com/Narwallets/meta-pool/blob/master/metapool/src/owner.rs#L163 Sadly we can not change pub fn get_known_deposited_balance because it is part of the standard staking-pool interface near/core-contracts/staking-pool