Skip to content

Cheat Sheet

Quick reference for current Xian contract development.

State Primitives

python
owner = Variable()
balances = Hash(default_value=0)
metadata = Hash()

foreign_owner = ForeignVariable(
    foreign_contract="currency",
    foreign_name="owner",
)

foreign_balances = ForeignHash(
    foreign_contract="currency",
    foreign_name="balances",
)

Events

python
TransferEvent = LogEvent(
    event="Transfer",
    params={
        "from": {"type": str, "idx": True},
        "to": {"type": str, "idx": True},
        "amount": {"type": (int, float, decimal)},
    },
)

Decorators

python
@construct
def seed():
    owner.set(ctx.caller)

@export
def transfer(to: str, amount: float):
    pass

def helper():
    pass

Export Signature Types

Allowed for exported arguments and exported return annotations:

python
str, int, float, bool, dict, list, Any
datetime.datetime, datetime.timedelta

Context

NameMeaning
ctx.callerimmediate caller
ctx.signeroriginal transaction signer
ctx.thiscurrent contract name
ctx.ownercurrent contract owner
ctx.entry(contract, function) entry point

Environment Values

NameMeaning
nowdeterministic block time
block_numcurrent block height
block_hashcurrent block hash
chain_idnetwork id

Imports

python
import currency
import hashlib
import datetime
import random
import importlib
import crypto
import decimal

Do not use from x import y.

Read / Write Patterns

python
owner.set("alice")
current_owner = owner.get()

balances["alice"] = 100
balances["alice", "con_dex"] = 25

bal = balances["alice"]
allowance = balances["alice", "con_dex"]

Client-side direct inspection:

python
client.get_var("con_token", "balances", arguments=["alice"])
client.set_var("con_token", "balances", arguments=["alice"], value=500)

Common Assertions

python
assert amount > 0, "Amount must be positive"
assert balances[ctx.caller] >= amount, "Insufficient balance"
assert ctx.caller == owner.get(), "Only owner"
assert ctx.caller == ctx.signer, "Direct calls only"

Common Token Surface

python
@export
def transfer(amount: float, to: str):
    ...

@export
def approve(amount: float, to: str):
    ...

@export
def transfer_from(amount: float, to: str, main_account: str):
    ...

@export
def balance_of(address: str) -> float:
    ...

Disallowed Patterns

  • classes
  • nested functions
  • try/except
  • lambda
  • async
  • generators / yield
  • stdlib imports
  • names starting or ending with _