'Web3 - Buy BSC token by code (crypto currency)

i'm trying to buy $TRY token with my code. It's BSC token. The problem is when i send money to the contract i don't receive any token :/

The contract i'm trying to buy : https://bscscan.com/token/0xc12ecee46ed65d970ee5c899fcc7ae133aff9b03

I'm coding in python using Web3 wrapper : https://pypi.org/project/web3/

 signed_txn = w3.eth.account.sign_transaction(dict(
        nonce=w3.eth.getTransactionCount(MY_WALLET),
        gasPrice=int(w3.eth.gasPrice*1.5),
        gas=200000,
        to=w3.toChecksumAddress('0xc12ecee46ed65d970ee5c899fcc7ae133aff9b03'),
        value=w3.toWei(amount, 'ether'),
        data=b'',
    ),
        'PRIVATEKEY',
    )

I tried with the contract address : https://bscscan.com/tx/0xb9652d6f36d22a13a5fc877ade45d7f1c882eec80fd224c87949284793f6fe1a Payment is OK but i don't receive any token.

I also tried with the pancake routers : https://bscscan.com/tx/0xc89b620fc08c37dec87f1daa0d79cb1a01f2f0e7b9c200e6fcaf52c66ebe5297 But i got an error "A status code indicating if the top-level call succeeded or failed (applicable for Post BYZANTIUM blocks only). I don't understand it.

My account detail (only 4 transactions) : https://bscscan.com/address/0x033f7eea8799696ff46293cf8d84903a6aeeab05

Do you have any idea about how i can buy or sell this token with code ? Manually i do it with pancakeswap. There is also an ETH $TRY token with same address but i got same problem ...



Solution 1:[1]

i manage to do what i want.

I don't send the money directly to the contract (some contract don't handle this, other yes but in my example no). So i use a function of the contract, you can see the function of your contract into the "Contract" -> "writeContract" section on bscscan. Depending of the guy you develop the contract you can find the function you are looking for :)

Here example of code use to interact with the contract :

contract = w3.eth.contract(CONTRACT_ADDRESS, abi=ABI_CODE)
contract.functions.FUNCTION_OF_THE_CONTRACT().transact()

The abi code is accessible on the "Contract" tabulations of bscscan, it's called "Contract ABI"

Solution 2:[2]

If you are using Pancakeswap try this.

balance = web3.eth.get_balance(address)
address = "Your Wallet Address"
#Contract Address of Token to buy
inputToken_adderss = web3.toChecksumAddress("ADDRESS_OF_TOKEN_TO_BUY")
tokenToBuy = InputToken_address
spend =web3.toChecksumAddress("0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c")  #wbnb contract

#Setup the PancakeSwap contract
#get the ABI FROM THEIR DOCS.
contract = web3.eth.contract(address=router_pancakev2, abi=panabi)

nonce = web3.eth.get_transaction_count(address)

PKSWP_V2_txn = contract.functions.swapExactETHForTokens(
0, # set to 0 for indefinite, or specify minimum amount to receive - Decimals matter Here.
[spend,tokenToBuy],
address,
(int(time.time()) + 10000)
).buildTransaction({
'from': address,
'value': web3.toWei(buy_amount,'ether'),#Token(BNB) amount you will use to swap
'gas': 250000,
'gasPrice': web3.toWei('5.5','gwei'),
'nonce': nonce,
})

signed_txn = web3.eth.account.sign_transaction(PKSWP_V2_txn, private_key=private_key)
tx_token = web3.eth.send_raw_transaction(signed_txn.rawTransaction)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Valentin Garreau
Solution 2 GrapeSoda3