Skip to main content
Version: Next

Paying out profits from the Taco Shop

In the previous tutorial we have learnt how to setup & interact with the LIGO CLI. Followed an implementation of a simple Taco Shop smart contract for our entrepreneur Pedro.

In this tutorial we will make sure Pedro has access to tokens that people have spent at his shop when buying tacos.


Icons made by Smashicons from www.flaticon.com is licensed by CC 3.0 BY

Analysing the Current Contract

taco-shop.jsligo

namespace TacoShop {
export type taco_supply = { current_stock: nat, max_price: tez };
export type taco_shop_storage = map<nat, taco_supply>;
@entry
function buy_taco(taco_kind_index: nat, taco_shop_storage: taco_shop_storage): [
list<operation>,
taco_shop_storage
] {
/* Retrieve the taco_kind from the contracts storage or fail */
const taco_kind : taco_supply =
match (Map.find_opt (taco_kind_index, taco_shop_storage)) {
when(Some(kind)): kind;
when(None()): failwith ("Unknown kind of taco")
};
const current_purchase_price : tez = taco_kind.max_price / taco_kind.current_stock ;
/* We won't sell tacos if the amount is not correct */
if ((Tezos.get_amount ()) != current_purchase_price) {
return failwith ("Sorry, the taco you are trying to purchase has a different price")
} else {
/* Update the storage decreasing the stock by 1n */
const taco_shop_storage = Map.update (
taco_kind_index,
(Some ({...taco_kind, current_stock : abs (taco_kind.current_stock - 1n) })),
taco_shop_storage );
return [[], taco_shop_storage]
}
};
};
const default_storage: TacoShop.taco_shop_storage =
Map.literal(
list(
[
[1n, { current_stock: 50n, max_price: 50000000mutez }],
[2n, { current_stock: 20n, max_price: 75000000mutez }]
]
)
);

Purchase Price Formula

Pedro's Taco Shop contract currently enables customers to buy tacos, at a price based on a simple formula.

const current_purchase_price : tez =
taco_kind.max_price / taco_kind.current_stock

Designing a Payout Scheme

Pedro is a standalone business owner, and in our case, he does not have to split profits and earnings of the taco shop with anyone. So for the sake of simplicity, we will payout all the earned XTZ directly to Pedro right after a successful purchase.

This means that after all the purchase conditions of our contract are met, e.g., the correct amount is sent to the contract, we will not only decrease the supply of the individual purchased taco kind, but we will also transfer this amount in a subsequent transaction to Pedro's personal address.

Forging a Payout Transaction

Defining the Recipient

In order to send tokens, we will need a receiver address, which, in our case, will be Pedro's personal account. Additionally we will wrap the given address as a contract (unit), which represents either a contract with no parameters, or an implicit account.

const ownerAddress : address = "tz1TGu6TN5GSez2ndXXeDX6LgUDvLzPLqgYV"
const receiver : contract<unit> =
match (Tezos.get_contract_opt(ownerAddress) as option<contract<unit>>) {
when(Some(contract)): contract;
when(None()): (failwith ("Not a contract") as contract<unit>)
}

Would you like to learn more about addresses, contracts and operations in LIGO? Check out the LIGO cheat sheet

Adding the Transaction to the List of Output Operations

Now we can transfer the amount received by buy_taco to Pedro's ownerAddress. We will do so by forging a transaction (unit, amount, receiver) within a list of operations returned at the end of our contract.

const payoutOperation : operation = Tezos.transaction (unit, Tezos.get_amount (), receiver) ;
const operations : list <operation> = [payoutOperation];

Finalising the Contract

taco-shop.jsligo

