# SDK

## API Reference

Complete API reference for the Risk Protocol SDK.

### TokenFactory

Core token operations and management.

#### Base Methods

| Method                        | Parameters      | Returns           | Description                      |
| ----------------------------- | --------------- | ----------------- | -------------------------------- |
| `getBaseToken()`              | -               | `Promise<string>` | Get base token address           |
| `decimals()`                  | -               | `Promise<number>` | Get token decimals               |
| `getSmartTokenAddress(index)` | `index: number` | `Promise<string>` | Get smart token address by index |

#### Management Fees

Access via `sdk.tokenFactory.managementFees`

| Method                                    | Parameters                                                         | Returns                        | Description                                 |
| ----------------------------------------- | ------------------------------------------------------------------ | ------------------------------ | ------------------------------------------- |
| `getRate()`                               | -                                                                  | `Promise<bigint>`              | Get current fee rate in wei (1 bps = 10^14) |
| `isFeeActive()`                           | -                                                                  | `Promise<boolean>`             | Check if fees are currently active          |
| `calculateManagementFee(amount, mgmtFee)` | <p><code>amount: string</code><br><code>mgmtFee: number</code></p> | `Promise<bigint>`              | Calculate fee for given amount              |
| `getTreasuryAddress()`                    | -                                                                  | `Promise<string>`              | Get treasury wallet address                 |
| `calculateRollOverValue()`                | -                                                                  | `Promise<bigint>`              | Calculate rollover value                    |
| `updateUserLastRebalanceCount(owner)`     | `owner: string`                                                    | `Promise<TransactionResponse>` | Update user's last rebalance count          |

**Admin Methods**

| Method                         | Parameters       | Returns                        | Description                 |
| ------------------------------ | ---------------- | ------------------------------ | --------------------------- |
| `setManagementFeeRate(rate)`   | `rate: string`   | `Promise<TransactionResponse>` | Set new fee rate            |
| `setManagementFeeState(state)` | `state: boolean` | `Promise<TransactionResponse>` | Enable/disable fees         |
| `setTreasuryWallet(wallet)`    | `wallet: string` | `Promise<TransactionResponse>` | Set treasury wallet address |

#### Rebalance

Access via `sdk.tokenFactory.rebalance`

| Method                                    | Parameters                                                                | Returns                        | Description                       |
| ----------------------------------------- | ------------------------------------------------------------------------- | ------------------------------ | --------------------------------- |
| `getRebalanceInterval()`                  | -                                                                         | `Promise<number>`              | Get rebalance interval in seconds |
| `getLastRebalanceTimestamp()`             | -                                                                         | `Promise<number>`              | Get timestamp of last rebalance   |
| `getScheduledRebalances()`                | -                                                                         | `Promise<any[]>`               | Get all scheduled rebalances      |
| `getNextSequenceNumber()`                 | -                                                                         | `Promise<number>`              | Get next sequence number          |
| `getLastTimeStamp()`                      | -                                                                         | `Promise<number>`              | Get last rebalance timestamp      |
| `getRebalanceNumber()`                    | -                                                                         | `Promise<number>`              | Get total rebalance count         |
| `getUserLastRebalanceCount(address)`      | `address: string`                                                         | `Promise<number>`              | Get user's last rebalance count   |
| `getInterval()`                           | -                                                                         | `Promise<number>`              | Get rebalance interval            |
| `applyRebalance(owner)`                   | `owner: string`                                                           | `Promise<TransactionResponse>` | Apply rebalance for user          |
| `verifyAndDecode(signature, encodedData)` | <p><code>signature: string</code><br><code>encodedData: string</code></p> | `Promise<any>`                 | Verify signature and decode data  |

**Admin Methods**

| Method                       | Parameters        | Returns                        | Description                   |
| ---------------------------- | ----------------- | ------------------------------ | ----------------------------- |
| `setSignersAddress(address)` | `address: string` | `Promise<TransactionResponse>` | Set authorized signer address |
| `getSignersAddress()`        | -                 | `Promise<string>`              | Get authorized signer address |

***

### SmartToken

#### Read Methods

| Method                                    | Parameters                                            | Returns           | Description                  |                             |
| ----------------------------------------- | ----------------------------------------------------- | ----------------- | ---------------------------- | --------------------------- |
| `balanceOf(address, contract?)`           | <p><code>address: string</code><br><code>contract?: 0 | 1</code></p>      | `Promise<bigint>`            | Get token balance           |
| `unScaledbalanceOf(address, contract?)`   | <p><code>address: string</code><br><code>contract?: 0 | 1</code></p>      | `Promise<bigint>`            | Get unscaled balance        |
| `hasPendingRebalance(address, contract?)` | <p><code>address: string</code><br><code>contract?: 0 | 1</code></p>      | `Promise<boolean>`           | Check for pending rebalance |
| `getTokenFactory(contract?)`              | `contract?: 0\|1`                                     | `Promise<string>` | Get TokenFactory address     |                             |
| `asset(contract?)`                        | `contract?: 0\|1`                                     | `Promise<string>` | Get underlying asset address |                             |
| `totalAssets(contract?)`                  | `contract?: 0\|1`                                     | `Promise<bigint>` | Get total assets in vault    |                             |
| `convertToShares(amount, contract?)`      | <p><code>amount: string</code><br><code>contract?: 0  | 1</code></p>      | `Promise<bigint>`            | Convert assets to shares    |
| `convertToAssets(amount, contract?)`      | <p><code>amount: string</code><br><code>contract?: 0  | 1</code></p>      | `Promise<bigint>`            | Convert shares to assets    |

#### Write Methods

| Method                                                 | Parameters                                                                             | Returns      | Description                    |                          |
| ------------------------------------------------------ | -------------------------------------------------------------------------------------- | ------------ | ------------------------------ | ------------------------ |
| `handlePendingRebalance(sender, recipient, contract?)` | <p><code>sender: string</code><br><code>recipient: string</code><br><code>contract?: 0 | 1</code></p> | `Promise<TransactionResponse>` | Handle pending rebalance |

#### Transactions

Access via `sdk.smartToken.transaction`

**Transfers**

| Method                                               | Parameters                                                                                                            | Returns      | Description                    |                                      |
| ---------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- | ------------ | ------------------------------ | ------------------------------------ |
| `transfer(recipient, amount, contract?)`             | <p><code>recipient: string</code><br><code>amount: string</code><br><code>contract?: 0                                | 1</code></p> | `Promise<TransactionResponse>` | Transfer tokens                      |
| `transferFrom(sender, recipient, amount, contract?)` | <p><code>sender: string</code><br><code>recipient: string</code><br><code>amount: string</code><br><code>contract?: 0 | 1</code></p> | `Promise<TransactionResponse>` | Transfer tokens from another address |

**Deposits**

| Method                                                               | Parameters                                                                                                                                                                                            | Returns      | Description                    |                                     |
| -------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ | ------------------------------ | ----------------------------------- |
| `maxDeposit(account, contract?)`                                     | <p><code>account: string</code><br><code>contract?: 0                                                                                                                                                 | 1</code></p> | `Promise<bigint>`              | Get max deposit allowed             |
| `previewDeposit(amount, contract?)`                                  | <p><code>amount: string</code><br><code>contract?: 0                                                                                                                                                  | 1</code></p> | `Promise<bigint>`              | Preview shares received for deposit |
| `deposit(amount, recipient, contract?)`                              | <p><code>amount: string</code><br><code>recipient: string</code><br><code>contract?: 0                                                                                                                | 1</code></p> | `Promise<TransactionResponse>` | Deposit assets for shares           |
| `depositWithNative(amount, recipient, contract?)`                    | <p><code>amount: string</code><br><code>recipient: string</code><br><code>contract?: 0                                                                                                                | 1</code></p> | `Promise<TransactionResponse>` | Deposit native token (ETH)          |
| `depositWithExpiry(amount, recipient, expiryDate, contract?)`        | <p><code>amount: string</code><br><code>recipient: string</code><br><code>expiryDate: string</code><br><code>contract?: 0                                                                             | 1</code></p> | `Promise<TransactionResponse>` | Deposit with expiry timestamp       |
| `depositWithPermit(amount, recipient, deadline, v, r, s, contract?)` | <p><code>amount: string</code><br><code>recipient: string</code><br><code>deadline: number</code><br><code>v: number</code><br><code>r: string</code><br><code>s: string</code><br><code>contract?: 0 | 1</code></p> | `Promise<TransactionResponse>` | Deposit with EIP-2612 permit        |

**Minting**

| Method                               | Parameters                                                                             | Returns      | Description                    |                                |
| ------------------------------------ | -------------------------------------------------------------------------------------- | ------------ | ------------------------------ | ------------------------------ |
| `maxMint(account, contract?)`        | <p><code>account: string</code><br><code>contract?: 0                                  | 1</code></p> | `Promise<bigint>`              | Get max mint allowed           |
| `previewMint(amount, contract?)`     | <p><code>amount: string</code><br><code>contract?: 0                                   | 1</code></p> | `Promise<bigint>`              | Preview assets needed for mint |
| `mint(amount, recipient, contract?)` | <p><code>amount: string</code><br><code>recipient: string</code><br><code>contract?: 0 | 1</code></p> | `Promise<TransactionResponse>` | Mint exact shares              |

**Withdrawals**

| Method                                                                | Parameters                                                                                                                                              | Returns      | Description                    |                                      |
| --------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ | ------------------------------ | ------------------------------------ |
| `maxWithdraw(account, contract?)`                                     | <p><code>account: string</code><br><code>contract?: 0                                                                                                   | 1</code></p> | `Promise<bigint>`              | Get max withdrawal allowed           |
| `previewWithdraw(amount, contract?)`                                  | <p><code>amount: string</code><br><code>contract?: 0                                                                                                    | 1</code></p> | `Promise<bigint>`              | Preview shares burned for withdrawal |
| `withdraw(amount, recipient, owner, contract?)`                       | <p><code>amount: string</code><br><code>recipient: string</code><br><code>owner: string</code><br><code>contract?: 0                                    | 1</code></p> | `Promise<TransactionResponse>` | Withdraw exact assets                |
| `withdrawWithExpiry(amount, recipient, owner, expiryDate, contract?)` | <p><code>amount: string</code><br><code>recipient: string</code><br><code>owner: string</code><br><code>expiryDate: string</code><br><code>contract?: 0 | 1</code></p> | `Promise<TransactionResponse>` | Withdraw with expiry timestamp       |

**Redemptions**

| Method                                        | Parameters                                                                                                           | Returns      | Description                    |                                        |
| --------------------------------------------- | -------------------------------------------------------------------------------------------------------------------- | ------------ | ------------------------------ | -------------------------------------- |
| `maxRedeem(account, contract?)`               | <p><code>account: string</code><br><code>contract?: 0                                                                | 1</code></p> | `Promise<bigint>`              | Get max redemption allowed             |
| `previewRedeem(amount, contract?)`            | <p><code>amount: string</code><br><code>contract?: 0                                                                 | 1</code></p> | `Promise<bigint>`              | Preview assets received for redemption |
| `redeem(amount, recipient, owner, contract?)` | <p><code>amount: string</code><br><code>recipient: string</code><br><code>owner: string</code><br><code>contract?: 0 | 1</code></p> | `Promise<TransactionResponse>` | Redeem exact shares for assets         |

***

### Orchestrator

Scheduled operations and Balancer pool management.

| Method                         | Parameters | Returns                        | Description                      |
| ------------------------------ | ---------- | ------------------------------ | -------------------------------- |
| `executeScheduledRebalances()` | -          | `Promise<TransactionResponse>` | Execute all scheduled rebalances |
| `getTokenFactory()`            | -          | `Promise<string>`              | Get TokenFactory address         |
| `operationsSize()`             | -          | `Promise<number>`              | Get number of operations         |
| `getBalancerPools()`           | -          | `Promise<string[]>`            | Get all Balancer pool addresses  |

**Admin Methods**

| Method                                             | Parameters                                                                                             | Returns                        | Description               |
| -------------------------------------------------- | ------------------------------------------------------------------------------------------------------ | ------------------------------ | ------------------------- |
| `addOperation(index, destination, data)`           | <p><code>index: number</code><br><code>destination: string</code><br><code>data: string</code></p>     | `Promise<TransactionResponse>` | Add new operation         |
| `removeOperation(index)`                           | `index: number`                                                                                        | `Promise<TransactionResponse>` | Remove operation by index |
| `setOperationEnabled(index, destination, enabled)` | <p><code>index: number</code><br><code>destination: string</code><br><code>enabled: boolean</code></p> | `Promise<TransactionResponse>` | Enable/disable operation  |
| `addBalancerPool(index, pool)`                     | <p><code>index: number</code><br><code>pool: string</code></p>                                         | `Promise<TransactionResponse>` | Add Balancer pool         |
| `removeBalancerPool(index)`                        | `index: number`                                                                                        | `Promise<TransactionResponse>` | Remove Balancer pool      |

