cashscript icon indicating copy to clipboard operation
cashscript copied to clipboard

consider sytactic suger for the activeInput

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

Rosco floated the idea that perhaps we could add syntactic sugar for the activeInput:

this.input (or this.activeInput) for tx.inputs[this.activeInputIndex] which would simplify common syntax

and to potentially do something similar for the corresponding output:

this.correspondingOutput for tx.outputs[this.activeInputIndex]

the drawback/danger of the syntax for the output is that it dilutes the meaning of this referring to the active script/input and is only slightly shorter (although no brackets)...

Example

With the new syntactic sugar, self-replicating covenants would then look like this:

    require(this.correspondingOutput.lockingBytecode == this.activeInput.lockingBytecode);
    require(this.correspondingOutput.tokenCategory == this.activeInput.tokenCategory);
    require(this.correspondingOutput.value == 1000);
    require(this.correspondingOutput.tokenAmount == this.activeInput.tokenAmount);
    require(this.correspondingOutput.nftCommitment == this.activeInput.nftCommitment);

whereas before:

    require(tx.outputs[this.activeInputIndex].lockingBytecode == tx.inputs[this.activeInputIndex].lockingBytecode);
    require(tx.outputs[this.activeInputIndex].tokenCategory == tx.inputs[this.activeInputIndex].tokenCategory);
    require(tx.outputs[this.activeInputIndex].value == 1000);
    require(tx.outputs[this.activeInputIndex].tokenAmount == tx.inputs[this.activeInputIndex].tokenAmount);
    require(tx.outputs[this.activeInputIndex].nftCommitment == tx.inputs[this.activeInputIndex].nftCommitment);

mr-zwets avatar Nov 05 '24 13:11 mr-zwets

My first thought is it would be nice to simplify down to something like:

    function doThing() {  
        require(this.inputOutput == 1); //optional, reads more simply/makes sense with below terms
        
        require(this.output.lockingBytecode == this.input.lockingBytecode);
        require(this.output.tokenCategory == this.input.tokenCategory);
        require(this.output.value == 1000);
        require(this.output.tokenAmount == this.input.tokenAmount);
        require(this.output.nftCommitment == this.input.nftCommitment);
    }

When I look at contracts I'm already assuming the contract is only referring to the individual input being processed against it, and having checks done against other inputs/outputs always requires referring to them directly via tx.input/output[#]. I think the only time I ever use this. is specifying the activeInputIndex, so I feel the above would line up with that still.

SayoshiNakamario avatar Nov 05 '24 17:11 SayoshiNakamario