Signum & AT concepts for developers
Signum smart contracts are Automated Transactions (ATs), programs for the CIYAM AT virtual machine. If you know Ethereum, a lot will feel familiar, and a few things work quite differently. This page covers what you need to write contracts that work and don’t waste SIGNA. For the SmartC language itself, see the SmartC repository.
A contract is an account
Section titled “A contract is an account”A deployed contract has its own account id and balance. It can receive SIGNA and tokens, send them, and keep state. Nobody holds its passphrase: only its code moves its funds. Once deployed, the code can’t be changed. A new version means a new contract.
Contracts run on transactions, block by block
Section titled “Contracts run on transactions, block by block”A contract does nothing on its own. It runs when transactions have arrived for it, in the block that follows (on Signum a block comes roughly every four minutes). By then several transactions may be waiting, so a contract handles them in a loop:
while ((currentTx.txId = getNextTx()) != 0) { // read sender, amount and message of this transaction, then act on it}Always drain the whole queue. A transaction you skip is still there on the next run, often against changed state.
In the Studio: the simulator makes the blocks explicit; you forge each one with ⛏ Next block.
Activation amount and fees
Section titled “Activation amount and fees”Running a contract costs SIGNA, paid per step:
- an ordinary AT instruction costs 1 step, a call to a built-in API function
(
getNextTx,getSender,sendAmount, map access, …) costs 10 steps; - a step costs 0.001 SIGNA;
- one run may take at most 100 000 steps.
Who pays? Every transaction to the contract must carry at least its
activation amount (#program activationAmount). That covers the run; the rest of
the amount is what the sender actually sends. getAmount(txId) returns the amount
above the activation amount.
If a run needs more than the contract can pay, it stops part-way and continues once more SIGNA arrive (the contract is frozen). So size the activation amount to the worst case of a single transaction. The Studio’s gas analysis does that for you, and API calls are the biggest lever for making contracts cheaper.
Messages and method codes
Section titled “Messages and method codes”There are no function calls from outside. A transaction carries a message, and the contract decides what it means. A message page is 32 bytes, read as four 64-bit numbers:
readMessage(currentTx.txId, 0, currentTx.message); // page 0 → message[0..3]switch (currentTx.message[0]) { // the method code case METHOD_DEPOSIT: deposit(currentTx.message[1]); break; case METHOD_WITHDRAW: withdraw(); break; default: /* refund or ignore unknown methods */ break;}By convention the first number is the method code, the others are arguments.
Longer messages span several pages (readMessage(txId, 1, buffer) and so on).
In the Studio: tests send messages with asHexMessage([method, arg]), scenarios with
messageHex; see Testing and
Simulating.
State: variables and maps
Section titled “State: variables and maps”A contract has two kinds of state:
- Variables: fixed memory slots (the data stack), declared as
long(64-bit integer) orfixed(64-bit fixed point with 8 decimals, handy for SIGNA amounts). - Maps: key/key/value storage.
setMapValue(key1, key2, value)andgetMapValue(key1, key2)store and read 64-bit values; anyone can read a contract’s maps from outside, and other contracts can too. Maps suit per-account data such as balances:key1names the kind of entry,key2is the account id.
In the Studio: the simulator shows variables, the inspector shows variables and maps of deployed contracts.
Who sent it? Permissions are your job
Section titled “Who sent it? Permissions are your job”Every transaction tells you its sender (getSender). There is no built-in access
control: if only the owner may withdraw, compare the sender with the owner yourself.
The Studio’s linter warns about handlers that change state or send funds without a
sender check.
Errors: record, don’t throw
Section titled “Errors: record, don’t throw”A contract can’t revert a transaction. If a request is invalid, ignore it, refund it,
or record an error code (for example in a map under the transaction id) for the
sender to read. A fault the code doesn’t handle, such as a division by zero, kills the
contract for good, with its balance lost. A void catch() function runs instead
and can wind the contract down safely.
Lifecycle
Section titled “Lifecycle”| State | Meaning |
|---|---|
| Finished | main() returned or exit ran; the next transaction starts main() again |
| Stopped | sleep or halt ran; the next run resumes after it (sleep N waits N blocks) |
| Frozen | Not enough balance to go on; resumes once funded |
| Dead | An unhandled fault; the contract never runs again |
Limits
Section titled “Limits”- Size: at most 10240 bytes (10 KiB) of compiled code. Above 8 KiB, XT Wallet can’t sign the deployment; see Large contracts.
- Types: only
longandfixed. No floating point, no heap, no strings beyond packing up to 8 characters into a number. - No outside world: a contract sees only the blockchain: transactions, blocks, accounts and other contracts. Data from elsewhere must come in through transactions.
- No true randomness: the random function is predictable to a determined attacker; don’t base valuable outcomes on it.
- One run is bounded by its funds and the step ceiling, so avoid loops that grow with the number of users.
Coming from Solidity
Section titled “Coming from Solidity”| Ethereum / Solidity | Signum / SmartC |
|---|---|
| Call a function with arguments | Send a transaction with a message: method code + up to 3 numbers per page |
| Gas paid by the caller, per call | Activation amount carried by each transaction |
| Runs in the same block | Runs in the next block, for all waiting transactions |
require / revert |
No revert: ignore, refund or record an error |
mapping(address => uint) |
setMapValue(KEY, sender, value) |
msg.sender, msg.value |
getSender(txId), getAmount(txId) |
| Upgradable proxies | No upgrades: deploy a new contract |
- Your first contract in 15 minutes
- Writing code and Testing
- Signum documentation for the network itself