Skip to content

Commit

Permalink
feat: Add add_transaction POST API
Browse files Browse the repository at this point in the history
  • Loading branch information
kadensungbincho committed Dec 14, 2021
1 parent 1f4c517 commit ba091c3
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion cryptocurrency/simple_cryptocurrency.py
Expand Up @@ -107,6 +107,7 @@ def update_chain(self):
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = False
blockchain = Blockchain()


@app.route('/mine_block', methods = ['GET'])
def mine_block():
previous_block = blockchain.get_previous_block()
Expand All @@ -120,7 +121,6 @@ def mine_block():
}
return jsonify(responses), 200


@app.route('/get_chain', methods = ['GET'])
def get_chain():
response = {
Expand All @@ -129,5 +129,20 @@ def get_chain():
}
return jsonify(response), 200

@app.route('/add_transaction', methods = ['POST'])
def add_transaction():
json = request.get_json()
transaction_keys = ['sender', 'receiver', 'amount']
if not all (key in json for key in transaction_keys):
return 'Some elements of the transaction are missing', 400
index = blockchain.add_transaction(
sender = json['sender'],
receiver = json['receiver'],
amount = json['amount']
)
response = {
'message': f'This transaction will be added to Block {index}'
}
return jsonify(response), 201

app.run(host = '0.0.0.0', port = 5000)

0 comments on commit ba091c3

Please sign in to comment.