demo.py
1 import os 2 import time 3 4 from beaglegaze.pay_per_call import pay_per_call, set_processor 5 from beaglegaze.smart_contract import SmartContract 6 from beaglegaze.async_batch_processor import AsyncBatchProcessor 7 from beaglegaze.batch_mode import BatchMode 8 from beaglegaze.contract_consumer import ContractConsumer 9 10 # --- Configuration --- 11 HARDHAT_URL = "http://localhost:8545" 12 13 CLIENT_PRIVATE_KEY = "0xcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc" 14 15 # --- Demo Class --- 16 class MonetizedLibrary: 17 """ 18 A demo class with a method that is monetized using the @pay_per_call decorator. 19 The MonetizedLibrary sets up its own AsyncBatchProcessor and SmartContract instance. 20 """ 21 def __init__(self, network_url: str, client_private_key: str): 22 # Set up the async batch processor 23 async_processor = AsyncBatchProcessor(BatchMode.OFF) 24 25 # Create smart contract instance 26 # the smart contract is deployed by the project maintainer(s) 27 # and hardcoded in the library 28 smart_contract = SmartContract( 29 "0x289B72CEeaB48832261626D62E3daA87Fd90B024", 30 network_url, 31 client_private_key, 32 10 # batch size 33 ) 34 35 # Create contract consumer and add as observer 36 contract_consumer = ContractConsumer(smart_contract) 37 async_processor.add_observer(contract_consumer) 38 39 # Set the global processor 40 set_processor(async_processor) 41 42 @pay_per_call(price=10000) 43 def important_function(self, *args, **kwargs): 44 """ 45 This function costs 10000 wei per call. 46 """ 47 print(f"Executing important_function with args={args} and kwargs={kwargs}") 48 return "Success" 49 50 # --- Main Demo Logic --- 51 def main(): 52 """Main function to run the demo.""" 53 54 # 6. Use the monetized library 55 lib = MonetizedLibrary(HARDHAT_URL, CLIENT_PRIVATE_KEY) 56 print("\n--- Calling monetized function ---") 57 for i in range(5): 58 print(f"\nCall {i+1}:") 59 lib.important_function(i) 60 time.sleep(1) # Give some time for events to be processed 61 print("\n--- Demo complete ---") 62 63 if __name__ == "__main__": 64 main()