├── CONCEPTS.md ├── LICENSE ├── README.md ├── ligo ├── id_strings.ligo └── send_one_tez.ligo └── liquidity ├── .gitignore ├── README.md ├── examples ├── data_publisher │ ├── README.md │ ├── data_publisher.init.tz │ ├── data_publisher.liq │ ├── data_publisher.tz │ ├── data_publisher_hash.liq │ └── data_publisher_hash.tz ├── id.liq ├── id_strings.init.tz ├── id_strings.liq ├── id_strings.tz ├── init_test.init.tz ├── init_test.tz ├── min_transaction_amount │ ├── README.md │ ├── min_transaction_amount.init.tz │ ├── min_transaction_amount.liq │ └── min_transaction_amount.tz ├── reverse_list │ ├── README.md │ ├── reverse_list.init.tz │ ├── reverse_list.liq │ └── reverse_list.tz ├── safe_queue │ ├── README.md │ ├── safe_queue.init.tz │ ├── safe_queue.liq │ ├── safe_queue.tz │ ├── safe_queue_big.init.tz │ ├── safe_queue_big.liq │ └── safe_queue_big.tz ├── send_one_tez │ ├── README.md │ ├── send_one_tez.init.tz │ ├── send_one_tez.liq │ └── send_one_tez.tz └── tezos-clients-data-format.md ├── manager ├── README.md ├── manager.liq └── manager.tz ├── swap_contract ├── README.md ├── swaptokens.liq └── swaptokens.tz └── token ├── README.md ├── token.initializer.tz ├── token.liq └── token.tz /CONCEPTS.md: -------------------------------------------------------------------------------- 1 | ### Concepts 2 | 3 | #### Basic 4 | 5 | ##### Fungible Token 6 | 7 | Implement a transferable fungible token. 8 | 9 | Interface functions: 10 | - Retrieve total supply 11 | - Retrieve account balance for a key 12 | - Mint tokens (permissioned to an owner) 13 | - Transfer tokens from the sender to another key 14 | - Permanently burn the sender's tokens 15 | 16 | A comparable Ethereum standard can be found [here](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md). 17 | 18 | ##### Non-fungible Token 19 | 20 | Implement a transferable non-fungible token. 21 | 22 | Interface functions: 23 | - Retrieve list of all tokens 24 | - Retrieve owner of a particular token 25 | - Retrieve tokens owned by a particular key 26 | - Mint a new token (permissioned to an owner) 27 | - Transfer a particular token from the sender to another key 28 | - Permanently destroy a particular token 29 | 30 | A comparable Ethereum standard can be found [here](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md). 31 | 32 | #### Intermediate 33 | 34 | ##### Order-based DEX 35 | 36 | Implement a simple decentralized exchange to swap specified amounts of fungible tokens with off-chain signed orders. 37 | 38 | Interface functions: 39 | - Deposit tokens 40 | - Withdraw tokens 41 | - Inspect token balances for a particular user & token 42 | - Validate order 43 | - Check order signature 44 | - Execute order match and transfer tokens 45 | 46 | A simple Ethereum implementation can be found [here](https://github.com/etherdelta/smart_contract). A more complex Ethereum implementation can be found [here](https://github.com/wyvernprotocol/wyvern-v3). 47 | 48 | ##### Floating-rate DEX 49 | 50 | Implement an escrow-based floating-rate decentralized exchange roughly as described in [the Uniswap whitepaper](https://hackmd.io/s/HJ9jLsfTz). 51 | 52 | ##### Weighted Multisignature 53 | 54 | Implement a numerical-threshold weighted-key multisignature contract (a bit like a shareholder-association DAO). 55 | 56 | Interface functions: 57 | - Inspect keys, weights, current threshold 58 | - Add a new key, specifying weight 59 | - Remove a key 60 | - Change required threshold to send a transaction 61 | - Sign a transaction (must provide signatures from enough keys) 62 | 63 | A comparable Ethereum implementation can be found [here](https://github.com/ProjectWyvern/wyvern-ethereum/blob/master/contracts/dao/DelegatedShareholderAssociation.sol). 64 | 65 | #### Advanced 66 | 67 | ##### Prediction Market 68 | 69 | Implement a tokenized prediction market based roughly on the model described in [this paper](https://www.truthcoin.info/papers/truthcoin-whitepaper.pdf). 70 | 71 | A description of a comparable Ethereum implementation can be found [here](https://www.augur.net/whitepaper.pdf). 72 | 73 | ##### LR Funding Dispensary 74 | 75 | Implement a smart contract to match donated funds to public goods projects according to the equations in [this paper](https://arxiv.org/pdf/1809.06421.pdf). 76 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ***Contracts in this repository are work-in-progress. Do not use them in production. No support will be provided.*** 2 | 3 | ## Smarter Contracts 4 | 5 | Application-layer smart contracts & tooling. 6 | 7 | Unless otherwise specified, contracts in this repository have *not* been audited and should be considered *experimental*. 8 | 9 | ### Concept Development 10 | 11 | See [CONCEPTS](CONCEPTS.md) for contract ideas. 12 | 13 | ### Liquidity (Tezos) 14 | 15 | Liquidity is a ML-like source language which compiles to Michelson. 16 | 17 | See the Liquidity [website](http://www.liquidity-lang.org/) and [reference documentation](http://www.liquidity-lang.org/doc/) for more information. 18 | 19 | Liquidity contracts live under [liquidity](liquidity). 20 | -------------------------------------------------------------------------------- /ligo/id_strings.ligo: -------------------------------------------------------------------------------- 1 | type storage = unit 2 | 3 | let%entry main 4 | (_parameter : string) 5 | storage = 6 | ( [], storage ) 7 | 8 | -------------------------------------------------------------------------------- /ligo/send_one_tez.ligo: -------------------------------------------------------------------------------- 1 | (* A contract that sends 1 tez to anyone who asks. *) 2 | 3 | type storage = unit 4 | 5 | (* Initialize storage to (). *) 6 | let%init storage = () 7 | 8 | let%entry main 9 | (key : key_hash) 10 | _storage = 11 | let op = Account.transfer ~dest:key ~amount:1tz in 12 | ( [op], () ) -------------------------------------------------------------------------------- /liquidity/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/metastatedev/smarter-contracts/3c7c80cb79b6c6727234e0c92cbfc34225e1319b/liquidity/.gitignore -------------------------------------------------------------------------------- /liquidity/README.md: -------------------------------------------------------------------------------- 1 | ## Liquidity Contracts 2 | 3 | [Liquidity](http://www.liquidity-lang.org/) required. 4 | 5 | Other example Liquidity contracts can be found [here](https://github.com/OCamlPro/liquidity/tree/next/tests/others). 6 | 7 | -------------------------------------------------------------------------------- /liquidity/examples/data_publisher/README.md: -------------------------------------------------------------------------------- 1 | # Deploy 2 | 3 | Using the tezos-client, I ran the following command to deploy the contract: 4 | - I named the contract *publisher* 5 | - **adam** (tz1M5538RTnMHsXgph8nQB7bke1y1gbDBXEx) is the manager 6 | - recall the ```--init``` option is the input for the initial storage. In this example the input is ```'Pair "tz1M5538RTnMHsXgph8nQB7bke1y1gbDBXEx" "init"'```. In Michelson we input a record with two fields as a *pair*. The first item of the *pair* is the address of the publisher (adam with address "tz1M5538RTnMHsXgph8nQB7bke1y1gbDBXEx"). The second item is the data to be published ("init"). 7 | 8 | ~~~~ 9 | tezos-client originate contract publisher for adam transferring 1 from adam running data_publisher.tz --init 'Pair "tz1M5538RTnMHsXgph8nQB7bke1y1gbDBXEx" "init"' --burn-cap 0.774 10 | ~~~~ 11 | ~~~~ 12 | Node is bootstrapped, ready for injecting operations. 13 | Estimated gas: 21524 units (will add 100 for safety) 14 | Estimated storage: 774 bytes added (will add 20 for safety) 15 | Operation successfully injected in the node. 16 | Operation hash is 'opMguaAvqFRf2qVcKt8XAh3og1WxJLDqC24SG6V3xZNCjcGAcS3' 17 | Waiting for the operation to be included... 18 | Operation found in block: BMMaSCEwLDKaXHNBrnMSvpzXGvCeZRWbErkgn1PkA6rdm26k3No (pass: 3, offset: 0) 19 | This sequence of operations was run: 20 | Manager signed operations: 21 | From: tz1M5538RTnMHsXgph8nQB7bke1y1gbDBXEx 22 | Fee to the baker: ꜩ0.002951 23 | Expected counter: 32830 24 | Gas limit: 21624 25 | Storage limit: 794 bytes 26 | Balance updates: 27 | tz1M5538RTnMHsXgph8nQB7bke1y1gbDBXEx .............. -ꜩ0.002951 28 | fees(tz1aWXP237BLwNHJcCD4b3DutCevhqq2T1Z9,1160) ... +ꜩ0.002951 29 | Origination: 30 | From: tz1M5538RTnMHsXgph8nQB7bke1y1gbDBXEx 31 | For: tz1M5538RTnMHsXgph8nQB7bke1y1gbDBXEx 32 | Credit: ꜩ1 33 | Script: 34 | { parameter (option string) ; 35 | storage (pair :storage (address %publisher) (string %data)) ; 36 | code { DUP ; 37 | DIP { CDR @storage_slash_1 } ; 38 | CAR @param_slash_2 ; 39 | DUP @param ; 40 | IF_NONE 41 | { PUSH mutez 1000000 ; 42 | AMOUNT ; 43 | COMPARE ; 44 | LT ; 45 | IF { PUSH string "Not enough money, queries cost 1 tez." ; FAILWITH } 46 | { { DIP { DUP @storage } ; SWAP } ; NIL operation ; PAIR } } 47 | { { DIP { DIP { DUP @storage } ; SWAP } ; SWAP } ; 48 | CAR %publisher ; 49 | SENDER ; 50 | COMPARE ; 51 | NEQ ; 52 | IF { PUSH string "Cannot authenticate." ; FAILWITH } 53 | { { DIP { DIP { DUP @storage } ; SWAP } ; SWAP } ; 54 | CAR %publisher ; 55 | { DIP { DUP @p } ; SWAP } ; 56 | SWAP ; 57 | PAIR %publisher %data ; 58 | NIL operation ; 59 | PAIR } ; 60 | DIP { DROP } } ; 61 | DIP { DROP ; DROP } } } 62 | Initial storage: (Pair "tz1M5538RTnMHsXgph8nQB7bke1y1gbDBXEx" "init") 63 | No delegate for this contract 64 | This origination was successfully applied 65 | Originated contracts: 66 | KT1UHgGu47CmP3MbBWRVBh1MyLXQuaEUUUc2 67 | Storage size: 517 bytes 68 | Paid storage size diff: 517 bytes 69 | Consumed gas: 21524 70 | Balance updates: 71 | tz1M5538RTnMHsXgph8nQB7bke1y1gbDBXEx ... -ꜩ0.517 72 | tz1M5538RTnMHsXgph8nQB7bke1y1gbDBXEx ... -ꜩ0.257 73 | tz1M5538RTnMHsXgph8nQB7bke1y1gbDBXEx ... -ꜩ1 74 | KT1UHgGu47CmP3MbBWRVBh1MyLXQuaEUUUc2 ... +ꜩ1 75 | 76 | New contract KT1UHgGu47CmP3MbBWRVBh1MyLXQuaEUUUc2 originated. 77 | The operation has only been included 0 blocks ago. 78 | We recommend to wait more. 79 | Use command 80 | tezos-client wait for opMguaAvqFRf2qVcKt8XAh3og1WxJLDqC24SG6V3xZNCjcGAcS3 to be included --confirmations 30 --branch BKvbfwbCqtSUnmA8yLh2wvremriKGjswc5FWnquQGFkDXHzGp9H 81 | and/or an external block explorer. 82 | Contract memorized as publisher. 83 | ~~~~ 84 | 85 | # Call to update data 86 | 87 | To update the data, adam has to call the contract, and the storage is updated: 88 | 89 | ~~~~ 90 | tezos-client transfer 0 from adam to publisher --arg 'Some "update"' --burn-cap 0.002 91 | ~~~~ 92 | ~~~~ 93 | Node is bootstrapped, ready for injecting operations. 94 | Estimated gas: 19611 units (will add 100 for safety) 95 | Estimated storage: 2 bytes added (will add 20 for safety) 96 | Operation successfully injected in the node. 97 | Operation hash is 'oofkDT4MZQHqCeGeBYspwbTAu8kRooczZK7NdoswLa2HZJHQ5tG' 98 | Waiting for the operation to be included... 99 | Operation found in block: BKwCsSaRGzVAHYxnqDPitDxUJU8vEezLcUjNPPU9n4i8rxEgMpi (pass: 3, offset: 0) 100 | This sequence of operations was run: 101 | Manager signed operations: 102 | From: tz1M5538RTnMHsXgph8nQB7bke1y1gbDBXEx 103 | Fee to the baker: ꜩ0.002241 104 | Expected counter: 32831 105 | Gas limit: 19711 106 | Storage limit: 22 bytes 107 | Balance updates: 108 | tz1M5538RTnMHsXgph8nQB7bke1y1gbDBXEx .............. -ꜩ0.002241 109 | fees(tz3Q67aMz7gSMiQRcW729sXSfuMtkyAHYfqc,1160) ... +ꜩ0.002241 110 | Transaction: 111 | Amount: ꜩ0 112 | From: tz1M5538RTnMHsXgph8nQB7bke1y1gbDBXEx 113 | To: KT1UHgGu47CmP3MbBWRVBh1MyLXQuaEUUUc2 114 | Parameter: (Some "update") 115 | This transaction was successfully applied 116 | Updated storage: 117 | (Pair 0x00000fb47b79f9e09a7078c52738196d68f542ec69d2 "update") 118 | Storage size: 519 bytes 119 | Paid storage size diff: 2 bytes 120 | Consumed gas: 19611 121 | Balance updates: 122 | tz1M5538RTnMHsXgph8nQB7bke1y1gbDBXEx ... -ꜩ0.002 123 | 124 | The operation has only been included 0 blocks ago. 125 | We recommend to wait more. 126 | Use command 127 | tezos-client wait for oofkDT4MZQHqCeGeBYspwbTAu8kRooczZK7NdoswLa2HZJHQ5tG to be included --confirmations 30 --branch BKnM5rJBttX4V6kUCca98wJAKRMuFZDpHFkmyejhNdNZU3RTijp 128 | and/or an external block explorer. 129 | ~~~~ 130 | 131 | When bob calls the contract to update the storage, the call fails because bob isn't the specified publisher: 132 | 133 | ~~~~ 134 | tezos-client transfer 0 from bob to publisher --arg 'Some "update"' --burn-cap 0.002 135 | ~~~~ 136 | ~~~~ 137 | Node is bootstrapped, ready for injecting operations. 138 | This simulation failed: 139 | Manager signed operations: 140 | From: tz1WGhT231BgqK7CrjbMB5xDsbdDSBJZiw5E 141 | Fee to the baker: ꜩ0 142 | Expected counter: 32826 143 | Gas limit: 800000 144 | Storage limit: 600000 bytes 145 | Transaction: 146 | Amount: ꜩ0 147 | From: tz1WGhT231BgqK7CrjbMB5xDsbdDSBJZiw5E 148 | To: KT1UHgGu47CmP3MbBWRVBh1MyLXQuaEUUUc2 149 | Parameter: (Some "update") 150 | This operation FAILED. 151 | 152 | Runtime error in contract KT1UHgGu47CmP3MbBWRVBh1MyLXQuaEUUUc2: 153 | 01: { parameter (option string) ; 154 | 02: storage (pair :storage (address %publisher) (string %data)) ; 155 | 03: code { DUP ; 156 | 04: DIP { CDR @storage_slash_1 } ; 157 | 05: CAR @param_slash_2 ; 158 | 06: DUP @param ; 159 | 07: IF_NONE 160 | 08: { PUSH mutez 1000000 ; 161 | 09: AMOUNT ; 162 | 10: COMPARE ; 163 | 11: LT ; 164 | 12: IF { PUSH string "Not enough money, queries cost 1 tez." ; FAILWITH } 165 | 13: { { DIP { DUP @storage } ; SWAP } ; NIL operation ; PAIR } } 166 | 14: { { DIP { DIP { DUP @storage } ; SWAP } ; SWAP } ; 167 | 15: CAR %publisher ; 168 | 16: SENDER ; 169 | 17: COMPARE ; 170 | 18: NEQ ; 171 | 19: IF { PUSH string "Cannot authenticate." ; FAILWITH } 172 | 20: { { DIP { DIP { DUP @storage } ; SWAP } ; SWAP } ; 173 | 21: CAR %publisher ; 174 | 22: { DIP { DUP @p } ; SWAP } ; 175 | 23: SWAP ; 176 | 24: PAIR %publisher %data ; 177 | 25: NIL operation ; 178 | 26: PAIR } ; 179 | 27: DIP { DROP } } ; 180 | 28: DIP { DROP ; DROP } } } 181 | At line 19 characters 55 to 63, 182 | script reached FAILWITH instruction 183 | with "Cannot authenticate." 184 | Fatal error: 185 | transfer simulation failed 186 | ~~~~ 187 | 188 | # Call to query data 189 | 190 | When the input data is *None*, it is a data query, and the contract checks that transfer amount is no less than 1tz. When the amount is less than 1tz, a runtime error occurs: 191 | 192 | ~~~~ 193 | tezos-client transfer 0 from bob to publisher --arg 'None' --burn-cap 0.002 194 | ~~~~ 195 | ~~~~ 196 | Node is bootstrapped, ready for injecting operations. 197 | This simulation failed: 198 | Manager signed operations: 199 | From: tz1WGhT231BgqK7CrjbMB5xDsbdDSBJZiw5E 200 | Fee to the baker: ꜩ0 201 | Expected counter: 32826 202 | Gas limit: 800000 203 | Storage limit: 600000 bytes 204 | Transaction: 205 | Amount: ꜩ0 206 | From: tz1WGhT231BgqK7CrjbMB5xDsbdDSBJZiw5E 207 | To: KT1UHgGu47CmP3MbBWRVBh1MyLXQuaEUUUc2 208 | Parameter: None 209 | This operation FAILED. 210 | 211 | Runtime error in contract KT1UHgGu47CmP3MbBWRVBh1MyLXQuaEUUUc2: 212 | 01: { parameter (option string) ; 213 | 02: storage (pair :storage (address %publisher) (string %data)) ; 214 | 03: code { DUP ; 215 | 04: DIP { CDR @storage_slash_1 } ; 216 | 05: CAR @param_slash_2 ; 217 | 06: DUP @param ; 218 | 07: IF_NONE 219 | 08: { PUSH mutez 1000000 ; 220 | 09: AMOUNT ; 221 | 10: COMPARE ; 222 | 11: LT ; 223 | 12: IF { PUSH string "Not enough money, queries cost 1 tez." ; FAILWITH } 224 | 13: { { DIP { DUP @storage } ; SWAP } ; NIL operation ; PAIR } } 225 | 14: { { DIP { DIP { DUP @storage } ; SWAP } ; SWAP } ; 226 | 15: CAR %publisher ; 227 | 16: SENDER ; 228 | 17: COMPARE ; 229 | 18: NEQ ; 230 | 19: IF { PUSH string "Cannot authenticate." ; FAILWITH } 231 | 20: { { DIP { DIP { DUP @storage } ; SWAP } ; SWAP } ; 232 | 21: CAR %publisher ; 233 | 22: { DIP { DUP @p } ; SWAP } ; 234 | 23: SWAP ; 235 | 24: PAIR %publisher %data ; 236 | 25: NIL operation ; 237 | 26: PAIR } ; 238 | 27: DIP { DROP } } ; 239 | 28: DIP { DROP ; DROP } } } 240 | At line 12 characters 72 to 80, 241 | script reached FAILWITH instruction 242 | with "Not enough money, queries cost 1 tez." 243 | Fatal error: 244 | transfer simulation failed 245 | ~~~~ 246 | 247 | When the amount is no less than 1tz, the call is successful: 248 | 249 | ~~~~ 250 | tezos-client transfer 1 from bob to publisher --arg 'None' --burn-cap 0.002 251 | ~~~~ 252 | ~~~~ 253 | Node is bootstrapped, ready for injecting operations. 254 | Estimated gas: 19482 units (will add 100 for safety) 255 | Estimated storage: no bytes added 256 | Operation successfully injected in the node. 257 | Operation hash is 'ong5LGTB958ErYi3aeXTRTYbHUbbKPkhJtMy4eH3UPzqT1P5Ebi' 258 | Waiting for the operation to be included... 259 | Operation found in block: BMQEjBUCzkKzNn9wBt5g9FRdNhdbgDofodhf1qTUkLmNiPbMYZa (pass: 3, offset: 0) 260 | This sequence of operations was run: 261 | Manager signed operations: 262 | From: tz1WGhT231BgqK7CrjbMB5xDsbdDSBJZiw5E 263 | Fee to the baker: ꜩ0.002219 264 | Expected counter: 32826 265 | Gas limit: 19582 266 | Storage limit: 0 bytes 267 | Balance updates: 268 | tz1WGhT231BgqK7CrjbMB5xDsbdDSBJZiw5E .............. -ꜩ0.002219 269 | fees(tz1boot1pK9h2BVGXdyvfQSv8kd1LQM6H889,1160) ... +ꜩ0.002219 270 | Transaction: 271 | Amount: ꜩ1 272 | From: tz1WGhT231BgqK7CrjbMB5xDsbdDSBJZiw5E 273 | To: KT1UHgGu47CmP3MbBWRVBh1MyLXQuaEUUUc2 274 | Parameter: None 275 | This transaction was successfully applied 276 | Updated storage: 277 | (Pair 0x00000fb47b79f9e09a7078c52738196d68f542ec69d2 "update") 278 | Storage size: 519 bytes 279 | Consumed gas: 19482 280 | Balance updates: 281 | tz1WGhT231BgqK7CrjbMB5xDsbdDSBJZiw5E ... -ꜩ1 282 | KT1UHgGu47CmP3MbBWRVBh1MyLXQuaEUUUc2 ... +ꜩ1 283 | 284 | The operation has only been included 0 blocks ago. 285 | We recommend to wait more. 286 | Use command 287 | tezos-client wait for ong5LGTB958ErYi3aeXTRTYbHUbbKPkhJtMy4eH3UPzqT1P5Ebi to be included --confirmations 30 --branch BLTusHoy24S2S4NmV55hKWb4mMZoouv66UrFy18cDU5rECDGbef 288 | and/or an external block explorer. 289 | ~~~~ 290 | 291 | One can think of the query function as a donation channel, because the audience can access the data without paying. One can ask the tezos-client the contract storage with the following command: 292 | 293 | ```tezos-client get script storage for [contract address/alias]``` -------------------------------------------------------------------------------- /liquidity/examples/data_publisher/data_publisher.init.tz: -------------------------------------------------------------------------------- 1 | Pair "edpku5LTYXxXKTjmzj9qtHR6TTYH1K6JU7bFe8MyJs6qabUs9kiZes" (Pair 0 "init") -------------------------------------------------------------------------------- /liquidity/examples/data_publisher/data_publisher.liq: -------------------------------------------------------------------------------- 1 | (* A contract that publishes data securely. *) 2 | 3 | type storage = { 4 | publisher : address; 5 | data : string 6 | } 7 | 8 | let%entry main (param : (string option)) storage = 9 | match param with 10 | | None -> 11 | if Current.amount() < 1tz then 12 | failwith "Not enough money, queries cost 1 tez." 13 | else 14 | ([], storage) 15 | | Some p -> 16 | if Current.sender () <> storage.publisher then 17 | failwith "Cannot authenticate." 18 | else 19 | ([], storage.data <- p) -------------------------------------------------------------------------------- /liquidity/examples/data_publisher/data_publisher.tz: -------------------------------------------------------------------------------- 1 | parameter (option string); 2 | storage (pair :storage (address %publisher) (string %data)); 3 | code { DUP ; 4 | DIP { CDR @storage_slash_1 } ; 5 | CAR @param_slash_2 ; 6 | DUP @param ; 7 | IF_NONE 8 | { PUSH mutez 1000000 ; 9 | AMOUNT ; 10 | COMPARE ; 11 | LT ; 12 | IF { PUSH string "Not enough money, queries cost 1 tez." ; FAILWITH } 13 | { DUUP @storage ; NIL operation ; PAIR } } 14 | { DUUUP @storage ; 15 | CAR %publisher ; 16 | SENDER ; 17 | COMPARE ; 18 | NEQ ; 19 | IF { PUSH string "Cannot authenticate." ; FAILWITH } 20 | { DUUUP @storage ; 21 | CAR %publisher ; 22 | DUUP @p ; 23 | SWAP ; 24 | PAIR %publisher %data ; 25 | NIL operation ; 26 | PAIR } ; 27 | DIP { DROP } } ; 28 | DIP { DROP ; DROP } }; 29 | -------------------------------------------------------------------------------- /liquidity/examples/data_publisher/data_publisher_hash.liq: -------------------------------------------------------------------------------- 1 | (* A contract that publishes data securely. *) 2 | 3 | type storage = { 4 | publisher : address; 5 | data : bytes 6 | } 7 | 8 | let%entry main (param : string) storage = 9 | if (Crypto.sha512 (Bytes.pack param)) = storage.data then 10 | if Current.amount() < 1tz then 11 | failwith "Not enough money, queries cost 1 tez." 12 | else 13 | ([], storage) 14 | else if Current.sender () <> storage.publisher then 15 | failwith "Cannot authenticate." 16 | else 17 | ([], storage.data <- Crypto.sha512 (Bytes.pack param)) -------------------------------------------------------------------------------- /liquidity/examples/data_publisher/data_publisher_hash.tz: -------------------------------------------------------------------------------- 1 | parameter string; 2 | storage (pair :storage (address %publisher) (bytes %data)); 3 | code { DUP ; 4 | DIP { CDR @storage_slash_1 } ; 5 | CAR @param_slash_2 ; 6 | DUUP @storage ; 7 | CDR %data ; 8 | DUUP @param ; 9 | PACK ; 10 | SHA512 ; 11 | COMPARE ; 12 | EQ ; 13 | IF { PUSH mutez 1000000 ; 14 | AMOUNT ; 15 | COMPARE ; 16 | LT ; 17 | IF { PUSH string "Not enough money, queries cost 1 tez." ; FAILWITH } 18 | { DUUP @storage ; NIL operation ; PAIR } } 19 | { DUUP @storage ; 20 | CAR %publisher ; 21 | SENDER ; 22 | COMPARE ; 23 | NEQ ; 24 | IF { PUSH string "Cannot authenticate." ; FAILWITH } 25 | { DUUP @storage ; 26 | CAR %publisher ; 27 | DUUP @param ; 28 | PACK ; 29 | SHA512 ; 30 | SWAP ; 31 | PAIR %publisher %data ; 32 | NIL operation ; 33 | PAIR } } ; 34 | DIP { DROP ; DROP } }; 35 | -------------------------------------------------------------------------------- /liquidity/examples/id.liq: -------------------------------------------------------------------------------- 1 | type storage = string * (* 0: S *) 2 | timestamp * (* 1: T *) 3 | (tez * tez) * (* 2: P N *) 4 | UnitContract.instance * (* 3: X *) 5 | UnitContract.instance * (* 4: A *) 6 | UnitContract.instance (* 5: B *) 7 | 8 | let%entry main 9 | (_parameter : timestamp) 10 | (storage : storage) = 11 | ( [], storage ) 12 | -------------------------------------------------------------------------------- /liquidity/examples/id_strings.init.tz: -------------------------------------------------------------------------------- 1 | Unit -------------------------------------------------------------------------------- /liquidity/examples/id_strings.liq: -------------------------------------------------------------------------------- 1 | (* Identity contract for strings. *) 2 | 3 | type storage = unit 4 | 5 | let%entry main 6 | (_parameter : string) 7 | storage = 8 | ( [], storage ) 9 | 10 | (* Initialize storage to (). *) 11 | let%init storage = () 12 | -------------------------------------------------------------------------------- /liquidity/examples/id_strings.tz: -------------------------------------------------------------------------------- 1 | parameter string; 2 | storage unit; 3 | code { CDR @storage_slash_1 ; NIL operation ; PAIR }; 4 | -------------------------------------------------------------------------------- /liquidity/examples/init_test.init.tz: -------------------------------------------------------------------------------- 1 | { "KT1LLcCCB9Fr1hrkGzfdiJ9u3ZajbdckBFrF" ; 2 | "KT1LLcCCB9Fr1hrkGzfdiJ9u3ZajbdckBFrF" } -------------------------------------------------------------------------------- /liquidity/examples/init_test.tz: -------------------------------------------------------------------------------- 1 | parameter string; 2 | storage (set address); 3 | code { CDR @storage_slash_1 ; NIL operation ; PAIR }; 4 | -------------------------------------------------------------------------------- /liquidity/examples/min_transaction_amount/README.md: -------------------------------------------------------------------------------- 1 | This contract checks that an incoming amount of tz is above a certain amount before accepting the amount. 2 | 3 | In this contract the storage is *the minimum amount of tz the contract will accept*. The parameter is *unit*. 4 | 5 | To verify that the script only let transfers that are above or equal to the specified amount to go through: 6 | 7 | (1) compile the min_transaction_amount.liq to .tz (by running ```liquidity min_transaction_amount.liq```) 8 | 9 | (2) typecheck the script with 10 | 11 | ```tezos-client typecheck script [path to]/min_transaction_amount.tz``` 12 | 13 | (3) run a tezos-node in the local network. Make sure it's bootstrapped. See [this](http://tezos.gitlab.io/mainnet/introduction/howtouse.html#rpc-interface). 14 | 15 | (4) run the tezos-client ```run script``` commands as follows: 16 | 17 | - This command should execute a transfer of 11tz, because it's higher than the 10tz required minimum. 18 | 19 | ```tezos-client run script [path to]/min_transaction_amount.tz on storage '10000000' and input Unit --trace-stack --amount '11.0'``` 20 | 21 | - This command should fail to execute, because the transfer amount if 9tz, less than the required minimum. 22 | ```tezos-client run script [path to]/min_transaction_amount.tz on storage '10000000' and input Unit --trace-stack --amount '9.00'``` 23 | 24 | Here is an excerpt of the command details from [this link](https://tezos.gitlab.io/alphanet/api/cli-commands.html#client-manual): 25 | 26 | ----------------------------------------------------- 27 | 28 | run script ***src*** on storage ***storage*** and input ***storage*** [--trace-stack] [--amount ] [-q --no-print-source] 29 | 30 | Ask the node to run a script. 31 | 32 | ***src***: source script 33 | Can be a script name, a file or a raw script literal. If the parameter is 34 | not the name of an existing script, the client will look for a file 35 | containing a script, and if it does not exist, the argument will be read as 36 | a raw script. 37 | Use 'alias:name', 'file:path' or 'text:literal' to disable autodetect. 38 | ***storage***: the storage data 39 | ***storage***: the input data 40 | ```--trace-stack```: show the stack after each step 41 | ```--amount ```: amount of the transfer in ꜩ 42 | Defaults to `0.05`. 43 | ```-q --no-print-source```: don't print the source code 44 | If an error is encountered, the client will print the contract's source 45 | code by default. 46 | This option disables this behaviour. 47 | 48 | ------------------------------------------------------- 49 | 50 | The storage or input data have to be in a specific format. See [this](https://github.com/cryptiumlabs/smarter-contracts/blob/master/liquidity/examples/tezos-clients-data-format.md). 51 | 52 | For this contract, the *storage data* is the storage, i.e., the minimum amount it is going to accept. The *input data* is the parameter of the contract, which is unit. The *storage data* you pass in has to be an integer. The integer represents 1/1000000 of 1tz. That is, 1tz in the liquidity language translates to 1000000 in the *storage data* parameter. However, the ```--amount``` option you pass in is in tz! 53 | 54 | If this contract works properly, a transfer can only go through if the transfer amount (passed in with the ```--amount``` option) is greater than the storage data. 55 | 56 | You can see the script working, with the specified amount of 10tz, by passing in an amount above and below 10 tz respectively: 57 | 58 | ### When transfer amount is higher than the required amount 59 | 60 | ```tezos-client run script [path to]/min_transaction_amount.tz on storage '10000000' and input Unit --trace-stack --amount '11.0'``` 61 | 62 | The ```--trace-stack``` option shows the stack operations. The tezos-client ran the script without problem, because 11tz is larger than 10 tz. The output: 63 | ~~~~ 64 | storage 65 | 10000000 66 | emitted operations 67 | 68 | trace 69 | - location: 6 (remaining gas: 399373 units remaining) 70 | [ (Pair Unit 10000000) ] 71 | - location: 7 (remaining gas: 399370 units remaining) 72 | [ (Pair Unit 10000000) 73 | (Pair Unit 10000000) ] 74 | - location: 10 (remaining gas: 399364 units remaining) 75 | [ 10000000 @min_amount_slash_1 ] 76 | - location: 9 (remaining gas: 399363 units remaining) 77 | [ 10000000 @min_amount_slash_1 ] 78 | - location: 8 (remaining gas: 399363 units remaining) 79 | [ (Pair Unit 10000000) 80 | 10000000 @min_amount_slash_1 ] 81 | - location: 11 (remaining gas: 399360 units remaining) 82 | [ Unit @__slash_2 83 | 10000000 @min_amount_slash_1 ] 84 | - location: 15 (remaining gas: 399353 units remaining) 85 | [ 10000000 @min_amount 86 | 10000000 @min_amount_slash_1 ] 87 | - location: 14 (remaining gas: 399352 units remaining) 88 | [ 10000000 @min_amount 89 | 10000000 @min_amount_slash_1 ] 90 | - location: 13 (remaining gas: 399352 units remaining) 91 | [ Unit @__slash_2 92 | 10000000 @min_amount 93 | 10000000 @min_amount_slash_1 ] 94 | - location: 16 (remaining gas: 399349 units remaining) 95 | [ 10000000 @min_amount 96 | Unit @__slash_2 97 | 10000000 @min_amount_slash_1 ] 98 | - location: -1 (remaining gas: 399348 units remaining) 99 | [ 10000000 @min_amount 100 | Unit @__slash_2 101 | 10000000 @min_amount_slash_1 ] 102 | - location: 17 (remaining gas: 399345 units remaining) 103 | [ 11000000 @amount 104 | 10000000 @min_amount 105 | Unit @__slash_2 106 | 10000000 @min_amount_slash_1 ] 107 | - location: 18 (remaining gas: 399342 units remaining) 108 | [ 1 109 | Unit @__slash_2 110 | 10000000 @min_amount_slash_1 ] 111 | - location: 19 (remaining gas: 399339 units remaining) 112 | [ False 113 | Unit @__slash_2 114 | 10000000 @min_amount_slash_1 ] 115 | - location: 27 (remaining gas: 399332 units remaining) 116 | [ Unit 117 | Unit @__slash_2 118 | 10000000 @min_amount_slash_1 ] 119 | - location: 26 (remaining gas: 399331 units remaining) 120 | [ Unit 121 | Unit @__slash_2 122 | 10000000 @min_amount_slash_1 ] 123 | - location: 28 (remaining gas: 399328 units remaining) 124 | [ Unit @__slash_2 125 | 10000000 @min_amount_slash_1 ] 126 | - location: 29 (remaining gas: 399325 units remaining) 127 | [ 10000000 @min_amount_slash_1 ] 128 | - location: 30 (remaining gas: 399319 units remaining) 129 | [ {} 130 | 10000000 @min_amount_slash_1 ] 131 | - location: 32 (remaining gas: 399311 units remaining) 132 | [ (Pair {} 10000000) ] 133 | - location: -1 (remaining gas: 399310 units remaining) 134 | [ (Pair {} 10000000) ] 135 | ~~~~ 136 | ### When transfer amount is lower than the required amount. 137 | 138 | ```tezos-client run script [path to]/min_transaction_amount.tz on storage '10000000' and input Unit --trace-stack --amount '9.00'``` 139 | 140 | Because the transfer amount is less than the required amount, a runtime error occur as desired. The output: 141 | ~~~~ 142 | Runtime error in contract KT1BEqzn5Wx8uJrZNvuS9DVHmLvG9td3fDLi: 143 | 144 | 01: parameter unit; 145 | 02: storage mutez; 146 | 03: code { DUP ; 147 | 04: DIP { CDR @min_amount_slash_1 } ; 148 | 05: CAR @__slash_2 ; 149 | 06: DUUP @min_amount ; 150 | 07: AMOUNT ; 151 | 08: COMPARE ; 152 | 09: LT ; 153 | 10: IF { PUSH string "Amount is less than the required minimum." ; FAILWITH } 154 | 11: { UNIT } ; 155 | 12: DROP ; 156 | 13: DROP ; 157 | 14: NIL operation ; 158 | 15: PAIR }; 159 | 16: 160 | At line 10 characters 70 to 78, 161 | script reached FAILWITH instruction 162 | with "Amount is less than the required minimum." 163 | trace 164 | - location: 6 (remaining gas: 399373 units remaining) 165 | [ (Pair Unit 10000000) ] 166 | - location: 7 (remaining gas: 399370 units remaining) 167 | [ (Pair Unit 10000000) 168 | (Pair Unit 10000000) ] 169 | - location: 10 (remaining gas: 399364 units remaining) 170 | [ 10000000 @min_amount_slash_1 ] 171 | - location: 9 (remaining gas: 399363 units remaining) 172 | [ 10000000 @min_amount_slash_1 ] 173 | - location: 8 (remaining gas: 399363 units remaining) 174 | [ (Pair Unit 10000000) 175 | 10000000 @min_amount_slash_1 ] 176 | - location: 11 (remaining gas: 399360 units remaining) 177 | [ Unit @__slash_2 178 | 10000000 @min_amount_slash_1 ] 179 | - location: 15 (remaining gas: 399353 units remaining) 180 | [ 10000000 @min_amount 181 | 10000000 @min_amount_slash_1 ] 182 | - location: 14 (remaining gas: 399352 units remaining) 183 | [ 10000000 @min_amount 184 | 10000000 @min_amount_slash_1 ] 185 | - location: 13 (remaining gas: 399352 units remaining) 186 | [ Unit @__slash_2 187 | 10000000 @min_amount 188 | 10000000 @min_amount_slash_1 ] 189 | - location: 16 (remaining gas: 399349 units remaining) 190 | [ 10000000 @min_amount 191 | Unit @__slash_2 192 | 10000000 @min_amount_slash_1 ] 193 | - location: -1 (remaining gas: 399348 units remaining) 194 | [ 10000000 @min_amount 195 | Unit @__slash_2 196 | 10000000 @min_amount_slash_1 ] 197 | - location: 17 (remaining gas: 399345 units remaining) 198 | [ 9000000 @amount 199 | 10000000 @min_amount 200 | Unit @__slash_2 201 | 10000000 @min_amount_slash_1 ] 202 | - location: 18 (remaining gas: 399342 units remaining) 203 | [ -1 204 | Unit @__slash_2 205 | 10000000 @min_amount_slash_1 ] 206 | - location: 19 (remaining gas: 399339 units remaining) 207 | [ True 208 | Unit @__slash_2 209 | 10000000 @min_amount_slash_1 ] 210 | - location: 22 (remaining gas: 399332 units remaining) 211 | [ "Amount is less than the required minimum." 212 | Unit @__slash_2 213 | 10000000 @min_amount_slash_1 ] 214 | Fatal error: 215 | error running script 216 | ~~~~ 217 | -------------------------------------------------------------------------------- /liquidity/examples/min_transaction_amount/min_transaction_amount.init.tz: -------------------------------------------------------------------------------- 1 | 10000000 -------------------------------------------------------------------------------- /liquidity/examples/min_transaction_amount/min_transaction_amount.liq: -------------------------------------------------------------------------------- 1 | (* A contract that rejects payments below the specified amount. *) 2 | 3 | type storage = tez 4 | 5 | (* The parameter is (). *) 6 | let%entry main 7 | () 8 | min_amount = 9 | if Current.amount () < min_amount then 10 | Current.failwith ("Amount is less than the required minimum."); 11 | ( [], min_amount ) -------------------------------------------------------------------------------- /liquidity/examples/min_transaction_amount/min_transaction_amount.tz: -------------------------------------------------------------------------------- 1 | parameter unit; 2 | storage mutez; 3 | code { DUP ; 4 | DIP { CDR @min_amount_slash_1 } ; 5 | CAR @__slash_2 ; 6 | DUUP @min_amount ; 7 | AMOUNT ; 8 | COMPARE ; 9 | LT ; 10 | IF { PUSH string "Amount is less than the required minimum." ; FAILWITH } 11 | { UNIT } ; 12 | DROP ; 13 | DROP ; 14 | NIL operation ; 15 | PAIR }; 16 | -------------------------------------------------------------------------------- /liquidity/examples/reverse_list/README.md: -------------------------------------------------------------------------------- 1 | This contract reverses a given list. 2 | 3 | In this contract the storage is the output string, of type string list. The parameter is the input string, of type string list. 4 | 5 | To verify that the script reverses the list: 6 | 7 | (1) compile reverse_list.liq to .tz (by running ```liquidity reverse_list.liq```) 8 | 9 | (2) typecheck the script with 10 | 11 | ```tezos-client typecheck script [path to]/min_transaction_amount.tz``` 12 | 13 | (3) run a tezos-node in the local network. Make sure it's bootstrapped. See [this](http://tezos.gitlab.io/mainnet/introduction/howtouse.html#rpc-interface). 14 | 15 | (4) run the tezos-client *run script* command as follows: 16 | 17 | ```tezos-client run script [path to]/reverse_list.tz on storage '{}' and input '{ "abc" ; "def" ; "x" ; "y" ; "z"}'``` 18 | 19 | Here is an excerpt of the command details from [this link](https://tezos.gitlab.io/alphanet/api/cli-commands.html#client-manual): 20 | 21 | ----------------------------------------------------------------------- 22 | 23 | run script ***src*** on storage ***storage*** and input ***storage*** [--trace-stack] [--amount ] [-q --no-print-source] 24 | 25 | Ask the node to run a script. 26 | 27 | ***src***: source script 28 | Can be a script name, a file or a raw script literal. If the parameter is 29 | not the name of an existing script, the client will look for a file 30 | containing a script, and if it does not exist, the argument will be read as 31 | a raw script. 32 | Use 'alias:name', 'file:path' or 'text:literal' to disable autodetect. 33 | ***storage***: the storage data 34 | ***storage***: the input data 35 | ```--trace-stack```: show the stack after each step 36 | ```--amount ```: amount of the transfer in ꜩ 37 | Defaults to `0.05`. 38 | ```-q --no-print-source```: don't print the source code 39 | If an error is encountered, the client will print the contract's source 40 | code by default. 41 | This option disables this behaviour. 42 | 43 | ----------------------------------------------------------------------------- 44 | 45 | The storage or input data have to be in a specific format. See [this](https://github.com/cryptiumlabs/smarter-contracts/blob/master/liquidity/examples/tezos-clients-data-format.md). 46 | 47 | For this contract, the *storage data* is the storage. It is of type string list. It will store the reversed list. It can be an empty list initially. The *input data* is the parameter of the contract, which is the input list, of type string list. 48 | 49 | If this contract works properly, the storage should be a reversed list of the input. 50 | 51 | You can see the script working from the output being the reversed list: 52 | 53 | ```tezos-client run script [path to]/reverse_list.tz on storage '{}' and input '{ "abc" ; "def" ; "x" ; "y" ; "z"}'``` 54 | ~~~~ 55 | storage 56 | { "z" ; "y" ; "x" ; "def" ; "abc" } 57 | emitted operations 58 | ~~~~ 59 | -------------------------------------------------------------------------------- /liquidity/examples/reverse_list/reverse_list.init.tz: -------------------------------------------------------------------------------- 1 | { "x" ; "y" } -------------------------------------------------------------------------------- /liquidity/examples/reverse_list/reverse_list.liq: -------------------------------------------------------------------------------- 1 | (* A contract that reverses a string list. *) 2 | 3 | type storage = string list 4 | 5 | (* The parameter is the string list. *) 6 | let%entry main 7 | (input_list : string list) 8 | _storage = 9 | let output_list = List.rev input_list in 10 | ( [], output_list ) -------------------------------------------------------------------------------- /liquidity/examples/reverse_list/reverse_list.tz: -------------------------------------------------------------------------------- 1 | parameter (list string); 2 | storage (list string); 3 | code { DUP ; 4 | DIP { CDR @_storage_slash_1 } ; 5 | CAR @input_list_slash_2 ; 6 | NIL string ; 7 | DUUP @input_list ; 8 | ITER { RENAME @arg_slash_3 ; 9 | DIP { DUP } ; 10 | DIIP { DROP } ; 11 | PAIR ; 12 | DUP ; 13 | CDR ; 14 | SWAP ; 15 | CAR ; 16 | CONS } ; 17 | DIP { DROP ; DROP } ; 18 | RENAME @output_list ; 19 | NIL operation ; 20 | PAIR }; 21 | -------------------------------------------------------------------------------- /liquidity/examples/safe_queue/README.md: -------------------------------------------------------------------------------- 1 | This contract contains a map-based queue. One can add an element to the end of the queue, and remove an element from the front of the queue. 2 | 3 | In this contract the storage is of type record. It contains the fields: 4 | - **map** or **big map** of type (int, string). I.e., the keys are of type int, the elements of the queue are of type string. See [this](http://www.liquidity-lang.org/doc/reference/liquidity.html#operations-on-maps). Big maps are a specific kind of maps, optimized for large data. You can only have one big map per smart contract, and it appears as the first element of the storage. 5 | - **first key**. Keys are positive integers. The first key record field stores the key of the first element of the queue. 6 | - **last key**. The last key record field stores the key of the last element of the queue. 7 | - **unit** 8 | 9 | The parameter is a *string option*. If the parameter is *None*, then the first item on the queue is removed. If the parameter is *Some string*, then that string is added to the end of the queue. 10 | 11 | To verify that the script implements a queue correctly: 12 | 13 | (1) compile safe_queue.liq or safe_queue.liq to .tz (by running ```liquidity safe_queue.liq```) 14 | 15 | (2) typecheck the script with 16 | ~~~~ 17 | tezos-client typecheck script [path to]/safe_queue.tz 18 | ~~~~ 19 | (3) run a tezos-node in the local network. Make sure it's bootstrapped. See [this](http://tezos.gitlab.io/mainnet/introduction/howtouse.html#rpc-interface). 20 | 21 | (4) run the tezos-client ```run script``` command with various initial storage and inputs. 22 | 23 | The storage or input data have to be in a specific format. See [this](https://github.com/cryptiumlabs/smarter-contracts/blob/master/liquidity/examples/tezos-clients-data-format.md). 24 | 25 | ## Big Map 26 | 27 | For the big map version, the output on storage is displayed in the form of diff: 28 | 29 | - When the queue is empty or has the empty string, removing the first element of the queue (with the parameter of *'None'*) has no effect: 30 | 31 | When the queue is empty: 32 | 33 | ```tezos-client run script safe_queue_big.tz on storage 'Pair {} (Pair 0 (Pair 0 Unit))' and input 'None'``` 34 | 35 | Output: 36 | ~~~~ 37 | storage 38 | (Pair {} (Pair 0 (Pair 0 Unit))) 39 | emitted operations 40 | 41 | map diff: 42 | ~~~~ 43 | 44 | When the queue has the empty string: 45 | 46 | ```tezos-client run script [path to]/safe_queue_big.tz on storage 'Pair { Elt 0 "" } (Pair 0 (Pair 0 Unit))' and input 'None'``` 47 | 48 | Output: 49 | 50 | ~~~~ 51 | storage 52 | (Pair {} (Pair 0 (Pair 0 Unit))) 53 | emitted operations 54 | 55 | map diff: 56 | + 0-> "" 57 | ~~~~ 58 | 59 | - When the parameter is *'(Some "string")'*, the string is added to the end of the queue. The first and last key counters are updated properly as well. Note that the storage required is larger and more expensive with a longer string. 60 | 61 | ```tezos-client run script [path to]/safe_queue_big.tz on storage 'Pair { Elt 0 "" } (Pair 0 (Pair 0 Unit))' and input '(Some "a")'``` 62 | 63 | Output: 64 | 65 | ~~~~ 66 | storage 67 | (Pair {} (Pair 1 (Pair 1 Unit))) 68 | emitted operations 69 | 70 | map diff: 71 | + 0-> "" 72 | + 1-> "a" 73 | ~~~~ 74 | 75 | - The last key counter properly goes up by 1 when another string is added. 76 | 77 | ```tezos-client run script [path to]/safe_queue_big.tz on storage '(Pair {Elt 0 "";Elt 1 "a"} (Pair 1 (Pair 1 Unit)))' and input '(Some "b")'``` 78 | 79 | Output: 80 | 81 | ~~~ 82 | storage 83 | (Pair {} (Pair 1 (Pair 2 Unit))) 84 | emitted operations 85 | 86 | map diff: 87 | + 0-> "" 88 | + 1-> "a" 89 | + 2-> "b" 90 | ~~~~ 91 | 92 | - The first element is properly removed and the first key counter properly goes up by 1. 93 | 94 | ```tezos-client run script [path to]/safe_queue_big.tz on storage '(Pair {Elt 0 "";Elt 1 "a";Elt 2 "b"} (Pair 1 (Pair 2 Unit)))' and input 'None'``` 95 | 96 | Output: 97 | 98 | ~~~~ 99 | storage 100 | (Pair {} (Pair 2 (Pair 2 Unit))) 101 | emitted operations 102 | 103 | map diff: 104 | + 0-> "" 105 | - 1 106 | + 2-> "b" 107 | ~~~~ 108 | 109 | ## Map 110 | 111 | For the map version, it works similarly, except the queue is displayed directly: 112 | 113 | ```tezos-client run script safe_queue.tz on storage '(Pair { Elt 1 "a" } (Pair 1 (Pair 1 Unit)))' and input 'None'``` 114 | 115 | Output: 116 | ~~~~ 117 | storage 118 | (Pair {} (Pair 2 (Pair 1 Unit))) 119 | emitted operations 120 | ~~~~ 121 | 122 | Removing the first element again gives the same output, as desired: 123 | 124 | ```tezos-client run script safe_queue.tz on storage '(Pair {} (Pair 2 (Pair 1 Unit)))' and input 'None'``` 125 | 126 | Output: 127 | ~~~~ 128 | storage 129 | (Pair {} (Pair 2 (Pair 1 Unit))) 130 | emitted operations 131 | -------------------------------------------------------------------------------- /liquidity/examples/safe_queue/safe_queue.init.tz: -------------------------------------------------------------------------------- 1 | Pair { Elt 0 "" } (Pair 0 (Pair 0 Unit)) -------------------------------------------------------------------------------- /liquidity/examples/safe_queue/safe_queue.liq: -------------------------------------------------------------------------------- 1 | (* A contract that contains a map-based queue. 2 | * If the parameter is None, then the first item on the queue is removed. 3 | * If the parameter is Some string, then that string is added to the end of the queue. *) 4 | 5 | type storage = { 6 | map : (int, string) map; 7 | first_key : int; 8 | last_key : int; 9 | nothing : unit 10 | } 11 | 12 | (* Initialize storage to an empty queue. *) 13 | let%init storage = { 14 | map = Map [0, ""]; 15 | first_key = 0; 16 | last_key = 0; 17 | nothing = () 18 | } 19 | 20 | let%entry main (param : string option) current_queue = 21 | let l_key = current_queue.last_key in 22 | let f_key = current_queue.first_key in 23 | let new_l_key = l_key + 1 in 24 | let new_f_key = f_key + 1 in 25 | let new_queue = match param with 26 | | None -> 27 | if l_key = 0 then 28 | current_queue 29 | else { 30 | map = (Map.remove f_key current_queue.map) ; 31 | first_key = (if new_f_key > l_key + 1 then f_key else new_f_key) ; 32 | last_key = l_key ; 33 | nothing = () } 34 | | Some v -> { 35 | map = (Map.add new_l_key v current_queue.map); 36 | first_key = (if f_key = 0 then new_f_key else f_key); 37 | last_key = new_l_key ; 38 | nothing = ()} 39 | in 40 | [], new_queue 41 | -------------------------------------------------------------------------------- /liquidity/examples/safe_queue/safe_queue.tz: -------------------------------------------------------------------------------- 1 | parameter (option string); 2 | storage 3 | (pair :storage 4 | (map %map int string) 5 | (pair (int %first_key) (pair (int %last_key) (unit %nothing)))); 6 | code { DUP ; 7 | DIP { CDR @current_queue_slash_1 } ; 8 | CAR @param_slash_2 ; 9 | DUUP @current_queue ; 10 | CDDAR @l_key %last_key ; 11 | DUUUP @current_queue ; 12 | CDAR @f_key %first_key ; 13 | PUSH int 1 ; 14 | DUUUP @l_key ; 15 | ADD @new_l_key ; 16 | PUSH int 1 ; 17 | DUUUP @f_key ; 18 | ADD @new_f_key ; 19 | DUUUUUP @param ; 20 | IF_NONE 21 | { PUSH int 0 ; 22 | DUUUUUP @l_key ; 23 | COMPARE ; 24 | EQ ; 25 | IF { DUUUUUUP @current_queue } 26 | { UNIT ; 27 | DUUUUUP @l_key ; 28 | PAIR %last_key %nothing ; 29 | PUSH int 1 ; 30 | DUUUUUUP @l_key ; 31 | ADD ; 32 | DUUUP @new_f_key ; 33 | COMPARE ; 34 | GT ; 35 | IF { DUUUUP @f_key } { DUUP @new_f_key } ; 36 | PAIR %first_key ; 37 | DUUUUUUUP @current_queue ; 38 | CAR %map ; 39 | DUUUUUP @f_key ; 40 | DIP { NONE string } ; 41 | UPDATE ; 42 | PAIR %map } } 43 | { UNIT ; 44 | DUUUUP @new_l_key ; 45 | PAIR %last_key %nothing ; 46 | PUSH int 0 ; 47 | DUUUUUUP @f_key ; 48 | COMPARE ; 49 | EQ ; 50 | IF { DUUUP @new_f_key } { DUUUUUP @f_key } ; 51 | PAIR %first_key ; 52 | DUUUUUUUUP @current_queue ; 53 | CAR %map ; 54 | DUUUP @v ; 55 | DUUUUUUP @new_l_key ; 56 | DIP { SOME } ; 57 | DIIIIP { DROP } ; 58 | UPDATE ; 59 | PAIR %map } ; 60 | DIP { DROP ; DROP ; DROP ; DROP ; DROP ; DROP } ; 61 | RENAME @new_queue ; 62 | NIL operation ; 63 | PAIR }; 64 | -------------------------------------------------------------------------------- /liquidity/examples/safe_queue/safe_queue_big.init.tz: -------------------------------------------------------------------------------- 1 | Pair { Elt 0 "" } (Pair 0 (Pair 0 Unit)) -------------------------------------------------------------------------------- /liquidity/examples/safe_queue/safe_queue_big.liq: -------------------------------------------------------------------------------- 1 | (* A contract that contains a map-based queue. 2 | * If the parameter is None, then the first item on the queue is removed. 3 | * If the parameter is Some string, then that string is added to the end of the queue. 4 | * big map, which is optimized for a large database, is used in this version. *) 5 | 6 | type storage = { 7 | big : (int, string) big_map; 8 | first_key : int; 9 | last_key : int; 10 | nothing : unit 11 | } 12 | 13 | (* Initialize storage to an empty queue. *) 14 | let%init storage = { 15 | big = BigMap [0, ""]; 16 | first_key = 0; 17 | last_key = 0; 18 | nothing = () 19 | } 20 | 21 | let%entry main (param : string option) current_queue = 22 | let l_key = current_queue.last_key in 23 | let f_key = current_queue.first_key in 24 | let new_l_key = l_key + 1 in 25 | let new_f_key = f_key + 1 in 26 | let new_queue = match param with 27 | | None -> 28 | if l_key = 0 then 29 | current_queue 30 | else { 31 | big = (Map.remove f_key current_queue.big) ; 32 | first_key = (if new_f_key > l_key + 1 then f_key else new_f_key) ; 33 | last_key = l_key ; 34 | nothing = () } 35 | | Some v -> { 36 | big = (Map.add new_l_key v current_queue.big); 37 | first_key = (if f_key = 0 then new_f_key else f_key); 38 | last_key = new_l_key ; 39 | nothing = ()} 40 | in 41 | [], new_queue 42 | -------------------------------------------------------------------------------- /liquidity/examples/safe_queue/safe_queue_big.tz: -------------------------------------------------------------------------------- 1 | parameter (option string); 2 | storage 3 | (pair :storage 4 | (big_map :big int string) 5 | (pair (int %first_key) (pair (int %last_key) (unit %nothing)))); 6 | code { DUP ; 7 | DIP { CDR @current_queue_slash_1 } ; 8 | CAR @param_slash_2 ; 9 | DUUP @current_queue ; 10 | CDDAR @l_key %last_key ; 11 | DUUUP @current_queue ; 12 | CDAR @f_key %first_key ; 13 | PUSH int 1 ; 14 | DUUUP @l_key ; 15 | ADD @new_l_key ; 16 | PUSH int 1 ; 17 | DUUUP @f_key ; 18 | ADD @new_f_key ; 19 | DUUUUUP @param ; 20 | IF_NONE 21 | { PUSH int 0 ; 22 | DUUUUUP @l_key ; 23 | COMPARE ; 24 | EQ ; 25 | IF { DUUUUUUP @current_queue } 26 | { UNIT ; 27 | DUUUUUP @l_key ; 28 | PAIR %last_key %nothing ; 29 | PUSH int 1 ; 30 | DUUUUUUP @l_key ; 31 | ADD ; 32 | DUUUP @new_f_key ; 33 | COMPARE ; 34 | GT ; 35 | IF { DUUUUP @f_key } { DUUP @new_f_key } ; 36 | PAIR %first_key ; 37 | DUUUUUUUP @current_queue ; 38 | CAR %big ; 39 | DUUUUUP @f_key ; 40 | DIP { NONE string } ; 41 | UPDATE ; 42 | PAIR %big } } 43 | { UNIT ; 44 | DUUUUP @new_l_key ; 45 | PAIR %last_key %nothing ; 46 | PUSH int 0 ; 47 | DUUUUUUP @f_key ; 48 | COMPARE ; 49 | EQ ; 50 | IF { DUUUP @new_f_key } { DUUUUUP @f_key } ; 51 | PAIR %first_key ; 52 | DUUUUUUUUP @current_queue ; 53 | CAR %big ; 54 | DUUUP @v ; 55 | DUUUUUUP @new_l_key ; 56 | DIP { SOME } ; 57 | DIIIIP { DROP } ; 58 | UPDATE ; 59 | PAIR %big } ; 60 | DIP { DROP ; DROP ; DROP ; DROP ; DROP ; DROP } ; 61 | RENAME @new_queue ; 62 | NIL operation ; 63 | PAIR }; 64 | -------------------------------------------------------------------------------- /liquidity/examples/send_one_tez/README.md: -------------------------------------------------------------------------------- 1 | This contract sends one tz to another account. 2 | 3 | In this contract the storage is *unit*. The parameter is the *key_hash* of the receiver of the 1tz. 4 | 5 | To verify the contract does what it intends to do, one has to deploy and call it, then check the balance of the sender and the receiver. 6 | 7 | E.g., I have two account alice and bob activated. Follow [this link](http://tezos.gitlab.io/mainnet/introduction/howtouse.html#how-to-use-tezos) for instructions on activating these accounts. 8 | 9 | I have alice originating the contract i.e. alice is the sender of 1tz. 10 | 11 | After compiling send_one_tez.liq to .tz (by running ```liquidity send_one_tez.liq```), deploy the contract with: (```--force``` is only necessary if you have originated this contract before.) ```--burn-cap``` set the burn cap. You can first run it without the burn-cap option, and the client will tell you how much it needs to be set to. 12 | 13 | ```tezos-client originate contract sendtez for alice transferring 1 from alice running [path to]/send_one_tez.tz --force --burn-cap 0.43``` 14 | 15 | This generates a contract named *sendtez* and it should return "new contract originated with a KT1 address. In my case 'KT1FNANtucrLC4FzGMnyBD945WDkXB86UJDU'. 16 | 17 | Running ```tezos-client list known contracts``` now list the new contract *sendtez*: 18 | 19 | ~~~~ 20 | sendtez: KT1FNANtucrLC4FzGMnyBD945WDkXB86UJDU 21 | bob: tz1Ra8yQVQN4Nd7LpPQ6UT6t3bsWWqHZ9wa6 22 | alice: tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS 23 | ~~~~ 24 | 25 | The contract is injected to alphanet. To call the contract, we need to know alice’s private key and hash, and bob’s hash. 26 | 27 | Running ```tezos-client show address alice -S --show-secret``` shows the following for alice: 28 | ~~~~ 29 | Hash: tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS 30 | Public Key: edpku5LTYXxXKTjmzj9qtHR6TTYH1K6JU7bFe8MyJs6qabUs9kiZes 31 | Secret Key: unencrypted:edsk2gKnmssYQB5sNCAL6sCcgfPRZDuK1zixBowAQ1H1WCt78JxEhf 32 | ~~~~ 33 | Running ```tezos-client show address bob -S --show-secret``` shows the following for bob: 34 | ~~~~ 35 | Hash: tz1Ra8yQVQN4Nd7LpPQ6UT6t3bsWWqHZ9wa6 36 | Public Key: edpktqrmQgDqvdCZM4msb9eDpe2adQ6NDgE7Jax34ABPzvw2Pk9SaT 37 | Secret Key: unencrypted:edsk2pyAwDyHGh22tFAre3Qghz1JY3D1eT6yWnZVbidVJstzYxTAKK 38 | ~~~~ 39 | Before we call the contract, which will execute the transfer of 1tz, let’s check the balances of alice and bob with 40 | 41 | ```tezos-client get balance for alice``` 42 | ```tezos-client get balance for bob``` 43 | 44 | Now we can call the contract with alice's private key, and the liquidity command (see the full list of commands with ```liquidity --help```) ```--call [contract address] [entry point name] [receipent's key_hash]``` (in my case, bob’s key hash): 45 | 46 | ```liquidity --tezos-node http://alphanet-node.tzscan.io --private-key edsk2gKnmssYQB5sNCAL6sCcgfPRZDuK1zixBowAQ1H1WCt78JxEhf --source tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS [path to]/send_one_tez.liq --call KT1FNANtucrLC4FzGMnyBD945WDkXB86UJDU main 'tz1Ra8yQVQN4Nd7LpPQ6UT6t3bsWWqHZ9wa6'``` 47 | 48 | 49 | The Tezos client should inform you the call was successful. To check that one tez is transferred from alice to bob, check their balances again. Note that alice has to pay for the transaction as well on top of the 1tz to bob. 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /liquidity/examples/send_one_tez/send_one_tez.init.tz: -------------------------------------------------------------------------------- 1 | Unit -------------------------------------------------------------------------------- /liquidity/examples/send_one_tez/send_one_tez.liq: -------------------------------------------------------------------------------- 1 | (* A contract that sends 1 tez to anyone who asks. *) 2 | 3 | type storage = unit 4 | 5 | (* Initialize storage to (). *) 6 | let%init storage = () 7 | 8 | let%entry main 9 | (key : key_hash) 10 | _storage = 11 | let op = Account.transfer ~dest:key ~amount:1tz in 12 | ( [op], () ) -------------------------------------------------------------------------------- /liquidity/examples/send_one_tez/send_one_tez.tz: -------------------------------------------------------------------------------- 1 | parameter key_hash; 2 | storage unit; 3 | code { DUP ; 4 | DIP { CDR @_storage_slash_1 } ; 5 | DIP { DROP } ; 6 | CAR @key_slash_2 ; 7 | IMPLICIT_ACCOUNT ; 8 | PUSH mutez 1000000 ; 9 | UNIT ; 10 | TRANSFER_TOKENS @op ; 11 | UNIT ; 12 | NIL operation ; 13 | DUUUP ; 14 | DIIIP { DROP } ; 15 | CONS ; 16 | PAIR }; 17 | -------------------------------------------------------------------------------- /liquidity/examples/tezos-clients-data-format.md: -------------------------------------------------------------------------------- 1 | # Tezos client data format documentation 2 | 3 | When running tezos-client commands that require data input, the data input have to be in a specific format. This documentation contains some translations (more will be added over time), and a guide to translate the Liquidity language data type to the required data format for the tezos-client. 4 | 5 | For example, the ```run script``` command requires input of storage/input data: 6 | 7 | From [the client manual](https://tezos.gitlab.io/alphanet/api/cli-commands.html#client-manual): 8 | 9 | ------------------------------------------------------------------------------ 10 | run script ***src*** on storage ***storage*** and input ***storage*** [--trace-stack] [--amount ] [-q --no-print-source] 11 | 12 | Ask the node to run a script. 13 | 14 | ***src***: source script 15 | Can be a script name, a file or a raw script literal. If the parameter is 16 | not the name of an existing script, the client will look for a file 17 | containing a script, and if it does not exist, the argument will be read as 18 | a raw script. 19 | Use 'alias:name', 'file:path' or 'text:literal' to disable autodetect. 20 | ***storage***: the storage data 21 | ***storage***: the input data 22 | ```--trace-stack```: show the stack after each step 23 | ```--amount ```: amount of the transfer in ꜩ 24 | Defaults to `0.05`. 25 | ```-q --no-print-source```: don't print the source code 26 | If an error is encountered, the client will print the contract's source 27 | code by default. 28 | This option disables this behaviour. 29 | 30 | -------------------------------------------------------------------------------- 31 | The *storage data* and the *input data* are in a specific format. Documentation regarding the required format seems missing. Below are some translations. 32 | 33 | ## Translations 34 | 35 | |Data Type | Liquidity Syntax | Tezos Client Format | 36 | | --- | --- | ----------- | 37 | | tez | 1tz | '1000000' | 38 | | string list | ["x" ; "y"] | '{ "x" ; "y" }' | 39 | | (int, string) Big map | BigMap [0, ""] | { Elt 0 "" } | 40 | 41 | ## Guide to translate other datatypes 42 | 43 | Here I provide a way to **translate between the [liquidity data format](http://www.liquidity-lang.org/doc/reference/liquidity.html#basic-types-and-values) and the required format**: 44 | 45 | (1) Add the storage initialization code (tradition is below the storage type declaration) to a .liq file: 46 | 47 | ```let%init storage = 10tz``` 48 | 49 | Make sure the storage type declared above is the same as the initial storage. E.g., the above specifies that the initial storage is *10tz*, of type *tez*. Consult the [liquidity documentation](http://www.liquidity-lang.org/doc/reference/liquidity.html#basic-types-and-values) for the specification of each data type. 50 | 51 | (2) Compile the .liq file: 52 | 53 | ```liquidity [path to]/filename.liq``` 54 | 55 | The result of the command should look like this: 56 | 57 | ~~~~ 58 | Constant initial storage generated in "[path to]/filename.init.tz" 59 | File "[path to]/filename.tz" generated 60 | If tezos is compiled, you may want to typecheck with: 61 | tezos-client typecheck script [path to]/filename.tz* 62 | ~~~~ 63 | (3) View the .init.tz file. In the above case, the content is: 64 | 10000000 65 | 66 | The content wrapped in single quotes is what you should pass in to the tezos-client. 67 | -------------------------------------------------------------------------------- /liquidity/manager/README.md: -------------------------------------------------------------------------------- 1 | ## Compile the contract 2 | 3 | Install Liquidity as per the [installation instruction](http://www.liquidity-lang.org/doc/installation/index.html). 4 | Compile manager.liq to manager.tz by running: 5 | 6 | ```liquidity [path to]/manager.liq``` 7 | 8 | ## Originate the contract on Tezos 9 | 10 | To originate the contract, run (see the [client manual](https://tezos.gitlab.io/alphanet/api/cli-commands.html#client-manual) for complete list of options): 11 | 12 | ```tezos-client originate contract *new* for *mgr* transferring *qty* from *src* running *prg* --init '"[owner's address]"'``` 13 | 14 | in which 15 | 16 | - **new**: the name you want to give to the contract 17 | - **mgr**: the manager of the new contract 18 | Can be a public key hash name, a file or a raw public key hash literal. 19 | If the parameter is not the name of an existing public key hash, 20 | the client will look for a file containing a public key hash, 21 | and if it does not exist, the argument will be read as a raw public key hash. 22 | Use 'alias:name', 'file:path' or 'text:literal' to disable autodetect. 23 | - **qty**: amount taken from source in ꜩ Text format: `DDDDDDD.DDDDDD`. 24 | Tez and mutez and separated by a period sign. 25 | Trailing and pending zeroes are allowed. 26 | - **src**: name of the source contract 27 | Can be an alias, a key, or a literal (autodetected in order). 28 | Use 'text:literal', 'alias:name', 'key:name' to force. 29 | - **prg**: script of the account. This is **the path to the .tz file that you have saved by compiling manager.liq**. 30 | 31 | The ```--init``` option initialized the storage to **the owner of this contract. This contract can only be called by this owner.** 32 | The tz1/KT1 address (in string format) is wrapped in double quotes. 33 | Alternatively, one can pass the address in the optimized format, in which case no double quotes should be passed. 34 | 35 | For example, I have an account with alias *alice* and I want to initiate the contract, calling it *new_contract*, with the owner being tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS, I'd run the following: 36 | 37 | ```tezos-client originate contract new_manager for alice transferring 1 from alice running ./manager.tz --init '"tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS"'``` 38 | 39 | This run fails because the burn-cap for this operation is higher than the default: 40 | 41 | ~~~~ 42 | Node is bootstrapped, ready for injecting operations. 43 | Fatal error: 44 | The operation will burn ꜩ1.579 which is higher than the configured burn cap (ꜩ0). 45 | Use `--burn-cap 1.579` to emit this operation. 46 | ~~~~ 47 | 48 | Run the command again, setting the burn cap to 1.579 or above: 49 | 50 | ```tezos-client originate contract new_manager for alice transferring 1 from alice running ./manager.tz --init '"tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS"' --burn-cap 1.579``` 51 | 52 | This time it successfully originates a contract, the output should look similar to this: 53 | 54 | ~~~~ 55 | Node is bootstrapped, ready for injecting operations. 56 | Estimated gas: 40109 units (will add 100 for safety) 57 | Estimated storage: 1579 bytes added (will add 20 for safety) 58 | Operation successfully injected in the node. 59 | Operation hash is 'ooEsf896wmE5YniSnSEYN98Qx38X1F53yLNCccCLKPKBsZzeKq8' 60 | Waiting for the operation to be included... 61 | Operation found in block: BKoY5wVTspvAwKm3PoEUu1miMKKnY5q3cuCRHEQsCKU48rDmM3C (pass: 3, offset: 0) 62 | This sequence of operations was run: 63 | Manager signed operations: 64 | From: tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS 65 | Fee to the baker: ꜩ0.005614 66 | Expected counter: 35394 67 | Gas limit: 40209 68 | Storage limit: 1599 bytes 69 | Balance updates: 70 | tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS ............. -ꜩ0.005614 71 | fees(tz1YCABRTa6H8PLKx2EtDWeCGPaKxUhNgv47,166) ... +ꜩ0.005614 72 | Origination: 73 | From: tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS 74 | For: tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS 75 | Credit: ꜩ1 76 | Script: 77 | { parameter 78 | (or :_entries 79 | (pair %_Liq_entry_spendFund key_hash mutez) 80 | (or (key_hash %_Liq_entry_change_delegate) 81 | (or (unit %_Liq_entry_remove_delegate) (address %_Liq_entry_change_owner)))) ; 82 | storage address ; 83 | code { DUP ; 84 | DIP { CDR @storage_slash_1 } ; 85 | CAR @parameter_slash_2 ; 86 | DUP @parameter ; 87 | IF_LEFT 88 | { RENAME @_toAddress_toAmount_slash_3 ; 89 | { DIP { DIP { DUP @storage } ; SWAP } ; SWAP } ; 90 | DUP @owner ; 91 | SENDER ; 92 | COMPARE ; 93 | NEQ ; 94 | IF { SENDER ; PUSH string "Only the owner can operate." ; PAIR ; FAILWITH } 95 | { DUUP ; 96 | CAR @toAddress ; 97 | IMPLICIT_ACCOUNT ; 98 | DUUUP ; 99 | CDR @toAmount ; 100 | UNIT ; 101 | TRANSFER_TOKENS @op ; 102 | { DIP { DUP @owner } ; SWAP } ; 103 | NIL operation ; 104 | { DIP { DIP { DUP @op } ; SWAP } ; SWAP } ; 105 | DIIIP { DROP } ; 106 | CONS ; 107 | PAIR } ; 108 | DIP { DROP ; DROP } } 109 | (IF_RIGHT 110 | (IF_RIGHT 111 | { RENAME @new_owner_slash_14 ; 112 | { DIP { DIP { DUP @storage } ; SWAP } ; SWAP } ; 113 | SENDER ; 114 | COMPARE ; 115 | NEQ ; 116 | IF { SENDER ; PUSH string "Only the owner can operate." ; PAIR ; FAILWITH } 117 | { DUP @new_owner ; NIL operation ; PAIR } ; 118 | DIP { DROP } } 119 | { RENAME @__slash_11 ; 120 | { DIP { DIP { DUP @storage } ; SWAP } ; SWAP } ; 121 | DUP @owner ; 122 | SENDER ; 123 | COMPARE ; 124 | NEQ ; 125 | IF { SENDER ; PUSH string "Only the owner can operate." ; PAIR ; FAILWITH } 126 | { NONE key_hash ; 127 | SET_DELEGATE @op ; 128 | { DIP { DUP @owner } ; SWAP } ; 129 | NIL operation ; 130 | { DIP { DIP { DUP @op } ; SWAP } ; SWAP } ; 131 | DIIIP { DROP } ; 132 | CONS ; 133 | PAIR } ; 134 | DIP { DROP ; DROP } }) 135 | { RENAME @new_del_slash_8 ; 136 | { DIP { DIP { DUP @storage } ; SWAP } ; SWAP } ; 137 | DUP @owner ; 138 | SENDER ; 139 | COMPARE ; 140 | NEQ ; 141 | IF { SENDER ; PUSH string "Only the owner can operate." ; PAIR ; FAILWITH } 142 | { { DIP { DUP @new_del } ; SWAP } ; 143 | SOME ; 144 | SET_DELEGATE @op ; 145 | { DIP { DUP @owner } ; SWAP } ; 146 | NIL operation ; 147 | { DIP { DIP { DUP @op } ; SWAP } ; SWAP } ; 148 | DIIIP { DROP } ; 149 | CONS ; 150 | PAIR } ; 151 | DIP { DROP ; DROP } }) ; 152 | DIP { DROP ; DROP } } } 153 | Initial storage: "tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS" 154 | No delegate for this contract 155 | This origination was successfully applied 156 | Originated contracts: 157 | KT1SimGFMnQyCSV2Hk8oSmx5hgPmuJin3Zsi 158 | Storage size: 1322 bytes 159 | Paid storage size diff: 1322 bytes 160 | Consumed gas: 40109 161 | Balance updates: 162 | tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS ... -ꜩ1.322 163 | tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS ... -ꜩ0.257 164 | tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS ... -ꜩ1 165 | KT1SimGFMnQyCSV2Hk8oSmx5hgPmuJin3Zsi ... +ꜩ1 166 | 167 | New contract KT1SimGFMnQyCSV2Hk8oSmx5hgPmuJin3Zsi originated. 168 | The operation has only been included 0 blocks ago. 169 | We recommend to wait more. 170 | Use command 171 | tezos-client wait for ooEsf896wmE5YniSnSEYN98Qx38X1F53yLNCccCLKPKBsZzeKq8 to be included --confirmations 30 --branch BM8fpfLjGDNrFKbkEZrzFSQnWmjYDzXNZo7xoor9iWQK2RS9mcZ 172 | and/or an external block explorer. 173 | Contract memorized as new_manager. 174 | 175 | ~~~~ 176 | 177 | ## Call the contract 178 | 179 | The contract has the following functions: 180 | 181 | 1. **Transfer funds**: 182 | - the parameters to pass are 183 | - the *key_hash* of the recipient that you want to send the fund to 184 | - the amount you want to send 185 | - first entry point of the contract, pass the parameters by passing ```Left [parameters]``` 186 | 2. **Set delegate**: 187 | - the parameter to pass is the *key_hash* of the delegate 188 | - second entry point of the contract, pass the parameter by passing ```(Right (Left [parameter]))``` 189 | 3. **Remove delegate**: 190 | - the parameter to pass is *unit* 191 | - third entry point of the contract, pass the parameter by passing ```(Right (Right (Left [parameter])))``` 192 | 4. **Change owner**: 193 | - the parameter to pass is the new owner's address 194 | - fourth entry point of the contract, pass the parameter by passing ```(Right (Right (Right [parameter])))``` 195 | 196 | See below for examples. 197 | 198 | ### Transfer funds 199 | 200 | To transfer 1tz to address tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS, run the following command: 201 | 202 | ```tezos-client transfer 10 from adam to new_manager --arg '(Left (Pair "tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS" 1000000))'``` 203 | 204 | Note that the input for 1tz is 1000000. 205 | 206 | The output should be similar to below, which states the change in the balances of the relevant addresses: 207 | 208 | ~~~~ 209 | Node is bootstrapped, ready for injecting operations. 210 | Estimated gas: 43664 units (will add 100 for safety) 211 | Estimated storage: no bytes added 212 | Operation successfully injected in the node. 213 | Operation hash is 'ooZ7j5bFknTAZm4VcXmVaiau55gtM9RdepBpFCu8UxNRMyKorvg' 214 | Waiting for the operation to be included... 215 | Operation found in block: BLJ1x4FEfkACgB5DfjU8zgVh4azEBUYpu4aZy99vbgPKsURNVbw (pass: 3, offset: 0) 216 | This sequence of operations was run: 217 | Manager signed operations: 218 | From: tz1fPjyo55HwUAkd1xcL5vo6DGzJrkxAMpiD 219 | Fee to the baker: ꜩ0.004685 220 | Expected counter: 40376 221 | Gas limit: 43764 222 | Storage limit: 0 bytes 223 | Balance updates: 224 | tz1fPjyo55HwUAkd1xcL5vo6DGzJrkxAMpiD ............. -ꜩ0.004685 225 | fees(tz3NdTPb3Ax2rVW2Kq9QEdzfYFkRwhrQRPhX,166) ... +ꜩ0.004685 226 | Transaction: 227 | Amount: ꜩ10 228 | From: tz1fPjyo55HwUAkd1xcL5vo6DGzJrkxAMpiD 229 | To: KT1PTu7zW8rDMkHsLL9wkBJUH5PqJ2kdmSi8 230 | Parameter: (Left (Pair "tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS" 1000000)) 231 | This transaction was successfully applied 232 | Updated storage: 0x0000d8aebdc7e7d86e00d26b5f9c038dd87b01631ba6 233 | Storage size: 1322 bytes 234 | Consumed gas: 33557 235 | Balance updates: 236 | tz1fPjyo55HwUAkd1xcL5vo6DGzJrkxAMpiD ... -ꜩ10 237 | KT1PTu7zW8rDMkHsLL9wkBJUH5PqJ2kdmSi8 ... +ꜩ10 238 | Internal operations: 239 | Transaction: 240 | Amount: ꜩ1 241 | From: KT1PTu7zW8rDMkHsLL9wkBJUH5PqJ2kdmSi8 242 | To: tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS 243 | Parameter: Unit 244 | This transaction was successfully applied 245 | Consumed gas: 10107 246 | Balance updates: 247 | KT1PTu7zW8rDMkHsLL9wkBJUH5PqJ2kdmSi8 ... -ꜩ1 248 | tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS ... +ꜩ1 249 | 250 | The operation has only been included 0 blocks ago. 251 | We recommend to wait more. 252 | Use command 253 | tezos-client wait for ooZ7j5bFknTAZm4VcXmVaiau55gtM9RdepBpFCu8UxNRMyKorvg to be included --confirmations 30 --branch BMdv1DTBMffstCzmEYFJQusMB36y4oKLyZrrJL1nXBKAGMyqWhC 254 | and/or an external block explorer. 255 | ~~~~ 256 | 257 | ### Set delegate 258 | 259 | To set a new delegate to tz1fPjyo55HwUAkd1xcL5vo6DGzJrkxAMpiD, run the following command: 260 | 261 | ```tezos-client transfer 10 from alice to new_manager --arg '(Right (Left "tz1Ra8yQVQN4Nd7LpPQ6UT6t3bsWWqHZ9wa6"))'``` 262 | 263 | The output should be similar to below, which states that the delegate is set to the new address that we pass in: 264 | 265 | ~~~~ 266 | Node is bootstrapped, ready for injecting operations. 267 | Estimated gas: 43492 units (will add 100 for safety) 268 | Estimated storage: no bytes added 269 | Operation successfully injected in the node. 270 | Operation hash is 'oovjp8KipYBpMDsXhhUUNVbVT9dbpf61BLCsmVYNRZj5tPThQsU' 271 | Waiting for the operation to be included... 272 | Operation found in block: BLsBaUyg7MDAFFBhXKTGU5DLgSdR2VMfvy5HRtgkNti7daEW57S (pass: 3, offset: 0) 273 | This sequence of operations was run: 274 | Manager signed operations: 275 | From: tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS 276 | Fee to the baker: ꜩ0.004664 277 | Expected counter: 35399 278 | Gas limit: 43592 279 | Storage limit: 0 bytes 280 | Balance updates: 281 | tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS ............. -ꜩ0.004664 282 | fees(tz1hodJSw6uv7LqArLW86zKuUjkXiayJvqCf,166) ... +ꜩ0.004664 283 | Transaction: 284 | Amount: ꜩ10 285 | From: tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS 286 | To: KT1PTu7zW8rDMkHsLL9wkBJUH5PqJ2kdmSi8 287 | Parameter: (Right (Left "tz1Ra8yQVQN4Nd7LpPQ6UT6t3bsWWqHZ9wa6")) 288 | This transaction was successfully applied 289 | Updated storage: 0x0000ba40187a5d588d3312be6321f7bdc077b0fc2a6a 290 | Storage size: 1322 bytes 291 | Consumed gas: 33492 292 | Balance updates: 293 | tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS ... -ꜩ10 294 | KT1PTu7zW8rDMkHsLL9wkBJUH5PqJ2kdmSi8 ... +ꜩ10 295 | Internal operations: 296 | Delegation: 297 | Contract: KT1PTu7zW8rDMkHsLL9wkBJUH5PqJ2kdmSi8 298 | To: tz1Ra8yQVQN4Nd7LpPQ6UT6t3bsWWqHZ9wa6 299 | This delegation was successfully applied 300 | Consumed gas: 10000 301 | 302 | The operation has only been included 0 blocks ago. 303 | We recommend to wait more. 304 | Use command 305 | tezos-client wait for oovjp8KipYBpMDsXhhUUNVbVT9dbpf61BLCsmVYNRZj5tPThQsU to be included --confirmations 30 --branch BM7Gxytb4kYiSFBubLBbMDsQdNQehFZfp2DNPf5ysFqpsDsVYwY 306 | and/or an external block explorer. 307 | ~~~~ 308 | 309 | Check again that the delegate is the new address: 310 | 311 | ```tezos-client get delegate for new_manager``` 312 | 313 | The output is the new delegate: 314 | 315 | ```tz1Ra8yQVQN4Nd7LpPQ6UT6t3bsWWqHZ9wa6 (known as bob)``` 316 | 317 | ### Remove delegate 318 | 319 | To removing a delegate, we pass the input *unit* to the third entry point by running the following: 320 | 321 | ```tezos-client transfer 10 from alice to new_manager --arg '(Right (Right (Left Unit)))'``` 322 | 323 | The output states that delegate is set to nobody: 324 | ~~~~ 325 | Node is bootstrapped, ready for injecting operations. 326 | Estimated gas: 43491 units (will add 100 for safety) 327 | Estimated storage: no bytes added 328 | Operation successfully injected in the node. 329 | Operation hash is 'opHtivqYJzmKEzQbNyQbUnxKJcPo2x5NW2BMtjWaKDJ5jtmJvEQ' 330 | Waiting for the operation to be included... 331 | Operation found in block: BKmaUwqtnGkGzEbTTKsq43LuVsKrv8r2x5g5M7pcwF9EgEBieqb (pass: 3, offset: 0) 332 | This sequence of operations was run: 333 | Manager signed operations: 334 | From: tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS 335 | Fee to the baker: ꜩ0.004627 336 | Expected counter: 35400 337 | Gas limit: 43591 338 | Storage limit: 0 bytes 339 | Balance updates: 340 | tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS ............. -ꜩ0.004627 341 | fees(tz3WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5,166) ... +ꜩ0.004627 342 | Transaction: 343 | Amount: ꜩ10 344 | From: tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS 345 | To: KT1PTu7zW8rDMkHsLL9wkBJUH5PqJ2kdmSi8 346 | Parameter: (Right (Right (Left Unit))) 347 | This transaction was successfully applied 348 | Updated storage: 0x0000ba40187a5d588d3312be6321f7bdc077b0fc2a6a 349 | Storage size: 1322 bytes 350 | Consumed gas: 33491 351 | Balance updates: 352 | tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS ... -ꜩ10 353 | KT1PTu7zW8rDMkHsLL9wkBJUH5PqJ2kdmSi8 ... +ꜩ10 354 | Internal operations: 355 | Delegation: 356 | Contract: KT1PTu7zW8rDMkHsLL9wkBJUH5PqJ2kdmSi8 357 | To: nobody 358 | This delegation was successfully applied 359 | Consumed gas: 10000 360 | 361 | The operation has only been included 0 blocks ago. 362 | We recommend to wait more. 363 | Use command 364 | tezos-client wait for opHtivqYJzmKEzQbNyQbUnxKJcPo2x5NW2BMtjWaKDJ5jtmJvEQ to be included --confirmations 30 --branch BLsBaUyg7MDAFFBhXKTGU5DLgSdR2VMfvy5HRtgkNti7daEW57S 365 | and/or an external block explorer. 366 | ~~~~ 367 | 368 | Check the delegate for new_manager again shows that there is indeed no delegate: 369 | ~~~~ 370 | tezos-client get delegate for new_manager 371 | none 372 | ~~~~ 373 | 374 | ### Change owner 375 | 376 | To change the owner of the contract to tz1fPjyo55HwUAkd1xcL5vo6DGzJrkxAMpiD, we run the following: 377 | 378 | ```tezos-client transfer 10 from alice to new_manager --arg '(Right (Right (Right "tz1fPjyo55HwUAkd1xcL5vo6DGzJrkxAMpiD")))'``` 379 | 380 | The output indicates that the storage has been updated to the new owner's address (in optimized format): 381 | 382 | ~~~~ 383 | Node is bootstrapped, ready for injecting operations. 384 | Estimated gas: 33427 units (will add 100 for safety) 385 | Estimated storage: no bytes added 386 | Operation successfully injected in the node. 387 | Operation hash is 'ooT9t9cAUkjPgmgHNDY4Uk9Kp6aB7yPhDyQeG5WoM4uDU6xpNWy' 388 | Waiting for the operation to be included... 389 | Operation found in block: BLdLuXvedcHzDK9BBR4deJWiD6mkeePo8uMbbVhtFNgtLHPCAfh (pass: 3, offset: 0) 390 | This sequence of operations was run: 391 | Manager signed operations: 392 | From: tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS 393 | Fee to the baker: ꜩ0.003659 394 | Expected counter: 35401 395 | Gas limit: 33527 396 | Storage limit: 0 bytes 397 | Balance updates: 398 | tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS ............. -ꜩ0.003659 399 | fees(tz3WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5,166) ... +ꜩ0.003659 400 | Transaction: 401 | Amount: ꜩ10 402 | From: tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS 403 | To: KT1PTu7zW8rDMkHsLL9wkBJUH5PqJ2kdmSi8 404 | Parameter: (Right (Right (Right "tz1fPjyo55HwUAkd1xcL5vo6DGzJrkxAMpiD"))) 405 | This transaction was successfully applied 406 | Updated storage: 0x0000d8aebdc7e7d86e00d26b5f9c038dd87b01631ba6 407 | Storage size: 1322 bytes 408 | Consumed gas: 33427 409 | Balance updates: 410 | tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS ... -ꜩ10 411 | KT1PTu7zW8rDMkHsLL9wkBJUH5PqJ2kdmSi8 ... +ꜩ10 412 | 413 | The operation has only been included 0 blocks ago. 414 | We recommend to wait more. 415 | Use command 416 | tezos-client wait for ooT9t9cAUkjPgmgHNDY4Uk9Kp6aB7yPhDyQeG5WoM4uDU6xpNWy to be included --confirmations 30 --branch BLJSrBbPiQDYavpAuE8hMXFwEfAuT15uE6WRkGpzfoBp8Pz8K7r 417 | and/or an external block explorer. 418 | ~~~~ 419 | 420 | Now, calling the contract with alice fails, because the owner is not alice anymore: 421 | 422 | ```tezos-client transfer 10 from alice to new_manager --arg '(Right (Right (Right "tz1fPjyo55HwUAkd1xcL5vo6DGzJrkxAMpiD")))'``` 423 | 424 | The output indicates that it fails with error "Only the owner can operate.": 425 | 426 | ~~~~ 427 | Node is bootstrapped, ready for injecting operations. 428 | This simulation failed: 429 | Manager signed operations: 430 | From: tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS 431 | Fee to the baker: ꜩ0 432 | Expected counter: 35402 433 | Gas limit: 400000 434 | Storage limit: 60000 bytes 435 | Transaction: 436 | Amount: ꜩ10 437 | From: tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS 438 | To: KT1PTu7zW8rDMkHsLL9wkBJUH5PqJ2kdmSi8 439 | Parameter: (Right (Right (Right "tz1fPjyo55HwUAkd1xcL5vo6DGzJrkxAMpiD"))) 440 | This operation FAILED. 441 | 442 | Runtime error in contract KT1PTu7zW8rDMkHsLL9wkBJUH5PqJ2kdmSi8: 443 | 01: { parameter 444 | 02: (or :_entries 445 | 03: (pair %_Liq_entry_spendFund key_hash mutez) 446 | 04: (or (key_hash %_Liq_entry_change_delegate) 447 | 05: (or (unit %_Liq_entry_remove_delegate) (address %_Liq_entry_change_owner)))) ; 448 | 06: storage address ; 449 | 07: code { DUP ; 450 | 08: DIP { CDR @storage_slash_1 } ; 451 | 09: CAR @parameter_slash_2 ; 452 | 10: DUP @parameter ; 453 | 11: IF_LEFT 454 | 12: { RENAME @_toAddress_toAmount_slash_3 ; 455 | 13: { DIP { DIP { DUP @storage } ; SWAP } ; SWAP } ; 456 | 14: DUP @owner ; 457 | 15: SENDER ; 458 | 16: COMPARE ; 459 | 17: NEQ ; 460 | 18: IF { SENDER ; PUSH string "Only the owner can operate." ; PAIR ; FAILWITH } 461 | 19: { DUUP ; 462 | 20: CAR @toAddress ; 463 | 21: IMPLICIT_ACCOUNT ; 464 | 22: DUUUP ; 465 | 23: CDR @toAmount ; 466 | 24: UNIT ; 467 | 25: TRANSFER_TOKENS @op ; 468 | 26: { DIP { DUP @owner } ; SWAP } ; 469 | 27: NIL operation ; 470 | 28: { DIP { DIP { DUP @op } ; SWAP } ; SWAP } ; 471 | 29: DIIIP { DROP } ; 472 | 30: CONS ; 473 | 31: PAIR } ; 474 | 32: DIP { DROP ; DROP } } 475 | 33: (IF_RIGHT 476 | 34: (IF_RIGHT 477 | 35: { RENAME @new_owner_slash_14 ; 478 | 36: { DIP { DIP { DUP @storage } ; SWAP } ; SWAP } ; 479 | 37: SENDER ; 480 | 38: COMPARE ; 481 | 39: NEQ ; 482 | 40: IF { SENDER ; PUSH string "Only the owner can operate." ; PAIR ; FAILWITH } 483 | 41: { DUP @new_owner ; NIL operation ; PAIR } ; 484 | 42: DIP { DROP } } 485 | 43: { RENAME @__slash_11 ; 486 | 44: { DIP { DIP { DUP @storage } ; SWAP } ; SWAP } ; 487 | 45: DUP @owner ; 488 | 46: SENDER ; 489 | 47: COMPARE ; 490 | 48: NEQ ; 491 | 49: IF { SENDER ; PUSH string "Only the owner can operate." ; PAIR ; FAILWITH } 492 | 50: { NONE key_hash ; 493 | 51: SET_DELEGATE @op ; 494 | 52: { DIP { DUP @owner } ; SWAP } ; 495 | 53: NIL operation ; 496 | 54: { DIP { DIP { DUP @op } ; SWAP } ; SWAP } ; 497 | 55: DIIIP { DROP } ; 498 | 56: CONS ; 499 | 57: PAIR } ; 500 | 58: DIP { DROP ; DROP } }) 501 | 59: { RENAME @new_del_slash_8 ; 502 | 60: { DIP { DIP { DUP @storage } ; SWAP } ; SWAP } ; 503 | 61: DUP @owner ; 504 | 62: SENDER ; 505 | 63: COMPARE ; 506 | 64: NEQ ; 507 | 65: IF { SENDER ; PUSH string "Only the owner can operate." ; PAIR ; FAILWITH } 508 | 66: { { DIP { DUP @new_del } ; SWAP } ; 509 | 67: SOME ; 510 | 68: SET_DELEGATE @op ; 511 | 69: { DIP { DUP @owner } ; SWAP } ; 512 | 70: NIL operation ; 513 | 71: { DIP { DIP { DUP @op } ; SWAP } ; SWAP } ; 514 | 72: DIIIP { DROP } ; 515 | 73: CONS ; 516 | 74: PAIR } ; 517 | 75: DIP { DROP ; DROP } }) ; 518 | 76: DIP { DROP ; DROP } } } 519 | At line 40 characters 84 to 92, 520 | script reached FAILWITH instruction 521 | with 522 | (Pair "Only the owner can operate." 0x0000ba40187a5d588d3312be6321f7bdc077b0fc2a6a) 523 | Fatal error: 524 | transfer simulation failed 525 | ~~~~ 526 | 527 | Calling the contract with adam (the new owner) works: 528 | ~~~~ 529 | tezos-client transfer 10 from adam to new_manager --arg '(Right (Right (Left Unit)))' 530 | Node is bootstrapped, ready for injecting operations. 531 | Estimated gas: 43491 units (will add 100 for safety) 532 | Estimated storage: no bytes added 533 | Operation successfully injected in the node. 534 | Operation hash is 'ooSsE3SwitQZv4JdmRoHjX9frpd3qbxz7CJt2ZLDHMAURRMbBm6' 535 | Waiting for the operation to be included... 536 | Operation found in block: BMLtAzzCVAEmD7GFv7rwDeyAfZ7iahjubMcqU4QGg3HnZBUuvV6 (pass: 3, offset: 0) 537 | This sequence of operations was run: 538 | Manager signed operations: 539 | From: tz1fPjyo55HwUAkd1xcL5vo6DGzJrkxAMpiD 540 | Fee to the baker: ꜩ0.00126 541 | Expected counter: 40372 542 | Gas limit: 10000 543 | Storage limit: 0 bytes 544 | Balance updates: 545 | tz1fPjyo55HwUAkd1xcL5vo6DGzJrkxAMpiD ............. -ꜩ0.00126 546 | fees(tz1hodJSw6uv7LqArLW86zKuUjkXiayJvqCf,166) ... +ꜩ0.00126 547 | Revelation of manager public key: 548 | Contract: tz1fPjyo55HwUAkd1xcL5vo6DGzJrkxAMpiD 549 | Key: edpkv9VKRsxHheJLX8ZnYZhT7cvZ34n9Dryz4RgWqabwr8F6tAUx7v 550 | This revelation was successfully applied 551 | Consumed gas: 10000 552 | Manager signed operations: 553 | From: tz1fPjyo55HwUAkd1xcL5vo6DGzJrkxAMpiD 554 | Fee to the baker: ꜩ0.004531 555 | Expected counter: 40373 556 | Gas limit: 43591 557 | Storage limit: 0 bytes 558 | Balance updates: 559 | tz1fPjyo55HwUAkd1xcL5vo6DGzJrkxAMpiD ............. -ꜩ0.004531 560 | fees(tz1hodJSw6uv7LqArLW86zKuUjkXiayJvqCf,166) ... +ꜩ0.004531 561 | Transaction: 562 | Amount: ꜩ10 563 | From: tz1fPjyo55HwUAkd1xcL5vo6DGzJrkxAMpiD 564 | To: KT1PTu7zW8rDMkHsLL9wkBJUH5PqJ2kdmSi8 565 | Parameter: (Right (Right (Left Unit))) 566 | This transaction was successfully applied 567 | Updated storage: 0x0000d8aebdc7e7d86e00d26b5f9c038dd87b01631ba6 568 | Storage size: 1322 bytes 569 | Consumed gas: 33491 570 | Balance updates: 571 | tz1fPjyo55HwUAkd1xcL5vo6DGzJrkxAMpiD ... -ꜩ10 572 | KT1PTu7zW8rDMkHsLL9wkBJUH5PqJ2kdmSi8 ... +ꜩ10 573 | Internal operations: 574 | Delegation: 575 | Contract: KT1PTu7zW8rDMkHsLL9wkBJUH5PqJ2kdmSi8 576 | To: nobody 577 | This delegation was successfully applied 578 | Consumed gas: 10000 579 | 580 | The operation has only been included 0 blocks ago. 581 | We recommend to wait more. 582 | Use command 583 | tezos-client wait for ooSsE3SwitQZv4JdmRoHjX9frpd3qbxz7CJt2ZLDHMAURRMbBm6 to be included --confirmations 30 --branch BLY6MXnBaxAy597XS3F8oabFV4VvEoV23DCVV69kuAbeSMTuWUi 584 | and/or an external block explorer. 585 | ~~~~ 586 | -------------------------------------------------------------------------------- /liquidity/manager/manager.liq: -------------------------------------------------------------------------------- 1 | type storage = address 2 | 3 | (* Spend fund. *) 4 | let%entry spendFund (toAddress,toAmount) owner = 5 | if Current.sender () <> owner then 6 | Current.failwith ("Only the owner can operate.", Current.sender ()) 7 | else 8 | let op = Account.transfer ~dest:toAddress ~amount:toAmount in 9 | [op], owner 10 | 11 | (* Change delegate. *) 12 | let%entry change_delegate (new_del : key_hash) owner = 13 | if Current.sender () <> owner then 14 | Current.failwith ("Only the owner can operate.", Current.sender ()) 15 | else 16 | let op = Contract.set_delegate (Some new_del) in 17 | [op], owner 18 | 19 | (* Remove delegate. *) 20 | let%entry remove_delegate () owner = 21 | if Current.sender () <> owner then 22 | Current.failwith ("Only the owner can operate.", Current.sender ()) 23 | else 24 | let op = Contract.set_delegate None in 25 | [op], owner 26 | 27 | (* Change owner. *) 28 | let%entry change_owner new_owner owner = 29 | if Current.sender () <> owner then 30 | Current.failwith ("Only the owner can operate.", Current.sender ()) 31 | else 32 | [], new_owner -------------------------------------------------------------------------------- /liquidity/manager/manager.tz: -------------------------------------------------------------------------------- 1 | parameter 2 | (or :_entries 3 | (pair %_Liq_entry_spendFund key_hash mutez) 4 | (or (key_hash %_Liq_entry_change_delegate) 5 | (or (unit %_Liq_entry_remove_delegate) (address %_Liq_entry_change_owner)))); 6 | storage address; 7 | code { DUP ; 8 | DIP { CDR @storage_slash_1 } ; 9 | CAR @parameter_slash_2 ; 10 | DUP @parameter ; 11 | IF_LEFT 12 | { RENAME @_toAddress_toAmount_slash_3 ; 13 | DUUUP @storage ; 14 | DUP @owner ; 15 | SENDER ; 16 | COMPARE ; 17 | NEQ ; 18 | IF { SENDER ; PUSH string "Only the owner can operate." ; PAIR ; FAILWITH } 19 | { DUUP ; 20 | CAR @toAddress ; 21 | IMPLICIT_ACCOUNT ; 22 | DUUUP ; 23 | CDR @toAmount ; 24 | UNIT ; 25 | TRANSFER_TOKENS @op ; 26 | DUUP @owner ; 27 | NIL operation ; 28 | DUUUP @op ; 29 | DIIIP { DROP } ; 30 | CONS ; 31 | PAIR } ; 32 | DIP { DROP ; DROP } } 33 | { IF_LEFT 34 | { RENAME @new_del_slash_8 ; 35 | DUUUP @storage ; 36 | DUP @owner ; 37 | SENDER ; 38 | COMPARE ; 39 | NEQ ; 40 | IF { SENDER ; PUSH string "Only the owner can operate." ; PAIR ; FAILWITH } 41 | { DUUP @new_del ; 42 | SOME ; 43 | SET_DELEGATE @op ; 44 | DUUP @owner ; 45 | NIL operation ; 46 | DUUUP @op ; 47 | DIIIP { DROP } ; 48 | CONS ; 49 | PAIR } ; 50 | DIP { DROP ; DROP } } 51 | { IF_LEFT 52 | { RENAME @__slash_11 ; 53 | DUUUP @storage ; 54 | DUP @owner ; 55 | SENDER ; 56 | COMPARE ; 57 | NEQ ; 58 | IF { SENDER ; PUSH string "Only the owner can operate." ; PAIR ; FAILWITH } 59 | { NONE key_hash ; 60 | SET_DELEGATE @op ; 61 | DUUP @owner ; 62 | NIL operation ; 63 | DUUUP @op ; 64 | DIIIP { DROP } ; 65 | CONS ; 66 | PAIR } ; 67 | DIP { DROP ; DROP } } 68 | { RENAME @new_owner_slash_14 ; 69 | DUUUP @storage ; 70 | SENDER ; 71 | COMPARE ; 72 | NEQ ; 73 | IF { SENDER ; PUSH string "Only the owner can operate." ; PAIR ; FAILWITH } 74 | { DUP @new_owner ; NIL operation ; PAIR } ; 75 | DIP { DROP } } } } ; 76 | DIP { DROP ; DROP } }; 77 | -------------------------------------------------------------------------------- /liquidity/swap_contract/README.md: -------------------------------------------------------------------------------- 1 | # Swapping fungible tokens on Tezos: 2 | 3 | This contract facilitates **token swaps between two parties**. Its use case includes a market place with asks and bids of two types of fungible tokens. Once an ask/bid offer is accepted, this contract can perform the transaction. The parties do not need to know or trust each other once an agreement for the swap is reached. 4 | 5 | The swap contract builds on tokens that are initiated with the [token contract](https://github.com/cryptiumlabs/smarter-contracts/tree/master/liquidity/token), and calls the *transferFrom* entry point of that contract. The swap contract ensures that the parties agree to the swapping amounts. 6 | 7 | For the transfers to succeed, the parties must have approved the agreed amounts in the respective token systems, and they must have enough balance for the agreed amounts. 8 | 9 | Once verified, the transfers of the two tokens are called. **Unless there are other errors**, the transfers should both succeed. If any of the transfers failed with other errors, none of the transfers would be performed. 10 | 11 | ## Contract Overview: 12 | 13 | To execute a swap the following contract must be originated in Tezos: 14 | - The token contracts (e.g., Token A and Token B) 15 | - The swap contract, it can swap any two types of tokens between any two Tezos addresses. 16 | 17 | ### Token contracts: 18 | 19 | The token systems of the tokens to be swapped must already be in place. The token systems' contract addresses will be passed to the swap contract as inputs. See [this](https://github.com/cryptiumlabs/smarter-contracts/blob/master/liquidity/token/README.md) for more information. 20 | 21 | ### The Swap contract: 22 | 23 | A swap contract has to be deployed (by anyone, but the offerer can do so without even knowing there is an accepter) with the offered amounts in the storage. Specifically: 24 | - offered amount by the offerer 25 | - accepted amount that the accepter has to send to the offerer 26 | - the offered token type’s token contract instance 27 | - the accepted token type’s token contract instance 28 | 29 | The offerer, after checking that the storage of a swap contract instance contains the correct offer, has to sign (with his/her private key) a message containing that swap contract instance’s address. This signature is the accepter’s knowledge. 30 | 31 | The accepter has to call the contract with the following two parameters: 32 | - the offerer’s public key, 33 | - the signature of the offerer signing the swap contract instance. 34 | 35 | The contract will only perform the transfers when the signature is correct and was signed by the offerer. Otherwise, ```failwith``` is fired. 36 | 37 | ## Implementation flaws 38 | 39 | This implementation of the swap contract presents a few flaws that can be eliminated in the next versions: 40 | 41 | First, a separate deployed swap contract is required for each set of agreed swap amounts, because the signed message only contains the swap contract instance. The code can be updated such that the offerer signed a message containing the agreed amounts. The deployed swap contract storage can be ```unit``. 42 | 43 | Second, this implementation requires that the offerer and the accepter perform certain actions themselves. Other contracts cannot perform the actions on their behalf. Shall this requirement become an issue the code has to be altered. 44 | 45 | ## An example contract call 46 | 47 | Here I provide an example on how the swap contract can be called. In this example, **Adam agrees to give Bob 1 Token A to get 1 Token B from Bob**. 48 | 49 | ### Initial balances 50 | 51 | For reference, let check Adam's (tz1M5538RTnMHsXgph8nQB7bke1y1gbDBXEx) balance of Token A before the transfers: 52 | 53 | ```tezos-client get big map value for '"tz1M5538RTnMHsXgph8nQB7bke1y1gbDBXEx"' of type address in tokenA``` 54 | 55 | The output shows that Adam has 170 Token A, and has approved Bob 30 before the transfer: 56 | 57 | ~~~~ 58 | Pair 170 { Elt 0x000074a095c7f5d1381f5136a970d67af5e569529e9b 30 } 59 | ~~~~ 60 | 61 | Similarly for Bob: 62 | 63 | ~~~~ 64 | tezos-client get big map value for '"tz1WGhT231BgqK7CrjbMB5xDsbdDSBJZiw5E"' of type address in tokenB 65 | Pair 140 {} 66 | ~~~~ 67 | 68 | ### Deploy the swap contract 69 | 70 | A swap contract with the initial storage containing the token amounts and the token contract addresses has to be deployed: 71 | 72 | ```tezos-client originate contract swap for adam transferring 100 from adam running ~/CryptiumLabs/smarter-contracts/liquidity/swap_contract/swaptokens.tz --init 'Pair 1 (Pair 1 (Pair "KT1Pv3DvEgKmtpiKG8mZWmUAiKFfDfRRjroH" "KT1P6h9yUAaeR3QzacQQdj8yTj8ruxk3s2fH"))' --burn-cap 3 --force``` 73 | 74 | The output shows the swap contract (KT1AP45uQwZN3qifwNousE1DL6YR2B4HUxUG) has been injected to the network: 75 | 76 | ~~~~ 77 | Node is bootstrapped, ready for injecting operations. 78 | Estimated gas: 575253 units (will add 100 for safety) 79 | Estimated storage: 2916 bytes added (will add 20 for safety) 80 | Operation successfully injected in the node. 81 | Operation hash is 'ootNJkfEKqDKtu18zoHghyfbWXbAPC7rNoUHbiTHAyJi2J6FVad' 82 | Waiting for the operation to be included... 83 | Operation found in block: BMF4fw68RmMrw11DMnuvt9TT8mjiBXCHx3faqBq9r4qEBrw9Sk7 (pass: 3, offset: 0) 84 | This sequence of operations was run: 85 | Manager signed operations: 86 | From: tz1M5538RTnMHsXgph8nQB7bke1y1gbDBXEx 87 | Fee to the baker: ꜩ0.060482 88 | Expected counter: 32829 89 | Gas limit: 575353 90 | Storage limit: 2936 bytes 91 | Balance updates: 92 | tz1M5538RTnMHsXgph8nQB7bke1y1gbDBXEx ............. -ꜩ0.060482 93 | fees(tz1boot2oCjTjUN6xDNoVmtCLRdh8cc92P1u,272) ... +ꜩ0.060482 94 | Origination: 95 | From: tz1M5538RTnMHsXgph8nQB7bke1y1gbDBXEx 96 | For: tz1M5538RTnMHsXgph8nQB7bke1y1gbDBXEx 97 | Credit: ꜩ100 98 | Script: 99 | [...] 100 | Initial storage: 101 | (Pair 1 102 | (Pair 1 103 | (Pair "KT1Pv3DvEgKmtpiKG8mZWmUAiKFfDfRRjroH" "KT1P6h9yUAaeR3QzacQQdj8yTj8ruxk3s2fH"))) 104 | No delegate for this contract 105 | This origination was successfully applied 106 | Originated contracts: 107 | KT1AP45uQwZN3qifwNousE1DL6YR2B4HUxUG 108 | Storage size: 2659 bytes 109 | Paid storage size diff: 2659 bytes 110 | Consumed gas: 575253 111 | Balance updates: 112 | tz1M5538RTnMHsXgph8nQB7bke1y1gbDBXEx ... -ꜩ2.659 113 | tz1M5538RTnMHsXgph8nQB7bke1y1gbDBXEx ... -ꜩ0.257 114 | tz1M5538RTnMHsXgph8nQB7bke1y1gbDBXEx ... -ꜩ100 115 | KT1AP45uQwZN3qifwNousE1DL6YR2B4HUxUG ... +ꜩ100 116 | 117 | New contract KT1AP45uQwZN3qifwNousE1DL6YR2B4HUxUG originated. 118 | The operation has only been included 0 blocks ago. 119 | We recommend to wait more. 120 | Use command 121 | tezos-client wait for ootNJkfEKqDKtu18zoHghyfbWXbAPC7rNoUHbiTHAyJi2J6FVad to be included --confirmations 30 --branch BM3icrvsx581DNPB1o8ZnTbumEZzktZF8KVGDpkpmEBD3epAqEt 122 | and/or an external block explorer. 123 | Contract memorized as swap. 124 | ~~~~ 125 | 126 | ### The offerer's actions 127 | 128 | The offerer follows the below steps to offer the above swap contract to the market: 129 | 130 | #### Check the storage 131 | 132 | First, the offerer verifies that the amounts and tokens in the storage are correct: 133 | 134 | ```tezos-client get script storage for swap``` 135 | 136 | ~~~~ 137 | Pair 1 138 | (Pair 1 139 | (Pair "KT1Pv3DvEgKmtpiKG8mZWmUAiKFfDfRRjroH" "KT1P6h9yUAaeR3QzacQQdj8yTj8ruxk3s2fH")) 140 | ~~~~ 141 | 142 | #### Sign the contract (message) 143 | 144 | Then, the offerer signs a message containing the address of the verified swap contract: 145 | 146 | First, pack the address into raw data to sign: 147 | 148 | ```tezos-client hash data '"KT1AP45uQwZN3qifwNousE1DL6YR2B4HUxUG"' of type address``` 149 | 150 | ~~~~ 151 | Raw packed data: 0x050a000000160113b90cc7fbf509ba177bca1e5ab63e90bad4ee4100 152 | Hash: expruCwtnWwwThLq2bNyumVWznVCMCZqBnYcbcP465JaN7SAaWyVCi 153 | Raw Blake2b hash: 0x5afb346b97da67c7e24a359232b4fd1f7bcc4b05e3d531eef38fb58f101957f2 154 | Raw Sha256 hash: 0xefa56bf16571addcc94b246cc9771503e31283221e1e626bd7d6a58a858d945f 155 | Raw Sha512 hash: 0xc297fdea1947e95d98d5aa7cf3421464a2b64c9d231409e4a6361df50d2e2a22f3d1400647d7917dc3db12e5c09cd07ae9fd6a1c052cc71f9623b96f9e2b333f 156 | Gas remaining: 799901 units remaining 157 | ~~~~ 158 | 159 | Then, sign it with ```tezos-client sign bytes data for src``` 160 | Sign a raw sequence of bytes and display it using the format expected by Michelson instruction `CHECK_SIGNATURE`. data: the raw data to sign src: source secret_key 161 | 162 | ```tezos-client sign bytes 0x050a000000160113b90cc7fbf509ba177bca1e5ab63e90bad4ee4100 for adam``` 163 | 164 | ~~~~ 165 | Signature: edsigu6d4Tsir3kWPeALPxN25qQ5HEokP5sBHVfEj6uxrh5YZWru6PcKzikQGwo1Q9g1uzHm7Qs1CbqNTCVVX3Dwg3kya6fP9d7 166 | ~~~~ 167 | 168 | Then, the offerer can annouce his offer and the signature. 169 | 170 | ### The accepter's actions 171 | 172 | The accepter has to call the swap contract with the following two parameters: 173 | - the offerer’s public key, 174 | - the signature of the offerer signing the swap contract (sent by the offerer) 175 | 176 | #### To get adam’s public key: 177 | 178 | ~~~~ 179 | tezos-client show address adam 180 | Hash: tz1M5538RTnMHsXgph8nQB7bke1y1gbDBXEx 181 | Public Key: edpktnEqY6x6NKdVWQHmLL3WVpqCaqhuGFQnNT62Kqk6hPbEhw8JnP 182 | ~~~~ 183 | 184 | #### Call the contract with the parameters: 185 | 186 | ```tezos-client transfer 10 from bob to swap --arg '(Pair "edpktnEqY6x6NKdVWQHmLL3WVpqCaqhuGFQnNT62Kqk6hPbEhw8JnP" "edsigu6d4Tsir3kWPeALPxN25qQ5HEokP5sBHVfEj6uxrh5YZWru6PcKzikQGwo1Q9g1uzHm7Qs1CbqNTCVVX3Dwg3kya6fP9d7")'``` 187 | 188 | ~~~~ 189 | Node is bootstrapped, ready for injecting operations. 190 | Estimated gas: 730605 units (will add 100 for safety) 191 | Estimated storage: no bytes added 192 | Operation successfully injected in the node. 193 | Operation hash is 'onxdQ7qZq3zpEMg4QqNkSHtXD8Tk8QpsDnAfjU7VDMbFeZXcarn' 194 | Waiting for the operation to be included... 195 | Operation found in block: BLL5EpzJ8GybMzvfWfMbHhUZhP1oLSTDRWdkB4nmpi1QxYr6q6S (pass: 3, offset: 0) 196 | This sequence of operations was run: 197 | Manager signed operations: 198 | From: tz1WGhT231BgqK7CrjbMB5xDsbdDSBJZiw5E 199 | Fee to the baker: ꜩ0.073496 200 | Expected counter: 32825 201 | Gas limit: 730705 202 | Storage limit: 0 bytes 203 | Balance updates: 204 | tz1WGhT231BgqK7CrjbMB5xDsbdDSBJZiw5E ............. -ꜩ0.073496 205 | fees(tz1a1AxBfCoGLfDWf2gX4QBc8towpAb3381W,272) ... +ꜩ0.073496 206 | Transaction: 207 | Amount: ꜩ10 208 | From: tz1WGhT231BgqK7CrjbMB5xDsbdDSBJZiw5E 209 | To: KT1AP45uQwZN3qifwNousE1DL6YR2B4HUxUG 210 | Parameter: (Pair "edpktnEqY6x6NKdVWQHmLL3WVpqCaqhuGFQnNT62Kqk6hPbEhw8JnP" 211 | "edsigu6d4Tsir3kWPeALPxN25qQ5HEokP5sBHVfEj6uxrh5YZWru6PcKzikQGwo1Q9g1uzHm7Qs1CbqNTCVVX3Dwg3kya6fP9d7") 212 | This transaction was successfully applied 213 | Updated storage: 214 | (Pair 1 215 | (Pair 1 216 | (Pair 0x01a82edc4acd7a090cdc5bbed108691d3510fee1e800 217 | 0x019f3a8ef25d44575a6d6ac5ae98bfc2f0bf2a779900))) 218 | Storage size: 2659 bytes 219 | Consumed gas: 310642 220 | Balance updates: 221 | tz1WGhT231BgqK7CrjbMB5xDsbdDSBJZiw5E ... -ꜩ10 222 | KT1AP45uQwZN3qifwNousE1DL6YR2B4HUxUG ... +ꜩ10 223 | Internal operations: 224 | Transaction: 225 | Amount: ꜩ0 226 | From: KT1AP45uQwZN3qifwNousE1DL6YR2B4HUxUG 227 | To: KT1Pv3DvEgKmtpiKG8mZWmUAiKFfDfRRjroH 228 | Parameter: (Right 229 | (Right 230 | (Left (Pair 0x00000fb47b79f9e09a7078c52738196d68f542ec69d2 231 | (Pair 0x000074a095c7f5d1381f5136a970d67af5e569529e9b 1))))) 232 | This transaction was successfully applied 233 | Updated storage: 234 | (Pair {} 235 | (Pair 0 236 | (Pair 1000 (Pair "tokenA" (Pair "A" 0x0000c6efaf487d66c90065db616da7432dbdc54b4401))))) 237 | Storage size: 9768 bytes 238 | Consumed gas: 210456 239 | Transaction: 240 | Amount: ꜩ0 241 | From: KT1AP45uQwZN3qifwNousE1DL6YR2B4HUxUG 242 | To: KT1P6h9yUAaeR3QzacQQdj8yTj8ruxk3s2fH 243 | Parameter: (Right 244 | (Right 245 | (Left (Pair 0x000074a095c7f5d1381f5136a970d67af5e569529e9b 246 | (Pair 0x00000fb47b79f9e09a7078c52738196d68f542ec69d2 1))))) 247 | This transaction was successfully applied 248 | Updated storage: 249 | (Pair {} 250 | (Pair 0 251 | (Pair 1000 (Pair "tokenB" (Pair "B" 0x0000c6efaf487d66c90065db616da7432dbdc54b4401))))) 252 | Storage size: 9737 bytes 253 | Consumed gas: 209507 254 | 255 | The operation has only been included 0 blocks ago. 256 | We recommend to wait more. 257 | Use command 258 | tezos-client wait for onxdQ7qZq3zpEMg4QqNkSHtXD8Tk8QpsDnAfjU7VDMbFeZXcarn to be included --confirmations 30 --branch BLu7gJSCvFuYVW8n6BNmHuYrRrJ5UGA1nDAAirjEjFiaZqbu8Yu 259 | and/or an external block explorer. 260 | ~~~~ 261 | 262 | #### When the signature is wrong, failwith is fired: 263 | ~~~~ 264 | tezos-client transfer 10 from bob to swap --arg '(Pair "edpktnEqY6x6NKdVWQHmLL3WVpqCaqhuGFQnNT62Kqk6hPbEhw8JnP" "edsigu6ZoAx2xUu1ukGBwt6SmUtw5fa3NArB1KptH2FKNSG7UwZuNUEBbqtpM4PJr7kqsVKiDUJVLVu1Zdwetgkdm8y8JKsH8aR")' 265 | ~~~~ 266 | 267 | ~~~~ 268 | Node is bootstrapped, ready for injecting operations. 269 | This simulation failed: 270 | Manager signed operations: 271 | From: tz1WGhT231BgqK7CrjbMB5xDsbdDSBJZiw5E 272 | Fee to the baker: ꜩ0 273 | Expected counter: 32825 274 | Gas limit: 800000 275 | Storage limit: 600000 bytes 276 | Transaction: 277 | Amount: ꜩ10 278 | From: tz1WGhT231BgqK7CrjbMB5xDsbdDSBJZiw5E 279 | To: KT1AP45uQwZN3qifwNousE1DL6YR2B4HUxUG 280 | Parameter: (Pair "edpktnEqY6x6NKdVWQHmLL3WVpqCaqhuGFQnNT62Kqk6hPbEhw8JnP" 281 | "edsigu6ZoAx2xUu1ukGBwt6SmUtw5fa3NArB1KptH2FKNSG7UwZuNUEBbqtpM4PJr7kqsVKiDUJVLVu1Zdwetgkdm8y8JKsH8aR") 282 | This operation FAILED. 283 | 284 | Runtime error in contract KT1AP45uQwZN3qifwNousE1DL6YR2B4HUxUG: 285 | [......] 286 | At line 117 characters 46 to 54, 287 | script reached FAILWITH instruction 288 | with "Wrong signature" 289 | Fatal error: 290 | transfer simulation failed 291 | ~~~~ 292 | 293 | 294 | 295 | 296 | -------------------------------------------------------------------------------- /liquidity/swap_contract/swaptokens.liq: -------------------------------------------------------------------------------- 1 | (* Token contract signature. *) 2 | contract type NatContract = sig 3 | type storage 4 | val%entry main : nat -> _ 5 | end 6 | 7 | contract type NatNatContract = sig 8 | type storage 9 | val%entry main : nat * nat -> _ 10 | end 11 | 12 | contract type Token = sig 13 | 14 | type account = { 15 | balance : nat; 16 | allowances : (address, nat) map; 17 | } 18 | 19 | type storage = { 20 | accounts : (address, account) big_map; 21 | version : nat; (* version of token standard *) 22 | totalSupply : nat; 23 | decimals : nat; 24 | name : string; 25 | symbol : string; 26 | owner : address; 27 | } 28 | 29 | val%entry transfer : (address * nat) -> _ 30 | val%entry approve : (address * nat) -> _ 31 | val%entry transferFrom : (address * address * nat) -> _ 32 | val%entry balanceOf : (address * NatContract.instance) -> _ 33 | val%entry allowance : (address * address * NatNatContract.instance) -> _ 34 | val%entry createAccount : (address * nat) -> _ 35 | val%entry createAccounts : (address * nat) list -> _ 36 | 37 | end 38 | (* End of signature. *) 39 | 40 | type storage = { 41 | offered_amount : nat; 42 | accepted_amount : nat; 43 | offered_token : Token.instance; 44 | accepted_token : Token.instance; 45 | } 46 | 47 | let%entry main (key, signature) storage = 48 | let amount = 0tz in 49 | let accepter = Current.source () in 50 | let offerer_hash = Crypto.hash_key key in 51 | let offerer = Contract.address (Account.default offerer_hash) in 52 | if Crypto.check key signature (Bytes.pack (Contract.self ())) then 53 | let opa = storage.offered_token.transferFrom (offerer,accepter,storage.offered_amount) ~amount in 54 | let opb = storage.accepted_token.transferFrom (accepter,offerer,storage.accepted_amount) ~amount in 55 | [opa;opb], storage 56 | else 57 | failwith "Wrong signature" 58 | -------------------------------------------------------------------------------- /liquidity/swap_contract/swaptokens.tz: -------------------------------------------------------------------------------- 1 | parameter (pair key signature); 2 | storage 3 | (pair :storage 4 | (nat %offered_amount) 5 | (pair (nat %accepted_amount) 6 | (pair (contract :Token %offered_token 7 | (or :_entries 8 | (pair %_Liq_entry_transfer address nat) 9 | (or (pair %_Liq_entry_approve address nat) 10 | (or (pair %_Liq_entry_transferFrom address (pair address nat)) 11 | (or (pair %_Liq_entry_balanceOf address (contract :NatContract nat)) 12 | (or (pair %_Liq_entry_allowance 13 | address 14 | (pair address (contract :NatNatContract (pair nat nat)))) 15 | (or (pair %_Liq_entry_createAccount address nat) 16 | (list %_Liq_entry_createAccounts (pair address nat))))))))) 17 | (contract :Token %accepted_token 18 | (or :_entries 19 | (pair %_Liq_entry_transfer address nat) 20 | (or (pair %_Liq_entry_approve address nat) 21 | (or (pair %_Liq_entry_transferFrom address (pair address nat)) 22 | (or (pair %_Liq_entry_balanceOf address (contract :NatContract nat)) 23 | (or (pair %_Liq_entry_allowance 24 | address 25 | (pair address (contract :NatNatContract (pair nat nat)))) 26 | (or (pair %_Liq_entry_createAccount address nat) 27 | (list %_Liq_entry_createAccounts (pair address nat)))))))))))); 28 | code { DUP ; 29 | DIP { CDR @storage_slash_1 } ; 30 | CAR @_key_signature_slash_2 ; 31 | DUP ; 32 | CAR @key ; 33 | PUSH @amount mutez 0 ; 34 | SOURCE @accepter ; 35 | DUUUP @key ; 36 | HASH_KEY @offerer_hash ; 37 | IMPLICIT_ACCOUNT ; 38 | ADDRESS @offerer ; 39 | SELF ; 40 | PACK ; 41 | DUUUUUUP ; 42 | CDR @signature ; 43 | DUUUUUUP @key ; 44 | CHECK_SIGNATURE ; 45 | IF { DUUUUUUP @storage ; 46 | CDDAR %offered_token ; 47 | DUUUUP @amount ; 48 | DUUUUUUUUP @storage ; 49 | CAR %offered_amount ; 50 | DUUUUUP @accepter ; 51 | PAIR ; 52 | DUUUUP @offerer ; 53 | PAIR ; 54 | LEFT %_Liq_entry_transferFrom % 55 | (or (pair %_Liq_entry_balanceOf address (contract :NatContract nat)) 56 | (or (pair %_Liq_entry_allowance 57 | address 58 | (pair address (contract :NatNatContract (pair nat nat)))) 59 | (or (pair %_Liq_entry_createAccount address nat) 60 | (list %_Liq_entry_createAccounts (pair address nat))))) ; 61 | RIGHT % %_ (pair address nat) ; 62 | RIGHT % %_ (pair address nat) ; 63 | TRANSFER_TOKENS @opa ; 64 | DUUUUUUUP @storage ; 65 | CDDDR %accepted_token ; 66 | DUUUUUP @amount ; 67 | DUUUUUUUUUP @storage ; 68 | CDAR %accepted_amount ; 69 | DUUUUUP @offerer ; 70 | PAIR ; 71 | DUUUUUUP @accepter ; 72 | PAIR ; 73 | LEFT %_Liq_entry_transferFrom % 74 | (or (pair %_Liq_entry_balanceOf address (contract :NatContract nat)) 75 | (or (pair %_Liq_entry_allowance 76 | address 77 | (pair address (contract :NatNatContract (pair nat nat)))) 78 | (or (pair %_Liq_entry_createAccount address nat) 79 | (list %_Liq_entry_createAccounts (pair address nat))))) ; 80 | RIGHT % %_ (pair address nat) ; 81 | RIGHT % %_ (pair address nat) ; 82 | TRANSFER_TOKENS @opb ; 83 | DUUUUUUUUP @storage ; 84 | NIL operation ; 85 | DUUUP @opb ; 86 | DIIIP { DROP } ; 87 | CONS ; 88 | DUUUP ; 89 | DIIIP { DROP } ; 90 | CONS ; 91 | PAIR } 92 | { PUSH string "Wrong signature" ; FAILWITH } ; 93 | DIP { DROP ; DROP ; DROP ; DROP ; DROP ; DROP } }; 94 | -------------------------------------------------------------------------------- /liquidity/token/README.md: -------------------------------------------------------------------------------- 1 | This token standard was inspired by [this](https://github.com/OCamlPro/liquidity/blob/next/tests/others/token.liq). A comparable Ethereum standard can be found [here](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md). 2 | 3 | # Start a token system 4 | 5 | To start a token system, one first has to originate the token contract on the Tezos network. Then, accounts can be created and endowed with tokens. 6 | 7 | ## Deploy the token contract 8 | 9 | To originate a token system using the tezos-client, run: 10 | 11 | ```tezos-client originate contract [name of the token contract] for [manager of the token contract] transferring [amount to transfer from source] from [address that pays for the origination] running [the token script in .tz] --init [the initial storage]``` 12 | 13 | Alternatively, one can originate the contract using [Liquidity](http://www.liquidity-lang.org/doc/usage/index.html#running-a-simulation-of-the-contract) or the [Liquidity online editor](http://www.liquidity-lang.org/edit/). 14 | 15 | The token script has a storage initializer that initializes the initial storage. The inputs to pass to the initializer include: 16 | 17 | - **owner** (owner of the token system, only this owner can create accounts /mint tokens, of type (KT1/tz1) *address*): 18 | e.g., tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS 19 | - **totalSupply** (total supply of this type of tokens, of type *nat*): e.g., 500p 20 | - **name** (the name of the token, of type *string*): e.g., "TokenA" or "TokenB" 21 | - **symbol** (symbol representation of the token, of type *string*): e.g.,"A" or "B" 22 | 23 | The initial storage has the following fields: 24 | ~~~~ 25 | { 26 | accounts = BigMap; 27 | version = 1p; 28 | totalSupply = 500p; 29 | name = "TokenB"; 30 | symbol = "B"; 31 | owner = (tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS : address) 32 | } 33 | ~~~~ 34 | After the deployment, the "accounts" field is an empty *BigMap*. The owner (in this example,tz1ccqAEwfPgeoipnXtjAv1iucrpQv3DFmmS) can add accounts and transfer tokens into these accounts with the *createAccount* or *createAccounts* entry points. 35 | 36 | ## Create account(s) 37 | 38 | To create an account, choose the entry point *createAccount* with the parameters: 39 | 40 | - the address of the owner 41 | - the amount of tokens this address owns 42 | 43 | To create mulitple accounts, choose the entry point *createAccounts*, and the parameter is a list of pairs of (address, [amount of tokens]). 44 | 45 | After the accounts are created, the big map of accounts is updated with the new accounts and their balances. The accounts also has a map of *allowance* associated with each address (see next section), describing the amounts allowed to other account holders from the associated account holder. 46 | 47 | To check that the accounts are created, one can use the Tezos CLI. To check that an account with 200 token A was created for Adam of address tz1Ra8yQVQN4Nd7LpPQ6UT6t3bsWWqHZ9wa6, run: 48 | 49 | ```tezos-client get big map value for '"tz1fPjyo55HwUAkd1xcL5vo6DGzJrkxAMpiD"' of type address in KT1Gu5MXA5RpXo2veYc7n33QVmH1rdTVZhum``` 50 | 51 | The output is a pair of *balance* of type *nat* and *allowance* of type *map*. Adam has a balance of 200 tokens. No allowance was set for him, so the value of *allowance* is an empty map: 52 | 53 | ~~~~ 54 | Pair 200 {} 55 | ~~~~ 56 | 57 | Bob of address tz1Ra8yQVQN4Nd7LpPQ6UT6t3bsWWqHZ9wa6 has no account and the ouput shows an error: 58 | ````tezos-client get big map value for '"tz1Ra8yQVQN4Nd7LpPQ6UT6t3bsWWqHZ9wa6"' of type address in KT1Gu5MXA5RpXo2veYc7n33QVmH1rdTVZhum```` 59 | 60 | ~~~~ 61 | Fatal error: 62 | No value associated to this key. 63 | ~~~~ 64 | 65 | ## Transfer tokens 66 | 67 | The *transfer* entry point lets an account holder transfer tokens to an address. The parameter input to this entry point is an *(address, nat) pair*, describing the recipient address and the tokens to be transferred. 68 | 69 | ## Set allowance (approve) 70 | 71 | The *approve* entry point lets an account holder set an amount of tokens that contracts can transfer on the account holder's behalf. *allowance* is of type *(address, nat) map*. Each map entry describes an allowance amount to a recipient address. 72 | 73 | After the allowance is set, other contracts can call the *transferFrom* entry point and perform a transfer from the account holder to the specified recipient, up to the allowance amount. Once a *transferFrom* is performed, the allowance amount is updated. 74 | 75 | For Adam to approve an amount of 30 token A to Bob, Adam has to call the contract (Token A's contract address) with the input of (tz1Ra8yQVQN4Nd7LpPQ6UT6t3bsWWqHZ9wa6,30p). After the operation was included, one can confirm that the new allowance is set: 76 | 77 | ```tezos-client get big map value for '"tz1fPjyo55HwUAkd1xcL5vo6DGzJrkxAMpiD"' of type address in KT1Gu5MXA5RpXo2veYc7n33QVmH1rdTVZhum``` 78 | 79 | The output now has the allowance map updated with Bob's address (in optimized format) and the allowance amount: 80 | 81 | ~~~~ 82 | Pair 200 { Elt 0x000041145574571df6030acad578fdc8d41c4979f0df 30 } 83 | ~~~~ 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /liquidity/token/token.initializer.tz: -------------------------------------------------------------------------------- 1 | parameter (pair address (pair nat (pair string string))); 2 | storage 3 | (pair :storage 4 | (big_map :accounts 5 | address 6 | (pair :account (nat %balance) (map %allowances address nat))) 7 | (pair (nat %version) 8 | (pair (nat %totalSupply) (pair (string %name) (pair (string %symbol) (address %owner)))))); 9 | code { DUP ; 10 | DIP { CDR @_storage } ; 11 | CAR @_parameter ; 12 | DUP ; 13 | CAR ; 14 | { DIP { DUP } ; SWAP } ; 15 | { CDR ; CAR } ; 16 | { DIP { { DIP { DUP } ; SWAP } } ; SWAP } ; 17 | { CDR ; CDR ; CAR } ; 18 | { DIP { { DIP { { DIP { DUP } ; SWAP } } ; SWAP } } ; SWAP } ; 19 | { CDR ; CDR ; CDR } ; 20 | PUSH (map address nat) {} ; 21 | { DIP { { DIP { { DIP { DUP @totalSupply } ; SWAP } } ; SWAP } } ; 22 | SWAP } ; 23 | PAIR @owner_account %balance %allowances ; 24 | { DIP { { DIP { { DIP { { DIP { { DIP { { DIP { DUP } ; SWAP } } ; SWAP } } ; SWAP } } ; 25 | SWAP } } ; 26 | SWAP } } ; 27 | SWAP } ; 28 | CAR ; 29 | { DIP { DUP @owner_account } ; SWAP } ; 30 | { DIP { { DIP { { DIP { { DIP { { DIP { { DIP { DUP @owner } ; SWAP } } ; SWAP } } ; 31 | SWAP } } ; 32 | SWAP } } ; 33 | SWAP } } ; 34 | SWAP } ; 35 | DIP { SOME } ; 36 | UPDATE @accounts ; 37 | { DIP { { DIP { { DIP { { DIP { { DIP { DUP @owner } ; SWAP } } ; SWAP } } ; 38 | SWAP } } ; 39 | SWAP } } ; 40 | SWAP } ; 41 | { DIP { { DIP { { DIP { DUP @symbol } ; SWAP } } ; SWAP } } ; 42 | SWAP } ; 43 | PAIR %symbol %owner ; 44 | { DIP { { DIP { { DIP { { DIP { DUP @name } ; SWAP } } ; SWAP } } ; 45 | SWAP } } ; 46 | SWAP } ; 47 | PAIR %name ; 48 | { DIP { { DIP { { DIP { { DIP { { DIP { DUP @totalSupply } ; SWAP } } ; SWAP } } ; 49 | SWAP } } ; 50 | SWAP } } ; 51 | SWAP } ; 52 | PAIR %totalSupply ; 53 | PUSH nat 1 ; 54 | PAIR %version ; 55 | { DIP { DUP @accounts } ; SWAP } ; 56 | PAIR %accounts ; 57 | DIP { DROP } ; 58 | DIP { DROP } ; 59 | DIP { DROP } ; 60 | DIP { DROP } ; 61 | DIP { DROP } ; 62 | DIP { DROP } ; 63 | NIL operation ; 64 | PAIR ; 65 | DIP { DROP ; DROP } }; 66 | -------------------------------------------------------------------------------- /liquidity/token/token.liq: -------------------------------------------------------------------------------- 1 | type account = { 2 | balance : nat; 3 | allowances : (address, nat) map; 4 | } 5 | 6 | type storage = { 7 | accounts : (address, account) big_map; 8 | version : nat; (* version of token standard *) 9 | totalSupply : nat; 10 | name : string; 11 | symbol : string; 12 | owner : address; 13 | } 14 | 15 | let%init storage owner totalSupply name symbol = 16 | let owner_account = 17 | { balance = totalSupply; 18 | allowances = Map } in 19 | let accounts = 20 | Map.add owner owner_account BigMap in 21 | { 22 | accounts; 23 | version = 1p; 24 | totalSupply; 25 | name; 26 | symbol; 27 | owner; 28 | } 29 | 30 | let get_account (a, (accounts : (address, account) big_map)) = 31 | match Map.find a accounts with 32 | | None -> { balance = 0p; allowances = Map } 33 | | Some account -> account 34 | 35 | let perform_transfer 36 | (from, dest, tokens, storage) = 37 | let accounts = storage.accounts in 38 | let account_sender = get_account (from, accounts) in 39 | let new_account_sender = match is_nat (account_sender.balance - tokens) with 40 | | None -> 41 | failwith ("Not enough tokens for transfer", account_sender.balance) 42 | | Some b -> account_sender.balance <- b in 43 | let accounts = Map.add from new_account_sender accounts in 44 | let account_dest = get_account (dest, accounts) in 45 | let new_account_dest = 46 | account_dest.balance <- account_dest.balance + tokens in 47 | let accounts = Map.add dest new_account_dest accounts in 48 | [], storage.accounts <- accounts 49 | 50 | let%entry transfer (dest, tokens) storage = 51 | perform_transfer (Current.sender (), dest, tokens, storage) 52 | 53 | let%entry approve (spender, tokens) storage = 54 | let account_sender = get_account (Current.sender (), storage.accounts) in 55 | let account_sender = 56 | account_sender.allowances <- 57 | if tokens = 0p then 58 | Map.remove spender account_sender.allowances 59 | else 60 | Map.add spender tokens account_sender.allowances in 61 | let storage = storage.accounts <- 62 | Map.add (Current.sender ()) account_sender storage.accounts in 63 | [], storage 64 | 65 | let%entry transferFrom (from, dest, tokens) storage = 66 | let account_from = get_account (from, storage.accounts) in 67 | let new_allowances_from = 68 | match Map.find dest account_from.allowances with 69 | | None -> failwith ("Not allowed to spend from", from) 70 | | Some allowed -> 71 | match is_nat (allowed - tokens) with 72 | | None -> 73 | failwith ("Not enough allowance for transfer", allowed) 74 | | Some allowed -> 75 | if allowed = 0p then 76 | Map.remove dest account_from.allowances 77 | else 78 | Map.add dest allowed account_from.allowances in 79 | let account_from = account_from.allowances <- new_allowances_from in 80 | let storage = storage.accounts <- 81 | Map.add from account_from storage.accounts in 82 | perform_transfer (from, dest, tokens, storage) 83 | 84 | 85 | 86 | (* ------------- Storage access from outside ------------- *) 87 | 88 | contract type NatContract = sig 89 | type storage 90 | val%entry main : nat -> _ 91 | end 92 | 93 | contract type NatNatContract = sig 94 | type storage 95 | val%entry main : nat * nat -> _ 96 | end 97 | 98 | let%entry balanceOf (spender, forward) storage = 99 | let spender_balance = match Map.find spender storage.accounts with 100 | | None -> 0p 101 | | Some account -> account.balance in 102 | [ forward.main spender_balance ~amount:0tz ], storage 103 | 104 | let%entry allowance (from, spender, forward) 105 | storage = 106 | let spender_allowance = match Map.find from storage.accounts with 107 | | None -> 0p, 0p 108 | | Some account -> match Map.find spender account.allowances with 109 | | None -> 0p, account.balance 110 | | Some allowed -> allowed, account.balance in 111 | [ forward.main spender_allowance ~amount:0tz ], storage 112 | 113 | 114 | (* -------------- Creating accounts ------------------------ *) 115 | 116 | let%entry createAccount (dest, tokens) storage = 117 | if Current.sender () <> storage.owner then 118 | failwith "Only owner can create accounts"; 119 | perform_transfer (storage.owner, dest, tokens, storage) 120 | 121 | let%entry createAccounts new_accounts storage = 122 | if Current.sender () <> storage.owner then 123 | failwith "Only owner can create accounts"; 124 | List.fold (fun ((dest, tokens), (_ops, storage)) -> 125 | perform_transfer (storage.owner, dest, tokens, storage) 126 | ) new_accounts ([], storage) -------------------------------------------------------------------------------- /liquidity/token/token.tz: -------------------------------------------------------------------------------- 1 | parameter 2 | (or :_entries 3 | (pair %_Liq_entry_transfer address nat) 4 | (or (pair %_Liq_entry_approve address nat) 5 | (or (pair %_Liq_entry_transferFrom address (pair address nat)) 6 | (or (pair %_Liq_entry_balanceOf address (contract :NatContract nat)) 7 | (or (pair %_Liq_entry_allowance 8 | address 9 | (pair address (contract :NatNatContract (pair nat nat)))) 10 | (or (pair %_Liq_entry_createAccount address nat) 11 | (list %_Liq_entry_createAccounts (pair address nat)))))))); 12 | storage 13 | (pair :storage 14 | (big_map :accounts 15 | address 16 | (pair :account (nat %balance) (map %allowances address nat))) 17 | (pair (nat %version) 18 | (pair (nat %totalSupply) (pair (string %name) (pair (string %symbol) (address %owner)))))); 19 | code { DUP ; 20 | DIP { CDR @storage_slash_1 } ; 21 | CAR @parameter_slash_2 ; 22 | LAMBDA @get_account 23 | (pair address 24 | (big_map address (pair :account (nat %balance) (map %allowances address nat)))) 25 | (pair :account (nat %balance) (map %allowances address nat)) 26 | { RENAME @_a_accounts_slash_3 ; 27 | DUP ; 28 | CDR @accounts ; 29 | DUUP ; 30 | CAR @a ; 31 | GET ; 32 | IF_NONE 33 | { PUSH (map address nat) {} ; PUSH nat 0 ; PAIR %balance %allowances } 34 | {} ; 35 | DIP { DROP } } ; 36 | DUP @get_account ; 37 | LAMBDA 38 | (pair (pair address 39 | (pair address 40 | (pair nat 41 | (pair :storage 42 | (big_map :accounts 43 | address 44 | (pair :account (nat %balance) (map %allowances address nat))) 45 | (pair (nat %version) 46 | (pair (nat %totalSupply) (pair (string %name) (pair (string %symbol) (address %owner))))))))) 47 | (lambda 48 | (pair address 49 | (big_map address (pair :account (nat %balance) (map %allowances address nat)))) 50 | (pair :account (nat %balance) (map %allowances address nat)))) 51 | (pair (list operation) 52 | (pair :storage 53 | (big_map :accounts 54 | address 55 | (pair :account (nat %balance) (map %allowances address nat))) 56 | (pair (nat %version) 57 | (pair (nat %totalSupply) (pair (string %name) (pair (string %symbol) (address %owner))))))) 58 | { RENAME @closure_env_slash_8 ; 59 | DUP ; 60 | CAR ; 61 | CAR @from ; 62 | DUUP ; 63 | CAR ; 64 | CDAR @dest ; 65 | DUUUP ; 66 | CAR ; 67 | CDDAR @tokens ; 68 | DUUUUP ; 69 | CAR ; 70 | CDDDR @storage ; 71 | DUP @storage ; 72 | CAR @accounts %accounts ; 73 | DUUUUUUP ; 74 | CDR @get_account_slash_7 ; 75 | DUUP @accounts ; 76 | DUUUUUUUP @from ; 77 | PAIR ; 78 | EXEC @account_sender ; 79 | DUUP @accounts ; 80 | DUUUUUP @tokens ; 81 | DUUUP @account_sender ; 82 | CAR %balance ; 83 | SUB ; 84 | ISNAT ; 85 | IF_NONE 86 | { DUUP @account_sender ; 87 | CAR %balance ; 88 | PUSH string "Not enough tokens for transfer" ; 89 | PAIR ; 90 | FAILWITH } 91 | { DUUUP @account_sender ; 92 | CDR %allowances ; 93 | SWAP ; 94 | PAIR %balance %allowances } ; 95 | RENAME @new_account_sender ; 96 | DUUUUUUUUP @from ; 97 | DIP { SOME } ; 98 | UPDATE @accounts ; 99 | DUUUUUUUUP ; 100 | CDR @get_account_slash_7 ; 101 | DUUP @accounts ; 102 | DUUUUUUUUP @dest ; 103 | PAIR ; 104 | EXEC @account_dest ; 105 | DUUUUUP @storage ; 106 | CDR ; 107 | DUUUP @accounts ; 108 | DUUUP @account_dest ; 109 | CDR %allowances ; 110 | DUUUUUUUUUP @tokens ; 111 | DUUUUUP @account_dest ; 112 | CAR %balance ; 113 | ADD ; 114 | PAIR @new_account_dest %balance %allowances ; 115 | DUUUUUUUUUUP @dest ; 116 | DIP { SOME } ; 117 | DIIIIP { DROP ; DROP ; DROP ; DROP ; DROP ; DROP ; DROP ; DROP ; DROP } ; 118 | UPDATE @accounts ; 119 | PAIR %accounts ; 120 | NIL operation ; 121 | PAIR } ; 122 | PAIR @perform_transfer_T4aanRstorageaccountsBaRaccountbalancenallowancesManversionntotalSupplynnamessymbolsowneraT2LoRstorageaccountsBaRaccountbalancenallowancesManversionntotalSupplynnamessymbolsownera ; 123 | DUUUP @parameter ; 124 | IF_LEFT 125 | { RENAME @_dest_tokens_slash_23 ; 126 | DUUP @perform_transfer_T4aanRstorageaccountsBaRaccountbalancenallowancesManversionntotalSupplynnamessymbolsowneraT2LoRstorageaccountsBaRaccountbalancenallowancesManversionntotalSupplynnamessymbolsownera ; 127 | DUUUUUUP @storage ; 128 | DUUUP ; 129 | CDR @tokens ; 130 | PAIR ; 131 | DUUUP ; 132 | CAR @dest ; 133 | PAIR ; 134 | SENDER ; 135 | PAIR ; 136 | DIP { DUP ; CAR ; SWAP ; CDR } ; 137 | DIIIP { DROP } ; 138 | PAIR ; 139 | EXEC } 140 | { IF_LEFT 141 | { RENAME @_spender_tokens_slash_27 ; 142 | DUUUUUP @storage ; 143 | DUUP ; 144 | CAR @spender ; 145 | DUUUP ; 146 | CDR @tokens ; 147 | DUUUUUUP @get_account ; 148 | DUUUUP @storage ; 149 | CAR %accounts ; 150 | SENDER ; 151 | PAIR ; 152 | EXEC @account_sender ; 153 | DUUUUP @storage ; 154 | CDR ; 155 | DUUUUUP @storage ; 156 | CAR %accounts ; 157 | DUUUP @account_sender ; 158 | CAR %balance ; 159 | PUSH nat 0 ; 160 | DUUUUUUP @tokens ; 161 | COMPARE ; 162 | EQ ; 163 | IF { DUUUUP @account_sender ; 164 | CDR %allowances ; 165 | DUUUUUUUP @spender ; 166 | DIP { NONE nat } ; 167 | UPDATE } 168 | { DUUUUP @account_sender ; 169 | CDR %allowances ; 170 | DUUUUUUP @tokens ; 171 | DUUUUUUUUP @spender ; 172 | DIP { SOME } ; 173 | UPDATE } ; 174 | SWAP ; 175 | PAIR @account_sender %balance %allowances ; 176 | SENDER ; 177 | DIP { SOME } ; 178 | DIIIIP { DROP ; DROP ; DROP ; DROP ; DROP } ; 179 | UPDATE ; 180 | PAIR @storage %accounts ; 181 | NIL operation ; 182 | PAIR } 183 | { IF_LEFT 184 | { RENAME @_from_dest_tokens_slash_34 ; 185 | DUUUUUP @storage ; 186 | DUUP ; 187 | CAR @from ; 188 | DUUUP ; 189 | CDAR @dest ; 190 | DUUUUP ; 191 | CDDR @tokens ; 192 | DUUUUUUUP @get_account ; 193 | DUUUUUP @storage ; 194 | CAR %accounts ; 195 | DUUUUUP @from ; 196 | PAIR ; 197 | EXEC @account_from ; 198 | DUUUUUUUP @perform_transfer_T4aanRstorageaccountsBaRaccountbalancenallowancesManversionntotalSupplynnamessymbolsowneraT2LoRstorageaccountsBaRaccountbalancenallowancesManversionntotalSupplynnamessymbolsownera ; 199 | DUUUUUUP @storage ; 200 | CDR ; 201 | DUUUUUUUP @storage ; 202 | CAR %accounts ; 203 | DUUUUP @account_from ; 204 | CAR %balance ; 205 | DUUUUUP @account_from ; 206 | CDR %allowances ; 207 | DUUUUUUUUP @dest ; 208 | GET ; 209 | IF_NONE 210 | { DUUUUUUUUP @from ; 211 | PUSH string "Not allowed to spend from" ; 212 | PAIR ; 213 | FAILWITH } 214 | { DUUUUUUUP @tokens ; 215 | DUUP @allowed ; 216 | SUB ; 217 | ISNAT ; 218 | IF_NONE 219 | { DUP @allowed ; 220 | PUSH string "Not enough allowance for transfer" ; 221 | PAIR ; 222 | FAILWITH } 223 | { PUSH nat 0 ; 224 | DUUP @allowed ; 225 | COMPARE ; 226 | EQ ; 227 | IF { DUUUUUUUP @account_from ; 228 | CDR %allowances ; 229 | DUUUUUUUUUUP @dest ; 230 | DIP { NONE nat } ; 231 | UPDATE } 232 | { DUUUUUUUP @account_from ; 233 | CDR %allowances ; 234 | DUUP @allowed ; 235 | DUUUUUUUUUUUP @dest ; 236 | DIP { SOME } ; 237 | UPDATE } ; 238 | DIP { DROP } } ; 239 | DIP { DROP } } ; 240 | RENAME @new_allowances_from ; 241 | SWAP ; 242 | PAIR @account_from %balance %allowances ; 243 | DUUUUUUUUP @from ; 244 | DIP { SOME } ; 245 | UPDATE ; 246 | PAIR @storage %accounts ; 247 | DUUUUP @tokens ; 248 | PAIR ; 249 | DUUUUUP @dest ; 250 | PAIR ; 251 | DUUUUUUP @from ; 252 | PAIR ; 253 | DIP { DUP ; CAR ; SWAP ; CDR } ; 254 | DIIIP { DROP ; DROP ; DROP ; DROP ; DROP ; DROP } ; 255 | PAIR ; 256 | EXEC } 257 | { IF_LEFT 258 | { RENAME @_spender_forward_slash_45 ; 259 | DUUUUUP @storage ; 260 | DUP @storage ; 261 | NIL operation ; 262 | DUUUUP ; 263 | CDR @forward ; 264 | PUSH mutez 0 ; 265 | DUUUUUP @storage ; 266 | CAR %accounts ; 267 | DUUUUUUUP ; 268 | CAR @spender ; 269 | GET ; 270 | IF_NONE { PUSH nat 0 } { CAR %balance } ; 271 | DIIIIIP { DROP ; DROP } ; 272 | RENAME @spender_balance ; 273 | TRANSFER_TOKENS ; 274 | CONS ; 275 | PAIR } 276 | { IF_LEFT 277 | { RENAME @_from_spender_forward_slash_51 ; 278 | DUUUUUP @storage ; 279 | DUP @storage ; 280 | NIL operation ; 281 | DUUUUP ; 282 | CDDR @forward ; 283 | PUSH mutez 0 ; 284 | DUUUUUP @storage ; 285 | CAR %accounts ; 286 | DUUUUUUUP ; 287 | CAR @from ; 288 | GET ; 289 | IF_NONE 290 | { PUSH (pair nat nat) (Pair 0 0) } 291 | { DUP @account ; 292 | CDR %allowances ; 293 | DUUUUUUUUP ; 294 | CDAR @spender ; 295 | GET ; 296 | IF_NONE 297 | { DUP @account ; CAR %balance ; PUSH nat 0 ; PAIR } 298 | { DUUP @account ; CAR %balance ; SWAP ; PAIR } ; 299 | DIP { DROP } } ; 300 | DIIIIIP { DROP ; DROP } ; 301 | RENAME @spender_allowance ; 302 | TRANSFER_TOKENS ; 303 | CONS ; 304 | PAIR } 305 | { IF_LEFT 306 | { RENAME @_dest_tokens_slash_59 ; 307 | DUUUUUP @storage ; 308 | DUP @storage ; 309 | CDDDDDR %owner ; 310 | SENDER ; 311 | COMPARE ; 312 | NEQ ; 313 | IF { PUSH string "Only owner can create accounts" ; FAILWITH } { UNIT } ; 314 | DROP ; 315 | DUUUP @perform_transfer_T4aanRstorageaccountsBaRaccountbalancenallowancesManversionntotalSupplynnamessymbolsowneraT2LoRstorageaccountsBaRaccountbalancenallowancesManversionntotalSupplynnamessymbolsownera ; 316 | DUUP @storage ; 317 | DUUUUP ; 318 | CDR @tokens ; 319 | PAIR ; 320 | DUUUUP ; 321 | CAR @dest ; 322 | PAIR ; 323 | DUUUP @storage ; 324 | CDDDDDR %owner ; 325 | PAIR ; 326 | DIP { DUP ; CAR ; SWAP ; CDR } ; 327 | DIIIP { DROP ; DROP } ; 328 | PAIR ; 329 | EXEC } 330 | { RENAME @new_accounts_slash_63 ; 331 | DUUUUUP @storage ; 332 | DUP @storage ; 333 | CDDDDDR %owner ; 334 | SENDER ; 335 | COMPARE ; 336 | NEQ ; 337 | IF { PUSH string "Only owner can create accounts" ; FAILWITH } { UNIT } ; 338 | DROP ; 339 | DUP @storage ; 340 | NIL operation ; 341 | PAIR ; 342 | DUUUP @new_accounts ; 343 | ITER { RENAME @_dest_tokens__ops_storage_slash_65 ; 344 | DIP { DUP } ; 345 | PAIR ; 346 | DUP ; 347 | CDR ; 348 | CDR @storage ; 349 | DUUUUUUP @perform_transfer_T4aanRstorageaccountsBaRaccountbalancenallowancesManversionntotalSupplynnamessymbolsowneraT2LoRstorageaccountsBaRaccountbalancenallowancesManversionntotalSupplynnamessymbolsownera ; 350 | DUUP @storage ; 351 | DUUUUP ; 352 | CAR ; 353 | CDR @tokens ; 354 | PAIR ; 355 | DUUUUP ; 356 | CAR ; 357 | CAR @dest ; 358 | PAIR ; 359 | DUUUP @storage ; 360 | CDDDDDR %owner ; 361 | PAIR ; 362 | DIP { DUP ; CAR ; SWAP ; CDR } ; 363 | DIIIP { DROP ; DROP ; DROP } ; 364 | PAIR ; 365 | EXEC } ; 366 | DIP { DROP ; DROP } } } } } } } ; 367 | DIP { DROP ; DROP ; DROP ; DROP } }; 368 | --------------------------------------------------------------------------------