Gift script evaluation failure #171
Answered
by
juliusfrost
juliusfrost
asked this question in
Q&A
-
I'm creating a simple gift script with pycardano and eopsin but I'm having issues redeeming from the script address. I get the following error from my script:
Here's the code I use: import click
from pycardano import OgmiosChainContext, Network, Address, TransactionBuilder, PaymentVerificationKey, \
PaymentSigningKey, TransactionOutput, Transaction, UTxO, PlutusV2Script, plutus_script_hash, Redeemer, RedeemerTag, \
PlutusData
from src.week02 import assets_dir
keys_dir = assets_dir.joinpath("../../../keys")
@click.command()
@click.argument("name")
def main(name):
context = OgmiosChainContext("ws://localhost:1337", network=Network.TESTNET)
with open(assets_dir.joinpath("gift", "testnet.addr")) as f:
script_address = Address.from_primitive(f.read())
with open(assets_dir.joinpath("gift", "script.cbor"), "r") as f:
script_hex = f.read()
gift_script = PlutusV2Script(bytes.fromhex(script_hex))
script_hash = plutus_script_hash(gift_script)
address = Address(script_hash, network=Network.TESTNET)
assert script_address == address
with open(keys_dir.joinpath(f"{name}.addr")) as f:
payment_address = Address.from_primitive(f.read())
utxo_to_spend = None
for utxo in context.utxos(str(script_address)):
utxo_to_spend = utxo
break
assert isinstance(utxo_to_spend, UTxO), "No UTxOs found!"
redeemer = Redeemer(RedeemerTag.SPEND, 0)
builder = TransactionBuilder(context)
builder.add_script_input(utxo_to_spend, script=gift_script, datum=PlutusData(), redeemer=redeemer)
amount = utxo_to_spend.output.amount - 2000000
builder.add_output(
TransactionOutput(address=payment_address, amount=amount)
)
non_nft_utxo = None
for utxo in context.utxos(str(payment_address)):
# multi_asset should be empty for collateral utxo
if not utxo.output.amount.multi_asset:
non_nft_utxo = utxo
break
builder.collaterals.append(non_nft_utxo)
signed_tx = sign(builder, name)
# context.submit_tx(signed_tx.to_cbor())
print(f"transaction id: {signed_tx.id}")
print(f"Cardanoscan: https://preview.cardanoscan.io/transaction/{signed_tx.id}")
def get_signing_info(name):
vkey_path = str(keys_dir.joinpath(f"{name}.vkey"))
skey_path = str(keys_dir.joinpath(f"{name}.skey"))
payment_vkey = PaymentVerificationKey.load(vkey_path)
payment_skey = PaymentSigningKey.load(skey_path)
payment_address = Address(payment_vkey.hash(), network=Network.TESTNET)
with open(keys_dir.joinpath(f"{name}.addr")) as f:
address = Address.from_primitive(f.read())
assert payment_address == address
return payment_vkey, payment_skey, payment_address
def sign(builder: TransactionBuilder, name) -> Transaction:
payment_vkey, payment_skey, payment_address = get_signing_info(name)
return builder.build_and_sign(
signing_keys=[payment_skey],
change_address=payment_address,
)
if __name__ == "__main__":
main() |
Beta Was this translation helpful? Give feedback.
Answered by
juliusfrost
Mar 5, 2023
Replies: 1 comment
-
It turns out I didn't send a datum in sending to the script address, and such transaction outputs are impossible to spend. See below,
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
juliusfrost
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It turns out I didn't send a datum in sending to the script address, and such transaction outputs are impossible to spend. See below,