cashscript icon indicating copy to clipboard operation
cashscript copied to clipboard

Add global contract variables / constructor

Open rkalis opened this issue 5 years ago • 2 comments

Currently the only variables that are accessible to all contract functions are the constructor parameters. However, it might be useful to have some variables that are not necessarily constructor parameters, but that you still want to access in multiple functions, such as global constants.

Furthermore, it could be useful to have a constructor function that executes some specific code using the constructor parameters, while also being able to store global contract variables. This could be useful if some common code has to be executed in all functions, since it takes fewer opcodes to include the code just once.

Something to keep in mind though is what the scoping would be for constructor parameters. They would likely need to be assigned to global contract variables to be accessible to the rest of the contract. For simple constructors, this might be less efficient than the current set up.

rkalis avatar Oct 30 '20 11:10 rkalis

it could be useful to have a constructor function

Want to confirm this! My sidechain covenant uses require(this.activeInputIndex == 0); on all three contract functions so that part is duplicated three times in the compiled script now. It also uses the following three times

bytes23 out = new LockingBytecodeP2SH(hash160(constructedBytecode));
require(tx.outputs[0].lockingBytecode == out);

but the constructedBytecode variable differs for all three cases so the global function would need to run after the normal functions assigning something to the constructedBytecode variable.

mr-zwets avatar Jun 12 '22 20:06 mr-zwets