# How It Works

The deal flow from creation to settlement.

---

## The flow

1. **Create a deal** — A deal is defined by a set of recipient wallets and the exact amount (in wei) each one receives. The creator can also set a `referrer` wallet (to receive Shaka's automatic 1% creator bonus) and an `affiliate` wallet (resolved from a referral link) — both optional, both fixed at creation. The contract returns a deal ID.

2. **Quote** — Before paying, the payer calls `quote(dealId)` to get the exact `grandTotal`, in wei, to send — the recipient amounts plus the referrer, affiliate, and Shaka fee.

3. **Pay** — The payer sends `grandTotal` to the contract's `pay()` function, referencing the deal ID.

4. **Route** — The contract distributes the recipient amounts, the referrer's bonus, the affiliate's share, and Shaka's fee — all in the same transaction. The transaction is final and irreversible.

A deal can also be **cancelled** by its creator at any point before it's paid — after that, it can never be paid.

## Key properties

- **Single transaction** — recipients, referrer, and Shaka are all settled in one `pay()` call. If a recipient or the referrer transfer fails, the whole payment reverts. The affiliate is the one exception: if its transfer fails, that share is redirected to Shaka (with a capped gas forward, so a broken affiliate wallet can't block the deal) and the rest of the payment still settles.
- **Immutable** — the contract cannot be modified after deployment. No admin can change the routing logic or fee rate.
- **Permissionless** — anyone can create a deal or pay into one. No account required.
- **Cancellable, not editable** — the creator can cancel an unpaid deal, but nothing about a deal (recipients, amounts, referrer, affiliate) can be changed after creation.

## Fee calculation

The fee (routed to the referrer, the affiliate, and Shaka) is added on top of the recipient amounts, not deducted from them — recipients always receive their amounts in full. Rather than computing it yourself, call `quote(dealId)` to get the exact total to send:

```javascript
const grandTotal = await shaka.quote(dealId);
await shaka.pay(dealId, { value: grandTotal });
```

See the [Fees](/docs/fees) page for how `grandTotal` breaks down between recipients, referrer, affiliate, and Shaka.

## Events

Every payment emits a `DealPaid` event:

```solidity
event DealPaid(
  bytes32 indexed dealId,
  address indexed payer,
  uint256 referrerAmount,
  uint256 affiliateAmount,
  uint256 shakaAmount,
  uint256 grandTotal
);
```

Cancelling a deal emits `DealCancelled`:

```solidity
event DealCancelled(
  bytes32 indexed dealId,
  address indexed creator
);
```