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 SmartJ and SmartC 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.
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.
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 useful tool for Smart Contract Developers, which allows to inspect all the information:
The Header contains information, which every contract contains in the same unified manner
- Smart Contract Address
- Creator Address
- Machine Code Hash Id
- Execution Status
- Minimum Activation Costs
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 Green Contracts.
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.
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 ContractDataView to get those data.