calcTools.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. from web3Tools import *
  2. def callV2V2Auto (balanceIn0, balanceOut0, fee0, balanceIn1, balanceOut1, fee1):
  3. amountIn = callBestAmountV2V2(balanceIn0, balanceOut0, fee0, balanceIn1, balanceOut1, fee1)
  4. if amountIn < 1 :
  5. return {'amountIn': amountIn, 'amountMid': 0, 'amountOut': 0, 'profit': 0}
  6. amountMid = calAmountOutV2(amountIn, balanceIn0, balanceOut0, fee0)
  7. amountOut = calAmountOutV2(amountMid, balanceIn1, balanceOut1, fee1)
  8. profit = amountOut - amountIn
  9. return {'amountIn': amountIn , 'amountMid': amountMid , 'amountOut': amountOut, 'profit': profit, 'profit-18': profit / 1e18}
  10. def calBestAmountABCA(balanceIn0, balanceOut0, fee0, balanceIn1, balanceOut1, fee1, balanceIn2, balanceOut2, fee2):
  11. F0 = 1 - fee0 / 1e6
  12. F1 = 1 - fee1 / 1e6
  13. F2 = 1 - fee2 / 1e6
  14. B0, B1, B2 = balanceIn0, balanceIn1, balanceIn2
  15. S0, S1, S2 = balanceOut0, balanceOut1, balanceOut2
  16. amountIn = ((B0 * B1 * B2 * F0 * F1 * F2 * S0 * S1 * S2) ** 0.5 - B0 * B1 * B2)
  17. if (F0 * F1 * F2 * S0 * S1 + B2 * F0 * F1 * S0 + B1 * B2 * F0) == 0:
  18. printTime('0 err', balanceIn0, balanceOut0, fee0, balanceIn1, balanceOut1, fee1, balanceIn2, balanceOut2, fee2)
  19. return {'amountIn':0, 'amountOut0':0 , 'amountOut1':0, 'amountOut2':0,'profit':0}
  20. amountIn = amountIn / (F0 * F1 * F2 * S0 * S1 + B2 * F0 * F1 * S0 + B1 * B2 * F0)
  21. if amountIn < 1 :
  22. amountIn = 0
  23. amountOut0 = calAmountOutV2(amountIn, balanceIn0, balanceOut0, fee0) - 2
  24. amountOut1 = calAmountOutV2(amountOut0, balanceIn1, balanceOut1, fee1) - 2
  25. amountOut2 = calAmountOutV2(amountOut1, balanceIn2, balanceOut2, fee2) - 2
  26. profit = amountOut2 - amountIn
  27. return {'amountIn':amountIn * 1.00001, 'amountOut0':amountOut0 * 1.000006 , 'amountOut1':amountOut1 * 1.000003, 'amountOut2':amountOut2,'profit':profit}
  28. def calBestAmountV2V2(balanceIn0, balanceOut0, fee0, balanceIn1, balanceOut1, fee1):
  29. F0, F1 = fee0, fee1
  30. F0 = 1 - F0 / 1e6
  31. F1 = 1 - F1 / 1e6
  32. B0, S0 = balanceIn0, balanceOut0
  33. B1, S1 = balanceIn1, balanceOut1
  34. if F0 * F1 * S0 + B1 * F0 == 0 :
  35. return {'amountIn':0, 'amountOut0':0, 'amountOut1':0, 'profit':0}
  36. I = ((B0 * B1 * F0 * F1 * S0 * S1) ** 0.5 - B0 * B1) / (F0 * F1 * S0 + B1 * F0)
  37. if I < 1 :
  38. I = 0
  39. amountOut0 = calAmountOutV2(I, balanceIn0, balanceOut0, fee0)
  40. amountOut1 = calAmountOutV2(amountOut0, balanceIn1, balanceOut1, fee1)
  41. profit = amountOut1 - I
  42. return {'amountIn':I * 1.00001, 'amountOut0':amountOut0 * 1.000005, 'amountOut1':amountOut1, 'profit':profit}
  43. def calAmountOutV2(amountIn, balanceIn, balanceOut, fee):
  44. if amountIn < 1 :
  45. return 0
  46. afterFee = (1e6 - fee) / 100
  47. amountInWithFee = amountIn * (afterFee)
  48. numerator = amountInWithFee * (balanceOut)
  49. denominator = balanceIn * (1e4) + (amountInWithFee)
  50. amountOut = numerator / denominator - 1
  51. return amountOut
  52. def calAmountInV2(amountOut, balanceIn, balanceOut, fee):
  53. fee = fee / 1000000
  54. k = balanceIn * balanceOut
  55. amountIn = k / (balanceOut - amountOut) - balanceIn
  56. amountIn = amountIn / (1 - fee)
  57. return amountIn
  58. def callV2V2Info(lp0, lp1, fee0, fee1, tokenIn, tokenOut, profit, blockNumber = 'latest'):
  59. try:
  60. function = '0x0ad5a3af'
  61. inputData = function + encodeInput(lp0, lp1, fee0, fee1, tokenIn, tokenOut, profit)
  62. result = web3Call(myAddress,v2v3CCAddress, inputData, blockNumber)
  63. info = w3.debug.decodeOutput(result)
  64. info = {"lpTradeRIn": info[4], "lpTradeROut": info[5], "lpLoanRIn": info[2], "lpLoanROut": info[3],
  65. "amountLoan": info[6], "midAmount": info[7], "outAmount": info[8], "profit": info[9], "profit-18": info[9] / 1e18, 'data':inputData}
  66. return info
  67. except BaseException as err:
  68. #print(err)
  69. return {"profit": 0, 'err':str(err)}
  70. def getV2V2SwapInput(lp0, lp1, fee0, fee1, tokenIn, tokenOut, profit):
  71. function = '0x0ad5a3af'
  72. inputData = function + encodeInput(lp0, lp1, fee0, fee1, tokenIn, tokenOut, profit)
  73. return inputData
  74. def callV2EEV2Info(lp0, lp1, fee0, fee1, tokenLp0Out, tokenLp1In, tokenTrade, profit, blockNumber):
  75. try:
  76. function = '0xa98eb068'
  77. inputData = function + encodeInput(lp0, lp1, fee0, fee1, tokenLp0Out, tokenLp1In, tokenTrade, profit)
  78. result = web3Call(myAddress, v2v3CCAddress, inputData, blockNumber)
  79. info = w3.debug.decodeOutput(result)
  80. info = {"lpTradeRIn": info[2], "lpTradeROut": info[3], "lpLoanRIn": info[0], "lpLoanROut": info[1],
  81. "amountLoan": info[6], "midAmount": info[7], "outAmount": info[5], "profit": info[-1], "profit-18": info[-1] / 1e18, 'data':inputData}
  82. return info
  83. except BaseException as err:
  84. #print(err)
  85. return {"profit": 0, 'err':str(err)}
  86. def getV2EEV2SwapInput(lp0, lp1, fee0, fee1, tokenLp0Out, tokenLp1In, tokenTrade, profit):
  87. function = '0xa98eb068'
  88. inputData = function + encodeInput(lp0, lp1, fee0, fee1, tokenLp0Out, tokenLp1In, tokenTrade, profit)
  89. return inputData
  90. def callV2V3Info(v2Pair, v2Fee, v3fee, tokenIn, tokenOut, amountInV2, blockNumber):
  91. try:
  92. function = '0x69e7fd5c'
  93. inputData = function + encodeInput(v2Pair, v2Fee, v3fee, tokenIn, tokenOut, amountInV2)
  94. result = web3Call(myAddress,v2v3Math, inputData, blockNumber)
  95. info = w3.debug.decodeOutput(result)
  96. info = {"amountInV2": info[0], "amountOutV2_amountInV3": info[1], "outAmountV3": info[2], "profit": info[3],
  97. "profit-18": info[3] / 1e18}
  98. return info
  99. except BaseException as err:
  100. print(err)
  101. return {"profit": 0, 'err':str(err)}
  102. def getV2V3SwapInput(poolV3, pairV2, tokenInV3_tokenOutV2, tokenOutV3_tokenInV2, amountInV3_amountOutV2, amountInV2, profit):
  103. function = '0x1530b468'
  104. inputData = function + encodeInput(poolV3, pairV2, tokenInV3_tokenOutV2, tokenOutV3_tokenInV2, amountInV3_amountOutV2, amountInV2, profit)
  105. return inputData
  106. def simulateSwap(inputdata, gasPrice ,blockNumber):
  107. r = mev.simulate(v2v3CC, [inputdata], blockNumber)
  108. data = {}
  109. data['profit'] = 0
  110. if not r :
  111. data['profit'] = 0
  112. return data
  113. if 'error' in r['results'][0]:
  114. printTime('sumilate err', r)
  115. return False
  116. data['gasUsed'] = r['results'][0]['gasUsed']
  117. value = r['results'][0]['value']
  118. profit = int(value[-60:],16)
  119. data['profit'] = profit
  120. data['profit-E18'] = profit / 1e18
  121. data['baseGasPrice'] = gasPrice
  122. data['baseGasPrice-E9'] = gasPrice / 1e9
  123. data['costBase'] = gasPrice * data['gasUsed']
  124. return data
  125. def gasCal(fomrAdd, toAdd, data, blockNumber = 'latest'):
  126. inputData = '0x4327f0db' + encodeInput(toAdd, 0x40, (len(data) - 2)/2 , data)
  127. info = web3Call(fomrAdd, '0xEAf90BcB75e7B92900678c4aBD80883b414bEDD6', inputData, blockNumber)
  128. info = w3.debug.decodeOutput(info)
  129. result = {}
  130. result['gasUsed'] = info[1]
  131. result['info'] = info[3:]
  132. return result
  133. printTime('CALC:OK')
  134. printTime(calAmountOutV2(14120103689241891864, 31975927096398017590445, 222178467759, 3000) - 97773396 , )