Skip to content

Add Global constants #264

Open
Open
@mr-zwets

Description

@mr-zwets

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    cashc-compilerRelates to the cashc compilerenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions