I have a Solidity function which requires an argument which value should be calculated from a value that I retrieve from my Ethereum node via a Web3 call. Once I have the value I have to pass it to a function in my smart contract.

To achieve this I have two scenarios:

1. Doing the whole logic within the smart contract.

In this case I take the raw value, pass it to the contract function and do the required processing on chain.

// Web3 call at the Web2 backend 


// Solidity part
function myFunction(uint256 rawValue) {

    valueToUse = rawValue * 2;
    // continue the rest of the function

2. Doing all needed calculations on my Web2 backend in Python or JS, and use the processed values directly.

In this case I do all calculations on my server (which don't require built in Solidity functions) and call the function with the processed, ready-to-use values.

// Web3 call at the Web2 backend 

    valueToUse = rawValue * 2

// Solidity part
function myFunctionB(uint256 valueToUse ) {
    // continue the rest of the function with the proper value the contract receives from Web3 call

My question is which solution leads to faster execution time? As doing calculations, loops and other code executions in Solidity cost gas, I think everything that can be done in my backend code should be done there. However I see contracts containing complicated functions (that can be done in backend code too) and the only thing I can think about is they do it because the execution time is less if it's done inside the contract. Am I right? Or what can be the logical reason behind not preparing everything as possible on a web2 server and just use the proper values to call the contract?

1 Answer 1


Basically do everything off-chain that you can. It saves gas, time, and space on the EVM. I think the only arguments for doing more on-chain:

  • usually more user friendly if no nice front end (front ends can be centralization risk)
  • there's manipulation risk to off-chain calculation (this is the big one). As an example, if you're supposed to multiply the rawValue * a users balance for instance. Of course you could do it off-chain, but you don't really want to trust users with correctly putting in their balance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.