> ## Documentation Index
> Fetch the complete documentation index at: https://docs.compasslabs.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Morpho: deposit into a vault

> Learn how to use the Compass Python or Typescript SDK to set USDC allowance and deposit into a Morpho vault on Base, complete with code snippets and troubleshooting.

export const GithubCodeBlock = ({typescript, python}) => {
  const [typescriptCode, setTypescriptCode] = useState("");
  const [pythonCode, setPythonCode] = useState("");
  function removeWhitespace(strings, x) {
    return strings.map(str => {
      let count = 0;
      let i = 0;
      while (i < str.length && count < x && str[i] === " ") {
        count++;
        i++;
      }
      return str.slice(i);
    });
  }
  function cropToSnippet({text, from, to}) {
    const lines = text.split("\n");
    let result = [];
    let isCollecting = false;
    for (const line of lines) {
      if (line.trim() === from) {
        isCollecting = true;
        continue;
      }
      if (line.trim() === to) {
        break;
      }
      if (isCollecting) {
        result.push(line);
      }
    }
    const whitespaceToDelete = result[0].length - result[0].trimStart().length;
    result = removeWhitespace(result, whitespaceToDelete);
    return result.join("\n");
  }
  function removeSnippetComments(code) {
    return code.replace(/(\/\/|#) SNIPPET (START|END) \d+(\n|$)/g, "");
  }
  useEffect(() => {
    if (!typescript) return;
    fetch(typescript.url).then(response => response.text()).then(text => {
      let code = text;
      if (typescript.snippetNumber) {
        code = cropToSnippet({
          text,
          from: `// SNIPPET START ${typescript.snippetNumber}`,
          to: `// SNIPPET END ${typescript.snippetNumber}`
        });
      } else {
        code = removeSnippetComments(code);
      }
      setTypescriptCode(code);
    }).catch(error => {
      console.error("Error loading file:", error);
    });
  }, [typescript]);
  useEffect(() => {
    if (!python) return;
    fetch(python.url).then(response => response.text()).then(text => {
      let code = text;
      if (python.snippetNumber) {
        code = cropToSnippet({
          text,
          from: `# SNIPPET START ${python.snippetNumber}`,
          to: `# SNIPPET END ${python.snippetNumber}`
        });
      } else {
        code = removeSnippetComments(code);
      }
      setPythonCode(code);
    }).catch(error => {
      console.error("Error loading file:", error);
    });
  }, [python]);
  if (!python && !typescript) return null;
  if (!python) {
    return <CodeBlock language="typescript" filename={typescript.name} expandable={typescript?.numOfLinesExpandable ? "true" : null} lines="true" icon="https://upload.wikimedia.org/wikipedia/commons/f/f5/Typescript.svg" code={typescriptCode} children={typescriptCode || !typescript?.numOfLinesExpandable || <div style={{
      whiteSpace: "pre-wrap"
    }}>
        {Array(typescript?.numOfLinesExpandable).fill("-").map(x => x).join("\n")}
      </div>} />;
  }
  if (!typescript) {
    return <CodeBlock language="python" filename={python.name} expandable={python?.numOfLinesExpandable ? "true" : null} lines="true" icon="https://upload.wikimedia.org/wikipedia/commons/c/c3/Python-logo-notext.svg" code={pythonCode} children={pythonCode || !python?.numOfLinesExpandable || <div style={{
      whiteSpace: "pre-wrap"
    }}>
        {Array(python?.numOfLinesExpandable).fill("-").map(x => x).join("\n")}
      </div>} />;
  }
  return <CodeGroup>
  <CodeBlock language="typescript" filename={typescript.name} expandable={typescript?.numOfLinesExpandable ? "true" : null} lines="true" icon="https://upload.wikimedia.org/wikipedia/commons/f/f5/Typescript.svg" code={typescriptCode} children={typescriptCode || !typescript?.numOfLinesExpandable || <div style={{
    whiteSpace: "pre-wrap"
  }}>
          {Array(typescript?.numOfLinesExpandable).fill("-").map(x => x).join("\n")}
        </div>} />
  <CodeBlock language="python" filename={python.name} expandable={python?.numOfLinesExpandable ? "true" : null} lines="true" icon="https://upload.wikimedia.org/wikipedia/commons/c/c3/Python-logo-notext.svg" code={pythonCode} children={pythonCode || !python?.numOfLinesExpandable || <div style={{
    whiteSpace: "pre-wrap"
  }}>
          {Array(python?.numOfLinesExpandable).fill("-").map(x => x).join("\n")}
        </div>} />
</CodeGroup>;
};

In this example, we set the USDC spending allowance for a Morpho vault, then perform a deposit.

> **Notes**
>
> * Amounts are in the token’s **smallest units** (USDC has 6 decimals). `1` = `0.000001` USDC.
> * Use the correct **vault address** for your asset.
> * You need a bit of ETH on **Base** for gas and some USDC for the deposit.
> * Full source code is available in our public GitHub repository: [CompassLabs/api\_usecases – deposit\_on\_morpho](https://github.com/CompassLabs/api_usecases/tree/main/v1/basic_examples/deposit_on_morpho/).

## Prerequisites

<Steps>
  <Step title="Install Dependencies">
    Install the required packages.

    <CodeGroup>
      ```shellscript Typescript theme={"system"}
      npm install @compass-labs/api-sdk viem dotenv
      ```

      ```shellscript Python theme={"system"}
      pip install compass_api_sdk web3 python-dotenv
      ```
    </CodeGroup>
  </Step>

  <Step title="Set Environment Variables">
    Create a `.env`:

    ```bash theme={"system"}
    # .env
    COMPASS_API_KEY="your_compass_api_key"
    PRIVATE_KEY="your_wallet_private_key"
    WALLET_ADDRESS="0xYourEOA"
    SPECIFIC_MORPHO_VAULT="0xVaultAddress"   # the vault that accepts USDC
    BASE_RPC_URL="https://base-mainnet.example"  # from your RPC provider
    ```
  </Step>
</Steps>

## Implementation

Set a USDC allowance and deposit into a Morpho vault.

<Steps>
  <Step title="Import Libraries & Environment Variables">
    <GithubCodeBlock
      typescript={{
name: "index.ts",
url: "https://raw.githubusercontent.com/CompassLabs/api_usecases/main/v1/basic_examples/deposit_on_morpho/typescript/src/index.ts",
snippetNumber: 21,
}}
      python={{
name: "main.py",
url: "https://raw.githubusercontent.com/CompassLabs/api_usecases/main/v1/basic_examples/deposit_on_morpho/python/main.py",
snippetNumber: 11,
}}
    />
  </Step>

  <Step title="Initialize SDK and Account">
    <GithubCodeBlock
      typescript={{
name: "index.ts",
url: "https://raw.githubusercontent.com/CompassLabs/api_usecases/main/v1/basic_examples/deposit_on_morpho/typescript/src/index.ts",
snippetNumber: 22,
}}
      python={{
name: "main.py",
url: "https://raw.githubusercontent.com/CompassLabs/api_usecases/main/v1/basic_examples/deposit_on_morpho/python/main.py",
snippetNumber: 12,
}}
    />
  </Step>

  <Step title="Build allowance transaction">
    Approve the vault to spend your USDC (smallest units).

    <GithubCodeBlock
      typescript={{
name: "index.ts",
url: "https://raw.githubusercontent.com/CompassLabs/api_usecases/main/v1/basic_examples/deposit_on_morpho/typescript/src/index.ts",
snippetNumber: 23,
}}
      python={{
name: "main.py",
url: "https://raw.githubusercontent.com/CompassLabs/api_usecases/main/v1/basic_examples/deposit_on_morpho/python/main.py",
snippetNumber: 13,
}}
    />
  </Step>

  <Step title="Send allowance">
    <GithubCodeBlock
      typescript={{
name: "index.ts",
url: "https://raw.githubusercontent.com/CompassLabs/api_usecases/main/v1/basic_examples/deposit_on_morpho/typescript/src/index.ts",
snippetNumber: 24,
}}
      python={{
name: "main.py",
url: "https://raw.githubusercontent.com/CompassLabs/api_usecases/main/v1/basic_examples/deposit_on_morpho/python/main.py",
snippetNumber: 14,
}}
    />
  </Step>

  <Step title="Build deposit transaction">
    <GithubCodeBlock
      typescript={{
name: "index.ts",
url: "https://raw.githubusercontent.com/CompassLabs/api_usecases/main/v1/basic_examples/deposit_on_morpho/typescript/src/index.ts",
snippetNumber: 25,
}}
      python={{
name: "main.py",
url: "https://raw.githubusercontent.com/CompassLabs/api_usecases/main/v1/basic_examples/deposit_on_morpho/python/main.py",
snippetNumber: 15,
}}
    />
  </Step>

  <Step title="Send deposit">
    <GithubCodeBlock
      typescript={{
name: "index.ts",
url: "https://raw.githubusercontent.com/CompassLabs/api_usecases/main/v1/basic_examples/deposit_on_morpho/typescript/src/index.ts",
snippetNumber: 26,
}}
      python={{
name: "main.py",
url: "https://raw.githubusercontent.com/CompassLabs/api_usecases/main/v1/basic_examples/deposit_on_morpho/python/main.py",
snippetNumber: 16,
}}
    />
  </Step>
</Steps>

## Full Code

<GithubCodeBlock
  typescript={{
name: "index.ts",
url: "https://raw.githubusercontent.com/CompassLabs/api_usecases/main/v1/basic_examples/deposit_on_morpho/typescript/src/index.ts"
}}
  python={{
name: "main.py",
url: "https://raw.githubusercontent.com/CompassLabs/api_usecases/main/v1/basic_examples/deposit_on_morpho/python/main.py"
}}
  numOfLinesExpandable={120}
/>

***

### Troubleshooting (quick)

* **Revert or underflow**: Check amount units and token decimals.
* **Allowance errors**: Ensure the vault address matches the USDC vault.
* **Insufficient funds**: Top up ETH on Base for gas.
* **Wrong network**: Confirm your RPC URL is **Base mainnet**.

<CardGroup cols={2}>
  <Card title="Compass API Docs" icon="compass" href="/">
    API reference and guides
  </Card>

  <Card title="GitHub Example" icon="github" href="https://github.com/CompassLabs/api_usecases/tree/main/v1/basic_examples/deposit_on_morpho/python_example">
    View the repository
  </Card>
</CardGroup>
