133 lines
5.2 KiB
Python
133 lines
5.2 KiB
Python
import asyncio
|
||
import os
|
||
|
||
from sqlalchemy import and_
|
||
from tonsdk.boc import begin_cell
|
||
from tonsdk.contract.wallet import Wallets
|
||
from tonsdk.utils import HighloadQueryId
|
||
from datetime import datetime, timedelta
|
||
|
||
from app.core._blockchain.ton.platform import platform
|
||
from app.core._blockchain.ton.toncenter import toncenter
|
||
from app.core.models.tasks import BlockchainTask
|
||
from app.core._config import MY_FUND_ADDRESS
|
||
from app.core._secrets import service_wallet
|
||
from app.core._utils.send_status import send_status
|
||
from app.core.storage import db_session
|
||
from app.core.logger import make_log
|
||
|
||
|
||
async def get_sw_seqno():
|
||
sw_seqno_result = await toncenter.run_get_method(service_wallet.address.to_string(1, 1, 1), 'seqno')
|
||
if sw_seqno_result.get('exit_code', -1) != 0:
|
||
sw_seqno_value = 0
|
||
else:
|
||
sw_seqno_value = int(sw_seqno_result.get('stack', [['num', '0x0']])[0][1], 16)
|
||
|
||
return sw_seqno_value
|
||
|
||
|
||
async def main_fn(memory):
|
||
make_log("TON", f"Service started, SW = {service_wallet.address.to_string(1, 1, 1)}", level="info")
|
||
sw_seqno_value = await get_sw_seqno()
|
||
make_log("TON", f"Service wallet run seqno method: {sw_seqno_value}", level="info")
|
||
if sw_seqno_value == 0:
|
||
make_log("TON", "Service wallet is not deployed, deploying...", level="info")
|
||
await toncenter.send_boc(
|
||
service_wallet.create_transfer_message(
|
||
[{
|
||
'address': service_wallet.address.to_string(1, 1, 1),
|
||
'amount': 1,
|
||
'send_mode': 1,
|
||
'payload': begin_cell().store_uint(0, 32).store_bytes(b"Init MY Node").end_cell()
|
||
}], 0
|
||
)['message'].to_boc(False)
|
||
)
|
||
await asyncio.sleep(5)
|
||
return await main_fn(memory)
|
||
|
||
if os.getenv("TON_BEGIN_COMMAND_WITHDRAW"):
|
||
await toncenter.send_boc(
|
||
service_wallet.create_transfer_message(
|
||
[{
|
||
'address': MY_FUND_ADDRESS,
|
||
'amount': 1,
|
||
'send_mode': 128,
|
||
'payload': begin_cell().end_cell()
|
||
}], sw_seqno_value
|
||
)['message'].to_boc(False)
|
||
)
|
||
make_log("TON", "Withdraw command sent", level="info")
|
||
await asyncio.sleep(10)
|
||
return await main_fn(memory)
|
||
|
||
# TODO: не деплоить если указан master_address и мы проверили что аккаунт существует. Сейчас platform у каждой ноды будет разным
|
||
platform_state = await toncenter.get_account(platform.address.to_string(1, 1, 1))
|
||
if not platform_state.get('code'):
|
||
make_log("TON", "Platform contract is not deployed, send deploy transaction..", level="info")
|
||
await toncenter.send_boc(
|
||
service_wallet.create_transfer_message(
|
||
[{
|
||
'address': platform.address.to_string(1, 1, 1),
|
||
'amount': int(0.08 * 10 ** 9),
|
||
'send_mode': 1,
|
||
'payload': begin_cell().store_uint(0, 32).store_uint(0, 64).end_cell(),
|
||
'state_init': platform.create_state_init()['state_init']
|
||
}], sw_seqno_value
|
||
)['message'].to_boc(False)
|
||
)
|
||
|
||
await send_status("ton_daemon", "working: deploying platform")
|
||
await asyncio.sleep(15)
|
||
return await main_fn(memory)
|
||
|
||
highload_wallet = Wallets.ALL['hv3'](
|
||
private_key=service_wallet.private_key,
|
||
public_key=service_wallet.public_key,
|
||
wc=0
|
||
)
|
||
make_log("TON", f"Highload wallet address: {highload_wallet.address.to_string(1, 1, 1)}", level="info")
|
||
highload_state = await toncenter.get_account(highload_wallet.address.to_string(1, 1, 1))
|
||
if not highload_state.get('code'):
|
||
make_log("TON", "Highload wallet contract is not deployed, send deploy transaction..", level="info")
|
||
created_at_ts = int(datetime.utcnow().timestamp()) - 60
|
||
await toncenter.send_boc(
|
||
highload_wallet.create_transfer_message(
|
||
service_wallet.address.to_string(1, 1, 1),
|
||
1, HighloadQueryId.from_seqno(0), created_at_ts, send_mode=1, payload="hello world", need_deploy=True
|
||
)['message'].to_boc(False)
|
||
)
|
||
await asyncio.sleep(15)
|
||
return await main_fn(memory)
|
||
|
||
while True:
|
||
try:
|
||
sw_seqno_value = await get_sw_seqno()
|
||
make_log("TON", f"Service running ({sw_seqno_value})", level="debug")
|
||
|
||
# with db_session() as session:
|
||
# next_task = session.query(BlockchainTask).filter(
|
||
# BlockchainTask.status == 'wait'
|
||
# ).first()
|
||
# if next_task:
|
||
# make_log("TON", f"Processing task {next_task.id}", level="info")
|
||
|
||
# next_task.status = 'processing'
|
||
|
||
# session.commit()
|
||
|
||
await asyncio.sleep(5)
|
||
await send_status("ton_daemon", f"working (seqno={sw_seqno_value})")
|
||
except BaseException as e:
|
||
make_log("TON", f"Error: {e}", level="error")
|
||
await asyncio.sleep(3)
|
||
|
||
# if __name__ == '__main__':
|
||
# loop = asyncio.get_event_loop()
|
||
# loop.run_until_complete(main())
|
||
# loop.close()
|
||
|
||
|
||
|
||
|