def test_sophia_validate_bytecode(compiler_fixture, chain_fixture): compiler = compiler_fixture.COMPILER account = chain_fixture.ALICE node_client = chain_fixture.NODE_CLI sourcecode = """contract SimpleStorage = record state = { data : int } entrypoint init(value : int) : state = { data = value } entrypoint get() : int = state.data stateful entrypoint set(value : int) = put(state{data = value})""" contract_native = ContractNative(client=node_client, source=sourcecode, compiler=compiler, account=account) contract_native.deploy(12) chain_bytecode = node_client.get_contract_code( pubkey=contract_native.address).bytecode result = compiler.validate_bytecode(sourcecode, chain_bytecode) assert result == {} sourcecode_identity = """contract Identity = entrypoint main(x : int) = x entrypoint mainString(x : string) = x""" try: result = compiler.validate_bytecode(sourcecode_identity, chain_bytecode) raise RuntimeError("Method call should fail") except Exception as e: assert str(e) == 'Invalid contract'
def test_contract_native_compiler_init_str(chain_fixture, compiler_fixture): identity_contract = "contract Identity =\n entrypoint main(x : int) = x" account = chain_fixture.ALICE contract_native = ContractNative( client=chain_fixture.NODE_CLI, source=identity_contract, compiler=compiler_fixture.COMPILER.compiler_url, account=account) contract_native.deploy() assert (contract_native.address is not None)
def test_contract_native_without_default_account(compiler_fixture, chain_fixture): identity_contract = "contract Identity =\n entrypoint main(x : int) = x" compiler = compiler_fixture.COMPILER contract_native = ContractNative(client=chain_fixture.NODE_CLI, source=identity_contract, compiler=compiler) contract_native.deploy(account=chain_fixture.ALICE) assert (contract_native.address is not None) call_info, call_result = contract_native.main(12, account=chain_fixture.BOB) assert (call_result == 12)
def test_node_contract_signature_delegation(compiler_fixture, chain_fixture): compiler = compiler_fixture.COMPILER account = chain_fixture.ALICE bob = chain_fixture.BOB contract_native = ContractNative(client=chain_fixture.NODE_CLI, source=contractAens, compiler=compiler, account=account) contract_native.deploy() assert (contract_native.address is not None) # the contract_id contract_id = contract_native.address # node client ae_cli = contract_native.client # example name name = random_domain(length=15) c_id, salt = hashing.commitment_id(name, 9876) name_ttl = 500000 client_ttl = 36000 name_fee = utils.amount_to_aettos("20AE") print(f"name is {name}, commitment_id: {c_id}") # aens calls signature = ae_cli.delegate_name_preclaim_signature(account, contract_id) call, r = contract_native.signedPreclaim(account.get_address(), c_id, signature) assert (call.return_type == 'ok') ae_cli.wait_for_confirmation(call.tx_hash) signature = ae_cli.delegate_name_claim_signature(account, contract_id, name) call, _ = contract_native.signedClaim(account.get_address(), name, salt, name_fee, signature) assert (call.return_type == 'ok') signature = ae_cli.delegate_name_transfer_signature( account, contract_id, name) call, _ = contract_native.signedTransfer(account.get_address(), bob.get_address(), name, signature) assert (call.return_type == 'ok') signature = ae_cli.delegate_name_revoke_signature(bob, contract_id, name) call, _ = contract_native.signedRevoke(bob.get_address(), name, signature) assert (call.return_type == 'ok')
def test_contract_native_without_dry_run(compiler_fixture, chain_fixture): identity_contract = "contract Identity =\n entrypoint main(x : int) = x" compiler = compiler_fixture.COMPILER account = chain_fixture.ALICE contract_native = ContractNative(client=chain_fixture.NODE_CLI, source=identity_contract, compiler=compiler, account=account) contract_native.deploy() assert (contract_native.address is not None) call_info, call_result = contract_native.main(12) assert (call_result == 12 and not hasattr(call_info, 'tx_hash')) call_info, call_result = contract_native.main(12, use_dry_run=False) assert (call_result == 12 and hasattr(call_info, 'tx_hash'))
def test_contract_native_verify_contract_id(compiler_fixture, chain_fixture): identity_contract = "contract Identity =\n entrypoint main(x : int) = x" compiler = compiler_fixture.COMPILER account = chain_fixture.ALICE contract_native = ContractNative(client=chain_fixture.NODE_CLI, source=identity_contract, compiler=compiler, account=account) contract_native.deploy() contract_native.at(contract_native.address) INVALID_ADDRESS = 'ct_M9yohHgcLjhpp1Z8SaA1UTmRMQzR4FWjJHajGga8KBoZTEPwC' try: contract_native.at(INVALID_ADDRESS) except Exception as e: assert str(e) == 'Contract not deployed'
def test_contract_native_default_account_overriding(compiler_fixture, chain_fixture): identity_contract = "contract Identity =\n entrypoint main(x : int) = x" compiler = compiler_fixture.COMPILER account = chain_fixture.ALICE contract_native = ContractNative(client=chain_fixture.NODE_CLI, source=identity_contract, compiler=compiler, account=account) contract_native.deploy() assert (contract_native.address is not None) call_info, _ = contract_native.main(12) assert (call_info.caller_id == chain_fixture.ALICE.get_address()) call_info, _ = contract_native.main(12, account=chain_fixture.BOB) assert (call_info.caller_id == chain_fixture.BOB.get_address()) contract_native.set_account(chain_fixture.BOB) call_info, _ = contract_native.main(12) assert (call_info.caller_id == chain_fixture.BOB.get_address())
def test_contract_native_without_default_account(compiler_fixture, chain_fixture): identity_contract = "contract Identity =\n entrypoint main(x : int) = x" compiler = compiler_fixture.COMPILER contract_native = ContractNative(client=chain_fixture.NODE_CLI, source=identity_contract, compiler=compiler) contract_native.deploy(account=chain_fixture.ALICE) assert (contract_native.address is not None) call_info, call_result = contract_native.main(12, account=chain_fixture.BOB) assert (call_result == 12) try: _, call_result = contract_native.main(12) raise ValueError("Method call should fail") except Exception as e: assert ( str(e) == "Please provide an account to sign contract call transactions. You can set a default account using 'set_account' method" )
def test_contract_native_verify_contract_id(compiler_fixture, chain_fixture): identity_contract = "contract Identity =\n entrypoint main(x : int) = x" identity_contract_2 = """contract Identity = entrypoint main(x : int) = x entrypoint mainString(x : string) = x""" compiler = compiler_fixture.COMPILER account = chain_fixture.ALICE contract_native = ContractNative(client=chain_fixture.NODE_CLI, source=identity_contract, compiler=compiler, account=account) contract_native.deploy() contract_native.at(contract_native.address) contract_native_2 = ContractNative(client=chain_fixture.NODE_CLI, source=identity_contract_2, compiler=compiler, account=account) contract_native_2.deploy() INVALID_ADDRESS = 'ct_M9yohHgcLjhpp1Z8SaA1UTmRMQzR4FWjJHajGga8KBoZTEPwC' try: contract_native_2.at(contract_native.address) raise RuntimeError("Method call should fail") except Exception as e: assert str(e) == 'Invalid contract' try: contract_native.at(INVALID_ADDRESS) raise RuntimeError("Method call should fail") except Exception as e: assert str(e) == 'Contract not deployed' try: contract_native.at('ak_11111') raise RuntimeError("Method call should fail") except Exception as e: assert str(e) == 'Invalid contract address ak_11111'
def test_contract_native_full(compiler_fixture, chain_fixture): compiler = compiler_fixture.COMPILER account = chain_fixture.ALICE contract_native = ContractNative(client=chain_fixture.NODE_CLI, source=contract, compiler=compiler, account=account) contract_native.deploy("abcd", 12, "A", {"key": "value"}) assert (contract_native.address is not None) _, call_result = contract_native.intFn(12) assert (call_result == 12) _, call_result = contract_native.stringFn("test_call") assert (call_result == 'test_call') _, call_result = contract_native.addressFn( chain_fixture.ALICE.get_address()) assert (call_result == chain_fixture.ALICE.get_address()) _, call_result = contract_native.boolFn(False) assert (call_result == False) _, call_result = contract_native.tupleFn(["test", 1]) assert (call_result == ["test", 1]) _, call_result = contract_native.listFn([1, 2, 3, 4, 5]) assert (call_result == [1, 2, 3, 4, 5]) _, call_result = contract_native.mapFn({ "ak_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi": ["test", 12] }) assert (call_result == { 'ak_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi': ['test', 12] }) _, call_result = contract_native.intOption(12) assert (call_result == 12) _, call_result = contract_native.setRecord({ "value": "test1", "key": 12, "testOption": "test2", "testmap": { "key1": "value1" } }) assert (call_result == []) _, call_result = contract_native.getRecord() assert (call_result == { 'key': 12, 'testOption': 'test2', 'value': 'test1', "testmap": { "key1": "value1" } }) _, call_result = contract_native.retrieve() assert (call_result == ['test1', 12]) _, call_result = contract_native.datTypeFn({"Year": []}) assert (call_result == 'Year') try: call_info = contract_native.intOption(12, 13) raise RuntimeError("Method call should fail") except Exception as e: expected_error_message = "Invalid number of arguments. Expected 1, Provided 2" assert (str(e) == expected_error_message) try: call_info, call_result = contract_native.cause_error_require() raise RuntimeError("Method call should fail") except Exception as e: expected_error_message = "Error occurred while executing the contract method. Error Type: abort. Error Message: require failed" assert str(e) == expected_error_message try: call_info, call_result = contract_native.cause_error_abort( use_dry_run=False) raise RuntimeError("Method call should fail") except Exception as e: expected_error_message = "Error occurred while executing the contract method. Error Type: abort. Error Message: triggered abort" assert str(e) == expected_error_message try: call_info, call_result = contract_native.spend_all() raise RuntimeError("Method call should fail") except Exception as e: expected_error_message = "Error occurred while executing the contract method. Error Type: error. Error Message: " assert str(e) == expected_error_message try: call_info, call_result = contract_native.try_switch('name1') raise RuntimeError("Method call should fail") except Exception as e: expected_error_message = "Error occurred while executing the contract method. Error Type: abort. Error Message: name not found" assert str(e) == expected_error_message
def test_contract_native(compiler_fixture, chain_fixture): compiler = compiler_fixture.COMPILER account = chain_fixture.ALICE contract_native = ContractNative(client=chain_fixture.NODE_CLI, source=contract, compiler=compiler, account=account) contract_native.deploy("abcd", 12, "A") assert (contract_native.address is not None) _, call_result = contract_native.intFn(12) assert (call_result == 12) _, call_result = contract_native.stringFn("test_call") assert (call_result == 'test_call') _, call_result = contract_native.addressFn( chain_fixture.ALICE.get_address()) assert (call_result == chain_fixture.ALICE.get_address()) _, call_result = contract_native.boolFn(False) assert (call_result == False) _, call_result = contract_native.tupleFn(["test", 1]) assert (call_result == ["test", 1]) _, call_result = contract_native.listFn([1, 2, 3, 4, 5]) assert (call_result == [1, 2, 3, 4, 5]) _, call_result = contract_native.mapFn({ "ak_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi": ["test", 12] }) assert (call_result == { 'ak_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi': ['test', 12] }) _, call_result = contract_native.intOption(12) assert (call_result == 12) _, call_result = contract_native.setRecord({ "value": "test1", "key": 12, "testOption": "test2" }) assert (call_result == []) _, call_result = contract_native.getRecord() assert (call_result == { 'key': 12, 'testOption': 'test2', 'value': 'test1' }) _, call_result = contract_native.retrieve() assert (call_result == ['test1', 12]) _, call_result = contract_native.datTypeFn({"Year": []}) assert (call_result == {'Year': []}) try: call_info = contract_native.intOption(12, 13) raise ValueError("Method call should fail") except Exception as e: expected_error_message = "Invalid number of arguments. Expected 1, Provided 2" assert (str(e) == expected_error_message)
def test_example_contract_native(chain_fixture): NODE_URL = os.environ.get('TEST_URL', 'http://127.0.0.1:3013') NODE_INTERNAL_URL = os.environ.get('TEST_DEBUG_URL', 'http://127.0.0.1:3113') COMPILER_URL = os.environ.get('TEST_COMPILER__URL', 'https://compiler.aepps.com') node_cli = NodeClient( Config( external_url=NODE_URL, internal_url=NODE_INTERNAL_URL, blocking_mode=True, )) compiler = CompilerClient(compiler_url=COMPILER_URL) sender_account = chain_fixture.ALICE # genrate ALICE account (and transfer AE to alice account) alice = Account.generate() node_cli.spend(sender_account, alice.get_address(), 5000000000000000000) CONTRACT_FILE = os.path.join(os.path.dirname(__file__), "testdata/CryptoHamster.aes") # read contract file with open(CONTRACT_FILE, 'r') as file: crypto_hamster_contract = file.read() """ Initialize the contract instance Note: To disable use of dry-run endpoint add the parameter use_dry_run=False """ crypto_hamster = ContractNative(client=node_cli, compiler=compiler, account=alice, source=crypto_hamster_contract) # deploy the contract # (also pass the args of thr init function - if any) tx = crypto_hamster.deploy() print(f"contract address: {crypto_hamster.address}") # PART 2 Call the contract CONTRACT_ID = crypto_hamster.address # CONTRACT_ID is the address of the deployed contract crypto_hamster.at(CONTRACT_ID) # call the contract method (stateful) tx_info, tx_result = crypto_hamster.add_test_value(1, 2) print(f"Transaction Hash: {tx_info.tx_hash}") print(f"Transaction Result/Return Data: {tx_result}") assert (tx_result == 3) assert (hasattr(tx_info, 'tx_hash')) # call contract method (not stateful) tx_info, tx_result = crypto_hamster.get_hamster_dna( "SuperCryptoHamster", None) print(f"Transaction Result/Return Data: {tx_result}") assert tx_result is not None assert not hasattr(tx_info, 'tx_hash')