# MovePosition Lend

## Overview

The `moveposition_simple::strategy` module implements a lending strategy for the MovePosition protocol. It manages lending positions using packets (serialized tickets) for position validation and state management.

## Terminology

### Packet

A signed binary packet containing a serialized ticket that authorizes the deposit. The packet is verified using Hocket and must contain valid ticket data

## Strategy Module

### Structs

### MovePositionStrategy

Core strategy struct storing vault and authorization references.

```rust
struct MovePositionStrategy has key {
    vault: Object<Vault>, // used to deposit/withdraw funds to/from 
    auth_ref: AuthRef     // used to retrieve strategy signer for depositing assets
}
```

### Witness

Internal witness type for strategy creation.

```rust
struct Witness has drop {}
```

## Events

* **StrategyCreated**: Event emitted when a new strategy is created.

## Error Codes

* `EINVALID_AMOUNT`: Invalid amount specified
* `EINSUFFICIENT_BALANCE`: Insufficient balance to perform the operation
* `EVAULT_MISMATCH`: Invalid vault specified
* `EWITHDRAWAL_ACCOUNT_MISMATCH`: Invalid withdrawal account specified
* `ECANNOT_EXCEED_DEBT`: Cannot exceed the strategy debt

## Public Functions

* `create<CoinType>(account: &signer, vault: Object<Vault>, debt_limit: u64)`: Creates a new instance of the strategy for the specified vault. Only callable by governance account.
* `deposit_coin<CoinType>(account: &signer, strategy: Object<BaseStrategy>, packet: vector<u8>, amount: u64)`: Deposits the strategy's base asset (Coin) directly into the strategy, using the internal deposit\_internal function. Requires a packet.
* `vault_deposit_coin<CoinType>(account: &signer, strategy: Object<BaseStrategy>, packet: vector<u8>, amount: u64)`: Deposits the strategy's base asset (Coin) from the vault into the strategy, using the internal deposit\_internal function. Requires a packet.
* `withdraw_coin<CoinType>(account: &signer, strategy: Object<BaseStrategy>, packet: vector<u8>, amount: u64, max_loss: Option<u64>)`: Withdraws the strategy's base asset (Coin) directly from the strategy. Uses internal functions prepare\_withdrawal\_internal to withdraw strategy shares from signer's primary fungible stores and process\_withdrawal\_internal to redeem the strategy shares for the strategy's base asset (Coin). Requires a packet.
* `vault_withdraw_coin<CoinType>(account: &signer, request: &mut WithdrawalRequest, strategy: Object<BaseStrategy>, packet: vector<u8>, amount: u64, max_loss: Option<u64>): Coin<CoinType>`: Withdraws the strategy's base asset (Coin) from a vault's strategy. Must be called with the vault's signer (typically via router). Uses vault's withdraw\_strategy\_shares function in the `satay::vault` module to withdraw strategy shares from vault's primary fungible stores. Then uses process\_withdrawal\_internal to redeem the strategy shares for the strategy's base asset (Coin). Requires a packet.
* `tend_coin<CoinType>(account: &signer, strategy: Object<BaseStrategy>, packet: vector<u8>)`: Deposits idle coins into the MovePosition market.
* `vault_report<CoinType>(account: &signer, strategy: Object<BaseStrategy>)`: Reports strategy performance to the vault by calling the report function in the `satay::vault` module.

## Internal Functions

* `borrow_strategy(strategy: Object<BaseStrategy>): &MovePositionStrategy`: Returns a reference to the strategy data.
* `deposit_internal<CoinType>(strategy: &MovePositionStrategy, packet: vector<u8>, coin: Coin<CoinType>): FungibleAsset`: Internal function for Coin deposits.
* `market_withdraw_internal<CoinType>(strategy_ref: &MovePositionStrategy, packet: vector<u8>, amount: u64): Coin<CoinType>`: Internal function for withdrawing Coin from market. Validates a packet using the internal validate\_ticket function, then calls the redeem function in `moveposition_block::moveposition_block` to redeem the shares for Coin.
* `market_deposit_internal<CoinType>(strategy: &MovePositionStrategy, packet: vector<u8>, coin: Coin<CoinType>)`: Internal function for depositing coins to market. Validates a packet using the internal validate\_ticket function. First deposits the Coin to the strategy signer, , then calls the lend function in `moveposition_block::moveposition_block` to deposit the Coin into the market.
* `validate_ticket<CoinType>(packet: vector<u8>, amount: u64, operation: String)`: Helper function to validate a ticket for market operations. Takes in a packet which is validated against the CoinType, amount and operation.
* `process_withdrawal_internal<CoinType>(strategy: Object<BaseStrategy>, packet: vector<u8>, shares_asset: FungibleAsset, max_loss: Option<u64>): Coin<CoinType>`: Internal function for processing withdrawals. Creates a withdrawal request with the provided shares and attempts base strategy withdrawal using the withdraw function in `satay::base_strategy`. If funds remain to be withdrawn, calculates needed amount and available collateral, then withdraws from market using market\_withdraw\_internal. Finally completes the withdrawal with specified loss tolerance using the complete\_withdrawal\_coin function in `satay::base_strategy` and returns the withdrawn Coin.
* `prepare_withdrawal_internal(account: &signer, strategy: Object<BaseStrategy>, amount: u64): FungibleAsset`: Internal function for preparing withdrawals. Withdraws and returns strategy shares from the signers account.
* `get_collateral_for_shares<CoinType>(strategy_ref: &MovePositionStrategy, amount: u64): u64`: Calculates collateral value for given amount of shares. Retrieves the collateral balance and total shares to calculate the collateral value for a given amount using the function below:

$$
\text{collateral\_value} = \frac{\text{amount} \times \text{collateral\_balance}}{\text{total\_shares}}
$$


---

# 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.canopyhub.xyz/canopys-four-layers/strategy-layer/simple-lending-strategy/moveposition-lend.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.
