Skip to content

Support utxo query with kupo for Vasil #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 64 commits into from
Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
0b6a569
incorporate split change logic
Feb 20, 2022
182f780
incorporate split change logic
Feb 20, 2022
7fdb334
incorporate split change logic
Feb 20, 2022
3c8ced2
minor modification to change combine logic
Feb 20, 2022
c2772e8
minor modification to change combine logic
Feb 20, 2022
d303a02
minor modification to change combine logic
Feb 20, 2022
2b0cd17
Merge branch 'cffls:main' into main
henryyuanheng-wang Feb 28, 2022
a008ed3
Merge branch 'cffls:main' into main
henryyuanheng-wang Mar 7, 2022
e8f37fa
integrate diff changes
Mar 7, 2022
4c7ba76
integrate diff changes and remove unused functions
Mar 7, 2022
415b3a6
Merge branch 'cffls:main' into main
henryyuanheng-wang Mar 13, 2022
d80063c
add change split test case. Modify txbuilder logic in handeling splits.
Mar 13, 2022
ee36493
add unit tests for change split logic
Mar 13, 2022
ca3b3f2
Bump pytest from 7.0.1 to 7.1.1
dependabot[bot] Mar 21, 2022
21e1b3f
Merge pull request #2 from henryyuanheng-wang/dependabot/pip/pytest-7…
henryyuanheng-wang Mar 27, 2022
8a38224
merge with original main
Mar 27, 2022
6d86610
Merge branch 'cffls-main'
Mar 27, 2022
cf788fe
Merge branch 'main' of https://github.com/henryyuanheng-wang/pycardano
Mar 27, 2022
86d588f
merge with main
Apr 12, 2022
89535d6
Merge branch 'cffls-main'
Apr 12, 2022
68cc134
replace max fee with more accurately estimated fees
Apr 19, 2022
e4eefd3
replace max fee with more precise fee estimation
Apr 20, 2022
0e6758b
remove max fee import
Apr 20, 2022
c713360
modify format to be consistent with code base
Apr 21, 2022
71d93e3
supports utxo queries using kupo
May 17, 2022
6ce979c
merge with main branch
May 17, 2022
1f2c081
Merge branch 'cffls-main'
May 17, 2022
dffeff4
support utxo query with Kupo
May 18, 2022
d514d3d
reformat
May 18, 2022
7122dcb
query utxo with Kupo when kupo url is provided
May 26, 2022
a666882
resolve merging conflict
May 26, 2022
fa7d14d
Merge branch 'cffls-main'
May 26, 2022
902d410
remove the hardcoded genesis utxo in test_all file
May 27, 2022
2de54da
pull latest from main
Jun 5, 2022
d58c537
Merge branch 'cffls-main'
Jun 5, 2022
d5b857a
Create usage guides for Plutus and fix hyperlinks
Jun 6, 2022
0e4b0ad
Merge branch 'cffls:main' into main
henryyuanheng-wang Jun 6, 2022
8af6486
create separate section for datum/redeemer serialiation
Jun 14, 2022
4a12d13
add -f flag to try to clean up the build if it exists
Jun 14, 2022
605400f
Merge branch 'main' of https://github.com/henryyuanheng-wang/pycardano
Jun 14, 2022
5c2c025
Create usage guides for Plutus and fix hyperlinks
Jun 6, 2022
bdf88c0
create separate section for datum/redeemer serialiation
Jun 14, 2022
7840dea
add -f flag to try to clean up the build if it exists
Jun 14, 2022
1190c9f
Merge branch 'cffls:main' into main
henryyuanheng-wang Jun 22, 2022
dd1b199
Merge branch 'cffls:main' into main
henryyuanheng-wang Jul 4, 2022
8cc6ab7
Merge branch 'cffls:main' into main
henryyuanheng-wang Jul 9, 2022
09c8607
Merge branch 'cffls:main' into main
henryyuanheng-wang Jul 17, 2022
54a20ef
Merge branch 'main' of https://github.com/henryyuanheng-wang/pycardano
Jul 17, 2022
e194656
Merge branch 'cffls:main' into main
henryyuanheng-wang Sep 17, 2022
ee4d330
hdwallet implementation and unit test
Sep 17, 2022
37f76ad
adjust poetry files
Sep 17, 2022
436a80a
modify poetry
Sep 17, 2022
be58baf
modify poetry file
Sep 18, 2022
6c428b9
modify poetry lock
Sep 18, 2022
4877fd4
Merge branch 'cffls:main' into main
henryyuanheng-wang Oct 5, 2022
80b4373
made changes to hdwallet and incorporated into bip32
Oct 5, 2022
d62e2a1
remove commented code block
Oct 5, 2022
65b659e
Merge remote-tracking branch 'origin/main' into main
henryyuanheng-wang Oct 30, 2022
1e8d8ec
support utxo query with kupo
henryyuanheng-wang Nov 3, 2022
c73d194
Merge branch 'main' of https://github.com/henryyuanheng-wang/pycardan…
henryyuanheng-wang Nov 3, 2022
24b5de8
move test_bip32
henryyuanheng-wang Nov 3, 2022
d4168ab
update utxo query with kupo
henryyuanheng-wang Nov 4, 2022
1b3c2df
update plutus guide with vasil
henryyuanheng-wang Nov 4, 2022
c763b47
Merge branch 'main' of https://github.com/henryyuanheng-wang/pycardan…
henryyuanheng-wang Nov 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions docs/source/guides/plutus.rst
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,71 @@ Taker/Unlocker provides collateral. Collateral has been introduced in Alonzo tra

The funds locked in script address is successfully retrieved to the taker address.

-------------
Vasil Upgrade
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome, thanks for adding these to the documentation!

-------------
As part of the Basho phase of Cardano roadmap, the Vasil upgrade brings new capabilities on Plutus, namely reference inputs, inline datums, reference scripts, collateral output and Plutus V2 primitives.

- **Reference inputs** (`CIP-31 <https://cips.cardano.org/cips/cip31/>`_): This upgrade enables data sharing on-chain. Previously, datums were carried in transaction outputs; they stored and provided access to information on the blockchain. However, to access information in this datum, one had to spend the output that the datum was attached to. This required the re-creation of a spent output. The addition of reference inputs now allows developers to look at the datum without extra steps. This facilitates access to information stored on the blockchain without the need for spending and re-creating UTXOs. This can be useful for oracles and other use cases where state need to be inspected.

- **Inline datums** (`CIP-32 <https://cips.cardano.org/cips/cip32/>`_): Transaction datums were previously attached to outputs as hashes. With the implementation of inline datums, developers can now create scripts and attach datums directly to outputs instead of using their hashes. This simplifies how datums are used – a user can see the actual datum rather than supply it to match the given hash.

- **Reference scripts** (`CIP-33 <https://cips.cardano.org/cips/cip33/>`_): In Alonzo, when spending an output locked within a Plutus script, one had to include the script in the spending transaction. This increased the size of the script and caused certain delays in its processing. The reference scripts upgrade allows developers to reference a script without including it in each transaction. This significantly reduces transaction size, improves throughput, and reduces script execution costs (since the script only needs to be paid for once).

- **Explicit collateral output** (`CIP-40 <https://cips.cardano.org/cips/cip40/>`_): Transactions that call Plutus smart contracts are required to put up collateral to cover the potential cost of smart contract execution failure. If contract execution fails during phase 2 validation, all the funds stored in the chose UTXO for the collateral will be lost. After Vasil, user can specify a change address for the script collateral. If the script fails phase-2 validation, only the collateral amount will be taken, and the remaining funds will be sent to the change address.

- **Plutus V2 scripts**: The Vasil upgrade includes a new cost model that's lower than before, and developers will be able to see redeemers for all inputs rather than just the one being passed to the currently executing script.

Using the same FortyTwo example, now in Vasil, we show how reference scripts can be used. Reference script exists at a particular transaction output, and it can be used to witness UTxO at the corresponding script address::

>>> builder = TransactionBuilder(context)
>>> builder.add_input_address(giver_address)
>>> datum = 42
>>> # Include scripts in the script address
>>> builder.add_output(
>>> TransactionOutput(script_address, 50000000, script=forty_two_script)
>>> )

With reference script, actual script doesn't need to be included in the transaction anymore in order to spend UTxO sitting at script address::

>>> utxo_to_spend = None
>>> # Spend the utxo that has datum/datum hash but no script
>>> for utxo in chain_context.utxos(str(script_address)):
>>> if not utxo.output.script and (
>>> utxo.output.datum_hash == datum_hash(datum)
>>> or utxo.output.datum == datum
>>> ):
>>> utxo_to_spend = utxo
>>> break

>>> builder = TransactionBuilder(context)
>>> builder.add_script_input(utxo_to_spend, datum=datum, redeemer=redeemer)
>>> take_output = TransactionOutput(taker_address, 25123456)
>>> builder.add_output(take_output)
>>> signed_tx = builder.build_and_sign([extended_payment_skey], taker_address)

