How to make a BSC token transfer with python?

Viewed 21

Does anyone coded to make transactions (transfer funds) in binance smart chain? Im trying to write a program for compromissed wallets but I'm not sure if this could really works (it's a merged code from different places)

from web3 import Web3
import json
import requests
import time
 
bsc = "https://bsc-dataseed.binance.org/"
web3 = Web3(Web3.HTTPProvider(bsc))
#print(web3.isConnected())
 
def send(privkey):
    sender_pk = privkey
    sender_addr = web3.eth.account.from_key(sender_pk).address
    receiver_addr = 'receiver_address'
    contract_address = web3.toChecksumAddress('0x20f663cea80face82acdfa3aae6862d246ce0333')
 
    url_eth = "https://api.bscscan.com/api"
 
    API_ENDPOINT = url_eth+"?module=contract&action=getabi&address="+str(contract_address)
    r = requests.get(url = API_ENDPOINT)
    response = r.json()
    abi=json.loads(response["result"])
 
    contract = web3.eth.contract(address=contract_address, abi=abi)
 
    address = web3.toChecksumAddress(sender_addr)
    balance = contract.functions.balanceOf(address).call()
 
    result = float(web3.fromWei(balance,'ether'))
 
    if result >= 0.2:
        to_send = result * 0.998
        #Try to send the max amount
        nonce = web3.eth.getTransactionCount(receiver_addr)
        drip_tx = contract.functions.transfer(
            contract_address,
            1,
            ).buildTransaction({
                'nonce': nonce,
                'to': receiver_addr, 
                'value': web3.toWei(to_send, 'ether'),
                'chainId': 56,
                'gas': 21000,
                'gasPrice': web3.toWei('50', 'gwei'),
            })
        signed_txn = web3.eth.account.signTransaction(drip_tx, private_key=sender_pk)
        tx_hash =  web3.eth.sendRawTransaction(signed_txn.rawTransaction)
        print(tx_hash)
    else:
        print("NO cash in "+sender_addr)
 
 
send('pk1')
time.sleep(4)
send('pk2')
time.sleep(2)
0 Answers
Related