Skip to content

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.

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.

  • 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.

  1. Open SmartC Studio.

  2. 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.c The contract, written in SmartC
    tip-jar.test.ts Unit tests for the contract
    tip-jar.scenario.json A list of transactions for the simulator
    README.md A short description of the project
  3. 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_0000

    The 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 with 1 (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;
    }
  4. Hover over a built-in function such as getNextTx to read its documentation. Errors and warnings are marked in the editor as you type; the Tip Jar has none.

  5. 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.asm next 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.

  1. Click Test in the workflow rail. The Studio opens tip-jar.test.ts.

  2. 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.

  3. 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.

  4. Try breaking it. Change 7n * SIGNA to 6n * SIGNA and click Run again. The test fails and shows the value the contract really holds. Change it back.

The simulator runs the contract on a pretend blockchain, block by block, so you can watch it work.

  1. Click Simulate in the workflow rail. The scenario tip-jar.scenario.json is 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.
  2. Click ▶ Start. The contract handles the transactions of block 1 until the round is over; the status reads finished.
  3. Open Contract Status → Variables on the right. totalTips and unpaidTips now show Alice’s first tip: 200000000, that is 2 SIGNA in planck, the smallest unit (1 SIGNA = 1_0000_0000).
  4. 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.
  5. Check the result: totalTips is 800000000 (8 SIGNA) and unpaidTips is 0, because the creator collected the tips in block 4.
  6. 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 (10775000000 planck).

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.

Deploying publishes the contract to the Signum blockchain. A deployed contract can’t be changed, so always deploy to Testnet first.

  1. 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.
  2. Click Deploy in the workflow rail. The deploy page shows a Contract Summary: the code size and the minimum fee for deploying.
  3. 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.
  4. Click Deploy Contract and confirm the transaction in XT Wallet.
  5. 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.

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.