PIGY Token Smart Contract Testing

I just try to test Marlowe playground. And it is really interesting. Especially for me who doesn’t have any Haskell background.
So, I try to play with it.
Suppose there are a delegator called Alice and a pool named Bob.
If Alice delegate to Bob’s pool whatever the value is, for 2 epoch. Bob will give Alice 100,000 PIGY.

Now, when I try to create the contract. I’m having this problem.

  1. Where should I put the condition when Alice delegate to Bob.
  2. Where to put the rewards will be 100,000 PIGY, since I put ConstantParam as Rewards
  3. How to make the contract check if Alice already delegate to Bob for 2 epoch? Then to get the result.

I put choice bounds 1 as a result of Delegation OK, and 0 as a result of Delegation not OK.

Am I doing it right?
Here’s the chart

Thank you


Anybody can help?

Your contract is close to what you’re attempting, but it needs some adjustments. Here are answers to your questions:

  1. The Marlowe language current doesn’t have constructs for delegation. A delegation transaction is mostly like a regular transaction, but it also has a delegation certificate attached to it. I think that Alice’s delegation transaction would have to occur outside of the contract.
  2. Yes, when you run the simulator in the Marlowe playground, you can enter 100,000 in the input field labeled Reward at the start of running the contract.
  3. Because of item #1, I think you’ll need an oracle (a trusted third party) to signal that Alice has indeed delegated to Bob’s pool.
  4. The contract that you posted has one dangerous aspect. When a deposit is made into an account, at the end of the contract anything left in that account is refunded to the account owner. Thus, the second and third close in that contract would refund the money to Alice the delegator, not to Bob’s pool. It’s important to test every eventuality in the simulator to make sure that funds aren’t refunded to the wrong role.

In the contract below, I corrected problem #4 and added Charlie the Oracle. The oracle doesn’t send funds to the contract, but instead it verifies that the delegation did indeed occur. Alice or Bob could serve as the oracle if they trusted each other.

Here is the transcript from running the contract when Charlie asserts that the delegation occurred:

Deposit 100,000 units of PIGY into account of BOBS POOL as BOBS POOL
Participant CHARLIE THE ORACLE chooses the value 1 for choice with id "ALICE DELEGATED FOR 2 EPOCHS"
The contract pays 100,000 units of PIGY to participant ALICE THE DELEGATOR

And here is the transcript from running the contract when Charlie asserts that the delegation did not occur:

Deposit 100,000 units of PIGY into account of BOBS POOL as BOBS POOL
Participant CHARLIE THE ORACLE chooses the value 0 for choice with id "ALICE DELEGATED FOR 2 EPOCHS"
The contract pays 100,000 units of PIGY to participant BOBS POOL

Here you can see that the second close caused the refund to Bob.

By the way, here is a discussion about using simple scripts to obtain a result similar to the Marlowe contract. It isn’t a practical solution yet because the commonly used wallets don’t support multiple wallets witnessing the same transaction, and it also requires a sophisticated delegator.

P.S. Maybe a fairer version of the contract would be for Alice to get 50,000 PIGY if Charlie doesn’t make any choice: to do this, the third close should be replaced by paying Alice half and letting the remainder be refunded to Bob.


Sorry, one more observation: Having thought about this some more, I believe that one could eliminate the need for an oracle if (1) Alice is willing to deposit ADA instead of just delegating it and (2) the Marlowe contract’s staking address is delegated to Bob’s Pool. (My understanding is that smart contracts can delegate and stake, though this is not expressible in Blockly.) In the diagram below you can see that Alice’s funds are completely safe because there is no option to pay them to Bob, and Alice can remove them at any time, though Alice wouldn’t receive the reward if they are withdrawn early.


Hi Thank you for responding my problem. I still confuse how oracle work. At first, before I make this testing. My main concern is how Bobs know that Alice is already delegate for 2 epoch. Then I think, in the cardano node there must be some marker or something that can give mark or sign that this alice already stay for 2 epoch. I have this idea because there is “Live Stake” and “Active Stake” right? So when Alice live stake become active stake, then it tells Bob that Alice already there for 2 epoch. Is that possible to use this sign or marker or whatever that is to be a contract for Bob to give Alice Pigy?

Thank you

The Marlowe documentation says “Oracles are being developed for the Cardano blockchain in general, and will be available for use within Marlowe on Cardano.” With luck, one of those oracles will provide exactly the staking and delegation information (derived from the data on the Cardano nodes or from services like blockfrost.io) that contracts like the one you envision need. (Judging from posts on this forum and the activities of stake pools, it looks like many folks would want to use such an oracle.) I think this is an area where we have to monitor the ongoing developments around Marlowe and Plutus.

1 Like

Wait, so you mean that such information that I need will be available when Oracles are ready?
If I want to try my project right now either in testnet or mainnet. That kind of services is available from blockfrost.io. Right?

But, right now. We still have to wait until Marlowe or Plutus are working with the Goguen. Right?

I haven’t seen detail about exactly what will be available and when from the larger Oracle projects, but I think it is worth paying attention to see what they announce.

As of today, we have the Marlowe and Plutus playgrounds. When Marlowe and Plutus work on testnet (hopefully very soon), one can start experimenting there with contracts that involve “home grown” oracles or workarounds. (It sounds like August is the target for mainnet, but that could change.) Over the next six months, there should be many new capabilities and possibilities, both on-chain and off-chain. I suggest focusing on the non-oracle parts for now, but checking the news and examples about how to use and create oracles.

In your example contract, perhaps one could approximate the action of an oracle by having Charlie send 1 ADA to the contract only if they had verified (from querying blockfrost.io or a staking-information website/API) that Alice was delegated to Bob’s pool; receiving that 1 ADA from Charlie would trigger the PIGY reward. (“Charlie” could be an automated process run by a trusted third party.) I’m curious if others have ideas for implementing or approximating oracles for delegation information?

