pub fn remove_liquidity_by_tokens(
        &mut self,
        sender_id: &AccountId,
        amounts: Vec<Balance>,
        max_burn_shares: Balance,
        fees: &AdminFees,
    ) -> Balance {
        let n_coins = self.token_account_ids.len();
        assert_eq!(amounts.len(), n_coins, "{}", ERR64_TOKENS_COUNT_ILLEGAL);
        let prev_shares_amount = self.shares.get(&sender_id).expect(ERR13_LP_NOT_REGISTERED);

        // make amounts into comparable-amounts
        let c_amounts = self.amounts_to_c_amounts(&amounts);
        for i in 0..n_coins {
            self.assert_min_reserve(self.c_amounts[i].checked_sub(c_amounts[i]).unwrap_or(0));
        }

        let invariant = self.get_invariant();
        let trade_fee = Fees::new(self.total_fee, &fees);

        let (burn_shares, fee_part) = invariant
            .compute_lp_amount_for_withdraw(
                &c_amounts,
                &self.c_amounts,
                self.shares_total_supply,
                &trade_fee,
            )
            .expect(ERR67_LPSHARE_CALC_ERR);

        assert!(
            burn_shares <= prev_shares_amount,
            "{}",
            ERR34_INSUFFICIENT_LP_SHARES
        );
        assert!(burn_shares <= max_burn_shares, "{}", ERR68_SLIPPAGE);

        for i in 0..n_coins {
            self.c_amounts[i] = self.c_amounts[i].checked_sub(c_amounts[i]).unwrap();
            self.assert_min_reserve(self.c_amounts[i]);
        }
        self.burn_shares(&sender_id, prev_shares_amount, burn_shares);
        env::log(
            format!(
                "LP {} removed {} shares by given tokens, and fee is {} shares",
                sender_id, burn_shares, fee_part
            )
            .as_bytes(),
        );

        if fee_part > 0 {
            // referral fee
            if let Some(referral) = &fees.referral_id {
                if self.shares.get(referral).is_some() {
                    let referral_share = fee_part * fees.referral_fee as u128 / FEE_DIVISOR as u128;
                    self.mint_shares(referral, referral_share);
                    env::log(
                        format!("Referral {} got {} shares", referral, referral_share).as_bytes(),
                    );
                }
            }
            // exchange fee
            let exchange_share = fee_part * fees.exchange_fee as u128 / FEE_DIVISOR as u128;
            self.mint_shares(&fees.exchange_id, exchange_share);
            env::log(
                format!("Admin {} got {} shares", &fees.exchange_id, exchange_share).as_bytes(),
            );
        }

        burn_shares
    }
/// Remove liquidity from the pool by fixed tokens-out,
/// allows to remove liquidity of a subset of tokens, by providing 0 in `amounts`.
/// Fee will be charged according to diff between ideal token portions.
Remove liquidity from the pool by fixed tokens-out,
allows to remove liquidity of a subset of tokens, by providing 0 in `amounts`.
Fee will be charged according to diff between ideal token portions.
Remove liquidity from the pool by fixed tokens - out allows to remove liquidity of a subset of tokens by providing 0 in ` amounts ` . Fee will be charged according to diff between ideal token portions .
pub fn remove_liquidity_by_tokens(
        &mut self,
        sender_id: &AccountId,
        amounts: Vec<Balance>,
        max_burn_shares: Balance,
        fees: &AdminFees,
    ) -> Balance {
        let n_coins = self.token_account_ids.len();
        assert_eq!(amounts.len(), n_coins, "{}", ERR64_TOKENS_COUNT_ILLEGAL);
        let prev_shares_amount = self.shares.get(&sender_id).expect(ERR13_LP_NOT_REGISTERED);
        // make amounts into comparable-amounts
        let c_amounts = self.amounts_to_c_amounts(&amounts);
        for i in 0..n_coins {
            self.assert_min_reserve(self.c_amounts[i].checked_sub(c_amounts[i]).unwrap_or(0));
        }
        let invariant = self.get_invariant();
        let trade_fee = Fees::new(self.total_fee, &fees);
        let (burn_shares, fee_part) = invariant
            .compute_lp_amount_for_withdraw(
                &c_amounts,
                &self.c_amounts,
                self.shares_total_supply,
                &trade_fee,
            )
            .expect(ERR67_LPSHARE_CALC_ERR);
        assert!(
            burn_shares <= prev_shares_amount,
            "{}",
            ERR34_INSUFFICIENT_LP_SHARES
        );
        assert!(burn_shares <= max_burn_shares, "{}", ERR68_SLIPPAGE);
        for i in 0..n_coins {
            self.c_amounts[i] = self.c_amounts[i].checked_sub(c_amounts[i]).unwrap();
            self.assert_min_reserve(self.c_amounts[i]);
        }
        self.burn_shares(&sender_id, prev_shares_amount, burn_shares);
        env::log(
            format!(
                "LP {} removed {} shares by given tokens, and fee is {} shares",
                sender_id, burn_shares, fee_part
            )
            .as_bytes(),
        );
        if fee_part > 0 {
            // referral fee
            if let Some(referral) = &fees.referral_id {
                if self.shares.get(referral).is_some() {
                    let referral_share = fee_part * fees.referral_fee as u128 / FEE_DIVISOR as u128;
                    self.mint_shares(referral, referral_share);
                    env::log(
                        format!("Referral {} got {} shares", referral, referral_share).as_bytes(),
                    );
                }
            }
            // exchange fee
            let exchange_share = fee_part * fees.exchange_fee as u128 / FEE_DIVISOR as u128;
            self.mint_shares(&fees.exchange_id, exchange_share);
            env::log(
                format!("Admin {} got {} shares", &fees.exchange_id, exchange_share).as_bytes(),
            );
        }
        burn_shares
    }
