cashscript icon indicating copy to clipboard operation
cashscript copied to clipboard

Add Global constants

Open mr-zwets opened this issue 1 year ago • 0 comments

Currently in CashScript there is no way to define global constants.

This feature has also been requested by @dagurval

one thing I’ve missed is defining (global) constants.

I can define constants now like int constant CONTRACT_IN = 1, but only within function and it also seems to put it on the stack, waisting op codes on juggling the stack around when I use it

To clarify, what you would like to have is that using these variables just inlines the value at compile time, is that right?

Correct!

I guess you could space optimize by having larger constants on the stack rather than inline? I guess it’s a design decision up to you guys :-). Now that we’re bumping the op code limit maybe it doesn’t matter

Here's some more cases when global constants might be used:

int constant SATS_PER_BCH = 100_000_000;
int constant DUST_AMOUNT= 1_000;
int constant ACTIVE_INPUT_INDEX = 2;

byte constant MUTABLE_CAPABILTY = 0x01;
byte constant MINTING_CAPABILTY = 0x02;

contract Example(...

We would disallow constants to be introspection variables (as these are 'runtime' variables), they would be a better fit for 're-usable functions'

int constant TOKEN_ID_ACTIVE_INPUT= tx.inputs[this.activeInputIndex].tokenCategory.split(32)[0];

This issue is related to #80 'Add global contract variables / constructor' but I decided to create a new topic because of the broad scope of the original.

mr-zwets avatar Jan 29 '25 09:01 mr-zwets