# @sly-rentals/core

> TypeScript SDK for the [`srsly`](https://github.com/wuwei-labs/srsly) rental program on Solana.

**Status**: pre-1.0. The instruction surface tracks the on-chain program; expect minor additions as new program features ship. See [`CHANGELOG.md`](./CHANGELOG.md) for release history.

## What it does

Builds and serializes instruction byte buffers for the `srsly` rental program. Generated by [Codama](https://github.com/codama-idl/codama) from the Anchor IDL, with thin convenience wrappers around the most-used flows (creating contracts, accepting rentals, claiming payments, settling discounts).

Dual-protocol exports — pick the entry point that matches your existing stack:

| Import | Returns | When to use |
| --- | --- | --- |
| `@sly-rentals/core` | `@solana/kit` instructions | New code; ESM-first, smaller bundle |
| `@sly-rentals/core/legacy` | `@solana/web3.js` `TransactionInstruction[]` | Existing wallet-adapter / web3.js stacks |
| `@sly-rentals/core/audit` | Decoded events + history helpers | Indexers, dashboards, accounting flows |
| `@sly-rentals/core/idl` | Raw Anchor IDL JSON | Custom client generation, tx decoders |

## Install

```bash
pnpm add @sly-rentals/core
```

## Quick start (kit)

```ts
import { setSdkConfig, createContract, acceptRental } from '@sly-rentals/core';

setSdkConfig({ network: 'mainnet-beta' });

// Owner: list a fleet for rent
const create = await createContract({
  owner: ownerSigner,
  fleet: fleetAddress,
  ownerProfile: profileAddress,
  rate: 100,                  // 100 ATLAS per period
  durationMax: { days: 30 },
  paymentsFreq: '@daily',
});

// Borrower: rent a listed fleet
const accept = await acceptRental({
  borrower: borrowerSigner,
  contract: contractAddress,
  duration: { days: 7 },
});
```

## Quick start (legacy / web3.js)

```ts
import { createContract } from '@sly-rentals/core/legacy';

const instructions = await createContract({
  owner: ownerKeypair,
  fleet: fleetPubkey,
  ownerProfile: profilePubkey,
  rate: 100,
  durationMax: { days: 30 },
  paymentsFreq: '@daily',
});

transaction.add(...instructions);
```

## Configuration

```ts
import { setSdkConfig } from '@sly-rentals/core';

setSdkConfig({
  network: 'mainnet-beta',           // 'mainnet-beta' | 'atlasnet' | 'localnet'
  rpcUrl: 'https://your-rpc.com',    // optional override
  commitment: 'confirmed',           // optional
});
```

Per-call overrides take precedence over global config:

```ts
const ix = await createContract(params, { rpcUrl: 'https://temporary-rpc.com' });
```

## Parameters

Instruction params accept multiple forms — pick what your stack uses:

- **Signers**: string addresses, `@solana/kit` signers, or `@solana/web3.js` `Keypair`
- **Addresses**: strings or `PublicKey`
- **Rates**: numbers (ATLAS) or `{ atlas: 1.5 }` / `{ stardust: 150_000_000 }`
- **Durations**: `{ days: 7 }`, `{ hours: 24 }`, `{ weeks: 2 }`, etc.
- **Schedules**: `'@hourly'`, `'@daily'`, `'@weekly'`, `'@monthly'`, or a cron expression

## Discount authorization

The SDK includes a server-side helper for affiliate discounts:

```ts
// Server: sign a discount attestation
import { createDiscount } from '@sly-rentals/core';

const discountAuth = await createDiscount({
  address: affiliateMemberAddress,
  discount: 10,                   // 10% off
  expirySlots: 1000,              // ~6–7 minutes
  discountAuthority: serverKeypair,
});

return discountAuth;              // JSON-safe; return directly via your API
```

```ts
// Client: redeem at acceptRental
import { acceptRental } from '@sly-rentals/core';

const ix = await acceptRental({
  borrower: wallet,
  contract: contractAddress,
  duration: { days: 7 },
  discountAuth: response.discountAuth,
  referrer: affiliateMemberAddress,
});
```

## Network addresses

```ts
import { getAddresses } from '@sly-rentals/core';

const addrs = getAddresses();   // current network
addrs.atlasMint;                // ATLAS token mint
addrs.sage;                     // SAGE program
addrs.srsly;                    // SRSLY program
```

## API reference

Full TypeDoc reference for every exported function: <https://wuwei-labs.github.io/srsly/api/core/>.

## Repository

Source, on-chain program, integration tests, and the playground UI live in the monorepo: <https://github.com/wuwei-labs/srsly>.

## License

[MIT](./LICENSE). The on-chain program (`srsly`) is AGPL-3.0-only; this SDK talks to it over RPC and is not a derivative work. See the workspace [`LICENSING.md`](https://github.com/wuwei-labs/srsly/blob/main/LICENSING.md) for the rationale.
