MM bot & P2P binding
Last updated
Last updated
Here we describe the magic of Kapinus. There will be some code
To successfully bind a rate, we need to solve two tasks: get the current course and align it if there are differences. And yes, when "flattening" rate, it creates a volatility on the exchanges, thereby generating a SWAP fee 💰
response = requests.post('https://p2p.binance.com/bapi/c2c/v2/friendly/c2c/adv/search', json=json_data)
URL for sending POST request
In the body of the request, we pass data:
Direction of exchange, for example BRL\USDT
Payment method
Very important to remove international payment methods, such as Wise
json_data = {
"asset": "USDT",
"fiat": "BRL",
"merchantCheck": "False",
"page": 1,
"payTypes": ["Mercadopago"],
"rows": 100,
"tradeType": "BUY"
}
Now we should get the average rate, the result is : 100
average_price = sum(prices) / len(prices)
# Defining POST Request Bodies
body1 = '[{"id":1,"jsonrpc":"2.0","method":"eth_getBalance","params":["0x0d9bdea6ebc8cd41bf6e110abc5f6a157cb9f708","latest"]},{"method":"eth_call","params":[{"to":"0xa7437d07806420820ba29dce931249565787f810","data":"0x70a082310000000000000000000000000d9bdea6ebc8cd41bf6e110abc5f6a157cb9f708"},"latest"],"id":2,"jsonrpc":"2.0"}]'
body2 = '[{"id":1,"jsonrpc":"2.0","method":"eth_getBalance","params":["0x0d9bdea6ebc8cd41bf6e110abc5f6a157cb9f708","latest"]},{"method":"eth_call","params":[{"to":"0x55d398326f99059fF775485246999027B3197955","data":"0x70a082310000000000000000000000000d9bdea6ebc8cd41bf6e110abc5f6a157cb9f708"},"latest"],"id":2,"jsonrpc":"2.0"}]'
try:
# Sending POST requests and receiving responses
response1 = requests.post('https://bsc-dataseed3.binance.org/', data=body1)
response2 = requests.post('https://bsc-dataseed3.binance.org/', data=body2)
# Extract values from responses and convert to decimal
result1 = int(response1.json()[1]['result'], 16)
result2 = int(response2.json()[1]['result'], 16)
# Calculate the result using the formula
cresult = result1 / result2
We see the current rate for P2P and the rate of the pool, now we understand if there is a match
We use Tatum to interact with the contract. The task: align the rate to the value of P2P.
import sys
import requests
import time
import json
from decimal import Decimal
from web3 import Web3
def transaction():
# build request for scrape p2p Binance rate
# json body
json_data = {
"asset": "USDT",
"fiat": "BRL",
"merchantCheck": "False",
"page": 1,
"payTypes": ["Pix"],
"rows": 100,
"tradeType": "BUY"
}
try:
# push POST request
response = requests.post('https://p2p.binance.com/bapi/c2c/v2/friendly/c2c/adv/search', json=json_data)
# get rate list
prices = [float(x['adv']['price']) for x in response.json()['data']]
# middle value
cresult = sum(prices) / len(prices)
# print result
print("P2P: ", cresult)
except (requests.exceptions.RequestException, KeyError, TypeError) as e:
# display errors
print("error P2P:", e)
return
# check rate in pool
url1 = 'https://api.bscscan.com/api'
params1 = {'module': 'account',
'action': 'tokenbalance',
'contractaddress': '0x55d398326f99059fF775485246999027B3197955',
'address': '0x0d9bdea6ebc8cd41bf6e110abc5f6a157cb9f708',
'tag': 'latest',
'apikey': 'bscscan_api'}
response1 = requests.get(url1, params=params1)
result1 = response1.json()['result']
# second request
url2 = 'https://api.bscscan.com/api'
params2 = {'module': 'account',
'action': 'tokenbalance',
'contractaddress': '0xA7437D07806420820ba29DCE931249565787f810',
'address': '0x0d9bdea6ebc8cd41bf6e110abc5f6a157cb9f708',
'tag': 'latest',
'apikey': 'bscscan_api'}
response2 = requests.get(url2, params=params2)
result2 = response2.json()['result']
# compare rates
result_ratio = float(result2) / float(result1)
print(f'BSC: {result_ratio}')
# calculate
result = (cresult / result_ratio)
print(f'equal: {result}')
if result > 0.985 and result < 1.015:
print("value in normal rate")
#sys.exit()
return
result_pump = 0
result_dump = 0
# if we have a low rate, dump scenario
if result <= 0.985:
url = 'https://api.bscscan.com/api'
params = {
'module': 'account',
'action': 'tokenbalance',
'contractaddress': '0x55d398326f99059fF775485246999027B3197955',
'address': '0x0d9bdea6ebc8cd41bf6e110abc5f6a157cb9f708',
'tag': 'latest',
'apikey': 'bscscan_api'
}
response = requests.get(url, params=params)
data = json.loads(response.text)
if data['status'] == '1':
usdt = int(data['result'])
usdt = int(Decimal(usdt) / (Decimal(10) ** 18))
result_dump = (usdt / 100) * 1.25
print(f'DUMP: {result_dump}')
else:
print('Error:', data.get('message', 'display error DUMP'))
#sys.exit()
return
# if we have a high rate, pump scenario
elif result >= 1.024:
url = 'https://api.bscscan.com/api'
params = {
'module': 'account',
'action': 'tokenbalance',
'contractaddress': '0xA7437D07806420820ba29DCE931249565787f810',
'address': '0x0d9bdea6ebc8cd41bf6e110abc5f6a157cb9f708',
'tag': 'latest',
'apikey': 'bscscan_api'
}
response = requests.get(url, params=params)
data = json.loads(response.text)
if data['status'] == '1':
brl = int(data['result'])
brl = int(Decimal(brl) / (Decimal(10) ** 18))
result_pump = (brl / 100) * 1.25
print(f'PUMP: {result_dump}')
else:
print('Error:', data.get('message', 'display error PUMP'))
return
# push tx for stable rate
if result_pump != 0:
# swap in/out
amountIn = str(result_pump)
amountOut1 = (float(amountIn) / result_ratio)
print(f'amountOut PUMP: {amountIn} / {result_ratio} = {amountOut1}')
amountOut = (amountOut1 * 0.98)
print(f'amountOut PUMP: {amountOut1} * 0.98 = {amountOut}')
# token addresses from/to
tokenAddresses = [
"0xA7437D07806420820ba29DCE931249565787f810",
"0x55d398326f99059ff775485246999027b3197955",
]
elif result_dump != 0:
# swap in/out
amountIn = str(result_dump)
amountOut1 = (float(amountIn) * result_ratio)
print(f'amountOut DUMP: {amountIn} * {result_ratio} = {amountOut1}')
amountOut = (amountOut1 * 0.98)
print(f'amountOut DUMP: {amountOut1} * 0.98 = {amountOut}')
# token addresses from/to
tokenAddresses = [
"0x55d398326f99059ff775485246999027b3197955",
"0xA7437D07806420820ba29DCE931249565787f810",
]
gasPrice = 3
gasLimit = 360000
address = "reserve_address" # reserve address wallet
btime = int(time.time() * 1000) + 60 * 60 * 1000
refValue = "0x0000000000000000000000000000000000000000"
print(f'amountIn0: {amountIn}')
if amountIn != "0":
amountIn = str(Decimal(amountIn) * (Decimal(10) ** 18)) # weis are int
integer_part, decimal_part = amountIn.split('.')
amountIn = integer_part
print(f'amountIn: {amountIn}')
if amountOut != "0":
amountOut = str(Decimal(amountOut) * (Decimal(10) ** 18))
integer_part, decimal_part = amountOut.split('.')
amountOut = integer_part
print(f'amountOut: {amountOut}')
# data for tx
params = [
amountIn,
amountOut,
tokenAddresses,
address,
str(btime),
refValue,
]
#def transaction():
url = "https://api.tatum.io/v3/bsc/smartcontract"
headers = {
"Content-Type": "application/json",
"x-api-key": "tatum_api", # Tatum API
}
payload = {
"contractAddress": "0x0ddb9d635cb92720896de709d18069450942dbba", # router address
"methodName": "swapExactTokensForTokens", # ABI
"methodABI": {
"inputs": [
{"internalType": "uint256", "name": "amountIn", "type": "uint256"},
{"internalType": "uint256", "name": "amountOutMin", "type": "uint256"},
{"internalType": "address[]", "name": "path", "type": "address[]"},
{"internalType": "address", "name": "to", "type": "address"},
{"internalType": "uint256", "name": "deadline", "type": "uint256"},
{"internalType": "address", "name": "refer", "type": "address"},
],
"name": "swapExactTokensForTokens",
"outputs": [
{"internalType": "uint256[]", "name": "amounts", "type": "uint256[]"},
],
"stateMutability": "nonpayable",
"type": "function",
},
"fee": {
"gasPrice": str(gasPrice),
"gasLimit": str(gasLimit),
},
"params": params,
"fromPrivateKey": "private_key", # private key for reserve wallet
}
response = requests.post(url, headers=headers, json=payload)
data = response.json()
print(data)
if __name__ == "__main__":
while True:
transaction()
time.sleep(1)
Thus, we received data on the rate from our pool and P2P Binance, calculated the difference and equalized, if any.
This is how we keep rate in our pools