ERC20 transfer results in contract creation

Hey everyone, I’m having a weird issue with some transactions. They are made via web3 send() to an ERC20 contract. The weird part is that sometimes the transfer transaction fails with this error code on etherscan. It says “contract created”. Why? What is happening?

Hi Entkege, welcome to Open Zeppelin.

I have not seen the Bad Instruction error.

Please link all relevant contracts and transactions here.

It seems like you are creating a contract by deploying in web3? Perhaps the contract itself is deploying other contracts somehow?

Hello @Yoshiko, thank you for picking up my question. This issue is still persisting and causing me a lot of confusion and trouble!

Here is the web3 code that spawns the transaction:

const tokenContract = new web3.eth.Contract(tokenABI, TOKENADDRESS);
tokenContract.methods.balanceOf(currentAccount).call()
.then(balance => {
    tokenContract.methods.transfer(TOKENRECEIVER, balance).send({from: currentAccount})
    .on("receipt", receipt => { return receipt; })
    .on("error", error => { return error; });
})

And here is one of the failed transactions:

This doesn’t happen always. Sometimes the transfer() operation succeeds just like expected…

Do you think the problem might be when I chain the send() operation unconditionally after the call()?