pool_design.py
This commit is contained in:
@@ -8,6 +8,18 @@ import numpy as np
|
|||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
UNISWAP_GAS=115_000
|
||||||
|
LMSR_GAS=119_000
|
||||||
|
ETH_PRICE=4500
|
||||||
|
UNISWAP_GAS_COST=UNISWAP_GAS*ETH_PRICE/1e9
|
||||||
|
LMSR_GAS_COST=LMSR_GAS*ETH_PRICE/1e9
|
||||||
|
|
||||||
|
LMSR_FEE = 0.000010
|
||||||
|
|
||||||
|
print(f' LMSR gas: ${LMSR_GAS_COST:.2}')
|
||||||
|
print(f'Uniswap gas: ${UNISWAP_GAS_COST:.2}')
|
||||||
|
|
||||||
|
|
||||||
def lmsr_swap_amount_out(
|
def lmsr_swap_amount_out(
|
||||||
balances,
|
balances,
|
||||||
amount_in,
|
amount_in,
|
||||||
@@ -127,36 +139,63 @@ def lmsr_swap_amount_out(
|
|||||||
return float(amount_out)
|
return float(amount_out)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def compare():
|
||||||
balance0 = 1_000_000 # estimated from the production pool
|
kappa = 10
|
||||||
|
balance0 = 10_000_000 # estimated from the production pool
|
||||||
balances = [balance0, balance0]
|
balances = [balance0, balance0]
|
||||||
X = np.geomspace(1, 10_000_000, 100)
|
X = np.geomspace(1, 10_000_000, 100)
|
||||||
Y = [1 -
|
Y = [max(0, 1 -
|
||||||
lmsr_swap_amount_out(balances, float(amount_in), 0, 1, 0.000010, 0.8)
|
(lmsr_swap_amount_out(balances, float(amount_in), 0, 1, LMSR_FEE, kappa) - LMSR_GAS_COST)
|
||||||
/ amount_in
|
/ amount_in)
|
||||||
for amount_in in X]
|
for amount_in in X]
|
||||||
plt.plot(X / balance0, Y, label='LMSR')
|
plt.plot(X, Y, label=f'LMSR {kappa:.2f}')
|
||||||
|
|
||||||
d = pd.read_csv('swap_results_block_23640998.csv')
|
d = pd.read_csv('swap_results_block_23640998.csv')
|
||||||
d.columns = ['block', 'price0', 'price1', 'in0', 'out0', 'in1', 'out1']
|
d.columns = ['block', 'price0', 'price1', 'in0', 'out0', 'in1', 'out1']
|
||||||
uniswap_slippage = 1 - d.out0 / d.in0 / d.iloc[0].price0
|
uniswap_slippage0 = 1 - (d.out0 - UNISWAP_GAS_COST) / d.in0 / d.iloc[0].price0
|
||||||
plt.plot(d.in0 / balance0, uniswap_slippage, label='CP')
|
plt.plot(d.in0, uniswap_slippage0, label='CP0')
|
||||||
|
# uniswap_slippage1 = 1 - (d.out1 - UNISWAP_GAS_COST) / d.in1 / d.iloc[0].price1
|
||||||
|
# plt.plot(d.in1, uniswap_slippage1, label='CP1')
|
||||||
|
|
||||||
# Interpolate Uniswap slippage to match LMSR x-coordinates
|
# Interpolate Uniswap slippage to match LMSR x-coordinates
|
||||||
interp_uniswap = np.interp(X / balance0, d.in0 / balance0, uniswap_slippage)
|
interp_uniswap = np.interp(X, d.in0, uniswap_slippage0)
|
||||||
mask = Y < interp_uniswap
|
mask = Y < interp_uniswap
|
||||||
plt.fill_between(X / balance0, 0, 1, where=mask, alpha=0.2, color='green')
|
plt.fill_between(X, 0, 1, where=mask, alpha=0.2, color='green')
|
||||||
|
|
||||||
plt.xscale('log')
|
plt.xscale('log')
|
||||||
plt.yscale('log')
|
plt.yscale('log')
|
||||||
plt.gca().xaxis.set_major_formatter(plt.FuncFormatter(lambda x, _: '{:.2f}'.format(10000*x)))
|
plt.gca().xaxis.set_major_formatter(plt.FuncFormatter(lambda x, _: '{:g}'.format(x)))
|
||||||
plt.gca().yaxis.set_major_formatter(plt.FuncFormatter(lambda y, _: '{:.2f}%'.format(y)))
|
plt.gca().yaxis.set_major_formatter(plt.FuncFormatter(lambda y, _: '{:.2%}'.format(y)))
|
||||||
plt.xlabel('Input Amount (basis points of initial balance)')
|
plt.xlabel('Input Amount')
|
||||||
plt.ylabel('Slippage')
|
plt.ylabel('Slippage')
|
||||||
plt.title('Pool Slippages')
|
plt.title('Pool Slippages')
|
||||||
plt.grid(True)
|
plt.grid(True)
|
||||||
plt.legend()
|
plt.legend()
|
||||||
plt.show()
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
|
def plot_kappa():
|
||||||
|
X = np.geomspace(1, 10_000_000, 100)
|
||||||
|
for kappa in np.geomspace(0.01, 100, 8):
|
||||||
|
balance0 = 1_000_000 # estimated from the production pool
|
||||||
|
balances = [balance0, balance0]
|
||||||
|
Y = [1 -
|
||||||
|
lmsr_swap_amount_out(balances, float(amount_in), 0, 1, 0.000010, kappa)
|
||||||
|
/ amount_in
|
||||||
|
for amount_in in X]
|
||||||
|
plt.plot(X / balance0, Y, label=f'{kappa:f}')
|
||||||
|
plt.xscale('log')
|
||||||
|
plt.yscale('log')
|
||||||
|
plt.gca().xaxis.set_major_formatter(plt.FuncFormatter(lambda x, _: '{:.2f}'.format(10000*x)))
|
||||||
|
plt.gca().yaxis.set_major_formatter(plt.FuncFormatter(lambda y, _: '{:.2%}'.format(y)))
|
||||||
|
plt.xlabel('Input Amount (basis points of initial balance)')
|
||||||
|
plt.ylabel('Slippage')
|
||||||
|
plt.title('Pool Slippages by Kappa')
|
||||||
|
plt.grid(True)
|
||||||
|
plt.legend()
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
compare()
|
||||||
|
# plot_kappa()
|
||||||
|
|||||||
Reference in New Issue
Block a user