Skip to main content

No project description provided

Project description

Enclave SDK Test Script

This script demonstrates how to interact with the Enclave SDK using Web3 to create a smart account, compute quotes, and submit transactions on the Optimism network.

Prerequisites

Before running the script, ensure you have the following:

  • Python 3.x
  • Required packages:
    • web3
    • python-dotenv
    • eth-account

You can install the required packages using pip:

pip install web3 python-dotenv eth-account

Environment Variables

Create a .env file in the same directory as your script and set the following environment variables:

ALCHEMY_KEY=<your_alchemy_key>
ENCLAVE_KEY=<your_enclave_key>
PRIVATE_KEY=<your_private_key>

Usage

  1. Create a new instance of Web3: Connect to the Optimism network using an Alchemy HTTP provider.

    from web3 import Web3
    import os
    
    w3 = Web3(Web3.HTTPProvider(f"https://opt-mainnet.g.alchemy.com/v2/{os.getenv('ALCHEMY_KEY')}"))
    
  2. Create a new instance of Enclave: Initialize the Enclave client with your Enclave key.

    from app import Enclave
    from dotenv import load_dotenv
    
    load_dotenv()
    client = Enclave(os.getenv("ENCLAVE_KEY"))
    
  3. Generate a new Ethereum account: Use your private key to generate an Ethereum account.

    eoa = w3.eth.account.from_key(os.getenv("PRIVATE_KEY"))
    
  4. Create an Enclave Smart Contract Wallet: Create a smart account for the generated Ethereum account.

    account = client.create_smart_account(eoa.address)
    
  5. Get balance for the Enclave Account: Retrieve and display the balance of the smart account.

    balance = client.get_smart_balance(account.wallet.scw_address)
    print('Smart Account Balance:', balance['netBalance'])
    
  6. Compute a quote: Compute a transaction quote based on the user's willingness to spend.

    quote = client.compute_quote(account.wallet.scw_address, network=10, amount=0.1 * 1e6, quote_type='AMOUNT_IN')
    
  7. Define transaction details: Set up the recipient address and amount to transfer.

    usdc_on_optimism = '0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85' 
    recipient_address = '0x4E2ef45077FB15Ef5f0FfF9E86B38991aA4bAe5C'  # Replace with the recipient's address
    credit_amount = int(quote['total_credit']) + int(quote['userWithdrawal'])
    
  8. Build and sign the transaction: Build and sign the transaction using the user's Ethereum account.

    transaction_details = [{
        'encodedData': usdc_contract.encode_abi("transfer", args=[recipient_address, credit_amount]),
        'targetContractAddress': usdc_on_optimism,
        'value': 0
    }]
    order_data = {'amount': str(int(amount)), 'type': 'AMOUNT_IN'}
    built_txn = client.build_transaction(transaction_details, 10, account.wallet.scw_address, order_data, None, 1)
    
    signature = eoa.sign_message(encode_defunct(hexstr=built_txn.message_to_sign))
    
  9. Submit the transaction: Submit the transaction to the network.

    response = client.submit_transaction("0x" + signature.signature.hex(), user_op_serializable, 10, account.wallet.scw_address, 1)
    print('Transaction submitted successfully:', response)
    

Conclusion

This script serves as a basic example of how to use the Enclave SDK to interact with the Ethereum blockchain on the Optimism network. Make sure to replace the placeholder values in the environment variables with your actual keys before running the script.

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.2.tar.gz (9.1 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.2-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: enclave_py_sdk-0.1.2.tar.gz
  • Upload date:
  • Size: 9.1 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.2.tar.gz
Algorithm Hash digest
SHA256 19138713e65faf84f7a7979e3a31bd42c98dcb4ca588980872e6603fe07ebfd0
MD5 f3dfd6e2c884263dbb37b69c2f8710a5
BLAKE2b-256 cf4247ee5d3e25ec20ab89df6ab08b12d9db72201c025d9dbf479527ca3a90bd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: enclave_py_sdk-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 10.8 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 568ebb2319c77b5f4116c53b867b7cd996e76f7d73a35dd1dbe2d552a19acfd1
MD5 4a78fbbfa504e4613b99dc1ff0d42cc5
BLAKE2b-256 88f5a385a7c959408022727ba64d1c8519d04aee324c96033cd5a577a6979e76

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