Skip to content

Instantly share code, notes, and snippets.

@bwbush
Last active July 19, 2023 16:40
Show Gist options
  • Save bwbush/149ac3173df3e4f4846f499a2a144faf to your computer and use it in GitHub Desktop.
Save bwbush/149ac3173df3e4f4846f499a2a144faf to your computer and use it in GitHub Desktop.
Example Usage of Marlowe Oracle
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "6ca9890b-2615-4d21-ae2c-dfeec836a194",
"metadata": {},
"source": [
"# Example Usage of Marlowe Oracle\n",
"\n",
"This example uses the Marlowe oracle to read a random value into a contract and pay that amount of ada to a recipient.\n",
"\n",
"The oracle resides at `addr_test1qzy4aluz9zcmxuhuqgjhl457ee0ma0hcq38w62ckaagjwgvnjlzdfawd9kvva673fht7737e3r5j322v7090uqhpn0wq7a2gly` on the `preprod` network.\n",
"\n",
"![Oracle Example](example-oracle-contract.png)"
]
},
{
"cell_type": "markdown",
"id": "f9a8624e-e794-4b1c-97d8-c4110d00ecdb",
"metadata": {},
"source": [
"## Setup"
]
},
{
"cell_type": "markdown",
"id": "3def68a6-ec4e-4f76-94e5-5ff8f538df23",
"metadata": {},
"source": [
"### Select the preproduction network"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "b24576cf-87a9-46a6-9890-e011d7a2e5c8",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"export CARDANO_NODE_SOCKET_PATH=node.socket\n",
"export CARDANO_TESTNET_MAGIC=1"
]
},
{
"cell_type": "markdown",
"id": "3c86b232-7afd-436e-a86a-1b164ddbb963",
"metadata": {},
"source": [
"### Set the location of Marlowe Runtime"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "66b9d78f-0e70-4158-9efd-9f46679a5bcf",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"export MARLOWE_RT_HOST=127.0.0.1\n",
"export MARLOWE_RT_PORT=13700"
]
},
{
"cell_type": "markdown",
"id": "4335f71f-1bbf-4f67-b9f9-adc014766840",
"metadata": {},
"source": [
"### Set the location of Marlowe executables"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "11e2726b-8760-41a9-afc9-47b6c4b84ead",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"MARLOWE_RT_CLI=marlowe-runtime-cli\n",
"MARLOWE_CLI=marlowe-cli"
]
},
{
"cell_type": "markdown",
"id": "f14703b5-bdf4-4a8e-b78c-8faa84329b01",
"metadata": {},
"source": [
"### Constants"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a77c9bf4-ebfb-4b54-940d-8dd8fd8a7844",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"ADA=1000000"
]
},
{
"cell_type": "markdown",
"id": "fb33e5e3-5ae3-4a01-9cee-854c845ea86a",
"metadata": {},
"source": [
"## Addresses"
]
},
{
"cell_type": "markdown",
"id": "ff773d61-b9a8-45e2-a754-5a926a84fb9c",
"metadata": {},
"source": [
"### Oracle address"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "8c087dff-c65f-460f-8301-9b79ea337c44",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"ORACLE_ADDR=addr_test1qzy4aluz9zcmxuhuqgjhl457ee0ma0hcq38w62ckaagjwgvnjlzdfawd9kvva673fht7737e3r5j322v7090uqhpn0wq7a2gly"
]
},
{
"cell_type": "markdown",
"id": "50479d0f-450c-48ae-a3de-a222c1ce26ec",
"metadata": {},
"source": [
"### Address for \"c.marlowe\" role"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "e001dec2-89b7-404d-96d7-bc2239999a84",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"PAYMENT_ADDR = addr_test1vq9prvx8ufwutkwxx9cmmuuajaqmjqwujqlp9d8pvg6gupczgtm9j\n"
]
}
],
"source": [
"PAYMENT_ADDR=$(cat payment.testnet.address)\n",
"PAYMENT_SKEY=payment.skey\n",
"echo \"PAYMENT_ADDR = $PAYMENT_ADDR\""
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "3c735de3-30c1-442c-9df2-6418eb0d7a32",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"ROLE_POLICY=8bb3b343d8e404472337966a722150048c768d0a92a9813596c5338d\n",
"ROLE_NAME=c.marlowe"
]
},
{
"cell_type": "markdown",
"id": "009478e5-aa58-4b86-bde6-d2f91a262f87",
"metadata": {},
"source": [
"### Address for recipient of funds"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "172749ff-9ecc-427b-8584-5ac3445c9b8f",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"RECIPIENT_ADDR=addr_test1qq9prvx8ufwutkwxx9cmmuuajaqmjqwujqlp9d8pvg6gupcvluken35ncjnu0puetf5jvttedkze02d5kf890kquh60slacjyp"
]
},
{
"cell_type": "markdown",
"id": "7b56807b-5076-47d4-8004-11034bb9c2fa",
"metadata": {},
"source": [
"## Contract"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "67c43143-15d4-4bb4-ade7-9b488221ccae",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\"timeout\":1689788076000,\"timeout_continuation\":\"close\",\"when\":[{\"case\":{\"deposits\":{\"multiply\":100,\"times\":1000000},\"into_account\":{\"role_token\":\"c.marlowe\"},\"of_token\":{\"currency_symbol\":\"\",\"token_name\":\"\"},\"party\":{\"role_token\":\"c.marlowe\"}},\"then\":{\"timeout\":1689791676000,\"timeout_continuation\":\"close\",\"when\":[{\"case\":{\"choose_between\":[{\"from\":50,\"to\":100}],\"for_choice\":{\"choice_name\":\"RANDOM\",\"choice_owner\":{\"address\":\"addr_test1qzy4aluz9zcmxuhuqgjhl457ee0ma0hcq38w62ckaagjwgvnjlzdfawd9kvva673fht7737e3r5j322v7090uqhpn0wq7a2gly\"}}},\"then\":{\"from_account\":{\"role_token\":\"c.marlowe\"},\"pay\":{\"multiply\":{\"value_of_choice\":{\"choice_name\":\"RANDOM\",\"choice_owner\":{\"address\":\"addr_test1qzy4aluz9zcmxuhuqgjhl457ee0ma0hcq38w62ckaagjwgvnjlzdfawd9kvva673fht7737e3r5j322v7090uqhpn0wq7a2gly\"}}},\"times\":1000000},\"then\":\"close\",\"to\":{\"party\":{\"address\":\"addr_test1qq9prvx8ufwutkwxx9cmmuuajaqmjqwujqlp9d8pvg6gupcvluken35ncjnu0puetf5jvttedkze02d5kf890kquh60slacjyp\"}},\"token\":{\"currency_symbol\":\"\",\"token_name\":\"\"}}}]}}]}\n"
]
}
],
"source": [
"yaml2json << EOI > example-oracle-contract.json\n",
"timeout: $((1000 * (`date -u +%s` + 1 * 60 * 60)))\n",
"timeout_continuation: close\n",
"when:\n",
"- case:\n",
" deposits:\n",
" multiply: 100\n",
" times: 1000000\n",
" into_account:\n",
" role_token: $ROLE_NAME\n",
" of_token:\n",
" currency_symbol: ''\n",
" token_name: ''\n",
" party:\n",
" role_token: c.marlowe\n",
" then:\n",
" timeout: $((1000 * (`date -u +%s` + 2 * 60 * 60)))\n",
" timeout_continuation: close\n",
" when:\n",
" - case:\n",
" choose_between:\n",
" - from: 50\n",
" to: 100\n",
" for_choice:\n",
" choice_name: RANDOM\n",
" choice_owner:\n",
" address: \"$ORACLE_ADDR\"\n",
" then:\n",
" from_account:\n",
" role_token: c.marlowe\n",
" pay:\n",
" multiply:\n",
" value_of_choice:\n",
" choice_name: RANDOM\n",
" choice_owner:\n",
" address: \"$ORACLE_ADDR\"\n",
" times: 1000000\n",
" then: close\n",
" to:\n",
" party:\n",
" address: \"$RECIPIENT_ADDR\"\n",
" token:\n",
" currency_symbol: ''\n",
" token_name: ''\n",
"EOI\n",
"cat example-oracle-contract.json"
]
},
{
"cell_type": "markdown",
"id": "e41884a9-ef0d-4b13-81b6-7e789e4077a6",
"metadata": {},
"source": [
"## Transactions"
]
},
{
"cell_type": "markdown",
"id": "13bbcec6-0e74-4a3f-8e6c-85b681cee2fd",
"metadata": {},
"source": [
"### 1. Create the contract"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "2f2f6703-584c-4c61-b630-28c6811dbbd8",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CONTRACT_ID = 8c1f3c01d8e7c388b61b387395a7890d1299b60ee7bac6f1d066cfb17a475ac6#1\n"
]
}
],
"source": [
"CONTRACT_ID=$(\n",
"\"$MARLOWE_RT_CLI\" create \\\n",
" --core-file example-oracle-contract.json \\\n",
" --role-token-policy-id \"$ROLE_POLICY\" \\\n",
" --min-utxo \"$((2 * ADA))\" \\\n",
" --change-address \"$PAYMENT_ADDR\" \\\n",
" --manual-sign tx-1.unsigned \\\n",
" 2> /dev/null \\\n",
"| jq -r 'fromjson | .contractId' \\\n",
")\n",
"echo \"CONTRACT_ID = $CONTRACT_ID\""
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "c80df130-8d4e-4fe6-9bb6-99cdd12fe756",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"TxId \"8c1f3c01d8e7c388b61b387395a7890d1299b60ee7bac6f1d066cfb17a475ac6\"\n"
]
}
],
"source": [
"\"$MARLOWE_CLI\" transaction submit \\\n",
" --tx-body-file tx-1.unsigned \\\n",
" --required-signer \"$PAYMENT_SKEY\" \\\n",
" --timeout 600s"
]
},
{
"cell_type": "markdown",
"id": "607fee92-49a7-4ce1-9dc1-7f11d7adb6ef",
"metadata": {},
"source": [
"### 2. Deposit the 100 ada"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "ca0b0923-a30c-470b-8704-9ad4264a45b6",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"txId: 192a0db14566e8c81c65c2342fe2e1571dce74669810e9895d34a864777ac2c9\n"
]
}
],
"source": [
"\"$MARLOWE_RT_CLI\" deposit \\\n",
" --contract \"$CONTRACT_ID\" \\\n",
" --from-party \"$ROLE_NAME\" \\\n",
" --to-party \"$ROLE_NAME\" \\\n",
" --lovelace \"$((100 * ADA))\" \\\n",
" --change-address \"$PAYMENT_ADDR\" \\\n",
" --manual-sign tx-2.unsigned \\\n",
"| jq 'fromjson' | json2yaml"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "7951d770-ffcc-4472-a502-01027ef70454",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"TxId \"192a0db14566e8c81c65c2342fe2e1571dce74669810e9895d34a864777ac2c9\"\n"
]
}
],
"source": [
"$MARLOWE_CLI transaction submit \\\n",
" --tx-body-file tx-2.unsigned \\\n",
" --required-signer \"$PAYMENT_SKEY\" \\\n",
" --timeout 600s"
]
},
{
"cell_type": "markdown",
"id": "a2a68525-3908-457e-9a16-72b62fc2a118",
"metadata": {},
"source": [
"### Wait for the oracle to report a random value to the contract"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "321f729a-81be-4424-8ee3-433c1cd56e81",
"metadata": {},
"outputs": [],
"source": [
"sleep 3m"
]
},
{
"cell_type": "markdown",
"id": "5609e195-f4a5-4cda-8c0d-2946a16f45ca",
"metadata": {},
"source": [
"### 3. View the report by the oracle"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "c9d11d61-e542-4fe3-9794-408ef7ef3cfe",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[93mtransaction 8c1f3c01d8e7c388b61b387395a7890d1299b60ee7bac6f1d066cfb17a475ac6 (creation)\n",
"\u001b[0mContractId: 8c1f3c01d8e7c388b61b387395a7890d1299b60ee7bac6f1d066cfb17a475ac6#1\n",
"SlotNo: 34101303\n",
"BlockNo: 1178418\n",
"BlockId: 6a99f47af1ffa8d2408fa574b80b2580063b88121e46c25371edf1cc9717f8b9\n",
"ScriptAddress: addr_test1wqhdyccahvnheppng3fut3phhp3jt5m37zp4529ezz535ms2u9jqv\n",
"Marlowe Version: 1\n",
"\n",
" When [\n",
" (Case\n",
" (Deposit (Role \"c.marlowe\") (Role \"c.marlowe\")\n",
" (Token \"\" \"\")\n",
" (MulValue\n",
" (Constant 100)\n",
" (Constant 1000000)))\n",
" (When [\n",
" (Case\n",
" (Choice\n",
" (ChoiceId \"RANDOM\" (Address \"addr_test1qzy4aluz9zcmxuhuqgjhl457ee0ma0hcq38w62ckaagjwgvnjlzdfawd9kvva673fht7737e3r5j322v7090uqhpn0wq7a2gly\")) [\n",
" (Bound 50 100)])\n",
" (Pay (Role \"c.marlowe\")\n",
" (Party (Address \"addr_test1qq9prvx8ufwutkwxx9cmmuuajaqmjqwujqlp9d8pvg6gupcvluken35ncjnu0puetf5jvttedkze02d5kf890kquh60slacjyp\"))\n",
" (Token \"\" \"\")\n",
" (MulValue\n",
" (ChoiceValue\n",
" (ChoiceId \"RANDOM\" (Address \"addr_test1qzy4aluz9zcmxuhuqgjhl457ee0ma0hcq38w62ckaagjwgvnjlzdfawd9kvva673fht7737e3r5j322v7090uqhpn0wq7a2gly\")))\n",
" (Constant 1000000)) Close))] 1689791676000 Close))] 1689788076000 Close\n",
"\n",
"\u001b[93mtransaction 192a0db14566e8c81c65c2342fe2e1571dce74669810e9895d34a864777ac2c9\n",
"\u001b[0mContractId: 8c1f3c01d8e7c388b61b387395a7890d1299b60ee7bac6f1d066cfb17a475ac6#1\n",
"SlotNo: 34101316\n",
"BlockNo: 1178419\n",
"BlockId: c50e8f3a226a2935de4b3e7370ebacbccb0bfb132b01bd3ace11c3db656a9c16\n",
"Inputs: [NormalInput (IDeposit \"c.marlowe\" \"c.marlowe\" (Token \"\" \"\") 100000000)]\n",
"\n",
" When [\n",
" (Case\n",
" (Choice\n",
" (ChoiceId \"RANDOM\" (Address \"addr_test1qzy4aluz9zcmxuhuqgjhl457ee0ma0hcq38w62ckaagjwgvnjlzdfawd9kvva673fht7737e3r5j322v7090uqhpn0wq7a2gly\")) [\n",
" (Bound 50 100)])\n",
" (Pay (Role \"c.marlowe\")\n",
" (Party (Address \"addr_test1qq9prvx8ufwutkwxx9cmmuuajaqmjqwujqlp9d8pvg6gupcvluken35ncjnu0puetf5jvttedkze02d5kf890kquh60slacjyp\"))\n",
" (Token \"\" \"\")\n",
" (MulValue\n",
" (ChoiceValue\n",
" (ChoiceId \"RANDOM\" (Address \"addr_test1qzy4aluz9zcmxuhuqgjhl457ee0ma0hcq38w62ckaagjwgvnjlzdfawd9kvva673fht7737e3r5j322v7090uqhpn0wq7a2gly\")))\n",
" (Constant 1000000)) Close))] 1689791676000 Close\n",
"\n",
"\u001b[93mtransaction 855ec14d6e784f917b243a327277f85b47177b4bc817b32b0583befb6d552bc6 (close)\n",
"\u001b[0mContractId: 8c1f3c01d8e7c388b61b387395a7890d1299b60ee7bac6f1d066cfb17a475ac6#1\n",
"SlotNo: 34101360\n",
"BlockNo: 1178421\n",
"BlockId: a2d3ae8b1c235793a3aa4d9945c1c0fe3b98abf3d9f574ae50e530e2f2ee5c93\n",
"Inputs: [NormalInput (IChoice (ChoiceId \"RANDOM\" \"\\\"addr_test1qzy4aluz9zcmxuhuqgjhl457ee0ma0hcq38w62ckaagjwgvnjlzdfawd9kvva673fht7737e3r5j322v7090uqhpn0wq7a2gly\\\"\") 91)]\n",
"\n",
"\n"
]
}
],
"source": [
"\"$MARLOWE_RT_CLI\" log \\\n",
" --show-contract \\\n",
" \"$CONTRACT_ID\""
]
},
{
"cell_type": "markdown",
"id": "d35d0453-e3b9-424f-83f3-97b51c0e82e6",
"metadata": {},
"source": [
"We see that the oracle has reported the value 91."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Bash with Marlowe Tools",
"language": "bash",
"name": "bash-minimal"
},
"language_info": {
"codemirror_mode": "shell",
"file_extension": ".sh",
"mimetype": "text/x-sh",
"name": "bash"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment