# RideShare Tutorial

Source: https://docs.formance.com/v3.2/examples/getting-started/rideshare-tutorial

Stack: v3.2

In this tutorial, we will set up and use Formance Ledger for a ride-sharing platform called "RideShare." We will design a Chart of Accounts, create a schema to enforce it, use Numscript to execute transactions, and check account balances.

- fctl CLI installed

- A sandbox stack created

## The Complete Schema

Here is the full ledger schema for the RideShare tutorial. It defines the account hierarchy and all four transaction patterns we'll walk through below.

```yaml
# yaml-language-server: $schema=https://ds.formance.com/schemas/ledger-schema/extended
meta:
  slug: rideshare-tutorial
  order: 1
  name: "RideShare Tutorial"
  description: "Tutorial marketplace: rider charge, driver earnings, platform fee."
  tags:
    - "tutorial"
    - "marketplace"
    - "rideshare"
  docsUrl: "https://docs.formance.com/examples/getting-started/rideshare-tutorial"
chart:
  world:
    .self: {}

  rider:
    $rider_id:
      ride:
        $ride_id:
          payment:
            .self: {}

  ride:
    $ride_id:
      main:
        .self: {}
      fees:
        .self: {}

  driver:
    $driver_id:
      ride:
        $ride_id:
          .self: {}
      main:
        .self: {}

transactions:
  RIDE_BOOKING:
    description: >
      Rider books a ride. Record the estimated payment from the external
      world into the rider's ride payment account.
    interpreter: experimental
    featureFlags: [experimental-account-interpolation]
    script: |
      #![feature("experimental-account-interpolation")]
      vars {
          number $amount
          account $rider_id
          account $ride_id
      }

      send [USD/2 $amount] (
          source = @world
          destination = @rider:$rider_id:ride:$ride_id:payment
      )

  RIDE_CONFIRMATION:
    description: >
      RideShare confirms the ride. Transfer the recorded amount from
      the rider's payment account to the ride's main account.
    interpreter: experimental
    featureFlags: [experimental-account-interpolation]
    script: |
      #![feature("experimental-account-interpolation")]
      vars {
          number $amount
          account $rider_id
          account $ride_id
      }

      send [USD/2 $amount] (
          source = @rider:$rider_id:ride:$ride_id:payment
          destination = @ride:$ride_id:main
      )

  RIDE_COMPLETION:
    description: >
      Driver finishes the ride and RideShare verifies it. Split the
      payment between the driver's earnings and RideShare's service fees.
    interpreter: experimental
    featureFlags: [experimental-account-interpolation]
    script: |
      #![feature("experimental-account-interpolation")]
      vars {
          number $amount
          account $ride_id
          account $driver_id
      }

      send [USD/2 $amount] (
          source = @ride:$ride_id:main
          destination = {
              10% to @ride:$ride_id:fees
              remaining to @driver:$driver_id:main
          }
      )

  DRIVER_PAYOUT:
    description: >
      Driver requests withdrawal. Transfer all accumulated earnings
      back to the external world.
    interpreter: experimental
    featureFlags: [experimental-account-interpolation]
    script: |
      #![feature("experimental-account-interpolation")]
      vars {
          account $driver_id
      }

      send [USD/2 *] (
          source = @driver:$driver_id:main
          destination = @world
      )
```

