Calculating Quotes

Calculating quotes for swaps and other operations requires knowledge of the current pool state including fee parameters, reserves, issued protocol tokens, etc. This information can be retrieved from the local state of the Tinyman app in the pool account. It is important to use fresh state data when calculating quotes as pool ratios and prices can change quickly.

Swap

Fixed Input

See Formula IV.A

Fixed Input Swap from asset 1 to asset 2

total_fee = (input_amount * state["total_fee_share"]) / 10000 swap_amount = input_amount - total_fee k = state["asset_1_reserves"] * state["asset_2_reserves"] swap_output = state["asset_2_reserves"] - (floor(k / (state["asset_1_reserves"] + swap_amount)) + 1)

Allowing for a slippage of 1%, min_output_amount should be set as swap_output * 0.99

Fixed Output

See Formula IV.B

Fixed Output Swap from asset 1 to asset 2

k = state["asset_1_reserves"] * state["asset_2_reserves"] swap_amount = (floor(k / (state["asset_2_reserves"] - output_amount)) + 1) - state["asset_1_reserves"] input_amount = floor((swap_amount * 10000) / (10000 - state["total_fee_share"]))

Allowing for a slippage of 1%, input_amount should be set as input_amount * 1.01

Last updated