Skip to content

Latest commit

 

History

History
71 lines (68 loc) · 3.32 KB

formulas.md

File metadata and controls

71 lines (68 loc) · 3.32 KB

Shadeswap Formulas

Calculating Swap Amount

References

Token_0: Base token (e.g. sSHD for the pair sSHD/sSCRT) Token_1: Quote token (e.g. sSCRT for the pair sSHD/sSCRT) Token_0_Pool: Total amount in the token pool of the base token Token_1_Pool: Total amount in the token pool of the quote token Slippage: Fraction as a decimal number (e.g. 0.02 for 2% slippage)

Buy

Amount_In: Amount of Token_0 Amount_Out: Amount of Token_1 Target_Token: Token_1 Source_Token: Token_0 Target_Token_Pool: Total amount in the token pool of the Target_Token Source_Token_Pool: Total amount in the token pool of the Source_Token

Sell

Amount_In: Amount of Token_1 Amount_Out: Amount of Token_0 Target_Token: Token_0 Source_Token: Token_1 Target_Token_Pool: Total amount in the token pool of the Target_Token Source_Token_Pool: Total amount in the token pool of the Source_Token

1. Calculation of fee

Fee = Amount_In * Fee.Nom / Fee.Denom

2. Calculation of Amount_Out

Amount_Out = Target_Token_Pool * (Amount_In - Fee) / (Source_Token_Pool + Amount_In - Fee)

3. Calculation of price

Price = Amount_Out / (Amount_In - Fee)

Calculating Liquidity

References

Deposit_0: The amount of deposited Token_0 Deposit_1: The amount of deposited Token_1 Slippage: Fraction as a decimal number (e.g. 0.02 for 2%) Slippage_amount: Accepting slippage LP Token: Liquidity token Withdraw_amount: Amount of LP Token to remove from liquidity Slippage: Fraction as a decimal number (e.g. 0.02 for 2%) Pool_withdraw_0: Amount of Token_0 to receive Pool_withdraw_1: Amount of Token_1 to receive Min: Function which returns the minimum of the two input

1. Assert slippage acceptance for adding liquidity

IF Slippage * Deposit_0 / Deposit_1 > Token_0_pool / Token_1_pool THEN Slippage_amount = 1 - Slippage OTHERWISE Thow an exception and don't allow to proceed.

2. Add liquidity calculation

IF LP balance is 0 THEN sqrt(Deposit_0 * Deposit_1) OTHERWISE min(Deposit_0 * LP_token_balance / Token_0_pool, Deposit_1 * LP_token_balance / Token_1_pool)

3. Remove liquidity calculation

Pool_withdraw_0 = Token_0_pool * Withdraw_amount / LP_token_balance Pool_withdraw_1 = Token_1_pool * Withdraw_amount / LP_token_balance

Staking

References

Current_timestamp: the timestamp of the block in milliseconds where the claim reward or adding new staker is called. Last_timestamp: the last stored timestamp of the block in milliseconds where claim reward method was called in staking contract. Staker_percentage: % from whole total staking amount belong to staker Staker: Person who is staking LP token Stake_amount: Amount of LP token which belongs to staker Total_staking_amount: total amount of lp token of all stakers Seconds: 24 * 60 * 60 * 1000 = 86,400,000 Daily_reward_amount: Staking amount which is constant and set by admin Cons: Constant value used for calculating Staker_percentage, which is 100

1. Staker_percentage (%)

Staker_percentage = Stake_amount * Cons / Total_staking_amount

2. Claim reward calculation

IF last_timestamp < current_timestamp THEN 0 OTHERWISE Daily_reward_amount * Staker_percentage * (current_timestamp - last_timestamp) / (Seconds * Cons)