Your first contract in 15 minutes
In this quick start you take a small, finished contract, the Tip Jar, through the four steps every contract goes through in SmartC Studio: write, test, simulate and deploy. You don’t need to write any code yourself; the goal is to see how the pieces fit together.
What you’ll build
Section titled “What you’ll build”The Tip Jar is a contract that anyone can send SIGNA to as a tip. It remembers how much each sender has tipped and how much has come in overall. The account that deployed it, the creator, can collect the tips by sending it a message.
What you need
Section titled “What you need”- A desktop browser. SmartC Studio runs entirely in the browser; there is nothing to install, and your projects are stored in this browser.
- Only for the last step (Deploy): the XT Wallet browser extension with an account on Testnet and a few test SIGNA. Testnet SIGNA are free and worth nothing, so you can’t lose real money there. The Signum documentation explains how to set up a wallet and an account.
You can do the first three steps without any wallet at all.
-
Open SmartC Studio.
-
Click New Project. As Project Type, choose Start with an example (Tip Jar), give the project a name of at least 4 characters and click Create.
The Studio creates four files and opens the contract,
tip-jar.smart.c:File What it is tip-jar.smart.cThe contract, written in SmartC tip-jar.test.tsUnit tests for the contract tip-jar.scenario.jsonA list of transactions for the simulator README.mdA short description of the project -
Read through the contract. The comments explain each part. Three ideas matter most.
Every transaction runs the contract. The contract asks for an activation amount of 0.25 SIGNA. Each transaction sent to it has to carry at least that much, which pays for running the code. Whatever comes on top of it is the tip:
#program activationAmount 0_2500_0000The contract handles its transactions one by one.
main()loops until no transaction is left. A transaction with an amount is recorded as a tip; one whose message starts with1(WITHDRAW) is a request to pay out:while ((currentTx.txId = getNextTx()) != 0) {currentTx.sender = getSender(currentTx.txId);currentTx.amount = getAmount(currentTx.txId);readMessage(currentTx.txId, 0, currentTx.message);if (currentTx.amount > 0) {recordTip();}if (currentTx.message[0] == WITHDRAW) {withdraw();}}Only the creator gets paid.
withdraw()ignores anyone else. A contract doesn’t throw errors; it simply does nothing:if (currentTx.sender != creator) {return;} -
Hover over a built-in function such as
getNextTxto read its documentation. Errors and warnings are marked in the editor as you type; the Tip Jar has none. -
Click Compile (or press Ctrl+Shift+C, on a Mac ⇧⌘C). The Studio confirms with “Smart Contract compiled successfully - Assembly file created!” and writes
tip-jar.asmnext to the contract. That is the code that will run on the blockchain.
At the top right you’ll see the workflow rail: Write ✓ compiles, then Test, Simulate and Deploy. It shows where your project stands, and you can click each step to go there.
Tests check that the contract does what it should, in a fraction of a second and without a blockchain.
-
Click Test in the workflow rail. The Studio opens
tip-jar.test.ts. -
Click Run. All three tests pass:
- records each tip per sender and in total
- pays the unpaid tips to the creator on withdraw
- ignores a withdraw from anyone but the creator
The rail now shows Test ✓ 3 green.
-
Look at the first test. It sends transactions to a freshly deployed contract and then checks the result:
it("records each tip per sender and in total", () => {const testbed = run([tip(1, ALICE, 2n), tip(2, BOB, 1n), tip(3, ALICE, 5n)]);expect(testbed.getContractMapValue(TIPS_BY_SENDER, ALICE)).toBe(7n * SIGNA);expect(testbed.getContractMapValue(TIPS_BY_SENDER, BOB)).toBe(1n * SIGNA);expect(testbed.getContractMemoryValue("totalTips")).toBe(8n * SIGNA);});Alice tips 2 and then 5 SIGNA, Bob tips 1: Alice’s total is 7, the jar holds 8.
-
Try breaking it. Change
7n * SIGNAto6n * SIGNAand click Run again. The test fails and shows the value the contract really holds. Change it back.
Simulate
Section titled “Simulate”The simulator runs the contract on a pretend blockchain, block by block, so you can watch it work.
- Click Simulate in the workflow rail. The scenario
tip-jar.scenario.jsonis already selected. It contains four transactions: Alice (account 1001) tips 2 SIGNA in block 1, Bob (1002) 1 SIGNA in block 2, Alice 5 SIGNA in block 3, and in block 4 the creator (555) sends the withdraw message. - Click ▶ Start. The contract handles the transactions of block 1 until the round is over; the status reads finished.
- Open Contract Status → Variables on the right.
totalTipsandunpaidTipsnow show Alice’s first tip:200000000, that is 2 SIGNA in planck, the smallest unit (1 SIGNA = 1_0000_0000). - Click ⛏ Next block to forge block 2, then ▶ Start to run it. Do the same for blocks 3 and 4, and watch the variables after each block.
- Check the result:
totalTipsis800000000(8 SIGNA) andunpaidTipsis0, because the creator collected the tips in block 4. - Click ⛏ Next block once more and open Ledger Status: the payment the
contract sent arrives with this block. The creator’s account (555) started with
100 SIGNA, paid 0.25 SIGNA for the withdraw request and received the 8 SIGNA in
tips, so it now holds 107.75 SIGNA (
10775000000planck).
To pause at a particular line, click its line number to set a breakpoint, and use Step to go through the code one statement at a time. ⟳ Reset starts over.
Deploy
Section titled “Deploy”Deploying publishes the contract to the Signum blockchain. A deployed contract can’t be changed, so always deploy to Testnet first.
- Click Connect Wallet at the bottom left and choose Connect to Testnet. Confirm the connection in XT Wallet, which must be set to a Testnet node.
- Click Deploy in the workflow rail. The deploy page shows a Contract Summary: the code size and the minimum fee for deploying.
- Under Deployment Settings, leave the values as they are: the activation amount comes from the contract (0.25 SIGNA), and the transaction fee is the minimum needed.
- Click Deploy Contract and confirm the transaction in XT Wallet.
- The Studio reports Deployment Successful! with the transaction ID. The arrow next to it opens the transaction in the Testnet explorer; Inspect opens the contract in the Studio’s inspector. The contract goes live once the transaction is included in a block, usually within a few minutes.
To try it, send your contract a tip from XT Wallet: any amount above 0.25 SIGNA.
Result
Section titled “Result”You took a contract through all four steps: you read and compiled it, proved its behaviour with tests, watched it run block by block in the simulator, and deployed it to Testnet.
- Signum & AT concepts for developers: what activation, fees, messages and maps really are.
- Writing code: the editor, compiling, linting and gas analysis.
- Testing: writing your own tests.
- Simulating & debugging: scenarios, breakpoints and the inspector.
- Deploying: parameters, deploying by reference and large contracts.