Skip to content

Commit 9ab1db0

Browse files
authored
Merge pull request #11 from CompassLabs/aidar/com-4714-sdk-examples-in-the-examples-mono
adding looping use cases
2 parents 20745d0 + 33216fe commit 9ab1db0

10 files changed

Lines changed: 275 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
aave-looping
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Aave Looping Example
2+
3+
This Python project demonstrates how to perform Aave looping using the Compass Labs API SDK.
4+
5+
## Requirements
6+
7+
- Python 3.11+
8+
- Poetry for dependency management
9+
10+
## Dependencies
11+
12+
- web3
13+
- python-dotenv
14+
- compass-api-sdk
15+
16+
## Setup
17+
18+
1. Install dependencies:
19+
```bash
20+
poetry install
21+
```
22+
23+
2. Create a `.env` file with your configuration:
24+
```bash
25+
COMPASS_API_KEY=your_api_key
26+
PRIVATE_KEY=your_private_key
27+
RPC_URL=your_rpc_url
28+
```
29+
30+
3. Run the example:
31+
```bash
32+
poetry run python src/main.py
33+
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[virtualenvs]
2+
in-project = true
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[tool.poetry]
2+
name = "aave-looping-example"
3+
version = "0.1.0"
4+
description = "Example of Aave looping in Python"
5+
authors = ["Compass Labs"]
6+
readme = "README.md"
7+
package-mode = false
8+
9+
[tool.poetry.dependencies]
10+
python = "^3.11"
11+
web3 = "^7.12.0"
12+
python-dotenv = "^1.1.0"
13+
compass-api-sdk = "^0.9.10"
14+
15+
16+
[build-system]
17+
requires = ["poetry-core"]
18+
build-backend = "poetry.core.masonry.api"
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from compass_api_sdk import CompassAPI
2+
import os
3+
import dotenv
4+
from web3 import Web3
5+
from eth_account import Account
6+
7+
dotenv.load_dotenv()
8+
9+
PRIVATE_KEY = os.getenv("PRIVATE_KEY")
10+
RPC_URL = os.getenv("RPC_URL")
11+
COMPASS_API_KEY = os.getenv("COMPASS_API_KEY")
12+
13+
def main():
14+
sdk = CompassAPI(api_key_auth=COMPASS_API_KEY)
15+
16+
private_key = PRIVATE_KEY
17+
if not private_key.startswith("0x"):
18+
private_key = f"0x{private_key}"
19+
20+
w3 = Web3(Web3.HTTPProvider(RPC_URL))
21+
account = Account.from_key(private_key)
22+
23+
auth = sdk.transaction_batching.authorization(
24+
chain="ethereum:mainnet",
25+
sender=account.address
26+
)
27+
28+
signed_auth = Account.sign_authorization(auth.model_dump(by_alias=True), private_key)
29+
30+
looping_tx = sdk.transaction_batching.aave_loop(
31+
chain="ethereum:mainnet",
32+
sender=account.address,
33+
signed_authorization=signed_auth.model_dump(),
34+
collateral_token="USDC",
35+
borrow_token="WETH",
36+
initial_collateral_amount=10,
37+
multiplier=1.5,
38+
max_slippage_percent=2.5,
39+
loan_to_value=70
40+
)
41+
42+
signed_tx = w3.eth.account.sign_transaction(looping_tx.model_dump(by_alias=True), private_key)
43+
tx_hash = w3.eth.send_raw_transaction(signed_tx.raw_transaction)
44+
45+
print(f"Transaction hash: {tx_hash.hex()}")
46+
47+
if __name__ == "__main__":
48+
main()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"useTabs": false,
3+
"tabWidth": 2,
4+
"semi": true,
5+
"singleQuote": true,
6+
"trailingComma": "es5",
7+
"printWidth": 100
8+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Aave Looping TypeScript Example
2+
3+
This example demonstrates how to use the Compass Labs API SDK to perform Aave looping operations on Ethereum mainnet.
4+
5+
## Prerequisites
6+
7+
- Node.js installed
8+
- A private key for an Ethereum wallet with sufficient funds
9+
- An RPC URL for Ethereum mainnet (e.g., from Infura, Alchemy)
10+
- A Compass Labs API key
11+
12+
## Setup
13+
14+
1. Install dependencies:
15+
```bash
16+
npm install
17+
```
18+
19+
2. Create a `.env` file in the root directory with the following variables:
20+
```
21+
PRIVATE_KEY=your_private_key_here
22+
RPC_URL=your_rpc_url_here
23+
COMPASS_API_KEY=your_compass_api_key_here
24+
```
25+
26+
Make sure your private key is prefixed with "0x".
27+
28+
## Running the Example
29+
30+
Execute the TypeScript example:
31+
32+
```bash
33+
npm start
34+
```
35+
36+
## What the Example Does
37+
38+
This example:
39+
1. Initializes the Compass Labs SDK
40+
2. Sets up authorization for transaction batching
41+
3. Creates an Aave looping transaction that:
42+
- Uses USDC as collateral
43+
- Borrows WETH
44+
- Sets an initial collateral amount of 5 USDC
45+
- Uses a multiplier of 1.5x
46+
- Sets max slippage to 2.5%
47+
- Sets a loan-to-value ratio of 70%
48+
49+
## Security Note
50+
51+
Never commit your `.env` file or expose your private key. Make sure to add `.env` to your `.gitignore` file.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "aave_looping_typescript_example",
3+
"version": "1.0.0",
4+
"type": "module",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"build": "tsc",
9+
"start": "tsc && node dist/index.js",
10+
"dev": "ts-node --esm src/index.ts"
11+
},
12+
"keywords": [],
13+
"author": "",
14+
"license": "ISC",
15+
"description": "",
16+
"devDependencies": {
17+
"@types/node": "^22.15.30",
18+
"ts-node": "^10.9.2",
19+
"typescript": "^5.8.3"
20+
},
21+
"dependencies": {
22+
"@compass-labs/api-sdk": "^0.5.10",
23+
"dotenv": "^16.5.0",
24+
"viem": "^2.30.6"
25+
}
26+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { CompassApiSDK } from "@compass-labs/api-sdk";
2+
import dotenv from "dotenv";
3+
import { privateKeyToAccount } from 'viem/accounts'
4+
import { mainnet } from 'viem/chains';
5+
import { http, SendTransactionRequest } from 'viem';
6+
7+
import { createWalletClient } from 'viem';
8+
9+
dotenv.config();
10+
11+
const PRIVATE_KEY = process.env.PRIVATE_KEY as `0x${string}`;
12+
const RPC_URL = process.env.RPC_URL as string;
13+
14+
const main = async () => {
15+
const compassApiSDK = new CompassApiSDK({
16+
apiKeyAuth: process.env.COMPASS_API_KEY,
17+
});
18+
19+
const account = privateKeyToAccount(PRIVATE_KEY);
20+
21+
const walletClient = createWalletClient({
22+
account,
23+
chain: mainnet,
24+
transport: http(RPC_URL as string),
25+
});
26+
27+
const auth = await compassApiSDK.transactionBatching.authorization({
28+
chain: "ethereum:mainnet",
29+
sender: account.address,
30+
});
31+
32+
const signedAuth = await walletClient.signAuthorization({
33+
account,
34+
contractAddress: auth.address as `0x${string}`,
35+
});
36+
37+
const loopingTx = await compassApiSDK.transactionBatching.aaveLoop({
38+
chain: "ethereum:mainnet",
39+
sender: account.address,
40+
signedAuthorization: {
41+
nonce: signedAuth.nonce,
42+
address: signedAuth.address,
43+
chainId: signedAuth.chainId,
44+
r: signedAuth.r,
45+
s: signedAuth.s,
46+
yParity: signedAuth.yParity as number,
47+
},
48+
collateralToken: "USDC",
49+
borrowToken: "WETH",
50+
initialCollateralAmount: 5,
51+
multiplier: 1.5,
52+
maxSlippagePercent: 2.5,
53+
loanToValue: 70,
54+
});
55+
56+
const tx = await walletClient.sendTransaction(loopingTx as unknown as SendTransactionRequest);
57+
58+
console.log(tx);
59+
}
60+
61+
62+
main();
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"module": "NodeNext",
5+
"moduleResolution": "NodeNext",
6+
"strict": true,
7+
"esModuleInterop": true,
8+
"resolveJsonModule": true,
9+
"outDir": "dist",
10+
"rootDir": "src",
11+
"types": ["node"],
12+
"sourceMap": true,
13+
"inlineSourceMap": false,
14+
"inlineSources": true,
15+
"declaration": true,
16+
"declarationMap": true,
17+
"allowJs": true,
18+
"checkJs": false
19+
},
20+
"ts-node": {
21+
"esm": true,
22+
"experimentalSpecifierResolution": "node"
23+
},
24+
"include": ["src"],
25+
"exclude": ["node_modules", "dist"]
26+
}

0 commit comments

Comments
 (0)