namespace TacoShop {
export type taco_supply = { current_stock: nat, max_price: tez };
export type taco_shop_storage = map<nat, taco_supply>;
const ownerAddress : address = "tz1TGu6TN5GSez2ndXXeDX6LgUDvLzPLqgYV";
const donationAddress : address = "tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx";
@entry
function buy_taco(taco_kind_index: nat, taco_shop_storage: taco_shop_storage): [
list<operation>,
taco_shop_storage
] {
/* Retrieve the taco_kind from the contracts storage or fail */
const taco_kind: taco_supply =
match(Map.find_opt(taco_kind_index, taco_shop_storage)) {
when (Some(kind)):
kind
when (None):
failwith("Unknown kind of taco")
};
const current_purchase_price: tez =
taco_kind.max_price / taco_kind.current_stock;
/* We won't sell tacos if the amount is not correct */
if ((Tezos.get_amount()) != current_purchase_price) {
return failwith(
"Sorry, the taco you are trying to purchase has a different price"
)
} else {
/* Update the storage decreasing the stock by 1n */
const taco_shop_storage =
Map.update(
taco_kind_index,
(
Some({
...taco_kind, current_stock: abs(taco_kind.current_stock - 1n)
})
),
taco_shop_storage
);
const receiver : contract<unit> =
match ((Tezos.get_contract_opt (ownerAddress))) {
when(Some(contract)): contract;
when(None()): ((failwith ("Not a contract")))
};
const donationReceiver : contract<unit> =
match ((Tezos.get_contract_opt (donationAddress))) {
when(Some(contract)): contract;
when(None()): ((failwith ("Not a contract")))
};
const donationAmount = ((Tezos.get_amount ()) / 10n) as tez;
// Pedro will get 90% of the amount
const op1 = match ((Tezos.get_amount ()) - donationAmount) {
when(Some(x)): Tezos.transaction (unit, x, receiver);
when(None()): failwith ("Insufficient balance")
};
const op2 = Tezos.transaction (unit, donationAmount, donationReceiver);
const operations : list<operation> = [ op1 , op2 ];
return [operations, taco_shop_storage]
}
};
}
const default_storage: TacoShop.taco_shop_storage =
Map.literal(
list(
[
[1n, { current_stock: 50n, max_price: 50000000mutez }],
[2n, { current_stock: 20n, max_price: 75000000mutez }]
]
)
);

Dry-run the Contract

To confirm that our contract is valid, we can dry-run it. As a result, we see a new operation in the list of returned operations to be executed subsequently.

ligo run dry-run taco-shop.jsligo --syntax jsligo -m TacoShop --amount 1 --entry-point buy_taco '1n' "default_storage"
Operation(...bytes) included in the output

Done! Our tokens are no longer locked in the contract, and instead they are sent to Pedro's personal account/wallet.


👼 Bonus: Donating Part of the Profits

Because Pedro is a member of the Speciality Taco Association (STA), he has decided to donate 10% of the earnings to the STA. We will just add a donationAddress to the contract, and compute a 10% donation sum from each taco purchase.

const ownerAddress : address = "tz1TGu6TN5GSez2ndXXeDX6LgUDvLzPLqgYV";
const donationAddress : address = "tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx";
const receiver : contract<unit> =
match ((Tezos.get_contract_opt (ownerAddress)) as option<contract<unit>>) {
when(Some(contract)): contract;
when(None()): ((failwith ("Not a contract")) as contract<unit>)
};
const donationReceiver : contract<unit> =
match ((Tezos.get_contract_opt (donationAddress)) as option<contract<unit>>) {
when(Some(contract)): contract;
when(None()): ((failwith ("Not a contract")) as contract<unit>)
};
const donationAmount = ((Tezos.get_amount ()) / 10n) as tez;
// Pedro will get 90% of the amount
const op1 = match ((Tezos.get_amount ()) - donationAmount) {
when(Some(x)): Tezos.transaction (unit, x, receiver);
when(None()): failwith ("Insufficient balance")
};
const op2 = Tezos.transaction (unit, donationAmount, donationReceiver);
const operations : list<operation> = [ op1 , op2 ];

This will result into two operations being subsequently executed on the blockchain:

  • Donation transfer (10%)
  • Pedro's profits (90%)