pub fn remove_liquidity_by_tokens ( & mut self , sender_id : & AccountId , amounts : Vec < Balance > , max_burn_shares : Balance , fees : & AdminFees , ) -> Balance { let n_coins = self . token_account_ids . len ( ) ; assert_eq ! ( amounts len ( ) n_coins " " ERR64_TOKENS_COUNT_ILLEGAL ) ; let prev_shares_amount = self . shares . get ( & sender_id ) . expect ( ERR13_LP_NOT_REGISTERED ) ; // make amounts into comparable-amounts let c_amounts = self . amounts_to_c_amounts ( & amounts ) ; for i in 0 .. n_coins { self . assert_min_reserve ( self . c_amounts [ i ] . checked_sub ( c_amounts [ i ] ) . unwrap_or ( 0 ) ) ; } let invariant = self . get_invariant ( ) ; let trade_fee = Fees :: new ( self . total_fee , & fees ) ; let ( burn_shares , fee_part ) = invariant . compute_lp_amount_for_withdraw ( & c_amounts , & self . c_amounts , self . shares_total_supply , & trade_fee , ) . expect ( ERR67_LPSHARE_CALC_ERR ) ; assert ! ( burn_shares prev_shares_amount " " ERR34_INSUFFICIENT_LP_SHARES ) ; assert ! ( burn_shares max_burn_shares " " ERR68_SLIPPAGE ) ; for i in 0 .. n_coins { self . c_amounts [ i ] = self . c_amounts [ i ] . checked_sub ( c_amounts [ i ] ) . unwrap ( ) ; self . assert_min_reserve ( self . c_amounts [ i ] ) ; } self . burn_shares ( & sender_id , prev_shares_amount , burn_shares ) ; env :: log ( format ! ( " " sender_id burn_shares fee_part ) . as_bytes ( ) , ) ; if fee_part > 0 { // referral fee if let Some ( referral ) = & fees . referral_id { if self . shares . get ( referral ) . is_some ( ) { let referral_share = fee_part * fees . referral_fee as u128 / FEE_DIVISOR as u128 ; self . mint_shares ( referral , referral_share ) ; env :: log ( format ! ( " " referral referral_share ) . as_bytes ( ) , ) ; } } // exchange fee let exchange_share = fee_part * fees . exchange_fee as u128 / FEE_DIVISOR as u128 ; self . mint_shares ( & fees . exchange_id , exchange_share ) ; env :: log ( format ! ( " " fees exchange_id exchange_share ) . as_bytes ( ) , ) ; } burn_shares }
Remove liquidity from the pool by fixed tokens-out,
allows to remove liquidity of a subset of tokens, by providing 0 in `amounts`.
Remove liquidity from the pool by fixed tokens - out allows to remove liquidity of a subset of tokens by providing 0 in ` amounts ` .
&mut self
sender_id
&AccountId
amounts
Vec<Balance>
max_burn_shares
Balance
fees
&AdminFees
&mut self
sender_id
&AccountId
amounts
Vec<Balance>
max_burn_shares
Balance
fees
&AdminFees