SATURN DEX v4 Developer Documentation

Build on the Saturn DEX
The future of on-chain liquidity.

A complete developer reference for every public method a builder can call.

One Protocol, Many Layers

Quickstart

Saturn contracts are deployed on Phantasma. Use the phantasma-sdk-ts or any Phantasma-compatible library to call them. Read methods are free and can be called from anywhere. A write method that takes a caller address (from, executor, …) must be signed by that wallet, its witness. Admin-only writes must be signed by the admin they name; the few other writes that take no address (for example saturnloans.flagLiquidation) can be sent by anyone.

1 · Install the SDK

npm install phantasma-sdk-ts

2 · Read contract data

import { PhantasmaAPI, ScriptBuilder, VMObject, VMType, Base16 } from "phantasma-sdk-ts";

// Devnet: "https://devnet.phantasma.info/rpc" with nexus "testnet".
const api = new PhantasmaAPI("https://pharpc1.phantasma.info/rpc", null, "mainnet");

// readContract(): the helper every read example on this site calls.
async function readContract(contract, method, args = []) {
  const script = new ScriptBuilder()
    .beginScript()
    .callContract(contract, method, args)
    .endScript();
  const res = await api.invokeRawScript("main", script);
  if (res.error) throw new Error(res.error); // holds the contract's revert string
  const values = res.results.map((hex) => {
    const v = VMObject.FromBytes(Base16.decodeUint8Array(hex));
    if (v.Type === VMType.Number) return v.Data <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(v.Data) : v.Data;
    if (v.Type === VMType.Bool) return v.Data;
    return v.AsString(); // strings, and addresses as P2K… text
  });
  return values.length === 1 ? values[0] : values; // 2+ yields (type*): an array
}

const info = await readContract("saturnrouter", "getPoolFullInfo", [33]);
// "tokenA:RA_tokenB:TAZ_resA:…_fee:30_active:1_campLocks:0_finLocks:0"

3 · Send a transaction

import { ScriptBuilder, Transaction, PhantasmaKeys, Address, Base16 } from "phantasma-sdk-ts";

// Reserve gas, call the method, pay for the gas used. gasPrice 100000 × gasLimit
// 200000 caps the fee at 2 KCAL (raw KCAL has 10 decimals); a typical call uses
// 0.02–0.15 KCAL and only the gas used is charged.
const script = new ScriptBuilder()
  .beginScript()
  .allowGas(from, Address.Null, 100000, 200000)
  .callContract("saturnswap", "swap", [from, poolId, amountIn, "SOUL", "KCAL", minOut])
  .spendGas(from)
  .endScript();
// The examples on this site shorten new ScriptBuilder().beginScript() to
// ScriptBuilder.begin() or sb.begin(), and write allowGas(from, null, gasPrice,
// gasLimit) for this allowGas(from, Address.Null, 100000, 200000) call.

// dApp: pass `script` to Poltergeist / Ecto (PhantasmaLink.signTx); the user signs.
// Bot: sign with your own key and send (devnet nexus: "testnet").
const tx = new Transaction("mainnet", "main", script, new Date(Date.now() + 5 * 60 * 1000), "");
tx.signWithKeys(PhantasmaKeys.fromWIF(process.env.WIF));
const hash = await api.sendRawTransaction(Base16.encodeUint8Array(tx.toByteArray(true)));

4 · Browse example scripts

// One script per method for contracts 1–16
// (saturnadmin to saturnvaults), set up for devnet:
//   github.com/Infinite-Star-Studios/
//     SaturnDex4ExampleScripts
git clone https://github.com/Infinite-Star-Studios/SaturnDex4ExampleScripts.git
Gas & storage: gas is paid in KCAL. A typical Saturn call burns 0.02–0.15 KCAL; createPool is the exception at roughly 2,500 KCAL because it mints the SATURN certificate NFT. No SOUL fee is charged by any Saturn contract any more, but Phantasma escrows a small amount of SOUL from the sender for every new storage key a transaction creates (about 0.002 SOUL per key, refunded when the key is deleted), so wallets that create pools, orders, bonds or loans should hold a little SOUL besides KCAL. The getContractVersion() tag shown on each contract page is what the live mainnet and devnet deployments return; deprecated methods that always revert are marked as such.

Community & Resources

Join the Saturn builder community, grab ready-to-run example scripts, and ship faster on Phantasma.