Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions v1/aave_looping/python/src/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# SNIPPET START 1
# IMPORT LIBRARIES & ENVIRONMENT VARIABLES
from compass_api_sdk import CompassAPI
import os
import dotenv
Expand All @@ -10,6 +11,11 @@
PRIVATE_KEY = os.getenv("PRIVATE_KEY")
ETHEREUM_RPC_URL = os.getenv("ETHEREUM_RPC_URL")

# SNIPPET END 1


# SNIPPET START 2
# INITIALIZE SDK AND ACCOUNT
w3 = Web3(Web3.HTTPProvider(ETHEREUM_RPC_URL))

compass_api_sdk = CompassAPI(
Expand All @@ -19,16 +25,17 @@
)

account = Account.from_key(PRIVATE_KEY)
# SNIPPET END 1

# SNIPPET END 2

# SNIPPET START 2
# SNIPPET START 3
# GET AND SIGN AUTHORIZATION
auth = compass_api_sdk.transaction_bundler.transaction_bundler_authorization(
chain="ethereum", sender=account.address
)

signed_auth = Account.sign_authorization(auth.model_dump(by_alias=True), PRIVATE_KEY)
# SNIPPET END 2
# SNIPPET END 3

swap_tx = compass_api_sdk.swap.swap_odos(
chain="ethereum",
Expand All @@ -44,7 +51,8 @@
tx_hash = w3.eth.send_raw_transaction(signed_tx.raw_transaction)
w3.eth.wait_for_transaction_receipt(tx_hash)

# SNIPPET START 3
# SNIPPET START 4
# CONFIGURE LEVERAGE PARAMETERS
looping_tx = compass_api_sdk.transaction_bundler.transaction_bundler_aave_loop(
chain="ethereum",
sender=account.address,
Expand All @@ -56,15 +64,16 @@
max_slippage_percent=2.5,
loan_to_value=70,
)
# SNIPPET END 3
# SNIPPET END 4

# SNIPPET START 4
# SNIPPET START 5
# SIGN AND BROADCAST TRANSACTION
signed_transaction = w3.eth.account.sign_transaction(
looping_tx.transaction.model_dump(by_alias=True), PRIVATE_KEY
)
tx_hash = w3.eth.send_raw_transaction(signed_transaction.raw_transaction)
receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
# SNIPPET END 4
# SNIPPET END 5

if receipt.status != 1:
raise Exception()
5 changes: 5 additions & 0 deletions v1/aave_looping/typescript/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// SNIPPET START 1
// IMPORT LIBRARIES & ENVIRONMENT VARIABLES
import { CompassApiSDK } from "@compass-labs/api-sdk";
import dotenv from "dotenv";
import { privateKeyToAccount } from "viem/accounts";
Expand All @@ -14,6 +15,7 @@ const ETHEREUM_RPC_URL = process.env.ETHEREUM_RPC_URL as string;
// SNIPPET END 1

// SNIPPET START 2
// INITIALIZE SDK AND ACCOUNT
const compassApiSDK = new CompassApiSDK({
apiKeyAuth: process.env.COMPASS_API_KEY,
serverURL: process.env.SERVER_URL || undefined, // For internal testing purposes. You do not need to set this.
Expand All @@ -34,6 +36,7 @@ const publicClient = createPublicClient({
// SNIPPET END 2

// SNIPPET START 3
// GET AND SIGN AUTHORIZATION
const auth =
await compassApiSDK.transactionBundler.transactionBundlerAuthorization({
chain: "ethereum",
Expand Down Expand Up @@ -70,6 +73,7 @@ await publicClient.waitForTransactionReceipt({
});

// SNIPPET START 4
// CONFIGURE LEVERAGE PARAMETERS
const loopingTx =
(await compassApiSDK.transactionBundler.transactionBundlerAaveLoop({
chain: "ethereum",
Expand All @@ -93,6 +97,7 @@ const loopingTx =
// SNIPPET END 4

// SNIPPET START 5
// SIGN AND BROADCAST TRANSACTION
const loopingTransaction = loopingTx.transaction as any;
const txHash = await walletClient.sendTransaction({
...loopingTransaction,
Expand Down
36 changes: 29 additions & 7 deletions v1/transaction_bundler/python/src/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# SNIPPET START 1
# IMPORT LIBRARIES & ENVIRONMENT VARIABLES
from compass_api_sdk import CompassAPI, models
from eth_account import Account
import os
Expand All @@ -9,28 +10,43 @@

PRIVATE_KEY = os.getenv("PRIVATE_KEY")
ETHEREUM_RPC_URL = os.getenv("ETHEREUM_RPC_URL")
# SNIPPET END 1

# SNIPPET START 2
# INITIALIZE SDK AND ACCOUNT
# Initialize Web3 client
w3 = Web3(Web3.HTTPProvider(ETHEREUM_RPC_URL))

# Initialize Compass API SDK
compass_api_sdk = CompassAPI(
api_key_auth=os.getenv("COMPASS_API_KEY"),
server_url=os.getenv("SERVER_URL")
or None, # For internal testing purposes. You do not need to set this.
)

# Initialize account
account = Account.from_key(PRIVATE_KEY)
# SNIPPET END 1
# SNIPPET END 2

# SNIPPET START 2
# SNIPPET START 3
# GET AND SIGN AUTHORIZATION


# Get unsigned authorization from Compass API
auth = compass_api_sdk.transaction_bundler.transaction_bundler_authorization(
chain="ethereum", sender=account.address
)

# Convert unsigned authorization to dictionary
auth_dict = auth.model_dump(mode="json", by_alias=True)

# Sign unsigned authorization using Compass API SDK
signed_auth = Account.sign_authorization(auth_dict, PRIVATE_KEY)

# Convert signed authorization to dictionary
signed_authorization = signed_auth.model_dump(by_alias=True)
# SNIPPET END 2
# SNIPPET END 3


swap_tx = compass_api_sdk.swap.swap_odos(
chain="ethereum",
Expand All @@ -46,7 +62,10 @@
tx_hash = w3.eth.send_raw_transaction(signed_tx.raw_transaction)
w3.eth.wait_for_transaction_receipt(tx_hash)

# SNIPPET START 3
# SNIPPET START 4
# CREATE BUNDLED TRANSACTIONS

# Perform bundle execution using Compass API SDK
bundler_tx = compass_api_sdk.transaction_bundler.transaction_bundler_execute(
chain="ethereum",
sender=account.address,
Expand All @@ -72,15 +91,18 @@
),
],
)
# SNIPPET END 3
# SNIPPET END 4

# SNIPPET START 4
# SNIPPET START 5
# SIGN AND BROADCAST TRANSACTION

# Sign bundle execution transaction using Web3
signed_transaction = w3.eth.account.sign_transaction(
bundler_tx.transaction.model_dump(by_alias=True), PRIVATE_KEY
)
tx_hash = w3.eth.send_raw_transaction(signed_transaction.raw_transaction)
receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
# SNIPPET END 4
# SNIPPET END 5

if receipt.status != 1:
raise Exception()
15 changes: 13 additions & 2 deletions v1/transaction_bundler/typescript/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// SNIPPET START 1
// IMPORT LIBRARIES & ENVIRONMENT VARIABLES

import { CompassApiSDK } from "@compass-labs/api-sdk";
import { privateKeyToAccount } from "viem/accounts";
import { mainnet } from "viem/chains";
Expand All @@ -11,14 +13,17 @@ dotenv.config();

const PRIVATE_KEY = process.env.PRIVATE_KEY as `0x${string}`;
const ETHEREUM_RPC_URL = process.env.ETHEREUM_RPC_URL as string;
// SNIPPET END 1


// SNIPPET START 2
// INITIALIZE SDK AND ACCOUNT
const compassApiSDK = new CompassApiSDK({
apiKeyAuth: process.env.COMPASS_API_KEY,
serverURL: process.env.SERVER_URL || undefined, // For internal testing purposes. You do not need to set this.
});
// SNIPPET END 1

// SNIPPET START 2

const account = privateKeyToAccount(PRIVATE_KEY);

const walletClient = createWalletClient({
Expand All @@ -34,6 +39,8 @@ const publicClient = createPublicClient({
// SNIPPET END 2

// SNIPPET START 3
// GET AND SIGN AUTHORIZATION

const auth =
await compassApiSDK.transactionBundler.transactionBundlerAuthorization({
chain: "ethereum",
Expand Down Expand Up @@ -70,6 +77,8 @@ await publicClient.waitForTransactionReceipt({
});

// SNIPPET START 4
// CREATE BUNDLED TRANSACTIONS

const bundlerTx =
await compassApiSDK.transactionBundler.transactionBundlerExecute({
chain: "ethereum",
Expand Down Expand Up @@ -106,6 +115,8 @@ const bundlerTx =
// SNIPPET END 4

// SNIPPET START 5
// SIGN AND BROADCAST TRANSACTION

const bundlerTransaction = bundlerTx.transaction as any;
const txHash = await walletClient.sendTransaction({
...bundlerTransaction,
Expand Down
Loading