Development
...
Recipes
Smart Contracts
7 min
signum is the pioneer of smart contracts when signum was launched back in august, 2014 it came already with a smart contract engine in the last years a lot of effort were put into the tooling, and now we have https //github com/signum network/signum smartj and https //github com/deleterium/smartc/tree/smartc signum compiler%402 0 0/docs to make development easier signumjs is not meant for writing smart contracts, but for interaction and data reading developing smart contracts is out of the scope of this documentation before we can hop into the smart contract examples some basic understanding is needed workflow of smart contract development the very first step is the idea of the contract all the required rules and states should be outlined carefully, as a once published contract is immutable if defined, the contract has to be implemented using either smartj or smartc both environments provide simulators, which facilitates the development nevertheless, thorough testing in testnet is required also anatomy of a smart contract before we can interact with a smart, contract we need to understand how it is basically structured a smart contract consists of the code itself, the data (or state) and some additional general unified information (let's call it header) the signum eco system provides a https //contracts inspector ohager vercel app/ , which allows to inspect all the information contract header the header contains information, which every contract contains in the same unified manner smart contract address creator address name description machine code hash id version balance execution status minimum activation costs contract code stack the code stack is the binary representation of the contracts program as the code stack is the only entirely static, i e constant section of a contract, it is possible to reuse it like a class in oop it is possible to instantiate a contract only by referencing a previously deployed code this way, the blockchain does not need to store redundant data making it cheaper and increases the amount of deployable contracts per block this is what we call https //github com/signum network/sips/blob/master/sip/sip 30 md contract data stack the data stack contains the current state of a contract it is organized in 8 byte blocks and can be read publicly the data per block is in big endian byte order, such that the data needs to be converted accordingly luckily, signumjs does this already, but one needs to know the positions/addresses of the data fields let's explain it with a real example the following code is an excerpt of a real smart contract all global variables in a contract are indexable and always in the same order as the are coded smartc #define version 1 0 // #define simulator \#define testnet // #define mainnet \#program name zthpayer \#program activationamount 25 \#pragma optimizationlevel 3 \#ifdef simulator \#define zth token id 1 \#endif \#ifdef testnet \#define zth token id 9866069454022597581 \#endif \#ifdef mainnet \#define zth token id 9518219425200752102 \#endif // global variables, // these are adressable in their order of appearance long thresholdsigna = 100 0000 0000; long thresholdquantity = 0; long isalive = true; struct txinfo { long txid, sender, amount, message\[4]; } currenttx; void main(void) { while ((currenttx txid = getnexttx()) != 0) { currenttx sender = getsender(currenttx txid); currenttx amount = getamount(currenttx txid); / / } } if we look into the inspector we can find the value of 100,000,000 at the fifth position, followed by zero and one (which equals true) this is exactly the order of the variables in the contracts code we will show later how to use signumjs's https //signum network github io/signumjs/classes/contracts contractdataview\ html to get those data