I began to write this application as a translation from Matej’s multi-sig smart contract in the resource listed above. You’ll see many similarities in my contract and his.

In short, the goal was to develop a contract where:

One person creates a contract by depositing some tez into a contract

This same person adds the recipient of the tez to the contract

Whenever both parties have signed this contract (effectively saying they agree to the terms) the funds are released from the contract to the recipient

We start here by importing SmartPy and setting the default values of the smart contract’s storage.

Owners, a Set with the type of address, is a set of the owners that will need to sign the contract to release the funds

OwnersSigned, a Set with the type of address, is a set of the owners that have signed the contract

Sets are used here so if one person calls an entrypoint that has logic to add to the owner or ownersSigned Set it won’t do it b/c Sets don’t add duplicates.

Recipient, with type of address, is the final address that the contract’s funds will be dispersed to

Proposition, with type of Tez, is the amount of tez that will be distributed to the recipient

Now, in order to make this contract useful we need to have somebody create a proposition and add a recipient. That is, create an amount to escrow and a person to escrow receive those funds when the contract has both parties signatures. In comes our first contract entrypoint:

createProposition

So here a user will call the createProposition entrypoint with the amount of Tez they wish to escrow and the recipient of the Tez once the contract has been signed by both parties.

Once this has happened, our owners Set will be populated with both the caller and the recipient. This now gives us a Set (owners) to compare with our ownersSigned Set so that we can have some security about when/who we want to send our funds to.

Thanks to the amazing SmartPy IDE we can now see exactly what our contract looks like at this point:

You can see the contract has the balance of 500TZ that was included in amount param in our createProposition call. You can also see that the recipient param has been included in this call too and has updated our Recipient storage. Finally you can see our Owners storage has been updated with both the caller and the recipient.

Alright, now we need an entrypoint that allows our owners to sign the contract…enter:

signProposition

The signProposition enrypoint allows only owners listed in the owners set to be able to sign the proposition, which will nudge the contract toward execution.

When activating this entrypoint, we add some safety measures to prevent the money from being stolen:

We utilize the canCall helper method which verifies that owners set contains the address of the user calling the signProposition entrypoint. This makes sure that only users who are listed as owners in the contract can sign the contract to execute the trade.

From here, the user is then added to the ownersSigned set. The next helper method will check to see if it’s time to release the smart contract balance to the recipient.

We utilize the checkForExecution helper method which checks that the owners set is the same length as the ownersSigned set and releases the proposition to the recipient if this is the case. Given that users can’t be added to the ownersSigned set unless they’re verified as part of the owners set, this shouldn’t allow for the ability to steal funds. I’m sure there are safer ways to do this, but for this demo this will do.

When the length of owners matches the length of ownersSigned we release the funds to the recipient.

So after the first owner signs the contract state will look like so:

And after the second owner signs the the balance of the contract will then be dispersed to the recipient

Now our funds have been dispersed to the recipient! WOOHOO! 🎉🎉🎉🎉

I hope seeing my process has been slightly helpful. I fully admit this isn’t really a beginner walkthrough, but hopefully it can shed some light on some minor aspect of SmartPy and help you along the way when you’re making your own contracts!!!

Feel free to checkout my personal website here and email me if you have any questions/comments. Thank you.