Thanks for all your effort. So, I guess I’ll wait for awhile. Until at least the smart contract work in testnet. But, I know what I waiting for, and try to prepare before the smart contract hit the mainnet.
Once again, Thank you

1 Like

FYI: Here is an example of an oracle in a Plutus contract. A similar strategy would perhaps work for Marlowe.

Hi I try to watch the video, but I’m not too understand. Then I got this link from the Goguen last update

It has the use case of oracle.
I only understand that Oracle is bringing the off chain data to feed the smart contract. Can you please describe to me what is exactly Oracle are? In the medium level language (I have no haskell background at all).

Thank you very much.

The short answer is that an oracle is a third party that everyone in a smart contract trusts to provide accurate information about things that the on-chain part of the smart contract cannot otherwise know. An oracle is like a referee or an escrow agent. Oracles can be very important for contracts involving high value in situations where the two parties need to trust each other, but do not.

In your PIGY rewards example, Alice might not trust that Bob’s Pool will pay the reward after Alice delegated for two epochs. If Alice trusts Charlie, then the smart contract can be constructed so that it automatically pays the reward when Charlie affirms that Alice did indeed delegate for two epochs. Bob needs to also trust Charlie, because otherwise Charlie could cheat and say the Alice had delegated for two epochs when Alice actually hadn’t, but Bob wouldn’t be able to prevent the contract from automatically paying the reward. (Charlie would likely be an automated computer program, but Charlie’s program was written by a person and is running on a machine controlled by a person: so Alice and Bob have to trust that the program was written correctly and that the person won’t change the program so that it cheats.)

I think that the PIGY reward system would probably work fine without an oracle because delegators like Alice would trust that Bob’s Pool would always pay the correct rewards because the pool always had paid them correctly in the past and the pool’s reputation would suffer greatly if they didn’t pay the rewards they promised. There is a strong incentive for Bob’s Pool to follow through with paying rewards.

Another example of a situation where an oracle is needed would be if Alice loaned Bob the ADA-equivalent of USD$100 today and promised to pay back the ADA-equivalent of USD$110 in one year. They would agree on the ADA-to-USD exchange rate today, but how could the contract know what ADA-to-USD would be in one year and what would happen if Alice and Bob couldn’t agree (or refused to agree) on the ADA-to-USD at the end of the year? If they both trusted Charlie, then the contract could be set up to use whatever Charlie said the ADA-to-USD rate is when the time comes. (Charlie would be a computer program that just looks up the price on an exchange like Coinbase and passes that information along to the smart contract.)

If you want, I can provide a more technical explanation about the information that on-chain parts of a smart contract have versus the information that off-chain parts have. The on-chain part has very little information, but is universally trusted by everyone using Cardano because the block-producing nodes of stake pools verify the on-chain parts of smart contracts. No one necessarily trusts the off-chain parts because the off-chain stuff is not verified by the block-producing nodes.

Thanks for replying my message.
So, this Oracle, this is like another independent project right? Like if you create an Oracle, then when I need it for PIGY token I will use your project, and if there are another token, RATS then they will use the same Oracle. Not every project like PIGY or RATS have to make their own Oracle. Am I correct?
If I remember correctly, this Oracle in Ethereum is called Chainlink. Right?

Back to PIGY case,
I see that there will be 2 option for this matter.
Option 1: With Oracle, Which Charlie the Oracle (the third party guy) will hold the Bob’s PIGY and automatically give to Alice, after Alice delegate for 2 epoch.
Option 2: Without Oracle, Which Bob will pay Alice with PIGY directly after Alice delegate for 2 epoch. Are there any chance that I can build some simple Javascript program that can be installed to the stakepool? So, the stakepool can know which one that already delegate for 2 epoch which one are not? What I’m confuse is where can I get this input parameter (about what is Alice address and the time Alice start to delegate to Bob’s pool). How can we look for this items?
If this can be done, then it will be much much easier to develop simple program to SPO to installed in their pool.
Thank you

Yes, many stakepools or other parties can use the same oracle. Right now, I think that nut.link is the major oracle that is actually running on mainnet, though it doesn’t report that delegation data that you want. It sounds that there are several large efforts to deploy oracles on Cardano when the Alonzo fork occurs. There is discussion about this in other parts of this forum: Will Ada develop their own oracle pool on chain? - #3 by Kindergarden.

In Option 1 the oracle doesn’t hold any PIGY or ADA, the oracle only provides information to the transaction.

Yes, in Option 2 a javascript or other program could query a service like pooltool.io to see which addresses are delegating to the stake pool and how long they have been delegated. The program could then do the PIGY reward computations and create transactions to send the right amount of PIGY to the correct addresses. I think that some SPOs are already doing stuff like this: searching this forum or google will probably lead you to them, or maybe they’ll chime into this thread and assist.

Hi isn’t it easy to fake the nut.link oracle when users just look for the key 1968? Everybody could create such metadata

Yes, anyone can create a transaction with the 1968 key. To trust the metadata, the transaction would have to be sent by a trusted address or contain a trusted NFT.

I’m currently running a SOFR and energy-price oracle. Trusting it requires (1) trusting the script that posts the metadata, (2) verifying that the transaction was made by the trusted address, and (3) trusting the operator who holds the signing key for the address. The Plutus-based oracle that I plan to deploy on Alonzo will require (1) trusting the on- and off-chain code, (2) trusting the holder of the control token, and (3) verifying that the data is accompanied by the datum token. Decentralized oracles aggregate individual oracles to distribute trust and provide cross-checks.