# TypeScript SDK

Trilobyte provides auto-generated TypeScript client bindings for all four contracts. These enable frontend applications, backend services, and keeper bots to interact with the protocol.

## Packages

| Package                      | Path                           | Description                                                |
| ---------------------------- | ------------------------------ | ---------------------------------------------------------- |
| `@trilobyte/globals-client`  | `contracts/globals/bindings/`  | Settings, roles, collateral, fees, asset whitelist         |
| `@trilobyte/vault-client`    | `contracts/vault/bindings/`    | Full vault lifecycle, payments, yield, debt tokens, SEP-41 |
| `@trilobyte/factory-client`  | `contracts/factory/bindings/`  | Vault creation, registry queries                           |
| `@trilobyte/timelock-client` | `contracts/timelock/bindings/` | Schedule, execute, cancel operations, AccessControl        |

## Generation

Bindings are generated using the Stellar CLI:

```bash
# Generate all bindings (build WASM + generate + compile)
./scripts/generate-bindings.sh
```

This runs `stellar contract bindings typescript` for each contract, generating fully typed TypeScript clients from the contract ABIs.

## Usage

### Installation

Add bindings as local path dependencies in your project's `package.json`:

```json
{
  "dependencies": {
    "@trilobyte/globals-client": "file:../trilobyte-new/contracts/globals/bindings",
    "@trilobyte/vault-client": "file:../trilobyte-new/contracts/vault/bindings",
    "@trilobyte/factory-client": "file:../trilobyte-new/contracts/factory/bindings",
    "@trilobyte/timelock-client": "file:../trilobyte-new/contracts/timelock/bindings"
  }
}
```

### Example: Query Vault Config

```typescript
import { Client as VaultClient } from '@trilobyte/vault-client';

const vault = new VaultClient({
  contractId: 'CABC...',
  networkPassphrase: 'Test SDF Network ; September 2015',
  rpcUrl: 'https://soroban-testnet.stellar.org',
});

const config = await vault.get_config();
console.log('Phase:', config.result.phase);
console.log('Outstanding:', config.result.outstanding);
```

### Example: Deposit into a Vault

```typescript
import { Client as VaultClient } from '@trilobyte/vault-client';

const vault = new VaultClient({
  contractId: 'CABC...',
  networkPassphrase: 'Test SDF Network ; September 2015',
  rpcUrl: 'https://soroban-testnet.stellar.org',
});

const tx = await vault.deposit({
  investor: 'GABC...',
  amount: BigInt(100_000_0000000), // 100,000 with 7 decimals
});

await tx.signAndSend();
```

### Example: Create a Vault

```typescript
import { Client as FactoryClient } from '@trilobyte/factory-client';

const factory = new FactoryClient({
  contractId: 'CDEF...',
  networkPassphrase: 'Test SDF Network ; September 2015',
  rpcUrl: 'https://soroban-testnet.stellar.org',
});

const tx = await factory.create_vault({
  params: {
    manager: 'GABC...',
    borrower: 'GDEF...',
    lent_token: 'CGHI...',
    principal: BigInt(500_000_0000000),
    interest_rate: 1000, // 10%
    loan_term: 12,
    split_ratio: 80,
    permissioned: false,
    grace_period: BigInt(2592000),
    funding_deadline: BigInt(0),
    approval_deadline: BigInt(0),
  },
  salt: Buffer.from('unique-salt-value'),
});

await tx.signAndSend();
```

## Keeper / Cron Integration

The TypeScript bindings are designed to be consumed by the `trilobyte-vault` NestJS backend. The keeper cron module uses them to:

1. **`apply_late_fee()`** — When a payment is overdue
2. **`check_default()`** — When past the grace period
3. **`check_funding_expiry()`** — For funding deadline enforcement
4. **`check_approval_expiry()`** — For approval deadline enforcement

All these functions are public entry points on the Vault contract, callable by any address.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.trilobyte.finance/technical-documentation/typescript-sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
