ServicesWorkVideoAboutContact

Field note · Sui Move ·

Reviewing my own Move contract

In February 2025 I wrote a small Sui Move contract for authorised fund release. Its three tests passed, and my site linked to it as proof that I write Move. This autumn I read it again the way I would read a client's contract. It did what its code said, and not what its README promised.

What it was meant to do

The flow in the README: a user asks for funds in a frontend, a backend validates the request, and the contract "verifies authorization and executes the fund transfer". The promise underneath is the one any payout system makes to its recipient: once you are approved, you get paid.

What the review found

Every finding below is pinned by a test, kept with the original code at the v1-review tag.

1. The contract holds no money

release_funds takes a coin from the admin at the moment of release:

public fun release_funds(
    proposals: &mut FundingProposals,
    proposal_id: u64,
    payment: Coin<SUI>,      // brought by the admin, now
    ctx: &mut TxContext
) {
    assert!(tx_context::sender(ctx) == proposals.admin, EUnauthorized);
    // ...
    assert!(coin::value(&payment) == proposal.approved_amount, EInvalidAmount);
    transfer::public_transfer(payment, proposal.recipient);
}

So an "approved" proposal reserves nothing. The recipient's guarantee is exactly as strong as the admin's willingness to pay later, which is what you had without a contract. The checks are real (only the admin, exact amount, once), but they protect the admin from mistakes; they promise the recipient nothing.

2. Approval is not modelled

Anyone can create a proposal, for any amount, to any address, themselves included; a test files one for u64::MAX and it is accepted as ACTIVE. Nothing is at risk since the admin pays, but the contract has no state for "the backend said yes". What it calls an approved amount is just the amount someone asked for.

3. Authority is an address in a field

The admin is whoever deployed the package, stored in a field, with no way to change it. If that key is lost or must be rotated, every open proposal is stuck. On Sui the idiom is a capability: an object whose possession is the permission, which can be transferred, held by a multisig or wrapped by another contract.

4. Time is measured in epochs

created_at and the event's timestamp come from tx_context::epoch: an epoch is about 24 hours on mainnet, not a timestamp. A deadline could not be built on it. The shared Clock object gives milliseconds.

5. Records never leave

Completed proposals stay in the table forever, each carrying its own object id. Storage on Sui is paid for and refunded when deleted; this contract never deletes, so it never gets the refund, and the shared table only grows.

6. Smaller things

  • SUI only, while a payout contract usually pays in a stablecoin.
  • The views abort with the table's generic error on an unknown id, not the module's own.
  • The README says MIT; there was no license file.

Why did three green tests miss all of this? They checked what the code did (the admin can release, a stranger cannot), not what the product promised. That is the gap a review is for.

The rewrite: an approval is a promise the contract keeps

Version 2 is an escrow. A Treasury<T> holds the funds, for any coin type. Approving a grant reserves its amount inside the treasury:

public fun approve<T>(
    treasury: &mut Treasury<T>,
    cap: &AdminCap,
    recipient: address,
    amount: u64,
    deadline_ms: u64,
    clock: &Clock,
): u64 {
    assert_cap(treasury, cap);
    assert!(amount > 0, EZeroAmount);
    assert!(amount <= treasury.available(), EInsufficientFunds);
    assert!(deadline_ms > clock.timestamp_ms(), EDeadlinePassed);

    let grant_id = treasury.next_grant_id;
    treasury.next_grant_id = grant_id + 1;
    treasury.reserved = treasury.reserved + amount;
    treasury.grants.add(grant_id, Grant { recipient, amount, deadline_ms });
    // ... event
    grant_id
}

From then on the recipient does not depend on the admin. They claim the grant themselves; nobody pushes funds to them:

public fun claim<T>(
    treasury: &mut Treasury<T>,
    grant_id: u64,
    clock: &Clock,
    ctx: &mut TxContext,
): Coin<T> {
    assert!(treasury.grants.contains(grant_id), EGrantNotFound);
    let grant = &treasury.grants[grant_id];
    assert!(grant.recipient == ctx.sender(), ENotRecipient);
    assert!(clock.timestamp_ms() <= grant.deadline_ms, EGrantExpired);

    let Grant { recipient, amount, .. } = treasury.grants.remove(grant_id);
    treasury.reserved = treasury.reserved - amount;
    // ... event
    coin::take(&mut treasury.balance, amount, ctx)
}

What the design guarantees, and how:

  • Reserved funds are out of everyone's reach, the admin included.withdraw and approve only see balance − reserved, and no function revokes a live grant.
  • Nothing is stuck forever. Each grant has a deadline; once it has passed, anyone can call expire, which only returns the amount to the unreserved balance.
  • Authority is a capability. One AdminCap per treasury, checked against the treasury's id; transferring it hands over the role, and open grants survive the handover (there is a test for exactly that).
  • Closed grants are deleted. Grants are plain store values removed from the table on claim or expiry, so storage is refunded.
  • One invariant: balance ≥ reserved after every call. Every function that moves funds is written around it, and the accounting test walks it through approvals, a claim and two expiries.

Errors are Move 2024 clever errors, so a failed transaction says why in words. Every change emits an event, timestamped from the Clock. 22 tests cover 100% of the module's lines, including the paths a reviewer goes for first: a cap from another treasury, a claim by the admin, a claim one millisecond after the deadline, paying a grant twice.

On testnet

The package is published on Sui testnet at 0x192f…b6cb, and was exercised end to end. The fourth transaction is the one that matters: the admin, holding the cap, asks for more than the unreserved balance, and the chain refuses.

StepTransaction
Publish the package3qG29f…
Create a Treasury<SUI>EFQfmh…
Deposit 0.1 SUI, approve two grants: 0.05 and 0.02FcPNWH…
The admin tries to withdraw 0.06 SUI: refused, EInsufficientFundsBM5bHX…
The recipient claims the 0.05 grantHRqZS4…
The 0.02 grant lapses and is expiredJ2kr9J…

What v2 still does not do

  • The admin is trusted to approve the right grants: the contract limits what happens after an approval, not the judgment behind it. Approval by several signers would sit in front of the cap (a multisig address holding it), not inside this module.
  • One shared treasury orders every grant through consensus. That is fine for payouts; a high-throughput system would give each grant its own object.
  • No partial claims and no vesting schedule: a grant is paid whole or expires.
  • It has not been audited. A review like this one is not a formal security audit.

Why publish this

Because this is the work I do for teams: reading a contract against what the product promises, writing the tests that pin each gap down, and fixing it so the guarantee lives in the code. The full history is public, the weak version included, in the repository.

Building on Sui, or need a contract read against your product's rules?