Again, with the same example, we show that you can send funds to script address with inline datums directly::

>>> builder = TransactionBuilder(context)
>>> builder.add_input_address(giver_address)
>>> datum = 42
>>> builder.add_output(
>>> TransactionOutput(script_address, 50000000, datum=datum, script=forty_two_script)
>>> )

With inline datum, we no longer have to include a datum within our transaction for our plutus spending scripts. Instead we can specify the transaction output where our datum exists to be used in conjunction with our Plutus spending script. This reduces the overall size of our transaction::

>>> utxo_to_spend = None
>>> # Speed the utxo that has both inline script and inline datum
>>> for utxo in chain_context.utxos(str(script_address)):
>>> if utxo.output.datum and utxo.output.script:
>>> utxo_to_spend = utxo
>>> break

>>> builder = TransactionBuilder(context)
>>> builder.add_script_input(utxo_to_spend, redeemer=redeemer)
>>> take_output = TransactionOutput(taker_address, 25123456)
>>> builder.add_output(take_output)
>>> signed_tx = builder.build_and_sign([extended_payment_skey], taker_address)


2 changes: 1 addition & 1 deletion integration-test/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ services:
max-file: "10"

kupo:
image: cardanosolutions/kupo
image: cardanosolutions/kupo:v2.1.0
environment:
NETWORK: "${NETWORK:-local}"

Expand Down
4 changes: 2 additions & 2 deletions integration-test/test/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class TestBase:

OGMIOS_WS = "ws://localhost:1337"

KUPO_URL = "http://localhost:1442/v1/matches"
KUPO_URL = "http://localhost:1442"

chain_context = OgmiosChainContext(OGMIOS_WS, Network.TESTNET)
chain_context = OgmiosChainContext(OGMIOS_WS, Network.TESTNET, kupo_url=KUPO_URL)

check_chain_context(chain_context)

Expand Down
2 changes: 1 addition & 1 deletion integration-test/test/test_plutus.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def test_plutus_v2_ref_script(self):
for utxo in self.chain_context.utxos(str(script_address)):
if not utxo.output.script and (
utxo.output.datum_hash == datum_hash(datum)
or utxo.output.datum == datum
or datum_hash(utxo.output.datum) == datum_hash(datum)
):
utxo_to_spend = utxo
break
Expand Down
31 changes: 29 additions & 2 deletions pycardano/backend/ogmios.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ def _utxos_kupo(self, address: str) -> List[UTxO]:
"kupo_url object attribute has not been assigned properly."
)

address_url = self._kupo_url + "/" + address
results = requests.get(address_url).json()
kupo_utxo_url = self._kupo_url + "/matches/" + address
results = requests.get(kupo_utxo_url).json()

utxos = []

Expand All @@ -272,17 +272,42 @@ def _utxos_kupo(self, address: str) -> List[UTxO]:

lovelace_amount = result["value"]["coins"]

script = None
script_hash = result.get("script_hash", None)
if script_hash:
kupo_script_url = self._kupo_url + "/scripts/" + script_hash
script = requests.get(kupo_script_url).json()
if script["language"] == "plutus:v2":
script = PlutusV2Script(
cbor2.loads(bytes.fromhex(script["script"]))
)
elif script["language"] == "plutus:v1":
script = PlutusV1Script(
cbor2.loads(bytes.fromhex(script["script"]))
)
else:
raise ValueError("Unknown plutus script type")

datum = None
datum_hash = (
DatumHash.from_primitive(result["datum_hash"])
if result["datum_hash"]
else None
)
if datum_hash:
kupo_datum_url = self._kupo_url + "/datums/" + result["datum_hash"]
datum_result = requests.get(kupo_datum_url).json()
if datum_result and datum_result["datum"] != datum_hash:
datum = RawCBOR(bytes.fromhex(datum_result["datum"]))
datum_hash = None

if not result["value"]["assets"]:
tx_out = TransactionOutput(
Address.from_primitive(address),
amount=lovelace_amount,
datum_hash=datum_hash,
datum=datum,
script=script,
)
else:
multi_assets = MultiAsset()
Expand All @@ -299,6 +324,8 @@ def _utxos_kupo(self, address: str) -> List[UTxO]:
Address.from_primitive(address),
amount=Value(lovelace_amount, multi_assets),
datum_hash=datum_hash,
datum=datum,
script=script,
)
utxos.append(UTxO(tx_in, tx_out))
else:
Expand Down