CashScript / cashscript

⚖️ Easily write and interact with Bitcoin Cash smart contracts
https://cashscript.org
MIT License
115 stars 80 forks source link

Add struct functionality #27

Open rkalis opened 5 years ago

rkalis commented 5 years ago

Structs can be defined by the implementer of a contract like this:

struct PriceMessage {
    bytes4 blockheight;
    bytes4 price;
}

The CashScript SDK can automatically serialise JSON data into a byte array containing these concatenated struct members. This serialised struct can then be passed into a contract function and the compiled script can use OP_SPLIT and OP_BIN2NUM to extract the correct struct members where needed.

To make sure the components can be extracted, they should use sized bytes types, so the usefulness of these structs is dependent on issue #20.

rkalis commented 4 years ago

You could probably have destructured assignment like this:

{ bytes4 blockheight, bytes4 price } = message;

Or direct access llike this:

message.price
mr-zwets commented 1 week ago

this is no longer only handy for datamessages used with checkDataSig but also for nft state kept by contracts, or to defined the commitments the contract interacts with!

for example:

struct LoanCommitment{
    bytes8 blockheight;
    bytes20 loanerPubKeyHash;
    bytes8 borrowedTokenAmount;
}

inspired by https://bitcoincashresearch.org/t/stable-asset-token-incentivized-floating-peg/1206

the idea that you could destructured borrowedTokenAmount is quite powerful