diff --git a/v2/create_earn_account/TESTING.md b/v2/create_earn_account/TESTING.md deleted file mode 100644 index 106046d3..00000000 --- a/v2/create_earn_account/TESTING.md +++ /dev/null @@ -1,63 +0,0 @@ -# Testing Guide - -Test both TypeScript and Python versions before pushing. - -## Prerequisites - -1. Create `.env` files in both directories with your actual values: - - `COMPASS_API_KEY`: Your Compass API key - - `WALLET_ADDRESS`: Your wallet address (0x...) - -## Test TypeScript - -```bash -cd typescript - -# Make sure .env file exists (copy from .env.example and fill in values) -cp .env.example .env -# Edit .env with your actual values - -# Run the script -npm run dev -``` - -**Expected output:** -- Earn Account Address: `0x...` -- Unsigned Transaction: `{ chainId, data, from, gas, ... }` - -## Test Python - -```bash -cd python - -# Install dependencies (if using uv or pip) -pip install compass-api-sdk python-dotenv -# OR if using uv: -# uv pip install compass-api-sdk python-dotenv - -# Make sure .env file exists -cp .env.example .env -# Edit .env with your actual values - -# Run the script -python main.py -``` - -**Expected output:** -- Earn Account Address: `0x...` -- Unsigned Transaction: `{ chainId, data, from, gas, ... }` - -## Verify Results - -Both scripts should: -1. ✅ Successfully connect to Compass API -2. ✅ Return an Earn Account address -3. ✅ Return an unsigned transaction object -4. ✅ No errors - -If you get errors, check: -- API key is valid -- Wallet address is correct format (0x...) -- Network connection -- SDK versions are up to date - diff --git a/v2/create_earn_account/python/.env.example b/v2/create_earn_account/python/.env.example index ee1b4cd7..fa1c0221 100644 --- a/v2/create_earn_account/python/.env.example +++ b/v2/create_earn_account/python/.env.example @@ -4,3 +4,7 @@ COMPASS_API_KEY=your_compass_api_key_here # Wallet Configuration # The wallet address that will own the Earn Account and pay for gas WALLET_ADDRESS=0xYourWalletAddress +PRIVATE_KEY=your_wallet_private_key_here + +# Base RPC Configuration +BASE_RPC_URL=https://base-mainnet.g.alchemy.com/v2/your-api-key diff --git a/v2/create_earn_account/python/README.md b/v2/create_earn_account/python/README.md index 108b31e0..7fd615ab 100644 --- a/v2/create_earn_account/python/README.md +++ b/v2/create_earn_account/python/README.md @@ -11,12 +11,12 @@ This example demonstrates how to create an Earn Account using the Compass API Py 1. Install dependencies (make sure you have the latest version): ```bash -pip install --upgrade compass-api-sdk python-dotenv +pip install --upgrade compass-api-sdk python-dotenv web3 eth-account ``` Or if using `uv`: ```bash -uv pip install compass-api-sdk python-dotenv +uv pip install compass-api-sdk python-dotenv web3 eth-account ``` **Note:** This example requires `compass-api-sdk` version 2.0.1 or later (which includes the `earn` endpoints). Make sure to upgrade if you have an older version. @@ -29,6 +29,8 @@ cp .env.example .env 3. Fill in your `.env` file with your actual values: - `COMPASS_API_KEY`: Your Compass API key - `WALLET_ADDRESS`: Your wallet address (will own the Earn Account) + - `PRIVATE_KEY`: Your wallet's private key (to sign the transaction) + - `BASE_RPC_URL`: Your Base mainnet RPC URL (to broadcast the transaction) ## Run @@ -38,12 +40,16 @@ python main.py ## What This Does -This example gets an unsigned transaction to create an Earn Account on Base. The transaction must be signed and broadcast separately. +This example: +1. Gets an unsigned transaction to create an Earn Account on Base +2. Signs the transaction with your private key +3. Broadcasts it to the Base network +4. Waits for confirmation ## Notes - **No Gas Sponsorship**: The `owner` (who controls the account) is also the `sender` (who pays for gas). Note that Earn Account creation can also be done WITH gas sponsorship (using the `/gas_sponsorship/prepare` endpoint), but this example does not use gas sponsorship. - The Earn Account address is deterministic and returned before the transaction is confirmed. -- This example only retrieves the unsigned transaction. You'll need to sign and broadcast it separately. +- Make sure your wallet has enough ETH on Base to cover gas fees. - **Important:** Requires `compass-api-sdk` version 2.0.1 or later (includes `earn` endpoints). Make sure to install the latest version. diff --git a/v2/create_earn_account/python/create_earn_account_python_example.egg-info/PKG-INFO b/v2/create_earn_account/python/create_earn_account_python_example.egg-info/PKG-INFO new file mode 100644 index 00000000..8564c07b --- /dev/null +++ b/v2/create_earn_account/python/create_earn_account_python_example.egg-info/PKG-INFO @@ -0,0 +1,8 @@ +Metadata-Version: 2.4 +Name: create_earn_account_python_example +Version: 1.0.0 +Summary: Example: Create an Earn Account using Compass API +Requires-Dist: compass-api-sdk +Requires-Dist: python-dotenv +Requires-Dist: web3 +Requires-Dist: eth-account diff --git a/v2/create_earn_account/python/create_earn_account_python_example.egg-info/SOURCES.txt b/v2/create_earn_account/python/create_earn_account_python_example.egg-info/SOURCES.txt new file mode 100644 index 00000000..9d22c48a --- /dev/null +++ b/v2/create_earn_account/python/create_earn_account_python_example.egg-info/SOURCES.txt @@ -0,0 +1,8 @@ +README.md +main.py +pyproject.toml +create_earn_account_python_example.egg-info/PKG-INFO +create_earn_account_python_example.egg-info/SOURCES.txt +create_earn_account_python_example.egg-info/dependency_links.txt +create_earn_account_python_example.egg-info/requires.txt +create_earn_account_python_example.egg-info/top_level.txt \ No newline at end of file diff --git a/v2/create_earn_account/python/create_earn_account_python_example.egg-info/dependency_links.txt b/v2/create_earn_account/python/create_earn_account_python_example.egg-info/dependency_links.txt new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/v2/create_earn_account/python/create_earn_account_python_example.egg-info/dependency_links.txt @@ -0,0 +1 @@ + diff --git a/v2/create_earn_account/python/create_earn_account_python_example.egg-info/requires.txt b/v2/create_earn_account/python/create_earn_account_python_example.egg-info/requires.txt new file mode 100644 index 00000000..dea15ae1 --- /dev/null +++ b/v2/create_earn_account/python/create_earn_account_python_example.egg-info/requires.txt @@ -0,0 +1,4 @@ +compass-api-sdk +python-dotenv +web3 +eth-account diff --git a/v2/create_earn_account/python/create_earn_account_python_example.egg-info/top_level.txt b/v2/create_earn_account/python/create_earn_account_python_example.egg-info/top_level.txt new file mode 100644 index 00000000..ba2906d0 --- /dev/null +++ b/v2/create_earn_account/python/create_earn_account_python_example.egg-info/top_level.txt @@ -0,0 +1 @@ +main diff --git a/v2/create_earn_account/python/main.py b/v2/create_earn_account/python/main.py index 068581f0..376f8736 100644 --- a/v2/create_earn_account/python/main.py +++ b/v2/create_earn_account/python/main.py @@ -1,34 +1,38 @@ # SNIPPET START 1 -# Import Libraries & Environment Variables from compass_api_sdk import CompassAPI, models import os from dotenv import load_dotenv +from web3 import Web3 load_dotenv() COMPASS_API_KEY = os.getenv("COMPASS_API_KEY") WALLET_ADDRESS = os.getenv("WALLET_ADDRESS") +PRIVATE_KEY = os.getenv("PRIVATE_KEY") +BASE_RPC_URL = os.getenv("BASE_RPC_URL") # SNIPPET END 1 # SNIPPET START 2 -# Initialize Compass SDK -compass = CompassAPI(api_key_auth=COMPASS_API_KEY) +with CompassAPI(api_key_auth=COMPASS_API_KEY) as compass_api: # SNIPPET END 2 # SNIPPET START 3 -# Create Earn Account (No Gas Sponsorship) -# Get unsigned transaction to create an Earn Account on Base -# owner: The address that will own and control the Earn Account -# sender: The address that will sign and pay for gas (same as owner = no gas sponsorship) -with CompassAPI(api_key_auth=COMPASS_API_KEY) as compass_api: + # Get unsigned transaction to create Earn Account create_account_response = compass_api.earn.earn_create_account( chain=models.CreateAccountRequestChain.BASE, sender=WALLET_ADDRESS, owner=WALLET_ADDRESS, estimate_gas=True, ) +# SNIPPET END 3 +# SNIPPET START 4 + # Sign and broadcast transaction + w3 = Web3(Web3.HTTPProvider(BASE_RPC_URL)) + tx_dict = create_account_response.model_dump(by_alias=True) + signed_tx = w3.eth.account.sign_transaction(tx_dict["transaction"], PRIVATE_KEY) + tx_hash = w3.eth.send_raw_transaction(signed_tx.raw_transaction) + w3.eth.wait_for_transaction_receipt(tx_hash) print("Earn Account Address:", create_account_response.earn_account_address) - print("Unsigned Transaction:", create_account_response.transaction) -# SNIPPET END 3 +# SNIPPET END 4 diff --git a/v2/create_earn_account/python/pyproject.toml b/v2/create_earn_account/python/pyproject.toml index 32261017..13e766b4 100644 --- a/v2/create_earn_account/python/pyproject.toml +++ b/v2/create_earn_account/python/pyproject.toml @@ -5,5 +5,7 @@ description = "Example: Create an Earn Account using Compass API" dependencies = [ "compass-api-sdk", "python-dotenv", + "web3", + "eth-account", ] diff --git a/v2/create_earn_account/typescript/.env.example b/v2/create_earn_account/typescript/.env.example index ee1b4cd7..fa1c0221 100644 --- a/v2/create_earn_account/typescript/.env.example +++ b/v2/create_earn_account/typescript/.env.example @@ -4,3 +4,7 @@ COMPASS_API_KEY=your_compass_api_key_here # Wallet Configuration # The wallet address that will own the Earn Account and pay for gas WALLET_ADDRESS=0xYourWalletAddress +PRIVATE_KEY=your_wallet_private_key_here + +# Base RPC Configuration +BASE_RPC_URL=https://base-mainnet.g.alchemy.com/v2/your-api-key diff --git a/v2/create_earn_account/typescript/README.md b/v2/create_earn_account/typescript/README.md index 7ce4ab4a..cc387dd8 100644 --- a/v2/create_earn_account/typescript/README.md +++ b/v2/create_earn_account/typescript/README.md @@ -22,6 +22,8 @@ cp .env.example .env 3. Fill in your `.env` file with your actual values: - `COMPASS_API_KEY`: Your Compass API key - `WALLET_ADDRESS`: Your wallet address (will own the Earn Account) + - `PRIVATE_KEY`: Your wallet's private key (to sign the transaction) + - `BASE_RPC_URL`: Your Base mainnet RPC URL (to broadcast the transaction) ## Run @@ -37,11 +39,15 @@ npm start ## What This Does -This example gets an unsigned transaction to create an Earn Account on Base. The transaction must be signed and broadcast separately. +This example: +1. Gets an unsigned transaction to create an Earn Account on Base +2. Signs the transaction with your private key +3. Broadcasts it to the Base network +4. Waits for confirmation ## Notes - **No Gas Sponsorship**: The `owner` (who controls the account) is also the `sender` (who pays for gas). Note that Earn Account creation can also be done WITH gas sponsorship (using the `/gas_sponsorship/prepare` endpoint), but this example does not use gas sponsorship. - The Earn Account address is deterministic and returned before the transaction is confirmed. -- This example only retrieves the unsigned transaction. You'll need to sign and broadcast it separately. +- Make sure your wallet has enough ETH on Base to cover gas fees. diff --git a/v2/create_earn_account/typescript/package.json b/v2/create_earn_account/typescript/package.json index 7c9598c5..d35b8fb7 100644 --- a/v2/create_earn_account/typescript/package.json +++ b/v2/create_earn_account/typescript/package.json @@ -13,7 +13,8 @@ "description": "Example: Create an Earn Account using Compass API", "dependencies": { "@compass-labs/api-sdk": "^1.0.26", - "dotenv": "^16.5.0" + "dotenv": "^16.5.0", + "viem": "^2.31.0" }, "devDependencies": { "@types/node": "^24.0.0", diff --git a/v2/create_earn_account/typescript/src/index.ts b/v2/create_earn_account/typescript/src/index.ts index 3c693031..d1144421 100644 --- a/v2/create_earn_account/typescript/src/index.ts +++ b/v2/create_earn_account/typescript/src/index.ts @@ -1,34 +1,57 @@ // SNIPPET START 1 -// Import Libraries & Environment Variables import { CompassApiSDK } from "@compass-labs/api-sdk"; import dotenv from "dotenv"; +import { privateKeyToAccount } from "viem/accounts"; +import { base } from "viem/chains"; +import { http, createWalletClient, createPublicClient } from "viem"; dotenv.config(); const COMPASS_API_KEY = process.env.COMPASS_API_KEY as string; const WALLET_ADDRESS = process.env.WALLET_ADDRESS as `0x${string}`; +const PRIVATE_KEY = process.env.PRIVATE_KEY as `0x${string}`; +const BASE_RPC_URL = process.env.BASE_RPC_URL as string; // SNIPPET END 1 // SNIPPET START 2 -// Initialize Compass SDK const compass = new CompassApiSDK({ apiKeyAuth: COMPASS_API_KEY, }); // SNIPPET END 2 // SNIPPET START 3 -// Create Earn Account (No Gas Sponsorship) -// Get unsigned transaction to create an Earn Account on Base -// owner: The address that will own and control the Earn Account -// sender: The address that will sign and pay for gas (same as owner = no gas sponsorship) +// Get unsigned transaction to create Earn Account const createAccountResponse = await compass.earn.earnCreateAccount({ chain: "base", sender: WALLET_ADDRESS, owner: WALLET_ADDRESS, estimateGas: true, }); +// SNIPPET END 3 + +// SNIPPET START 4 +// Sign and broadcast transaction +const account = privateKeyToAccount(PRIVATE_KEY); +const walletClient = createWalletClient({ + account, + chain: base, + transport: http(BASE_RPC_URL), +}); +const publicClient = createPublicClient({ + chain: base, + transport: http(BASE_RPC_URL), +}); +const transaction = createAccountResponse.transaction as any; +const txHash = await walletClient.sendTransaction({ + ...transaction, + value: BigInt(transaction.value || 0), + gas: BigInt(transaction.gas), + maxFeePerGas: BigInt(transaction.maxFeePerGas), + maxPriorityFeePerGas: BigInt(transaction.maxPriorityFeePerGas), +}); + +await publicClient.waitForTransactionReceipt({ hash: txHash }); console.log("Earn Account Address:", createAccountResponse.earnAccountAddress); -console.log("Unsigned Transaction:", createAccountResponse.transaction); -// SNIPPET END 3 +// SNIPPET END 4 diff --git a/v2/fund_earn_account/python/README.md b/v2/fund_earn_account/python/README.md new file mode 100644 index 00000000..96136e9c --- /dev/null +++ b/v2/fund_earn_account/python/README.md @@ -0,0 +1,58 @@ +# Fund Earn Account - Python Example + +This example demonstrates how to fund an Earn Account by transferring USDC from your wallet to the Earn Account using the Compass API Python SDK. + +## Prerequisites + +- Python 3.8+ installed +- A Compass API key ([Get one here](https://auth-compasslabs-ai.auth.eu-west-2.amazoncognito.com/login?client_id=2l366l2b3dok7k71nbnu8r1u36&redirect_uri=https://api.compasslabs.ai/auth/callback&response_type=code&scope=openid+email+profile)) +- An existing Earn Account +- USDC balance in your wallet on Base + +## Setup + +1. Install dependencies: +```bash +pip install -e . +``` + +Or install directly: +```bash +pip install compass-api-sdk python-dotenv web3 eth-account +``` + +**Note:** Make sure you have `compass-api-sdk` version 2.0.1 or later. + +2. Copy the example environment file: +```bash +cp .env.example .env +``` + +3. Fill in your `.env` file with your actual values: + - `COMPASS_API_KEY`: Your Compass API key + - `WALLET_ADDRESS`: Your wallet address (owner of the Earn Account) + - `PRIVATE_KEY`: Your wallet's private key (to sign the transaction) + - `BASE_RPC_URL`: Your Base mainnet RPC URL (to broadcast the transaction) + +## Run + +```bash +python main.py +``` + +## What This Does + +This example: +1. Gets an unsigned transaction to transfer 2 USDC from your wallet to your Earn Account +2. Signs the transaction with your private key +3. Broadcasts it to the Base network +4. Waits for confirmation + +## Notes + +- This example transfers 2 USDC from your wallet to your Earn Account +- The `owner` must be the address that owns the Earn Account +- Make sure your wallet has sufficient USDC balance on Base +- Make sure your wallet has enough ETH on Base to cover gas fees +- Gas sponsorship is disabled (`gas_sponsorship=False`) + diff --git a/v2/fund_earn_account/python/fund_earn_account_python_example.egg-info/PKG-INFO b/v2/fund_earn_account/python/fund_earn_account_python_example.egg-info/PKG-INFO new file mode 100644 index 00000000..06a23188 --- /dev/null +++ b/v2/fund_earn_account/python/fund_earn_account_python_example.egg-info/PKG-INFO @@ -0,0 +1,8 @@ +Metadata-Version: 2.4 +Name: fund_earn_account_python_example +Version: 1.0.0 +Summary: Example: Fund Earn Account (Transfer USDC) using Compass API +Requires-Dist: compass-api-sdk +Requires-Dist: python-dotenv +Requires-Dist: web3 +Requires-Dist: eth-account diff --git a/v2/fund_earn_account/python/fund_earn_account_python_example.egg-info/SOURCES.txt b/v2/fund_earn_account/python/fund_earn_account_python_example.egg-info/SOURCES.txt new file mode 100644 index 00000000..898cec0c --- /dev/null +++ b/v2/fund_earn_account/python/fund_earn_account_python_example.egg-info/SOURCES.txt @@ -0,0 +1,8 @@ +README.md +main.py +pyproject.toml +fund_earn_account_python_example.egg-info/PKG-INFO +fund_earn_account_python_example.egg-info/SOURCES.txt +fund_earn_account_python_example.egg-info/dependency_links.txt +fund_earn_account_python_example.egg-info/requires.txt +fund_earn_account_python_example.egg-info/top_level.txt \ No newline at end of file diff --git a/v2/fund_earn_account/python/fund_earn_account_python_example.egg-info/dependency_links.txt b/v2/fund_earn_account/python/fund_earn_account_python_example.egg-info/dependency_links.txt new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/v2/fund_earn_account/python/fund_earn_account_python_example.egg-info/dependency_links.txt @@ -0,0 +1 @@ + diff --git a/v2/fund_earn_account/python/fund_earn_account_python_example.egg-info/requires.txt b/v2/fund_earn_account/python/fund_earn_account_python_example.egg-info/requires.txt new file mode 100644 index 00000000..dea15ae1 --- /dev/null +++ b/v2/fund_earn_account/python/fund_earn_account_python_example.egg-info/requires.txt @@ -0,0 +1,4 @@ +compass-api-sdk +python-dotenv +web3 +eth-account diff --git a/v2/fund_earn_account/python/fund_earn_account_python_example.egg-info/top_level.txt b/v2/fund_earn_account/python/fund_earn_account_python_example.egg-info/top_level.txt new file mode 100644 index 00000000..ba2906d0 --- /dev/null +++ b/v2/fund_earn_account/python/fund_earn_account_python_example.egg-info/top_level.txt @@ -0,0 +1 @@ +main diff --git a/v2/fund_earn_account/python/main.py b/v2/fund_earn_account/python/main.py new file mode 100644 index 00000000..24e217bb --- /dev/null +++ b/v2/fund_earn_account/python/main.py @@ -0,0 +1,45 @@ +# SNIPPET START 1 +from compass_api_sdk import CompassAPI, models +import os +from dotenv import load_dotenv +from web3 import Web3 + +load_dotenv() + +COMPASS_API_KEY = os.getenv("COMPASS_API_KEY") +WALLET_ADDRESS = os.getenv("WALLET_ADDRESS") +PRIVATE_KEY = os.getenv("PRIVATE_KEY") +BASE_RPC_URL = os.getenv("BASE_RPC_URL") +# SNIPPET END 1 + +# SNIPPET START 2 +with CompassAPI(api_key_auth=COMPASS_API_KEY) as compass_api: +# SNIPPET END 2 + +# SNIPPET START 3 + # Get unsigned transaction to fund Earn Account with USDC + transfer_response = compass_api.earn.earn_transfer( + owner=WALLET_ADDRESS, + chain=models.Chain.BASE, + token="USDC", + amount="2", + action=models.EarnTransferRequestAction.DEPOSIT, + gas_sponsorship=False, + ) +# SNIPPET END 3 + +# SNIPPET START 4 + # Sign and broadcast transaction + w3 = Web3(Web3.HTTPProvider(BASE_RPC_URL)) + tx_dict = transfer_response.model_dump(by_alias=True) + signed_tx = w3.eth.account.sign_transaction(tx_dict["transaction"], PRIVATE_KEY) + tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction) + + print(f"Transaction hash: {tx_hash.hex()}") + print(f"View on BaseScan: https://basescan.org/tx/{tx_hash.hex()}") + + receipt = w3.eth.wait_for_transaction_receipt(tx_hash) + print(f"Transaction confirmed in block: {receipt.blockNumber}") + print("Earn Account funded successfully!") +# SNIPPET END 4 + diff --git a/v2/fund_earn_account/python/pyproject.toml b/v2/fund_earn_account/python/pyproject.toml new file mode 100644 index 00000000..fa84bcd8 --- /dev/null +++ b/v2/fund_earn_account/python/pyproject.toml @@ -0,0 +1,11 @@ +[project] +name = "fund_earn_account_python_example" +version = "1.0.0" +description = "Example: Fund Earn Account (Transfer USDC) using Compass API" +dependencies = [ + "compass-api-sdk", + "python-dotenv", + "web3", + "eth-account", +] + diff --git a/v2/fund_earn_account/typescript/README.md b/v2/fund_earn_account/typescript/README.md new file mode 100644 index 00000000..be3acc74 --- /dev/null +++ b/v2/fund_earn_account/typescript/README.md @@ -0,0 +1,57 @@ +# Fund Earn Account - TypeScript Example + +This example demonstrates how to fund an Earn Account by transferring USDC from your wallet to the Earn Account using the Compass API TypeScript SDK. + +## Prerequisites + +- Node.js 18+ installed +- A Compass API key ([Get one here](https://auth-compasslabs-ai.auth.eu-west-2.amazoncognito.com/login?client_id=2l366l2b3dok7k71nbnu8r1u36&redirect_uri=https://api.compasslabs.ai/auth/callback&response_type=code&scope=openid+email+profile)) +- An existing Earn Account +- USDC balance in your wallet on Base + +## Setup + +1. Install dependencies: +```bash +npm install +``` + +2. Copy the example environment file: +```bash +cp .env.example .env +``` + +3. Fill in your `.env` file with your actual values: + - `COMPASS_API_KEY`: Your Compass API key + - `WALLET_ADDRESS`: Your wallet address (owner of the Earn Account) + - `PRIVATE_KEY`: Your wallet's private key (to sign the transaction) + - `BASE_RPC_URL`: Your Base mainnet RPC URL (to broadcast the transaction) + +## Run + +```bash +npm run dev +``` + +Or build and run: +```bash +npm run build +npm start +``` + +## What This Does + +This example: +1. Gets an unsigned transaction to transfer 2 USDC from your wallet to your Earn Account +2. Signs the transaction with your private key +3. Broadcasts it to the Base network +4. Waits for confirmation + +## Notes + +- This example transfers 2 USDC from your wallet to your Earn Account +- The `owner` must be the address that owns the Earn Account +- Make sure your wallet has sufficient USDC balance on Base +- Make sure your wallet has enough ETH on Base to cover gas fees +- Gas sponsorship is disabled (`gasSponsorship: false`) + diff --git a/v2/fund_earn_account/typescript/package.json b/v2/fund_earn_account/typescript/package.json new file mode 100644 index 00000000..569f0dea --- /dev/null +++ b/v2/fund_earn_account/typescript/package.json @@ -0,0 +1,26 @@ +{ + "name": "fund_earn_account_typescript_example", + "version": "1.0.0", + "type": "module", + "main": "index.js", + "scripts": { + "build": "tsc", + "start": "tsc && node dist/index.js", + "dev": "ts-node --esm src/index.ts" + }, + "author": "", + "license": "ISC", + "description": "Example: Fund Earn Account (Transfer USDC) using Compass API", + "dependencies": { + "@compass-labs/api-sdk": "^1.3.5", + "dotenv": "^16.5.0", + "viem": "^2.31.0" + }, + "devDependencies": { + "@types/node": "^24.0.0", + "prettier": "^3.6.2", + "ts-node": "^10.9.2", + "typescript": "^5.8.3" + } +} + diff --git a/v2/fund_earn_account/typescript/src/index.ts b/v2/fund_earn_account/typescript/src/index.ts new file mode 100644 index 00000000..125cff75 --- /dev/null +++ b/v2/fund_earn_account/typescript/src/index.ts @@ -0,0 +1,65 @@ +// SNIPPET START 1 +import { CompassApiSDK } from "@compass-labs/api-sdk"; +import dotenv from "dotenv"; +import { privateKeyToAccount } from "viem/accounts"; +import { base } from "viem/chains"; +import { http, createWalletClient, createPublicClient } from "viem"; + +dotenv.config(); + +const COMPASS_API_KEY = process.env.COMPASS_API_KEY as string; +const WALLET_ADDRESS = process.env.WALLET_ADDRESS as `0x${string}`; +const PRIVATE_KEY = (process.env.PRIVATE_KEY?.startsWith("0x") + ? process.env.PRIVATE_KEY + : `0x${process.env.PRIVATE_KEY}`) as `0x${string}`; +const BASE_RPC_URL = process.env.BASE_RPC_URL as string; +// SNIPPET END 1 + +// SNIPPET START 2 +const compass = new CompassApiSDK({ + apiKeyAuth: COMPASS_API_KEY, +}); +// SNIPPET END 2 + +// SNIPPET START 3 +// Get unsigned transaction to fund Earn Account with USDC +const transferResponse = await compass.earn.earnTransfer({ + owner: WALLET_ADDRESS, + chain: "base", + token: "USDC", + amount: "2", + action: "DEPOSIT", + gasSponsorship: false, +}); +// SNIPPET END 3 + +// SNIPPET START 4 +// Sign and broadcast transaction +const account = privateKeyToAccount(PRIVATE_KEY); +const walletClient = createWalletClient({ + account, + chain: base, + transport: http(BASE_RPC_URL), +}); +const publicClient = createPublicClient({ + chain: base, + transport: http(BASE_RPC_URL), +}); + +const transaction = transferResponse.transaction as any; +const txHash = await walletClient.sendTransaction({ + ...transaction, + value: BigInt(transaction.value || 0), + gas: BigInt(transaction.gas), + maxFeePerGas: BigInt(transaction.maxFeePerGas), + maxPriorityFeePerGas: BigInt(transaction.maxPriorityFeePerGas), +}); + +console.log(`Transaction hash: ${txHash}`); +console.log(`View on BaseScan: https://basescan.org/tx/${txHash}`); + +const receipt = await publicClient.waitForTransactionReceipt({ hash: txHash }); +console.log(`Transaction confirmed in block: ${receipt.blockNumber}`); +console.log("Earn Account funded successfully!"); +// SNIPPET END 4 + diff --git a/v2/fund_earn_account/typescript/tsconfig.json b/v2/fund_earn_account/typescript/tsconfig.json new file mode 100644 index 00000000..d127c035 --- /dev/null +++ b/v2/fund_earn_account/typescript/tsconfig.json @@ -0,0 +1,17 @@ +{ + "compilerOptions": { + "target": "es2020", + "module": "ESNext", + "lib": ["ES2020"], + "moduleResolution": "node", + "esModuleInterop": true, + "skipLibCheck": true, + "strict": true, + "resolveJsonModule": true, + "outDir": "./dist", + "rootDir": "./src" + }, + "include": ["src/**/*"], + "exclude": ["node_modules"] +} + diff --git a/v2/manage_earn_position/python/.env.example b/v2/manage_earn_position/python/.env.example new file mode 100644 index 00000000..eb04ff5c --- /dev/null +++ b/v2/manage_earn_position/python/.env.example @@ -0,0 +1,10 @@ +# Compass API Configuration +COMPASS_API_KEY=your_compass_api_key_here + +# Wallet Configuration +# The wallet address that owns the Earn Account +WALLET_ADDRESS=0xYourWalletAddress +PRIVATE_KEY=your_wallet_private_key_here + +# Base RPC Configuration +BASE_RPC_URL=https://base-mainnet.g.alchemy.com/v2/your-api-key diff --git a/v2/manage_earn_position/python/README.md b/v2/manage_earn_position/python/README.md new file mode 100644 index 00000000..ce171cd5 --- /dev/null +++ b/v2/manage_earn_position/python/README.md @@ -0,0 +1,57 @@ +# Manage Earn Position - Python Example + +This example demonstrates how to deposit into a Morpho vault using the Compass API Python SDK. + +## Prerequisites + +- Python 3.8+ installed +- A Compass API key ([Get one here](https://auth-compasslabs-ai.auth.eu-west-2.amazoncognito.com/login?client_id=2l366l2b3dok7k71nbnu8r1u36&redirect_uri=https://api.compasslabs.ai/auth/callback&response_type=code&scope=openid+email+profile)) +- An existing Earn Account + +## Setup + +1. Install dependencies (make sure you have the latest version): +```bash +pip install --upgrade compass-api-sdk python-dotenv web3 eth-account +``` + +Or if using `uv`: +```bash +uv pip install compass-api-sdk python-dotenv web3 eth-account +``` + +**Note:** This example requires `compass-api-sdk` version 2.0.1 or later (which includes the `earn` endpoints). Make sure to upgrade if you have an older version. + +2. Copy the example environment file: +```bash +cp .env.example .env +``` + +3. Fill in your `.env` file with your actual values: + - `COMPASS_API_KEY`: Your Compass API key + - `WALLET_ADDRESS`: Your wallet address (owner of the Earn Account) + - `PRIVATE_KEY`: Your wallet's private key (to sign the transaction) + - `BASE_RPC_URL`: Your Base mainnet RPC URL (to broadcast the transaction) + +## Run + +```bash +python main.py +``` + +## What This Does + +This example: +1. Gets an unsigned transaction to deposit 1 USDC into a Morpho vault +2. Signs the transaction with your private key +3. Broadcasts it to the Base network +4. Waits for confirmation + +## Notes + +- This example deposits 1 USDC into the Morpho vault at `0xbeeF010f9cb27031ad51e3333f9aF9C6B1228183` +- The `owner` must be the address that owns the Earn Account +- Make sure your Earn Account has sufficient USDC balance +- Make sure your wallet has enough ETH on Base to cover gas fees +- **Important:** Requires `compass-api-sdk` version 2.0.1 or later (includes `earn` endpoints). Make sure to install the latest version. + diff --git a/v2/manage_earn_position/python/main.py b/v2/manage_earn_position/python/main.py new file mode 100644 index 00000000..c4a6e46c --- /dev/null +++ b/v2/manage_earn_position/python/main.py @@ -0,0 +1,65 @@ +# SNIPPET START 1 +from compass_api_sdk import CompassAPI, models +import os +from dotenv import load_dotenv +from web3 import Web3 + +load_dotenv() + +COMPASS_API_KEY = os.getenv("COMPASS_API_KEY") +WALLET_ADDRESS = os.getenv("WALLET_ADDRESS") +PRIVATE_KEY = os.getenv("PRIVATE_KEY") +BASE_RPC_URL = os.getenv("BASE_RPC_URL") +# SNIPPET END 1 + +# SNIPPET START 2 +with CompassAPI(api_key_auth=COMPASS_API_KEY) as compass_api: +# SNIPPET END 2 + +# SNIPPET START 3 + # Get unsigned transaction to deposit into Morpho vault + manage_response = compass_api.earn.earn_manage( + owner=WALLET_ADDRESS, + chain=models.Chain.BASE, + venue={ + "type": "VAULT", + "vault_address": "0xbeeF010f9cb27031ad51e3333f9aF9C6B1228183", + }, + action=models.EarnManageRequestAction.DEPOSIT, + amount="0.5", + gas_sponsorship=False, + fee=None, + ) +# SNIPPET END 3 + +# SNIPPET START 4 + # Sign and broadcast transaction + w3 = Web3(Web3.HTTPProvider(BASE_RPC_URL)) + tx_dict = manage_response.model_dump(by_alias=True) + signed_tx = w3.eth.account.sign_transaction(tx_dict["transaction"], PRIVATE_KEY) + tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction) + + print(f"Transaction hash: {tx_hash.hex()}") + print(f"View on BaseScan: https://basescan.org/tx/{tx_hash.hex()}") + + receipt = w3.eth.wait_for_transaction_receipt(tx_hash) + print(f"Transaction confirmed in block: {receipt.blockNumber}") + print("Deposit transaction confirmed") +# SNIPPET END 4 + +# SNIPPET START 5 + # Check Earn Account positions + positions_response = compass_api.earn.earn_positions( + chain=models.Chain.BASE, + user_address=WALLET_ADDRESS, + days=100, + ) + + print(f"\nPositions: {len(positions_response.user_positions)}") + for position in positions_response.user_positions: + if position.TYPE == "VAULT": + print(f"{position.vault_name}: {position.amount_in_underlying_token} {position.token_name}") + elif position.TYPE == "AAVE": + print(f"Aave {position.token_name}: {position.amount_in_underlying_token}") +# SNIPPET END 5 + diff --git a/v2/manage_earn_position/python/pyproject.toml b/v2/manage_earn_position/python/pyproject.toml new file mode 100644 index 00000000..cf96c2ba --- /dev/null +++ b/v2/manage_earn_position/python/pyproject.toml @@ -0,0 +1,11 @@ +[project] +name = "manage_earn_position_python_example" +version = "1.0.0" +description = "Example: Manage Earn Position (Deposit into Morpho Vault) using Compass API" +dependencies = [ + "compass-api-sdk", + "python-dotenv", + "web3", + "eth-account", +] + diff --git a/v2/manage_earn_position/typescript/.env.example b/v2/manage_earn_position/typescript/.env.example new file mode 100644 index 00000000..eb04ff5c --- /dev/null +++ b/v2/manage_earn_position/typescript/.env.example @@ -0,0 +1,10 @@ +# Compass API Configuration +COMPASS_API_KEY=your_compass_api_key_here + +# Wallet Configuration +# The wallet address that owns the Earn Account +WALLET_ADDRESS=0xYourWalletAddress +PRIVATE_KEY=your_wallet_private_key_here + +# Base RPC Configuration +BASE_RPC_URL=https://base-mainnet.g.alchemy.com/v2/your-api-key diff --git a/v2/manage_earn_position/typescript/README.md b/v2/manage_earn_position/typescript/README.md new file mode 100644 index 00000000..f8cce852 --- /dev/null +++ b/v2/manage_earn_position/typescript/README.md @@ -0,0 +1,55 @@ +# Manage Earn Position - TypeScript Example + +This example demonstrates how to deposit into a Morpho vault using the Compass API TypeScript SDK. + +## Prerequisites + +- Node.js 18+ installed +- A Compass API key ([Get one here](https://auth-compasslabs-ai.auth.eu-west-2.amazoncognito.com/login?client_id=2l366l2b3dok7k71nbnu8r1u36&redirect_uri=https://api.compasslabs.ai/auth/callback&response_type=code&scope=openid+email+profile)) +- An existing Earn Account + +## Setup + +1. Install dependencies: +```bash +npm install +``` + +2. Copy the example environment file: +```bash +cp .env.example .env +``` + +3. Fill in your `.env` file with your actual values: + - `COMPASS_API_KEY`: Your Compass API key + - `WALLET_ADDRESS`: Your wallet address (owner of the Earn Account) + - `PRIVATE_KEY`: Your wallet's private key (to sign the transaction) + - `BASE_RPC_URL`: Your Base mainnet RPC URL (to broadcast the transaction) + +## Run + +```bash +npm run dev +``` + +Or build and run: +```bash +npm run build +npm start +``` + +## What This Does + +This example: +1. Gets an unsigned transaction to deposit 1 USDC into a Morpho vault +2. Signs the transaction with your private key +3. Broadcasts it to the Base network +4. Waits for confirmation + +## Notes + +- This example deposits 1 USDC into the Morpho vault at `0xbeeF010f9cb27031ad51e3333f9aF9C6B1228183` +- The `owner` must be the address that owns the Earn Account +- Make sure your Earn Account has sufficient USDC balance +- Make sure your wallet has enough ETH on Base to cover gas fees + diff --git a/v2/manage_earn_position/typescript/package.json b/v2/manage_earn_position/typescript/package.json new file mode 100644 index 00000000..5420c3de --- /dev/null +++ b/v2/manage_earn_position/typescript/package.json @@ -0,0 +1,26 @@ +{ + "name": "manage_earn_position_typescript_example", + "version": "1.0.0", + "type": "module", + "main": "index.js", + "scripts": { + "build": "tsc", + "start": "tsc && node dist/index.js", + "dev": "ts-node --esm src/index.ts" + }, + "author": "", + "license": "ISC", + "description": "Example: Manage Earn Position (Deposit into Morpho Vault) using Compass API", + "dependencies": { + "@compass-labs/api-sdk": "1.3.5", + "dotenv": "^16.5.0", + "viem": "^2.31.0" + }, + "devDependencies": { + "@types/node": "^24.0.0", + "prettier": "^3.6.2", + "ts-node": "^10.9.2", + "typescript": "^5.8.3" + } +} + diff --git a/v2/manage_earn_position/typescript/src/index.ts b/v2/manage_earn_position/typescript/src/index.ts new file mode 100644 index 00000000..c5ad4c8f --- /dev/null +++ b/v2/manage_earn_position/typescript/src/index.ts @@ -0,0 +1,87 @@ +// SNIPPET START 1 +import { CompassApiSDK } from "@compass-labs/api-sdk"; +import dotenv from "dotenv"; +import { privateKeyToAccount } from "viem/accounts"; +import { base } from "viem/chains"; +import { http, createWalletClient, createPublicClient } from "viem"; + +dotenv.config(); + +const COMPASS_API_KEY = process.env.COMPASS_API_KEY as string; +const WALLET_ADDRESS = process.env.WALLET_ADDRESS as `0x${string}`; +const PRIVATE_KEY = (process.env.PRIVATE_KEY?.startsWith("0x") + ? process.env.PRIVATE_KEY + : `0x${process.env.PRIVATE_KEY}`) as `0x${string}`; +const BASE_RPC_URL = process.env.BASE_RPC_URL as string; +// SNIPPET END 1 + +// SNIPPET START 2 +const compass = new CompassApiSDK({ + apiKeyAuth: COMPASS_API_KEY, +}); +// SNIPPET END 2 + +// SNIPPET START 3 +// Get unsigned transaction to deposit into Morpho vault +const manageResponse = await compass.earn.earnManage({ + owner: WALLET_ADDRESS, + chain: "base", + venue: { + type: "VAULT", + vaultAddress: "0xbeeF010f9cb27031ad51e3333f9aF9C6B1228183", + }, + action: "DEPOSIT", + amount: "0.5", + gasSponsorship: false, + fee: null, +}); +// SNIPPET END 3 + +// SNIPPET START 4 +// Sign and broadcast transaction +const account = privateKeyToAccount(PRIVATE_KEY); +const walletClient = createWalletClient({ + account, + chain: base, + transport: http(BASE_RPC_URL), +}); +const publicClient = createPublicClient({ + chain: base, + transport: http(BASE_RPC_URL), +}); + +const transaction = manageResponse.transaction as any; +const txHash = await walletClient.sendTransaction({ + ...transaction, + value: BigInt(transaction.value || 0), + gas: BigInt(transaction.gas), + maxFeePerGas: BigInt(transaction.maxFeePerGas), + maxPriorityFeePerGas: BigInt(transaction.maxPriorityFeePerGas), +}); + +console.log(`Transaction hash: ${txHash}`); +console.log(`View on BaseScan: https://basescan.org/tx/${txHash}`); + +const receipt = await publicClient.waitForTransactionReceipt({ hash: txHash }); +console.log(`Transaction confirmed in block: ${receipt.blockNumber}`); +console.log("Deposit transaction confirmed"); +// SNIPPET END 4 + +// SNIPPET START 5 +// Check Earn Account positions +const positionsResponse = await compass.earn.earnPositions({ + chain: "base", + userAddress: WALLET_ADDRESS, + days: 100, +}); + +console.log(`\nPositions: ${positionsResponse.userPositions.length}`); +for (const position of positionsResponse.userPositions) { + if (position.type === "VAULT") { + console.log(`${position.vaultName}: ${position.amountInUnderlyingToken} ${position.tokenName}`); + } else if (position.type === "AAVE") { + console.log(`Aave ${position.tokenName}: ${position.amountInUnderlyingToken}`); + } +} +// SNIPPET END 5 + diff --git a/v2/manage_earn_position/typescript/tsconfig.json b/v2/manage_earn_position/typescript/tsconfig.json new file mode 100644 index 00000000..d127c035 --- /dev/null +++ b/v2/manage_earn_position/typescript/tsconfig.json @@ -0,0 +1,17 @@ +{ + "compilerOptions": { + "target": "es2020", + "module": "ESNext", + "lib": ["ES2020"], + "moduleResolution": "node", + "esModuleInterop": true, + "skipLibCheck": true, + "strict": true, + "resolveJsonModule": true, + "outDir": "./dist", + "rootDir": "./src" + }, + "include": ["src/**/*"], + "exclude": ["node_modules"] +} +