0

I have a contract with methods to create items and add them to categories.

I want to create a wrapper contract that would combine several calls into one: create an item and add it to several categories in one call.

What is the approximate price of calling the wrapper instead of calling the base contract directly?

Help me to decide if it is worth to call the base contract rather than the wrapper one directly, if the number of categories to add the created item to is zero.

Case 1: Create item and add it to categories 11 an 17.

Case 2: Create item and not to add it to categories.

2
  • Please show the two cases separately. Jul 13, 2020 at 14:31
  • @goodvibration Done.
    – porton
    Jul 13, 2020 at 17:21

1 Answer 1

3

Short answer:

It is approproximately the sum of the cost of executing all called functions plus the cost of executing the wrapper which includes small gas expenditures to pack and unpack function arguments.

Better answer:

The cost difference isn't serious. The decision should probably be based on your intended flow and other factors. For example, if you need multiple actions to complete as an atomic transaction, then a wrapper combined with access control on the target functions (e.g. onlyWrapper) to ensure that the individual functions are not called by any other process.

You can, of course, play with it to observe the costs and confirm your findings align with expectations. Given two targets A & B and a Wrapper, the cost should be:

cost A + cost B + cost Wrapper

A contract's role is primarily to safeguard the internal integrity of the system and enforce the rules.

  • Can A or B ever be called individually by an external entity? Is that a valid way to use the contract?
  • Should A & B always be called together?
  • Is it useful to sometimes call A & B together but it is not a strict requirement?

Corresponding implementations that all cost roughly the same at runtime:

  • Let the user call A or B
  • Force the user to call the Wrapper
  • Let the user call A, B or the Wrapper

Hope it helps.

Your Answer

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

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