Sophia Contracts
Reactive provides first-class support for deploying and interacting with Sophia smart contracts on Aeternity.
Overview
Sophia is Aeternity's functional smart contract language. Contracts are compiled to bytecode and deployed on-chain. Reactive uses the ACI (Application Call Interface) — a JSON schema describing the contract's functions and types — for type-safe interactions.
Deploying a Contract
import { deployContract } from '@growae/reactive/actions'
const result = await deployContract(config, {
aci: tokenAci,
bytecode: tokenBytecode,
args: ['MyToken', 18n, 1000000n],
})
console.log('Contract address:', result.address) // ct_...Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
aci | Aci | — | Required. Contract ACI. |
bytecode | string | — | Required. Compiled bytecode. |
args | unknown[] | [] | Init function arguments. |
amount | bigint | 0n | AE to send to the contract. |
ttl | number | 300 | Transaction TTL in blocks relative to current height. Set to 0 for no expiration. |
Default TTL
All transactions default to a TTL of 300 blocks (~15 hours). This prevents stale transactions from lingering indefinitely. Override with ttl: 0 for no expiration.
Reading Contract State
Use readContract for stateless (dry-run) calls — no transaction, no fees:
import { readContract } from '@growae/reactive/actions'
const balance = await readContract(config, {
address: 'ct_token...',
aci: tokenAci,
fn: 'balance',
args: ['ak_owner...'],
})Writing to a Contract
Use callContract for stateful calls that create on-chain transactions:
import { callContract } from '@growae/reactive/actions'
const result = await callContract(config, {
address: 'ct_token...',
aci: tokenAci,
fn: 'transfer',
args: ['ak_recipient...', 500n],
})Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
address | string | — | Required. Contract address (ct_...). |
aci | Aci | — | Required. Contract ACI. |
fn | string | — | Required. Function name. |
args | unknown[] | [] | Function arguments. |
amount | bigint | 0n | AE to attach (payable). |
ttl | number | 300 | Transaction TTL in blocks relative to current height. Set to 0 for no expiration. |
Working with ACI
The ACI is generated by the Sophia compiler. You can obtain it via:
- CLI:
reactive generatereads contract source and outputs typed ACI - Compiler: Use the HTTP compiler API directly
- Manual: Include the JSON ACI in your project
import { tokenAci } from './generated/token'
const balance = await readContract(config, {
address: 'ct_...',
aci: tokenAci,
fn: 'balance',
args: ['ak_...'],
})AEX-9 Tokens
AEX-9 is the Aeternity fungible token standard (equivalent to ERC-20). A typical AEX-9 contract exposes:
| Function | Type | Description |
|---|---|---|
meta_info | read | Token name, symbol, decimals |
total_supply | read | Total token supply |
balance | read | Balance of an account |
allowance | read | Spending allowance |
transfer | write | Transfer tokens |
approve | write | Approve spending |
transfer_allowance | write | Transfer on behalf of |
const info = await readContract(config, {
address: 'ct_token...',
aci: aex9Aci,
fn: 'meta_info',
args: [],
})
// { name: 'MyToken', symbol: 'MTK', decimals: 18n }