# Enable Trading

> Build, sign, and submit a one-time approval transaction for a user's Safe wallet.

## Usage

```typescript
import { BuilderClient, UserClient } from '@opinion-labs/opinion-clob-sdk';

const builder = new BuilderClient({
  host: 'https://openapi.opinion.trade/openapi',
  builderApiKey: 'YOUR_BUILDER_KEY',
  chainId: 56,
  rpcUrl: 'https://bsc-dataseed.binance.org',
});

// Step 1: Build the Enable Trading transaction
const txResult = await builder.buildEnableTradingTx(safeAddress);

console.log(txResult.safeTx);       // Safe transaction parameters
console.log(txResult.eip712Data);   // EIP-712 typed data for signing
console.log(txResult.safeTxHash);   // Safe transaction hash

// Step 2: User signs the EIP-712 typed data
const user = new UserClient('0xUserPrivateKey...');
const signature = await user.signTypedData(txResult.eip712Data);

// Step 3: Submit to backend for relay
const result = await builder.submitSafeTx(userAddress, txResult, signature);
```

## Parameters

### buildEnableTradingTx

| Parameter     | Type                    | Required | Description                                                        |
| ------------- | ----------------------- | -------- | ------------------------------------------------------------------ |
| `safeAddress` | string                  | Yes      | User's Gnosis Safe wallet address                                  |
| `quoteTokens` | Record\<string, string> | No       | Map of token address to exchange address. Auto-fetched if omitted. |

### submitSafeTx

| Parameter       | Type         | Required | Description                            |
| --------------- | ------------ | -------- | -------------------------------------- |
| `walletAddress` | string       | Yes      | User's wallet address (Safe owner EOA) |
| `safeTxResult`  | SafeTxResult | Yes      | Result from `buildEnableTradingTx()`   |
| `signature`     | string       | Yes      | User's signature on the EIP-712 data   |

## Response

`buildEnableTradingTx()` returns a `SafeTxResult`:

```typescript
interface SafeTxResult {
  safeTx: SafeTxParams;          // Raw Safe transaction parameters
  eip712Data: {                  // EIP-712 data for wallet signing
    types: Record<string, Array<{ name: string; type: string }>>;
    primaryType: string;         // 'SafeTx'
    domain: Record<string, unknown>;
    message: Record<string, unknown>;
  };
  safeTxHash: string;            // Hash of the Safe transaction
  submissionDataTemplate: Record<string, unknown>;
}
```

## Notes

* Enable Trading only needs to be called **once** per user. Check `userInfo.enableTrading` before calling.
* This operation requires `rpcUrl` in the BuilderClient config (used to read the Safe nonce).
* The transaction is relayed by the backend -- no gas is required from the user.
* Safe TX signing uses `user.signTypedData(eip712Data)`. This is different from order signing which uses `user.signHash(structHash)`.
* On-chain confirmation typically takes 30-60 seconds after submission.
