Created
November 3, 2021 21:19
-
-
Save Kygandomi/4e5eb76f443d16d6d1b6d68a4d77a0d6 to your computer and use it in GitHub Desktop.
Chia Password Coin Tutorial 5 Snippet 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.route('/spend', methods=('GET', 'POST')) | |
async def spend(): | |
await setup_blockchain_connection() | |
# If a post request was made | |
if request.method == 'POST': | |
# Get variables from the form | |
password = (await request.form)['password'] | |
address = (await request.form)['address'] | |
# Get Spend Bundle Parameters | |
coin_reveal = create_coin_puzzle( | |
create_coin_password_hash_from_string(password)) | |
coin_treehash = create_coin_treehash( | |
create_coin_password_hash_from_string(password)) | |
coin_records = await full_node_rpc_client.get_coin_records_by_puzzle_hash(coin_treehash) | |
# Let's spend the first available coin with this password | |
# Note: Since all coins with the same password created with the puzzle in /password/password.clsp have the same | |
# puzzle_hash they will all be retrieved here. Even the ones that were NOT created by you! | |
coin_to_spend = None | |
for coin_record in coin_records: | |
if not coin_record.spent: | |
coin_to_spend = coin_record | |
break | |
# If there's no coin redirect back to the spend template | |
if coin_to_spend == None: | |
print("NO COIN AVAILABLE") | |
# TODO: Show error on client | |
return await render_template('spend.html') | |
# Get the coin solution | |
decoded_address = decode_puzzle_hash(address) | |
coin_solution = solution_for_password( | |
coin_to_spend.coin, password, decoded_address) | |
# Put together our spend bundle | |
tx_spend_bundle = SpendBundle( | |
[ | |
CoinSpend( | |
coin_to_spend.coin, | |
coin_reveal, | |
coin_solution, | |
) | |
], | |
G2Element(), | |
) | |
# Try to send the spend bundle to the network | |
await full_node_rpc_client.push_tx(tx_spend_bundle) | |
# Redirect back to the home page on success | |
return redirect(url_for('index')) | |
# Show the spend form template | |
# If GET method | |
return await render_template('spend.html') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment