I have some mathematical algorithm I have reason to run as an on-chain function in a smart contract. After unrolling for-loops I’m looking at I’m guessing 10000 lines of code where each line is doing simple arithmetic (adding or multiplying floating point numbers in small arrays).

I know for a fact the function can be computed 3000-5000 times per second on a 2.3 GHz Intel i5, when written in C depending on parameter tweaks.

Before writing the code my ‘gut feel’ is I’m looking at 1,000,000 gas for something like this, based on some simple hardhat tests with large for-loops.

Running this on a main chain looks like a joke, but running this on Polygon or some kind of roll-up looks like it might not be ridiculously expensive?

I estimate users will need to call the function/transaction maybe 5 or 10 times total. I also estimate that the on-chain data will be around 5 kilobytes per transaction (or 2kb with on-chain decompression). The majority of participants to the smart contract would likely be willing to spend $120 total on those 5 or 10 transactions but $10000 might not be out of the question for certain corporate use-cases.

Ballpark, does this sound like it adds up or have I vastly underestimated the cost of gas or on-chain data?

  • Dragounz@alien.topB
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year ago

    If one line = one simple operation, a tx will cost way less than 1 million gas

  • fediverser@alien.top
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year ago

    This post is an automated archive from a submission made on /r/ethereum, powered by Fediverser software running on alien.top. Responses to this submission will not be seen by the original author until they claim ownership of their alien.top account. Please consider reaching out to them let them know about this post and help them migrate to Lemmy.

    Lemmy users: you are still very much encouraged to participate in the discussion. There are still many other subscribers on !ethereum@blockchained.world that can benefit from your contribution and join in the conversation.

    Reddit users: you can also join the fediverse right away by getting by visiting https://portal.alien.top. If you are looking for a Reddit alternative made for and by an independent community, check out Fediverser.

  • mooremo@alien.topB
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year ago

    Write your program out in solidity. It’ll tell you the gas cost, you can then calculate what that would cost at varying gas prices.

  • dericecourcy@alien.top
    cake
    B
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year ago

    Yo i already wrote a lot, but i’m curious what you mean by this:

    I also estimate that the on-chain data will be around 5 kilobytes per transaction (or 2kb with on-chain decompression).

  • manyQuestionMarks@alien.topB
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year ago

    Depending on what you intend to do, did you consider writing it as a ZK circuit? You’d pay the verification cost (somewhere between 200k and 300k) but it would be a fixed cost

  • dericecourcy@alien.top
    cake
    B
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year ago

    Do you actually need the math function on-chain? What makes this function so special and complicated that it can’t be simplified with existing gas-efficient libraries? Have you already written up the algo in solidity? (assuming you have since you mentioned hardhat)

    Have you considered using an optimistic prover? You can publish a pairing of {inputs,outputs} without actually running the algorithm, and if anyone challenges it, then they can pay the gas to run this complex algorithm (I think this can be done with ZK provers as well) and show the results were wrong. This might require some stake (0.1 ETH?) when people push new {input,output} sets though, since there needs to be a penalty for lying. Of course whoever calls you out for lying should get their gas reimbursed, plus maybe some reward.

    To answer your original question, polygon or a rollup may be the right choice. Consider also: Gnosis chain and Celo. Those are fairly cheap at the moment, but still large enough to offer good security. My recent calculations were showing less than 0.03 USD per 1 million gas for all of these chains. Fuse is a very inexpensive EVM chain but is less secure due to low marketcap of the FUSE token, so keep this in mind if you expect your project to have significant TVL.

    Finally, note that gas costs are primarily driven by storage reads/writes, which is non-volatile memory. “memory” in the solidity sense is expensive-ish, and that is data that doesn’t persist after the transaction finishes. Try to minimize writes/reads of storage first, then memory if possible to reduce your gas costs. Keep in mind one “read/write” is 32 bytes, so you can pack data cleverly to reduce total reads/writes

  • sus-is-sus@alien.topB
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year ago

    Maybe try Neo blockchain. It is much faster and the transactions are cheaper. It also has a offchain vm that does another of work and you can write the contracts in many different programming languages.

  • frank__costello@alien.topB
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year ago

    ZK circuts is the solution for verifying complex computations on-chain

    But the real question: why does this code need to be verified on-chain at all?