def test_voucher_institution_get(client): res = client.get('/api/vouchers/institution') assert res._status_code == 200 assert len(res.json) == 2 assert res.json[0]["id"] == 1 assert res.json[0]["amount"] == 2 assert res.json[0]["institutionid"] == 1 assert res.json[0]["institutionName"] == "MSGraphic" assert res.json[0][ "subject"] == "Der Computer malt ein täuschend echtes Bild für sie" assert res.json[0]["title"] == "Von Computer gemaltes Bild" assert res.json[0]["validTime"] == 2 * 31536000 assert not res.json[0]["available"] assert res.json[0]["price"] == WEB3.toWei(0.01, 'ether') assert res.json[1]["id"] == 2 assert res.json[1]["amount"] == 2 assert res.json[1]["institutionid"] == 1 assert res.json[1]["institutionName"] == "MSGraphic" assert res.json[1]["subject"] == "Software für ein Hochschulprojet" assert res.json[1]["title"] == "Software" assert res.json[1]["validTime"] == 2 * 31536000 assert res.json[1]["available"] assert res.json[1]["price"] == WEB3.toWei(0.02, 'ether')
def test_donations_vote2(client_w_eth): headers = { "authToken": TOKEN_2, "idproject": 1, "amount": int(WEB3.toWei(0.02, 'ether')), "voteEnabled": 1 } res = client_w_eth.post('/api/donations', headers=headers) assert res._status_code == 201 headers = { "authToken": TOKEN_2, "idproject": 1, "amount": int(WEB3.toWei(0.02, 'ether')), "voteEnabled": 1 } res = client_w_eth.post('/api/donations', headers=headers) assert res._status_code == 201 headers = {"authToken": TOKEN_2, "id": 5, "vote": 1} res = client_w_eth.post('/api/donations/vote', headers=headers) assert res._status_code == 200 assert len(res.json) == 1 assert res.json["status"] == "ok" headers = {"authToken": TOKEN_2, "id": 6, "vote": 0} res = client_w_eth.post('/api/donations/vote', headers=headers) assert res._status_code == 400 assert len(res.json) == 1 assert res.json["error"] == "already voted"
def test_donations_post2(client_w_eth): test_user_post(client_w_eth) res = client_w_eth.get( '/api/users?username=sw2020testuser1337.id.blockstack') assert res._status_code == 200 WEB3.eth.sendTransaction({ 'from': WEB3.eth.accounts[9], 'to': res.json[0]["publickey"], 'value': 1 * 10**18 }) headers = { "authToken": TOKEN_3, "idproject": 1, "amount": int(WEB3.toWei(0.02, 'ether')), "voteEnabled": 1 } res = client_w_eth.post('/api/donations', headers=headers) assert res._status_code == 201 assert len(res.json) == 1 assert res.json["status"] == "Spende wurde verbucht" res = client_w_eth.get('/api/donations?iduser=8') assert res._status_code == 200 assert len(res.json) == 1 assert res.json[0]["id"] == 5 assert res.json[0]["amount"] == int(WEB3.toWei(0.02, 'ether')) assert res.json[0]["userid"] == 8 assert res.json[0]["milestoneid"] == 1
def test_donations_get(client): res = client.get('/api/donations') assert res._status_code == 200 assert len(res.json) == 4 assert res.json[0]["id"] == 1 assert res.json[0]["amount"] == WEB3.toWei(0.03, 'ether') assert res.json[0]["userid"] == 1 assert res.json[0]["milestoneid"] == 1 assert res.json[0]["projectid"] == 1 assert res.json[0]["projectname"] == "Computer malt Bild" assert res.json[1]["id"] == 2 assert res.json[1]["amount"] == WEB3.toWei(0.02, 'ether') assert res.json[1]["userid"] == 2 assert res.json[1]["milestoneid"] == 2 assert res.json[1]["projectid"] == 1 assert res.json[1]["projectname"] == "Computer malt Bild" assert res.json[2]["id"] == 3 assert res.json[2]["amount"] == WEB3.toWei(0.01, 'ether') assert res.json[2]["userid"] == 3 assert res.json[2]["milestoneid"] == 3 assert res.json[2]["projectid"] == 1 assert res.json[2]["projectname"] == "Computer malt Bild" assert res.json[3]["id"] == 4 assert res.json[3]["amount"] == WEB3.toWei(0.04, 'ether') assert res.json[3]["userid"] == 4 assert res.json[3]["milestoneid"] == 4 assert res.json[3]["projectid"] == 2 assert res.json[3]["projectname"] == "Rangaroek verteidigen"
def test_voucher_user_expired2(client): res = client.get('/api/vouchers/user?expired=0') assert res._status_code == 200 assert len(res.json) == 3 assert res.json[0]["id"] == 2 assert res.json[0]["userid"] == 2 assert res.json[0]["idvoucher"] == 2 assert res.json[0]["untilTime"] == datetime(2022, 5, 17).timestamp() assert not res.json[0]["used"] assert res.json[0]["price"] == WEB3.toWei(0.02, 'ether') assert res.json[1]["id"] == 3 assert res.json[1]["userid"] == 6 assert res.json[1]["idvoucher"] == 1 assert res.json[1]["untilTime"] == datetime(2022, 1, 13).timestamp() assert not res.json[1]["used"] assert res.json[1]["price"] == WEB3.toWei(0.01, 'ether') assert res.json[2]["id"] == 4 assert res.json[2]["userid"] == 7 assert res.json[2]["idvoucher"] == 2 assert res.json[2]["untilTime"] == datetime(2021, 5, 17).timestamp() assert res.json[2]["used"] assert res.json[2]["price"] == WEB3.toWei(0.02, 'ether')
def add_voucher(user: User, institution: Institution, description, expires): """ Creates a new voucher for the given user. :parameter user the user instance who will retrieve an voucher :parameter institution the institution instance which offer the voucher :parameter description a description for the voucher :parameter expires the period of validity in days :return: the index of the created voucher """ description = WEB3.toBytes(text=description) contract = WEB3.eth.contract(address=institution.scAddress, abi=INSTITUTION_JSON["abi"]) tx_hash = contract.functions.addVoucher(user.publickeyUser, description, expires).buildTransaction({ 'nonce': WEB3.eth.getTransactionCount(WEB3.eth.defaultAccount), 'from': WEB3.eth.defaultAccount }) # signed_tx = WEB3.eth.account.sign_transaction(tx_hash, private_key=master_key) # tx_raw = WEB3.eth.sendRawTransaction(signed_tx.rawTransaction) tx_raw = WEB3.eth.sendTransaction(tx_hash) tx_receipt = WEB3.eth.waitForTransactionReceipt(tx_raw) if tx_receipt.status != 1: raise RuntimeError("SC Call failed!") processed_receipt = contract.events.newVoucher().processReceipt(tx_receipt) index = processed_receipt[0].args._index # pylint:disable=protected-access print(index) return index
def test_voucher_user_get_idvoucher(client): res = client.get('/api/vouchers/user?idVoucher=2') assert res._status_code == 200 assert len(res.json) == 2 assert res.json[0]["id"] == 2 assert res.json[0]["userid"] == 2 assert res.json[0]["idvoucher"] == 2 assert res.json[0]["untilTime"] == datetime(2022, 5, 17).timestamp() assert not res.json[0]["used"] assert res.json[0]["price"] == WEB3.toWei(0.02, 'ether') assert res.json[1]["id"] == 4 assert res.json[1]["userid"] == 7 assert res.json[1]["idvoucher"] == 2 assert res.json[1]["untilTime"] == datetime(2021, 5, 17).timestamp() assert res.json[1]["used"] assert res.json[1]["price"] == WEB3.toWei(0.02, 'ether')
def voucher_post(session, user): """ Handles POST for resource <base>/api/voucher/user . :return: json data of projects """ id_voucher = request.headers.get('idVoucher') if not id_voucher: return jsonify({'error': 'missing id'}), 400 try: check_params_int([id_voucher]) except ValueError: return jsonify({"error": "bad argument"}), 400 try: voucher = session.query(Voucher).filter( Voucher.idVoucher == id_voucher).one() balance = WEB3.eth.getBalance(user.publickeyUser) if balance < int(voucher.priceVoucher): # ToDo: gas-cost? return jsonify({'error': 'not enough balance'}), 406 if not voucher.available: return jsonify({'error': 'voucher not available'}), 406 association = VoucherUser( usedVoucher=False, expires_unixtime=(datetime.now() + timedelta(0, 2 * 31536000)), voucher=voucher, user=user) inst: Institution = session.query(Institution).filter( Institution.idInstitution == voucher.institution_id).one() transaction = { 'nonce': WEB3.eth.getTransactionCount(user.publickeyUser), 'to': inst.publickeyInstitution, 'value': int(voucher.priceVoucher), 'gas': 200000, 'gasPrice': WEB3.toWei('50', 'gwei') } signed_transaction = WEB3.eth.account.sign_transaction( transaction, user.privatekeyUser) WEB3.eth.sendRawTransaction(signed_transaction.rawTransaction) association.redeem_id = add_voucher( user, inst, voucher.titleVoucher, abs(association.expires_unixtime - datetime.now()).days) session.add(voucher) session.add(association) session.commit() except InvalidAddress: return jsonify({'error': 'given publickey is not valid'}), 400 except NoResultFound: return jsonify({'error': 'Voucher doesnt exist'}), 404 return jsonify({'status': 'voucher bought'}), 200
def project_constructor(owner: User, description: str, goal: int): projects_sc = WEB3.eth.contract(abi=PROJECT_JSON["abi"], bytecode=PROJECT_JSON["bytecode"]) description_bytes = WEB3.toBytes(text=str(description)) # constructor(_owner, _admin, _partial_payment, _projectTargetName, _projectTargetAmount, _minDonation) ctor = projects_sc.constructor(owner.publickeyUser, WEB3.eth.defaultAccount, 80, description_bytes, goal, int(WEB3.toWei(0.01, 'ether'))) tx_hash = ctor.buildTransaction({'nonce': WEB3.eth.getTransactionCount(owner.publickeyUser), 'from': owner.publickeyUser}) signed_tx = WEB3.eth.account.sign_transaction(tx_hash, private_key=owner.privatekeyUser) tx_hash = WEB3.eth.sendRawTransaction(signed_tx.rawTransaction) tx_receipt = WEB3.eth.waitForTransactionReceipt(tx_hash) if tx_receipt.status != 1: raise RuntimeError("SC Call failed!") return tx_receipt.contractAddress
def test_donations_get_w_minamount(client): res = client.get('/api/donations?minamount=40000000000000000') assert res._status_code == 200 assert len(res.json) == 1 assert res.json[0]["id"] == 4 assert res.json[0]["amount"] == WEB3.toWei(0.04, 'ether') assert res.json[0]["userid"] == 4 assert res.json[0]["milestoneid"] == 4 assert res.json[0]["projectid"] == 2 assert res.json[0]["projectname"] == "Rangaroek verteidigen"
def test_donations_get_w_maxamount(client): res = client.get('/api/donations?maxamount=10000000000000000') assert res._status_code == 200 assert len(res.json) == 1 assert res.json[0]["id"] == 3 assert res.json[0]["amount"] == WEB3.toWei(0.01, 'ether') assert res.json[0]["userid"] == 3 assert res.json[0]["milestoneid"] == 3 assert res.json[0]["projectid"] == 1 assert res.json[0]["projectname"] == "Computer malt Bild"
def test_voucher_user_expired(client): res = client.get('/api/vouchers/user?expired=1') assert res._status_code == 200 assert len(res.json) == 1 assert res.json[0]["id"] == 1 assert res.json[0]["userid"] == 1 assert res.json[0]["idvoucher"] == 1 assert res.json[0]["untilTime"] == datetime(2020, 1, 1).timestamp() assert not res.json[0]["used"] assert res.json[0]["price"] == WEB3.toWei(0.01, 'ether')
def test_projects_id_get_existant_param(client): """get for project id with existant id.""" res = client.get('/api/projects/1') assert res._status_code == 200 assert len(res.json) == 14 assert res.json["id"] == 1 assert res.json["idinstitution"] == 1 assert res.json["name"] == "Computer malt Bild" assert res.json["webpage"] == "http://www.cmb.de" assert res.json["address"] == "Address1" assert res.json["until"] == 1693094933 assert res.json["goal"] == WEB3.toWei(1, 'ether') assert len(res.json["milestones"]) == 4 assert res.json["milestones"][0]["id"] == 1 assert res.json["milestones"][0]["idProjekt"] == 1 assert res.json["milestones"][0]["goal"] == WEB3.toWei(0.1, 'ether') assert res.json["milestones"][0]["currentVotes"] == 112 assert res.json["milestones"][0]["until"] == 1693094933 assert res.json["milestones"][0]["totalDonated"] == WEB3.toWei( 0.03, 'ether') assert res.json["milestones"][1]["id"] == 2 assert res.json["milestones"][1]["idProjekt"] == 1 assert res.json["milestones"][1]["goal"] == WEB3.toWei(0.2, 'ether') assert res.json["milestones"][1]["currentVotes"] == 12 assert res.json["milestones"][1]["until"] == 1693094933 assert res.json["milestones"][1]["totalDonated"] == WEB3.toWei( 0.02, 'ether') assert res.json["milestones"][2]["id"] == 3 assert res.json["milestones"][2]["idProjekt"] == 1 assert res.json["milestones"][2]["goal"] == WEB3.toWei(0.3, 'ether') assert res.json["milestones"][2]["currentVotes"] == 0 assert res.json["milestones"][2]["until"] == 1693094933 assert res.json["milestones"][2]["totalDonated"] == WEB3.toWei( 0.01, 'ether') assert res.json["milestones"][3]["id"] == 7 assert res.json["milestones"][3]["idProjekt"] == 1 assert res.json["milestones"][3]["goal"] == WEB3.toWei(0.5, 'ether') assert res.json["milestones"][3]["currentVotes"] == 400 assert res.json["milestones"][3]["until"] == 1693094933 assert res.json["milestones"][3]["totalDonated"] == 0 assert res.json['totalDonated'] == \ res.json["milestones"][0]["totalDonated"] + \ res.json["milestones"][1]["totalDonated"] + \ res.json["milestones"][2]["totalDonated"] + \ res.json["milestones"][3]["totalDonated"]
def project_donate_check(project: Project, user_inst: User, amount: int, vote_enabled: bool) -> Optional[str]: reg_check = project_donate_register_check(project, user_inst) if reg_check: return reg_check # require(donors[msg.sender].exists); - cant check if vote_enabled: if not amount > int(WEB3.toWei(0.01, 'ether')): # require(msg.value >= minDonation); return "amount too low to vote" return None
def test_voucher_user_delete(client_w_eth): headers = {"authToken": TOKEN_1, "id": 3} res = client_w_eth.delete('/api/vouchers/user', headers=headers) assert res._status_code == 201 res = client_w_eth.get('/api/vouchers/user?used=0') assert res._status_code == 200 assert len(res.json) == 2 assert res.json[0]["id"] == 1 assert res.json[0]["userid"] == 1 assert res.json[0]["idvoucher"] == 1 assert res.json[0]["untilTime"] == datetime(2020, 1, 1).timestamp() assert not res.json[0]["used"] assert res.json[0]["price"] == WEB3.toWei(0.01, 'ether') assert res.json[1]["id"] == 2 assert res.json[1]["userid"] == 2 assert res.json[1]["idvoucher"] == 2 assert res.json[1]["untilTime"] == datetime(2022, 5, 17).timestamp() assert not res.json[1]["used"] assert res.json[1]["price"] == WEB3.toWei(0.02, 'ether') res = client_w_eth.get('/api/vouchers/user?used=1') assert res._status_code == 200 assert len(res.json) == 2 assert res.json[0]["id"] == 3 assert res.json[0]["userid"] == 6 assert res.json[0]["idvoucher"] == 1 assert res.json[0]["untilTime"] == datetime(2022, 1, 13).timestamp() assert res.json[0]["used"] assert res.json[0]["price"] == WEB3.toWei(0.01, 'ether') assert res.json[1]["id"] == 4 assert res.json[1]["userid"] == 7 assert res.json[1]["idvoucher"] == 2 assert res.json[1]["untilTime"] == datetime(2021, 5, 17).timestamp() assert res.json[1]["used"] assert res.json[1]["price"] == WEB3.toWei(0.02, 'ether')
def test_donations_post(client_w_eth): headers = { "authToken": TOKEN_2, "idproject": 1, "amount": int(WEB3.toWei(0.02, 'ether')), "voteEnabled": 1 } res = client_w_eth.post('/api/donations', headers=headers) assert res._status_code == 201 assert len(res.json) == 1 assert res.json["status"] == "Spende wurde verbucht" res = client_w_eth.get('/api/donations?iduser=7') assert res._status_code == 200 assert len(res.json) == 1 assert res.json[0]["id"] == 5 assert res.json[0]["amount"] == int(WEB3.toWei(0.02, 'ether')) assert res.json[0]["userid"] == 7 assert res.json[0]["milestoneid"] == 1
def test_voucher_institution_get_available(client): res = client.get('/api/vouchers/institution?available=1') assert res._status_code == 200 assert len(res.json) == 1 assert res.json[0]["id"] == 2 assert res.json[0]["amount"] == 2 assert res.json[0]["institutionid"] == 1 assert res.json[0]["subject"] == "Software für ein Hochschulprojet" assert res.json[0]["title"] == "Software" assert res.json[0]["validTime"] == 2 * 31536000 assert res.json[0]["available"] assert res.json[0]["price"] == WEB3.toWei(0.02, 'ether')
def test_projects_patch_wo_params(client): headers = {"authToken": TOKEN_1} res = client.patch('/api/projects/1', headers=headers) assert res._status_code == 201 assert res.json["status"] == "ok" res = client.get('/api/projects/1') assert res._status_code == 200 assert res.json["id"] == 1 assert res.json["idinstitution"] == 1 assert res.json["name"] == "Computer malt Bild" assert res.json["webpage"] == "http://www.cmb.de" assert len(res.json["milestones"]) == 4 assert res.json["milestones"][0]["id"] == 1 assert res.json["milestones"][0]["idProjekt"] == 1 assert res.json["milestones"][0]["goal"] == WEB3.toWei(0.1, 'ether') assert res.json["milestones"][0]["currentVotes"] == 112 assert res.json["milestones"][0]["until"] == 1693094933 assert res.json["milestones"][1]["id"] == 2 assert res.json["milestones"][1]["idProjekt"] == 1 assert res.json["milestones"][1]["goal"] == WEB3.toWei(0.2, 'ether') assert res.json["milestones"][1]["currentVotes"] == 12 assert res.json["milestones"][1]["until"] == 1693094933 assert res.json["milestones"][2]["id"] == 3 assert res.json["milestones"][2]["idProjekt"] == 1 assert res.json["milestones"][2]["goal"] == WEB3.toWei(0.3, 'ether') assert res.json["milestones"][2]["currentVotes"] == 0 assert res.json["milestones"][2]["until"] == 1693094933 assert res.json["milestones"][3]["id"] == 7 assert res.json["milestones"][3]["idProjekt"] == 1 assert res.json["milestones"][3]["goal"] == WEB3.toWei(0.5, 'ether') assert res.json["milestones"][3]["currentVotes"] == 400 assert res.json["milestones"][3]["until"] == 1693094933
def test_voucher_institution_get_available2(client): res = client.get('/api/vouchers/institution?available=0') assert res._status_code == 200 assert len(res.json) == 1 assert res.json[0]["id"] == 1 assert res.json[0]["amount"] == 2 assert res.json[0]["institutionid"] == 1 assert res.json[0][ "subject"] == "Der Computer malt ein täuschend echtes Bild für sie" assert res.json[0]["title"] == "Von Computer gemaltes Bild" assert res.json[0]["validTime"] == 2 * 31536000 assert not res.json[0]["available"] assert res.json[0]["price"] == WEB3.toWei(0.01, 'ether')
def test_voucher_user_delete_bad_user(client): headers = {"authToken": TOKEN_2, "id": 2} res = client.delete('/api/vouchers/user', headers=headers) assert res._status_code == 404 res = client.get('/api/vouchers/user?used=1') assert res._status_code == 200 assert len(res.json) == 1 assert res.json[0]["id"] == 4 assert res.json[0]["userid"] == 7 assert res.json[0]["idvoucher"] == 2 assert res.json[0]["untilTime"] == datetime(2021, 5, 17).timestamp() assert res.json[0]["used"] assert res.json[0]["price"] == WEB3.toWei(0.02, 'ether')
def test_voucher_user_post(client_w_eth): headers = {"authToken": TOKEN_2, "idVoucher": 2} res = client_w_eth.post('/api/vouchers/user', headers=headers) assert res._status_code == 200 res = client_w_eth.get('/api/vouchers/user?idUser=7') assert res._status_code == 200 assert len(res.json) == 2 assert res.json[0]["id"] == 4 assert res.json[0]["userid"] == 7 assert res.json[0]["idvoucher"] == 2 assert res.json[0]["untilTime"] == datetime(2021, 5, 17).timestamp() assert res.json[0]["used"] assert res.json[0]["price"] == WEB3.toWei(0.02, 'ether') assert res.json[1]["id"] == 5 assert res.json[1]["userid"] == 7 assert res.json[1]["idvoucher"] == 2 assert abs( int(res.json[1]["untilTime"]) - int((datetime.now() + timedelta(0, 2 * 31536000)).timestamp())) <= 3 assert not res.json[1]["used"] assert res.json[1]["price"] == WEB3.toWei(0.02, 'ether')
def test_voucher_user_post2(client_w_eth): headers = {"authToken": TOKEN_2, "idVoucher": 1} res = client_w_eth.post('/api/vouchers/user', headers=headers) assert res._status_code == 406 res = client_w_eth.get('/api/vouchers/user?idUser=7') assert res._status_code == 200 assert len(res.json) == 1 assert res.json[0]["id"] == 4 assert res.json[0]["userid"] == 7 assert res.json[0]["idvoucher"] == 2 assert res.json[0]["untilTime"] == datetime(2021, 5, 17).timestamp() assert res.json[0]["used"] assert res.json[0]["price"] == WEB3.toWei(0.02, 'ether')
def test_voucher_user_post_bad_voucherid(client_w_eth): headers = {"authToken": TOKEN_1, "idVoucher": 1337} res = client_w_eth.post('/api/vouchers/user', headers=headers) assert res._status_code == 404 res = client_w_eth.get('/api/vouchers/user?idUser=6') assert res._status_code == 200 assert len(res.json) == 1 assert res.json[0]["id"] == 3 assert res.json[0]["userid"] == 6 assert res.json[0]["idvoucher"] == 1 assert res.json[0]["untilTime"] == datetime(2022, 1, 13).timestamp() assert not res.json[0]["used"] assert res.json[0]["price"] == WEB3.toWei(0.01, 'ether')
def project_add_milestone(project: Project, owner: User, name: str, goal: int, until: int): projects_sc = WEB3.eth.contract(address=project.scAddress, abi=PROJECT_JSON["abi"]) tx_hash = projects_sc.functions.addMilestone(WEB3.toBytes(text=name), int(goal), int(until)). \ buildTransaction({'nonce': WEB3.eth.getTransactionCount(owner.publickeyUser), 'from': owner.publickeyUser}) signed_tx = WEB3.eth.account.sign_transaction(tx_hash, private_key=owner.privatekeyUser) tx_hash = WEB3.eth.sendRawTransaction(signed_tx.rawTransaction) tx_receipt = WEB3.eth.waitForTransactionReceipt(tx_hash) if tx_receipt.status != 1: raise RuntimeError("SC addMilestone Call failed!") processed_receipt = projects_sc.events.AddMilestone().processReceipt(tx_receipt) if len(processed_receipt) < 1: raise RuntimeError("SC addMilestone no Events") if WEB3.toText(processed_receipt[0].args._name) != name: # pylint:disable=protected-access raise RuntimeError("SC addMilestone Event name wrong!") if processed_receipt[0].args._amount != goal: # pylint:disable=protected-access raise RuntimeError("SC addMilestone Event goal wrong!") return processed_receipt[0].args.milestone_id
def redeem_voucher_check(user: User, voucher_user: VoucherUser, sc_address) -> Optional[str]: # require(voucher[msg.sender].length > _index); - invariant # require(v.used == false); if voucher_user.usedVoucher is not False: return "already used voucher" # require(v.expires_unixtime >= block.timestamp); if not voucher_user.expires_unixtime >= datetime.now(): return "voucher is expired" if not WEB3.isAddress(sc_address): return "invalid sc_address" if user: pass return None
def voucher_constructor_check(institution_owner_address) -> Optional[str]: if not WEB3.isAddress(institution_owner_address): return "invalid public key" return None
def voucher_post(user): """ Handles POST for resource <base>/api/voucher/user . :return: json data of projects """ id_voucher = request.headers.get('idVoucher') if not id_voucher: return jsonify({'error': 'missing id'}), 400 try: check_params_int([id_voucher]) except ValueError: return jsonify({"error": "bad argument"}), 400 session = DB_SESSION() try: voucher = session.query(Voucher).filter( Voucher.idVoucher == id_voucher).one() balance = WEB3.eth.getBalance(user.publickeyUser) if balance < voucher.priceVoucher: # ToDo: gas-cost? return jsonify({'error': 'not enough balance'}), 406 if not voucher.available: return jsonify({'error': 'voucher not available'}), 406 association = VoucherUser( usedVoucher=False, expires_unixtime=(datetime.now() + timedelta(0, 2 * 31536000)), voucher=voucher, user=user) inst: Institution = session.query(Institution).filter( Institution.idInstitution == voucher.institution_id).one() transaction = { 'nonce': WEB3.eth.getTransactionCount(user.publickeyUser), 'to': inst.publickeyInstitution, 'value': voucher.priceVoucher, 'gas': 200000, 'gasPrice': WEB3.toWei('50', 'gwei') } signed_transaction = WEB3.eth.account.sign_transaction( transaction, user.privatekeyUser) WEB3.eth.sendRawTransaction(signed_transaction.rawTransaction) cfg_parser: configparser.ConfigParser = configparser.ConfigParser() cfg_parser.read("backend_config.ini") voucher_sc = WEB3.eth.contract( address=WEB3.toChecksumAddress(cfg_parser["Voucher"]["ADDRESS"]), abi=json.loads(cfg_parser["Voucher"]["ABI"])) transaction = voucher_sc.functions.addVoucher(user.publickeyUser, WEB3.toBytes(text=voucher.titleVoucher), 666) \ .buildTransaction({'nonce': WEB3.eth.getTransactionCount(user.publickeyUser)}) signed_transaction = WEB3.eth.account.sign_transaction( transaction, user.privatekeyUser) WEB3.eth.sendRawTransaction(signed_transaction.rawTransaction) session.add(voucher) session.add(association) session.commit() except InvalidAddress: return jsonify({'error': 'given publickey is not valid'}), 400 except NoResultFound: return jsonify({'error': 'Voucher doesnt exist'}), 404 return jsonify({'status': 'voucher bought'}), 200
def test_projects_patch_w_milestones(client_w_eth): milestones = [ { "name": "goal_1", "goal": WEB3.toWei(0.6, 'ether'), "until": 1693094933 }, { "name": "goal_2", "goal": WEB3.toWei(0.7, 'ether'), "until": 1693094933 }, ] headers = { "authToken": TOKEN_1, "milestones": json.dumps(milestones), "idInstitution": 4 } res = client_w_eth.patch('/api/projects/1', headers=headers) assert res._status_code == 201 assert res.json["status"] == "ok" res = client_w_eth.get('/api/projects/1') assert res._status_code == 200 assert res.json["id"] == 1 assert res.json["idinstitution"] == 1 assert res.json["name"] == "Computer malt Bild" assert res.json["webpage"] == "http://www.cmb.de" assert len(res.json["milestones"]) == 6 assert res.json["milestones"][0]["id"] == 1 assert res.json["milestones"][0]["idProjekt"] == 1 assert res.json["milestones"][0]["goal"] == WEB3.toWei(0.1, 'ether') assert res.json["milestones"][0]["currentVotes"] == 112 assert res.json["milestones"][0]["until"] == 1693094933 assert res.json["milestones"][1]["id"] == 2 assert res.json["milestones"][1]["idProjekt"] == 1 assert res.json["milestones"][1]["goal"] == WEB3.toWei(0.2, 'ether') assert res.json["milestones"][1]["currentVotes"] == 12 assert res.json["milestones"][1]["until"] == 1693094933 assert res.json["milestones"][2]["id"] == 3 assert res.json["milestones"][2]["idProjekt"] == 1 assert res.json["milestones"][2]["goal"] == WEB3.toWei(0.3, 'ether') assert res.json["milestones"][2]["currentVotes"] == 0 assert res.json["milestones"][2]["until"] == 1693094933 assert res.json["milestones"][3]["id"] == 7 assert res.json["milestones"][3]["idProjekt"] == 1 assert res.json["milestones"][3]["goal"] == WEB3.toWei(0.5, 'ether') assert res.json["milestones"][3]["currentVotes"] == 400 assert res.json["milestones"][3]["until"] == 1693094933 assert res.json["milestones"][4]["id"] == 8 assert res.json["milestones"][4]["idProjekt"] == res.json["id"] assert res.json["milestones"][4]["goal"] == milestones[0]["goal"] assert res.json["milestones"][4]["currentVotes"] == 0 assert res.json["milestones"][4]["until"] == milestones[0]["until"] assert res.json["milestones"][5]["id"] == 9 assert res.json["milestones"][5]["idProjekt"] == res.json["id"] assert res.json["milestones"][5]["goal"] == milestones[1]["goal"] assert res.json["milestones"][5]["currentVotes"] == 0 assert res.json["milestones"][5]["until"] == milestones[1]["until"]