Untitled

mail@pastecode.io avatar
unknown
python
5 months ago
1.5 kB
10
Indexable
import logging
import base64
import asyncio

from pytoniq.contract.wallets import WalletV4R2
from src.ton import client
from src.ton.utils import get_wallet_address
from config import settings

async def ensure_wallet_active(wallet):
    if not wallet.is_active:
        logging.info(f'Deploying wallet contract {wallet.address}')
        await wallet.deploy_via_external()
        return True
    return False

async def transfer_funds(wallet_id, to_address, amount, private_key):
    private_key_bytes = base64.b64decode(private_key)

    try:
        await client.connect()
        
        wallet = await WalletV4R2.from_private_key(provider=client, private_key=private_key_bytes, wallet_id=wallet_id, version='v4r2')
        _, nonbounceable_address = await get_wallet_address(wallet)
        
        if not wallet.is_active:
            await ensure_wallet_active(wallet)
            # Re-initialize wallet to update its status after deployment
            wallet = await WalletV4R2.from_private_key(provider=client, private_key=private_key_bytes, wallet_id=wallet_id, version='v4r2')
        
        logging.info(f'Trying transfer from wallet with address -> {nonbounceable_address}, to {to_address}')
        
        await wallet.transfer(
            destination=to_address,
            amount=amount,
        )
       
        await client.close()
    except Exception as e:
        logging.error(f"Error during transfer: {e}")
        await client.close()
Leave a Comment