***

### BalancerHelper

Balancer pool calculations and utilities.

| Method                                                                                                                                           | Parameters                                                                                                                                                                                                                                                                                                                                       | Returns                                                                                                               | Description                                                                                 |
| ------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------- |
| `getExpectedAmountOut(bpoolCoreAddress, tokenInAddress, tokenOutAddress, amountIn)`                                                              | <p><code>bpoolCoreAddress: string</code><br><code>tokenInAddress: string</code><br><code>tokenOutAddress: string</code><br><code>amountIn: bigint</code></p>                                                                                                                                                                                     | `Promise<bigint>`                                                                                                     | Calculate expected output for swap                                                          |
| `calculatePoolAmountOut(bpoolEsPAddress, tokens, outAmount)`                                                                                     | <p><code>bpoolEsPAddress: string</code><br><code>tokens: string\[]</code><br><code>outAmount: bigint</code></p>                                                                                                                                                                                                                                  | `Promise<{amountsIn: bigint[], approveAmt: bigint[]}>`                                                                | Calculate amounts needed for liquidity provision                                            |
| `getDollarAmounts(underlying, fees, inputToken, outputToken, inputAmount, tokenInAddress, tokenOutAddress, underlyingAddress, bpoolCoreAddress)` | <p><code>underlying: number</code><br><code>fees: number</code><br><code>inputToken: string</code><br><code>outputToken: string</code><br><code>inputAmount: number</code><br><code>tokenInAddress: string</code><br><code>tokenOutAddress: string</code><br><code>underlyingAddress: string</code><br><code>bpoolCoreAddress: string</code></p> | `Promise<{ronDollarValue: Decimal, roffDollarValue: Decimal, underlying: Decimal, LPfee: Decimal, swapFee: Decimal}>` | Calculate dollar values and fees for token swaps. Valid tokens: "ron", "roff", "underlying" |

***

### PriceEstimates

Price discovery, NTV calculations, and slippage management.

| Method                                                 | Parameters                                                                                            | Returns                                                  | Description                           |
| ------------------------------------------------------ | ----------------------------------------------------------------------------------------------------- | -------------------------------------------------------- | ------------------------------------- |
| `getGasInfo()`                                         | -                                                                                                     | `Promise<any>`                                           | Get current gas price information     |
| `getBalancerPrices(chainId)`                           | `chainId: number`                                                                                     | `Promise<any>`                                           | Get all Balancer token prices         |
| `getBalancerPrice(tokenAddress, chainId)`              | <p><code>tokenAddress: string</code><br><code>chainId: number</code></p>                              | `Promise<number>`                                        | Get price for specific token          |
| `getNTVPrice(tokenIn, tokenOut, discount)`             | <p><code>tokenIn: string</code><br><code>tokenOut: string</code><br><code>discount: number</code></p> | `Promise<{outRatio: number}>`                            | Get NTV price with discount           |
| `getMaxSlippage(classicSlippage, discountNTVSlippage)` | <p><code>classicSlippage: number</code><br><code>discountNTVSlippage: number</code></p>               | `number`                                                 | Calculate maximum slippage            |
| `getSplitOutputAmounts(smartTokenX, input)`            | <p><code>smartTokenX: string</code><br><code>input: string</code></p>                                 | `Promise<{outputAmount: bigint, mgmtFeeAmount: bigint}>` | Calculate output for split operation  |
| `getRedeemOutputAmounts(smartTokenX, input)`           | <p><code>smartTokenX: string</code><br><code>input: string</code></p>                                 | `Promise<{outputAmount: bigint, mgmtFeeRefund: bigint}>` | Calculate output for redeem operation |
| `getSignedFeeData()`                                   | -                                                                                                     | `Promise<any>`                                           | Get signed fee data                   |

***