[Open in Studio](https://studio.formance.com/ledger-schemas/editor?template=rideshare-tutorial\&utm_source=docs\&utm_campaign=ledger-schema-cookbook\&utm_template=rideshare-tutorial)

Edit this template in the Formance Studio editor.

## Design the Chart of Accounts

The [Chart of Accounts](https://docs.formance.com/v3.2/modules/ledger/advanced/architecting-for-scale#chart-of-accounts) should reflect all account types relevant to your flow of funds and reporting needs.

We recommend starting with the key questions you need the ledger to answer. For RideShare:

- How much did we pay the drivers?
- How much did we collect in service fees?
- How much did an individual rider pay?
- How much did an individual driver earn?

And key events the ledger must support:

- Rider books a ride
- RideShare confirms the ride
- Driver finishes the ride
- RideShare completes ride verification
- Driver requests earnings withdrawal

The schema defines four account groups to support these questions and events:

### World

```yaml
chart:
  world:
    .self: {}
```

The `@world` account represents the external world — funds entering and exiting the ledger.

### Rider

```yaml
chart:
  rider:
    $rider_id:
      ride:
        $ride_id:
          payment:
            .self: {}
```

Each rider gets a per-ride payment account (`rider:$rider_id:ride:$ride_id:payment`) that records the estimated charge for that ride.

### Ride

```yaml
chart:
  ride:
    $ride_id:
      main:
        .self: {}
      fees:
        .self: {}
```

Each ride has a `main` account (total charged) and a `fees` account (RideShare's service fee).

### Driver

```yaml
chart:
  driver:
    $driver_id:
      ride:
        $ride_id:
          .self: {}
      main:
        .self: {}
```

Each driver has per-ride earning accounts and a `main` account that accumulates total earnings across rides.

The structured [naming convention](https://docs.formance.com/v3.2/modules/ledger/core-concepts/accounts#naming-accounts) helps organize accounts into segments, making it easier to manage and query them.

## Create the Schema

Now let's create the [schema](https://docs.formance.com/v3.2/modules/ledger/working-with/ledger-schema) to enforce the Chart of Accounts. First, get your sandbox API endpoint:

```bash
fctl stack show
```

Then create the schema using the API. You can copy the YAML from the schema viewer above, or use the JSON equivalent:

curl

```bash
curl -X POST $FORMANCE_API_URL/api/ledger/v2/default/schemas/v1.0.0 \
  -H "Content-Type: application/json" \
  -d '{
    "chart": {
      "world": {},
      "rider": {
        "$rider_id": {
          "ride": {
            "$ride_id": {
              "payment": {}
            }
          }
        }
      },
      "ride": {
        "$ride_id": {
          "main": {},
          "fees": {}
        }
      },
      "driver": {
        "$driver_id": {
          "ride": {
            "$ride_id": {}
          },
          "main": {}
        }
      }
    },
    "transactions": {}
  }'
```

By default, the ledger runs in `audit` mode—transactions are validated against your schema but allowed through. For strict enforcement, see [enforcement modes](https://docs.formance.com/v3.2/modules/ledger/working-with/ledger-schema#enforcement-modes).

## Introduce Money into the Ledger

When a rider books a ride, we record the estimated payment from `@world` into the rider's ride payment account.

```yaml
transactions:
  RIDE_BOOKING:
    description: |
      Rider books a ride. Record the estimated payment from the external world into the rider's ride payment account.
    interpreter: experimental
    featureFlags:
      - experimental-account-interpolation
    script: |
      #![feature("experimental-account-interpolation")]
      vars {
          number $amount
          account $rider_id
          account $ride_id
      }

      send [USD/2 $amount] (
          source = @world
          destination = @rider:$rider_id:ride:$ride_id:payment
      )
```

Execute this transaction:

curl

```bash
curl -X POST $FORMANCE_API_URL/api/ledger/v2/default/transactions \
  -H "Content-Type: application/json" \
  -d '{
    "script": {
      "plain": "send [USD/2 2000] (\n  source = @world\n  destination = @rider:xx:ride:yy:payment\n)"
    }
  }'
```

fctl

```bash
fctl ledger transactions num intro.num
```

This adds $20 to the `@rider:xx:ride:yy:payment` account.

## Run Transactions on the Ledger

### Ride Confirmation

When RideShare confirms the ride, transfer the payment from the rider's account to the ride's main account.

```yaml
transactions:
  RIDE_CONFIRMATION:
    description: |
      RideShare confirms the ride. Transfer the recorded amount from the rider's payment account to the ride's main account.
    interpreter: experimental
    featureFlags:
      - experimental-account-interpolation
    script: |
      #![feature("experimental-account-interpolation")]
      vars {
          number $amount
          account $rider_id
          account $ride_id
      }

      send [USD/2 $amount] (
          source = @rider:$rider_id:ride:$ride_id:payment
          destination = @ride:$ride_id:main
      )
```

curl

```bash
curl -X POST $FORMANCE_API_URL/api/ledger/v2/default/transactions \
  -H "Content-Type: application/json" \
  -d '{
    "script": {
      "plain": "send [USD/2 2000] (\n  source = @rider:xx:ride:yy:payment\n  destination = @ride:yy:main\n)"
    }
  }'
```

fctl

```bash
fctl ledger transactions num payment.num
```

### Ride Completion

After the ride is completed, split the payment between the driver's earnings and RideShare's service fees.

```yaml
transactions:
  RIDE_COMPLETION:
    description: |
      Driver finishes the ride and RideShare verifies it. Split the payment between the driver's earnings and RideShare's service fees.
    interpreter: experimental
    featureFlags:
      - experimental-account-interpolation
    script: |
      #![feature("experimental-account-interpolation")]
      vars {
          number $amount
          account $ride_id
          account $driver_id
      }

      send [USD/2 $amount] (
          source = @ride:$ride_id:main
          destination = {
              10% to @ride:$ride_id:fees
              remaining to @driver:$driver_id:main
          }
      )
```

curl

```bash
curl -X POST $FORMANCE_API_URL/api/ledger/v2/default/transactions \
  -H "Content-Type: application/json" \
  -d '{
    "script": {
      "plain": "send [USD/2 2000] (\n  source = @ride:yy:main\n  destination = {\n    10% to @ride:yy:fees\n    remaining to @driver:zz:main\n  }\n)"
    }
  }'
```

fctl

```bash
fctl ledger transactions num payment_split.num
```

### Driver Payout

After some days, the driver requests their earnings. Transfer all accumulated earnings back to `@world` to represent funds leaving the ledger. The wildcard `*` sends the entire balance.

```yaml
transactions:
  DRIVER_PAYOUT:
    description: |
      Driver requests withdrawal. Transfer all accumulated earnings back to the external world.
    interpreter: experimental
    featureFlags:
      - experimental-account-interpolation
    script: |
      #![feature("experimental-account-interpolation")]
      vars {
          account $driver_id
      }

      send [USD/2 *] (
          source = @driver:$driver_id:main
          destination = @world
      )
```

curl

```bash
curl -X POST $FORMANCE_API_URL/api/ledger/v2/default/transactions \
  -H "Content-Type: application/json" \
  -d '{
    "script": {
      "plain": "send [USD/2 *] (\n  source = @driver:zz:main\n  destination = @world\n)"
    }
  }'
```

fctl

```bash
fctl ledger transactions num driver_payment.num
```

## Check Account Balances

You can check the balances of the accounts using the [Formance Console](https://portal.formance.cloud).
