Testing Intelligent Contracts on GenLayer
Testing Intelligent Contracts on GenLayer involves deploying contracts, sending transactions, validating their behavior, and identifying issues. Here is some guidance on how to test using the tools provided in the local development environment and GenLayer Studio.
1. Testing in the Local Environment
For a more advanced and controlled testing setup, you can leverage the GenLayer Project Boilerplate and the provided test helpers.
Please note that you need the Studio running to run the tests by sending requests to the it.
Testing Workflow
1. Create Accounts
Use the create_new_account
helper to generate accounts for testing. These accounts simulate users interacting with the contract.
from tools.request import create_new_account
account = create_new_account()
2. Set Up Validators
Validators are essential for processing transactions in GenLayer. Use the sim_createRandomValidators
method to initialize them.
from tools.request import payload, post_request_localhost
validators_response = post_request_localhost(
payload("sim_createRandomValidators", 5, 8, 12, ["openai"], ["gpt-4o"])
).json()
3. Deploy the Contract
Deploy your Intelligent Contract using the deploy_intelligent_contract
helper:
from tools.request import deploy_intelligent_contract
contract_code = open("contracts/my_contract.py", "r").read()
contract_address, deploy_response = deploy_intelligent_contract(account, contract_code, "{}")
4. Interact with the Contract
Use send_transaction
for writing data to the contract and call_contract_method
for reading data:
from tools.request import send_transaction, call_contract_method
# Write data
send_transaction(account, contract_address, "method_name", [arg1, arg2])
# Read data
result = call_contract_method(contract_address, account, "get_state", [])
5. Assertions
Validate the responses using provided assertion helpers:
from tools.response import assert_dict_struct, has_success_status
assert has_success_status(result)
assert_dict_struct(result, expected_structure)
6. Clean Up
After completing the tests, delete the validators or reset the environment:
delete_response = post_request_localhost(payload("sim_deleteAllValidators")).json()