def chain_fixture(scope="module"): # create a new account and fill it with some money ACCOUNT = Account.generate() ACCOUNT_1 = Account.generate() # used by for oracles # set the key folder as environment variables genesis = Account.from_private_key_string(PRIVATE_KEY) # Instantiate the node client for the tests NODE_CLI = NodeClient(Config( external_url=NODE_URL, internal_url=NODE_URL_DEBUG, # network_id=NETWORK_ID, blocking_mode=True, debug=True, )) NODE_CLI.spend(genesis, ACCOUNT.get_address(), 5000000000000000000000) # 5000AE a = NODE_CLI.get_account_by_pubkey(pubkey=ACCOUNT.get_address()) print(f"Test account is {ACCOUNT.get_address()} with balance {a.balance}") NODE_CLI.spend(genesis, ACCOUNT_1.get_address(), 5000000000000000000000) # 5000AE a = NODE_CLI.get_account_by_pubkey(pubkey=ACCOUNT_1.get_address()) print(f"Test account (1) is {ACCOUNT_1.get_address()} with balance {a.balance}") return namedtupled.map({"NODE_CLI": NODE_CLI, "ALICE": ACCOUNT, "BOB": ACCOUNT_1}, _nt_name="TestData")
def client_fixture(scope="module"): # Instantiate the node client for the tests NODE_CLI = NodeClient(Config( external_url=NODE_URL, internal_url=NODE_URL_DEBUG, # network_id=NETWORK_ID, blocking_mode=True, debug=True, )) return namedtupled.map({"NODE_CLI": NODE_CLI}, _nt_name="TestData")
def test_tutorial_spend(chain_fixture): NODE_URL = os.environ.get('TEST_URL', 'https://testnet.aeternity.io') node_cli = NodeClient(Config( external_url=NODE_URL, blocking_mode=True, )) # generate ALICE account alice = Account.generate() # generate BOB account bob = Account.generate() # retrieve the balances for the accounts bob_balance = node_cli.get_balance(bob) alice_balance = node_cli.get_balance(alice) # print the balance of the two accounts print(f"Alice address is {alice.get_address()}") print(f"with balance {utils.format_amount(alice_balance)}") print(f"Bob address is {bob.get_address()}") print(f"with balance {utils.format_amount(bob_balance)}") # begin - tests execution section # top up the account from the test suite account, # outside the tests use the faucet to top_up an account tx = node_cli.spend(chain_fixture.ALICE, alice.get_address(), "5AE") # end - tests execution section #TODO pause the execution while using the faucet # execute the spend transaction tx = node_cli.spend(alice, bob.get_address(), "3AE") print(f"transaction hash: {tx.hash}") print(f"inspect transaction at {NODE_URL}/v2/transactions/{tx.hash}") # begin - tests execution section assert bob.get_address() == tx.data.tx.data.recipient_id assert alice.get_address() == tx.data.tx.data.sender_id # end - tests execution section # retrieve the balances for the accounts bob_balance = node_cli.get_balance(bob) alice_balance = node_cli.get_balance(alice) print(f"Alice balance is {utils.format_amount(alice_balance)}") print(f"Bob balance is {utils.format_amount(bob_balance)}") # begin - tests execution section assert bob_balance > 0 assert alice_balance > 0 assert alice_balance < bob_balance
def client_fixture(scope="module"): # Instantiate the node client for the tests fc = True if FORCE_COMPATIBILITY == "true" else False NODE_CLI = NodeClient(Config( external_url=NODE_URL, internal_url=NODE_URL_DEBUG, # network_id=NETWORK_ID, blocking_mode=True, debug=True, force_compatibility=fc, )) return Munch.fromDict({"NODE_CLI": NODE_CLI})
def _node_cli(network_id=None): try: ctx = click.get_current_context() # set the default configuration cfg = Config(external_url=ctx.obj.get(CTX_NODE_URL), internal_url=ctx.obj.get(CTX_NODE_URL_DEBUG), websocket_url=ctx.obj.get(CTX_NODE_WS), force_compatibility=ctx.obj.get(CTX_FORCE_COMPATIBILITY), blocking_mode=ctx.obj.get(CTX_BLOCKING_MODE), network_id=network_id) # load the aeternity node client return NodeClient(cfg) except exceptions.ConfigException as e: _print_error(e, title="configuration error", exit_code=1) except exceptions.UnsupportedNodeVersion as e: _print_error(e, exit_code=1)
def test_tutorial_offline_tx(chain_fixture): # Accounts addresses account = Account.generate() # --- hide --- override the account for tests account = chain_fixture.ALICE # /--- hide --- # instantiate the transactions builder build = TxBuilder() # we will be creating 5 transactions for later broadcast TODO: warn about the nonce limit txs = [] # each transaction is going to be a spend amount = utils.amount_to_aettos("0.05AE") payload = b'' for i in range(5): # increase the account nonce account.nonce = account.nonce + 1 # build the transaction tx = build.tx_spend( account.get_address(), # sender Account.generate().get_address(), # random generated recipient amount, payload, defaults.FEE, defaults.TX_TTL, account.nonce) # save the transaction txs.append(tx) # Sign the transactions # define the network_id network_id = identifiers.NETWORK_ID_TESTNET # --- hide --- override the network_id for tests network_id = chain_fixture.NODE_CLI.config.network_id # /--- hide --- # instantiate a transaction signer signer = TxSigner(account, network_id) # collect the signed tx for broadcast signed_txs = [] # sign all transactions for tx in txs: signature = signer.sign_transaction(tx) signed_tx = build.tx_signed([signature], tx) signed_txs.append(signed_tx) # Broadcast the transactions NODE_URL = os.environ.get('TEST_URL', 'https://testnet.aeternity.io') node_cli = NodeClient(Config( external_url=NODE_URL, blocking_mode=False, )) # broadcast all transactions for stx in signed_txs: node_cli.broadcast_transaction(stx) # verify that all transactions have been posted for stx in signed_txs: height = node_cli.wait_for_transaction(stx) assert (height > 0)
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')