Commit 443e21e4 by haojie

Initial commit

parents
/venv
/recharge_error.log
/test.py
/callback.py
\ No newline at end of file
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.8 (inscription-trade-py)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="PyDocumentationSettings">
<option name="format" value="PLAIN" />
<option name="myDocStringFormat" value="Plain" />
</component>
</module>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (inscription-trade-py)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/inscription-trade-py.iml" filepath="$PROJECT_DIR$/.idea/inscription-trade-py.iml" />
</modules>
</component>
</project>
\ No newline at end of file
This diff is collapsed. Click to expand it.
import asyncio
import json
import time
import traceback
import requests
from Helper import RedisCon, Helper
import logging
logging.basicConfig(level=logging.ERROR, filename='recharge_error.log', datefmt='%Y/%m/%d %H:%M:%S',
format='%(asctime)s - %(name)s - %(levelname)s - %(lineno)d - %(module)s - %(message)s')
logger = logging.getLogger(__name__)
logger.setLevel(level=logging.DEBUG)
def pay_order_status(status, url, data):
header = {
'User-Agent': 'Apipost client Runtime/+https://www.apipost.cn/',
}
data['status'] = status
print('准备回调', data)
result = requests.post(url, headers=header, data=data)
print(result)
# if result.text == 'success':
# print("请求成功!")
# else:
# print("请求失败!")
logger.debug(f">>>>tx_hash:{data['hash']} status:{status} url:{url}"
f" 请求状态码:{result.status_code}")
async def rechargeList(web3, data):
tx_hash = data['hash'] # 交易 hash
url = data['notify_url']
to_address = data['to'].lower()
# 交易类型
tran_type = data['type']
status = 3 # 默认充值失败
if 'retry' in data:
await asyncio.sleep(1)
try:
if tran_type == 2:
# 转账
receipt = await web3.eth.get_transaction_receipt(tx_hash)
print('下面是回调')
print(receipt)
if receipt:
log = receipt['logs'][0]
amount = data['amount']
pay_type = log['topics'][0].hex()
# pay_amount = eval(log['data'].replace('\x00', '')) / (10 ** 6)
pay_amount = int(log['data'].hex(), 16) / (10 ** 6)
pay_to_address = (Helper.hex2bsc(log['topics'][2].hex())).lower()
# transfer交易, 交易金额正确!
if pay_type == tran_str and pay_amount >= amount:
if pay_to_address == to_address:
# print("支付成功,接收地址正确!")
status = 2
else:
# print("支付成功,接收地址错误!")
status = 4
elif tran_type == 1:
inscription_hash = data['inscription_hash']
# 铭文转移-直接查钱包铭文列表
check_url = "https://mainnet-api.ethscriptions.com/api/ethscriptions/owned_by/" + to_address
result = requests.get(check_url).text
# json转list
result = json.loads(result)
# 检测5次
for i in range(0, 5):
for item in result:
# 判断是否存在
if item['transaction_hash'] == inscription_hash:
status = 2
data['inscription'] = item
break
if status == 2:
break
else:
time.sleep(3)
pay_order_status(status, url, data)
except Exception as e:
print(e)
if 'retry' in data:
data['retry'] += 1
else:
# 进入队列重试,添加重试标志
data['retry'] = 1
if data['retry'] <= 5:
RedisCon.task_list(key, json.dumps(data))
else:
# 重试多次无法获取交易 hash
pay_order_status(5, url, data)
logger.error(f"Recharge Fail:>>>>tx_hash:{tx_hash}>>>>{traceback.format_exc()}")
def main():
while True:
tasks = list()
task_num = RedisCon.get_task_num(key)
print(task_num, '个任务')
nums = 10
if task_num and task_num > 0:
if task_num < nums:
process = task_num
else:
process = nums
loop = asyncio.get_event_loop()
for i in range(process):
task = RedisCon.get_task(key)
if task:
web3 = Helper.getWeb3Eth()
task = json.loads(task)
# task
tasks.append(rechargeList(web3, task))
loop.run_until_complete((asyncio.wait(tasks, timeout=30)))
else:
time.sleep(3)
continue
if __name__ == '__main__':
key = 'laravel_database_admin_check_receipt'
tran_str = Helper.tran_str()
main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment