0

Let's see a Solidity pseudocode example:

function myFunction() external payable onlyOwner {
  ExternalContract contract = ExternalContract(address);
  uint result = contract.readFunction();
  required(result > 0, 'might failed here') //if FALSE transaction not executing at all (works as **view**)
  myCustomWriteLogic();
}

The gas fee will NOT be charged if required() will fail.

  1. Is that mean I'm performing "READ" to the blockchain and then putting the transaction to txpool?
  2. How to force push the transaction to txpool? In my case, I belive that result willl be >0 at the execution moment.

I'm executing a transaction via truffle and I want to push it EVEN it might failed:

const obj = await MyContract.deployed();  
obj.myFunction({value: 1000});
Yurii
  • 1
  • 3
  • It seems the same issue as here. But the issue is still unsolved. https://ethereum.stackexchange.com/questions/100054/how-to-force-push-a-failing-transaction-using-web3-js – Yurii Jul 13 '22 at 15:30

1 Answers1

0

The gas fee will NOT be charged if required() will fail.

This is correct only if the snippet is invoked using a (read-only) call. If it's invoked using a transaction, gas fees will be deducted for executing of the code until the point where the require() condition fails and produces a revert.

Calls do not go through the mempool, they are executed directly on the node that you're connected to.

How to force push transaction if your wallet recommends you to not send it (as it might fail)? That depends on the specific wallet or code that you're using to broadcast the transaction. For example the MetaMask UI shows a button to force sending the transaction - see the screenshot:

MetaMask force send

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
  • My function marked as "external payable" buy has behavior exactly as you describe in "call". For invoking I'm using truffle(BSC testnet) and the transaction falls without being executed by a miner. I'm sending transactions like : obj.functionName('arg', {value: 10000}) – Yurii Jul 13 '22 at 13:42
  • @Yurii Function modifiers serve only as a recommendation for the client software/code whether to send a transaction or a call. Based on the context of your comment, you seem to be sending a transaction. If you want to send a call, try adding a `view` modifier to the Solidity function (if you're able to redeploy the contract) or forcing your code to do so (the actual steps depend on the library that you're using to invoke `functionName()` and you might also need to connect to a node, as some wallets such as MetaMask don't process calls - only transactions). – Petr Hejda Jul 13 '22 at 13:47
  • The thing is I have a function and if `require(true)` the transaction executes, but if `require(false)` it works as a `call` (no execution at all). I've updated the code snippet in the description – Yurii Jul 13 '22 at 13:52
  • @Yurii If you do `require(false`), the transaction might still be executed (until the failed `require()` condition) but the state changes are reverted, so it seems like nothing was executed. But it also depends on whether you have actually sent the failing transaction. You should be able to see it in your local node. – Petr Hejda Jul 13 '22 at 14:21
  • Yep, but in bscscan I can't see any transactions, so it looks like readonly call. – Yurii Jul 13 '22 at 14:26
  • I checked BNB balance after a "failed" transaction. There is no changes, same as for "readonly" call. – Yurii Jul 13 '22 at 14:32
  • @Yurii In that case you most likely didn't send the transaction. Not sure which wallet you're using to broadcast the transaction or if you're signing and broadcasting it from your code, but check that you really do send it. Also, if you're using Truffle as a local node (and not just some of their other products as a helper library), any transactions on this local network are not going to be available on BSCScan, as local networks are not scanned by BSCScan. – Petr Hejda Jul 13 '22 at 14:37
  • I'm using BSC testnet as a test network and this node: https://data-seed-prebsc-1-s1.binance.org:8545 – Yurii Jul 13 '22 at 14:47