Skip to main content

No project description provided

Project description

Enclave Python SDK Example

This README provides an overview of how to use the Enclave Python SDK to interact with the Ethereum blockchain, specifically on the Optimism network. The example demonstrates creating a smart account, computing a quote for a transaction, building, signing, and submitting a transaction.

Note: This is a chain-abstracted transaction demo on Optimism, allowing users to make transactions by depositing USDC to any supported chain, even if it is not Optimism.

Prerequisites

  • Python 3.x
  • Required libraries:
    • web3
    • enclave_py_sdk
    • python-dotenv

Setup

  1. Install the required libraries:

    pip install web3 enclave_py_sdk python-dotenv
    
  2. Create a .env file in your project directory with the following variables:

    ALCHEMY_KEY=your_alchemy_key
    ENCLAVE_KEY=your_enclave_key
    PRIVATE_KEY=your_private_key
    

Example Usage

The following code demonstrates how to create a smart account, compute a transaction quote, and submit a transaction.

# Environment Setup
import os
from web3 import Web3
from enclave_py_sdk import Enclave
from dotenv import load_dotenv

load_dotenv()

# Web3 and Enclave Initialization
w3 = Web3(Web3.HTTPProvider(
    f"https://opt-mainnet.g.alchemy.com/v2/{os.getenv('ALCHEMY_KEY')}"))
client = Enclave(os.getenv("ENCLAVE_KEY"))

# Account Generation
eoa = w3.eth.account.from_key(os.getenv("PRIVATE_KEY"))
print("Address:", eoa.address)

# Smart Account Creation
account = client.create_smart_account(eoa.address)
print("Smart account created:", account.wallet.scw_address)

# Balance Retrieval
balance = client.get_smart_balance(account.wallet.scw_address)
print('Smart Account Balance:', balance['netBalance'])

# Quote Computation
network = 10
amount = 0.1 * 1e6  # Amount to compute the quote for
quote_type = 'AMOUNT_IN' 
quote = client.compute_quote(account.wallet.scw_address, network, amount, quote_type)
print('Quote computed:', quote)

# Transaction Preparation
usdc_on_optimism = '0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85' 
recipient_address = '0x4E2ef45077FB15Ef5f0FfF9E86B38991aA4bAe5C'  # Replace with the recipient's address
credit_amount = int(quote['total_credit']) + int(quote['userWithdrawal'])

usdc_contract = w3.eth.contract(address=usdc_on_optimism, abi=[{
    'constant': False,
    'inputs': [
        {'name': 'to', 'type': 'address'},
        {'name': 'amount', 'type': 'uint256'}
    ],
    'name': 'transfer',
    'outputs': [{'name': '', 'type': 'bool'}],
    'payable': False,
    'stateMutability': 'nonpayable',
    'type': 'function'
}])
encoded_data = usdc_contract.encode_abi(
    "transfer",
    args=[
        Web3.to_checksum_address(recipient_address),
        credit_amount
    ]
)

transaction_details = [{
    'encodedData': encoded_data, 
    'targetContractAddress': usdc_on_optimism,
    'value': 0  # Assuming no ETH is being transferred, only USDC
}]

order_data = {
    'amount': str(int(amount)),  # Amount of USDC required for the transfer 
    'type': 'AMOUNT_IN'
}

# Signing and Submission
built_txn = client.build_transaction(
    transaction_details,
    10,  # Target network
    account.wallet.scw_address,  # User's smart account address
    order_data,
    None,
    1  # Sign mode
)

signature = eoa.sign_message(encode_defunct(hexstr=built_txn.message_to_sign))

try:
    response = client.submit_transaction(
        "0x" + signature.signature.hex(),
        user_op_serializable,
        10,  # Target network
        account.wallet.scw_address,
        1  # Signature type
    )
    print('Transaction submitted successfully:', response)
except Exception as e:
    print('Error submitting transaction:', str(e))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

enclave_py_sdk-0.1.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

enclave_py_sdk-0.1.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file enclave_py_sdk-0.1.0.tar.gz.

File metadata

  • Download URL: enclave_py_sdk-0.1.0.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.12.4 Darwin/23.1.0

File hashes

Hashes for enclave_py_sdk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 681eaecb7f65dcc70697992793ccb5d01b764519fd25a3b828ef371ac266475f
MD5 8d814c7701f076c3d08cb2690b4243b7
BLAKE2b-256 3de5efd95437be63d4f58ffdb407303948d9f9e90f6b4a7e22fa940362730e1c

See more details on using hashes here.

File details

Details for the file enclave_py_sdk-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: enclave_py_sdk-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.12.4 Darwin/23.1.0

File hashes

Hashes for enclave_py_sdk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bca3d713deefd23c29e4bb49539360a2d63e083a1df7c067c4f3340e42fb727f
MD5 27c5929feb17f8a4c0258244afecd296
BLAKE2b-256 62c191f95c20f553f887db3d511c78b631f797834674bef4770495b3ba384d0e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page