示例#1
0
文件: vendor.py 项目: acid9reen/bas
def register_vendor(_w3: Web3, _name: str, _deposit: float):
    """
    Register new vendor

    :param Web3 _w3: Web3 instance
    :param str _name: Vendor's name
    :param float _deposit: Vendor's deposit in eth
    :return: Vendor id or error message
    :rtype: str
    """

    mgmt_contract = utils.init_management_contract(_w3)
    tx = TX_TEMPLATE

    if _w3.eth.getBalance(tx['from']) < _deposit:
        return "Failed. No enough funds to deposit."

    if _w3.eth.getBalance(tx['from']) + _deposit < get_fee(_w3) * 1000:
        return "Failed. Not enough funds to register"

    else:
        try:
            tx['value'] = _deposit
            tx_hash = mgmt_contract.functions.registerVendor(
                _name.encode()).transact(tx)
            receipt = web3.eth.wait_for_transaction_receipt(
                _w3, tx_hash, 120, 0.1)

            if receipt.status == 1:
                return mgmt_contract.events.Vendor().processReceipt(
                    receipt, errors=DISCARD)[0]['args']['tokenId']

        except ValueError:
            return "Failed. The vendor name is not unique."
示例#2
0
def set_fee(_w3: Web3, _fee: float):
    """
    Set new fee for battery registraton

    :param Web3 _w3: Web3 instance
    :param float _fee: New fee to set
    :return: Status message
    :rtype: str
    """

    data = utils.open_data_base(ACCOUNT_DB_NAME)
    actor = data['account']

    tx = {'from': actor, 'gasPrice': utils.get_actual_gas_price(_w3)}

    mgmt_contract = utils.init_management_contract(_w3)

    utils.unlock_account(_w3, actor, data['password'])

    tx_hash = mgmt_contract.functions.setFee(_w3.toWei(_fee, 'ether')).transact(tx)
    receipt = web3.eth.wait_for_transaction_receipt(_w3, tx_hash, 120, 0.1)
    result = receipt.status

    if result == 1:
        return f"{bcolors.OKGREEN}Fee setting was successfull{bcolors.ENDC}"
    else:
        return f"{bcolors.FAIL}Fee setting failed{bcolors.ENDC}"
示例#3
0
文件: vendor.py 项目: acid9reen/bas
def get_fee(_w3: Web3) -> float:
    """
    Get registration fee from managmentContract

    :param Web3_w3: Web3 instance
    :return: Service fee
    :rtype: float
    """

    mgmt_contract = utils.init_management_contract(_w3)

    fee = mgmt_contract.functions.getFee().call()

    return _w3.fromWei(fee, 'ether')
示例#4
0
文件: vendor.py 项目: acid9reen/bas
def get_deposit(_w3: Web3):
    """
    Get account deposit

    :param Web3 _w3: Web3 instance
    :return: Vendor's deposit in ether
    :rtype: float
    """

    data = utils.open_data_base(ACCOUNT_DB_NAME)
    actor = data['account']

    mgmt_contract = utils.init_management_contract(_w3)

    try:
        deposit = mgmt_contract.functions.getDeposit().call({'from': actor})

        return _w3.fromWei(deposit, 'ether')

    except:
        sys.exit(f"{bcolors.FAIL}The vendor doesn't exist{bcolors.ENDC}")
示例#5
0
def register_car(_w3: Web3):
    """
    Register new car

    :param Web3 _w3: Web3 instance
    """

    data = utils.open_data_base(MGMT_CONTRACT_DB_NAME)

    if data is None:
        return f'{bcolors.FAIL}Cannot access management contract database{bcolors.ENDC}'

    data = CONFIG

    if data is None:
        return f'{bcolors.FAIL}Cannot access account database{bcolors.ENDC}'

    private_key = data['key']
    mgmt_contract = utils.init_management_contract(_w3)
    car_address = _w3.eth.account.privateKeyToAccount(private_key).address
    registration_required_gas = 50000
    gas_price = utils.get_actual_gas_price(_w3)

    if registration_required_gas * gas_price > _w3.eth.getBalance(car_address):
        return 'No enough funds to send transaction'

    nonce = _w3.eth.getTransactionCount(car_address)
    tx = {'gasPrice': gas_price, 'nonce': nonce}

    regTx = mgmt_contract.functions.registerCar().buildTransaction(tx)
    signTx = _w3.eth.account.signTransaction(regTx, private_key)
    txHash = _w3.eth.sendRawTransaction(signTx.rawTransaction)
    receipt = web3.eth.wait_for_transaction_receipt(_w3, txHash, 120, 0.1)

    if receipt.status == 1:
        return f'{bcolors.OKGREEN}Registered successfully{bcolors.ENDC}'
    else:
        return f'{bcolors.FAIL}Car registration failed{bcolors.ENDC}'
示例#6
0
文件: vendor.py 项目: acid9reen/bas
def register_battery(_w3: Web3, _count: int, _value: float = 0):
    """
    Register battery

    :param Web3 _w3: Web3 instance
    :param int _count: Number of batteries
    :param float _value: Deposit in eth
    :return: Batteries ids or error message
    :rtype: str
    """

    tx = TX_TEMPLATE
    tx['value'] = _value
    bat_keys = []
    args = []

    for i in range(_count):
        priv_key = _w3.sha3(os.urandom(20))
        bat_keys.append(
            (priv_key, _w3.eth.account.privateKeyToAccount(priv_key).address))

    for i in range(len(bat_keys)):
        args.append(_w3.toBytes(hexstr=bat_keys[i][1]))

    mgmt_contract = utils.init_management_contract(_w3)
    txHash = mgmt_contract.functions.registerBatteries(args).transact(tx)
    receipt = web3.eth.wait_for_transaction_receipt(_w3, txHash, 120, 0.1)
    result = receipt.status

    if result >= 1:
        for battery in bat_keys:
            utils.create_script_from_tmpl(
                hex(int.from_bytes(battery[0], byteorder='big')), battery[1])

        return [x[1] for x in bat_keys]

    else:
        sys.exit('Batteries registration failed')
示例#7
0
def register_scenter(_w3: Web3):
    """
    Register new service center

    :param Web3 _w3: Web3 instance
    :return: Registration status message
    :rtype: str
    """

    mgmt_contract = utils.init_management_contract(_w3)
    data = utils.open_data_base(ACCOUNT_DB_NAME)

    if data is None:
        sys.exit(f"{bcolors.FAIL}Cannot access account database{bcolors.ENDC}")

    actor = data['account']

    tx = {'from': actor, 'gasPrice': utils.get_actual_gas_price(_w3)}

    if REGISTRATION_REQUIRED_GAS * tx['gasPrice'] > _w3.eth.getBalance(actor):
        sys.exit(
            f"{bcolors.FAIL}No enough funds to send transaction{bcolors.ENDC}")

    utils.unlock_account(_w3, actor, data['password'])

    try:
        tx_hash = mgmt_contract.functions.registerServiceCenter().transact(tx)
    except ValueError:
        sys.exit(f"{bcolors.FAIL}Already registered{bcolors.ENDC}")

    receipt = web3.eth.wait_for_transaction_receipt(_w3, tx_hash, 120, 0.1)

    if receipt.status == 1:
        return f"{bcolors.OKGREEN}Registered successfully{bcolors.ENDC}"

    else:
        return f"{bcolors.FAIL}Registration failed{bcolors.ENDC}"