Пример #1
0
def function_call_set_delete_state(account, i, node_account):
    assert i < len(function_call_state)

    if not function_call_state[i]:
        action = "add"
    elif len(function_call_state[i]) >= 100:
        action = "delete"
    else:
        action = random.choice(["add", "delete"])

    if action == "add":
        next_id = random.randrange(mocknet.NUM_ACCOUNTS)
        next_val = random.randint(0, 1000)
        next_account_id = mocknet.load_testing_account_id(
            node_account.key.account_id, next_id)
        s = f'{{"account_id": "account_{next_val}", "message":"{next_val}"}}'
        logger.info(
            f'Calling function "set_state" of account {next_account_id} with arguments {s} from account {account.key.account_id}'
        )
        tx_res = mocknet_helpers.retry_and_ignore_errors(
            lambda: account.send_call_contract_raw_tx(
                next_account_id, 'set_state', s.encode('utf-8'), 0))
        logger.info(
            f'{account.key.account_id} set_state on {next_account_id} {tx_res}')
        function_call_state[i].append((next_id, next_val))
    else:
        assert function_call_state[i]
        item = random.choice(function_call_state[i])
        (next_id, next_val) = item
        next_account_id = mocknet.load_testing_account_id(
            node_account.key.account_id, next_id)
        s = f'{{"account_id": "account_{next_val}"}}'
        logger.info(
            f'Calling function "delete_state" of account {next_account_id} with arguments {s} from account {account.key.account_id}'
        )
        tx_res = mocknet_helpers.retry_and_ignore_errors(
            lambda: account.send_call_contract_raw_tx(
                next_account_id, 'delete_state', s.encode('utf-8'), 0))
        logger.info(
            f'{account.key.account_id} delete_state on {next_account_id} {tx_res}'
        )
        if item in function_call_state[i]:
            function_call_state[i].remove(item)
            logger.info(
                f'Successfully removed {item} from function_call_state. New #items: {len(function_call_state[i])}'
            )
        else:
            logger.info(
                f'{item} is not in function_call_state even though this is impossible. #Items: {len(function_call_state[i])}'
            )
def init_token2_account(account, i):
    s = f'{{"account_id": "{account.key.account_id}"}}'
    tx_res = mocknet_helpers.retry_and_ignore_errors(
        lambda: account.send_call_contract_raw_tx(
            mocknet.TOKEN2_ACCOUNT, 'storage_deposit', bytes(s,
                                                             encoding='utf-8'),
            1250000000000000000000))  # 0.00125 * 1e24)
    logger.info(f'Account {account.key.account_id} storage_deposit {tx_res}')

    s = f'{{"token_account_id": "{mocknet.TOKEN2_ACCOUNT}"}}'
    logger.info(
        f'Calling function "register_token" with arguments {s} on account {i}')
    tx_res = mocknet_helpers.retry_and_ignore_errors(
        lambda: account.
        send_call_contract_raw_tx(mocknet.SKYWARD_ACCOUNT, 'register_token',
                                  bytes(s, encoding='utf-8'), 0.01 * 1e24))
    logger.info(
        f'Account {account.key.account_id} register_token token2 {tx_res}')

    # The next transaction depends on the previous transaction succeeded.
    # Sleeping for 1 second is the poor man's solution for waiting for that transaction to succeed.
    # This works because the contracts are being deployed slow enough to keep block production above 1 bps.
    mocknet_helpers.wait_at_least_one_block()

    s = f'{{"receiver_id": "{account.key.account_id}", "amount": "1000000000000000000"}}'
    logger.info(
        f'Calling function "ft_transfer" with arguments {s} on account {i}')
    tx_res = mocknet_helpers.retry_and_ignore_errors(
        get_token2_owner_account().send_call_contract_raw_tx(
            mocknet.TOKEN2_ACCOUNT, 'ft_transfer', bytes(s, encoding='utf-8'),
            1))
    logger.info(
        f'{get_token2_owner_account().key.account_id} ft_transfer to {account.key.account_id} {tx_res}'
    )
def function_call(account, i, node_account):
    if random.randint(0, 1) == 0:
        s = f'{{"token_account_id": "token2.near"}}'
        logger.info(
            f'Calling function "withdraw_token" with arguments {s} on account {i}'
        )
        tx_res = mocknet_helpers.retry_and_ignore_errors(
            lambda: account.send_call_contract_raw_tx(
                mocknet.SKYWARD_ACCOUNT, 'withdraw_token',
                bytes(s, encoding='utf-8'), 0))
        logger.info(f'Account {account.key.account_id} withdraw_token {tx_res}')
    else:
        s = '{"sale_id": 0, "amount": "1"}'
        logger.info(
            f'Calling function "sale_deposit_in_token" with arguments {s} on account {i}'
        )
        tx_res = mocknet_helpers.retry_and_ignore_errors(
            lambda: account.send_call_contract_raw_tx(
                mocknet.SKYWARD_ACCOUNT, 'sale_deposit_in_token',
                bytes(s, encoding='utf-8'), 1))
        logger.info(
            f'Account {account.key.account_id} sale_deposit_in_token {tx_res}')
Пример #4
0
def function_call_ft_transfer_call(account, i, node_account):
    next_id = random.randint(0, mocknet.NUM_ACCOUNTS - 1)
    dest_account_id = mocknet.load_testing_account_id(
        node_account.key.account_id, next_id)

    s = f'{{"receiver_id": "{dest_account_id}", "amount": "3", "msg": "\\"hi\\""}}'
    logger.info(
        f'Calling function "ft_transfer_call" with arguments {s} on account {account.key.account_id} contract {dest_account_id}'
    )
    tx_res = mocknet_helpers.retry_and_ignore_errors(
        lambda: account.send_call_contract_raw_tx(
            dest_account_id, 'ft_transfer_call', s.encode('utf-8'), 1))
    logger.info(
        f'{account.key.account_id} ft_transfer to {dest_account_id} {tx_res}')
Пример #5
0
def init_ft_account(node_account, account):
    s = f'{{"account_id": "{account.key.account_id}"}}'
    tx_res = mocknet_helpers.retry_and_ignore_errors(
        lambda: account.send_call_contract_raw_tx(
            node_account.key.account_id, 'storage_deposit', s.encode('utf-8'),
            (10**24) // 800))
    logger.info(f'Account {account.key.account_id} storage_deposit {tx_res}')

    # The next transaction depends on the previous transaction succeeded.
    # Sleeping for 1 second is the poor man's solution for waiting for that transaction to succeed.
    # This works because the contracts are being deployed slow enough to keep block production above 1 bps.
    mocknet_helpers.wait_at_least_one_block()

    s = f'{{"receiver_id": "{account.key.account_id}", "amount": "{10**18}"}}'
    logger.info(
        f'Calling function "ft_transfer" with arguments {s} on account {account.key.account_id}'
    )
    tx_res = mocknet_helpers.retry_and_ignore_errors(
        lambda: node_account.send_call_contract_raw_tx(
            node_account.key.account_id, 'ft_transfer', s.encode('utf-8'), 1))
    logger.info(
        f'{node_account.key.account_id} ft_transfer to {account.key.account_id} {tx_res}'
    )