├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── dev-node-setup.md ├── package-lock.json ├── package.json ├── rust-toolchain ├── step-0 ├── .cargo │ └── config ├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── README.md ├── build.sh ├── rust-toolchain └── src │ └── lib.rs ├── step-1 ├── .cargo │ └── config ├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── README.md ├── build.sh ├── rust-toolchain └── src │ └── lib.rs ├── step-2 ├── .cargo │ └── config ├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── README.md ├── build.sh ├── rust-toolchain └── src │ └── lib.rs ├── step-3 ├── .cargo │ └── config ├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── README.md ├── build.sh ├── rust-toolchain └── src │ └── lib.rs ├── step-4 ├── .cargo │ └── config ├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── README.md ├── build.sh ├── rust-toolchain └── src │ └── lib.rs ├── step-5 ├── .cargo │ └── config ├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── README.md ├── build.sh ├── rust-toolchain └── src │ └── lib.rs ├── wasm-dev-chain.json └── wasm-install.sh /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | cargo: cache 3 | rust: 4 | - nightly 5 | before_install: 6 | - rustup target add wasm32-unknown-unknown 7 | - command -v wasm-build || cargo install pwasm-utils-cli --bin wasm-build 8 | - ./wasm-install.sh 9 | env: 10 | - TUTOR_DIR=step-0 11 | - TUTOR_DIR=step-1 12 | - TUTOR_DIR=step-2 13 | - TUTOR_DIR=step-3 14 | - TUTOR_DIR=step-4 15 | - TUTOR_DIR=step-5 16 | script: 17 | - cd $TUTOR_DIR 18 | - ./build.sh 19 | - ../wasm-install/bin/wasm-validate target/pwasm_tutorial_contract.wasm 20 | -------------------------------------------------------------------------------- /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 | ## Tutorial Prerequisites 2 | There is a list of all tools and dependencies required for this tutorial. 3 | 4 | ### Rust 5 | [rustup](https://github.com/rust-lang-nursery/rustup.rs#installation) is the easiest way to install Rust toolchains. Rust nightly toolchain is required since our contracts require some unstable features: 6 | 7 | ```bash 8 | rustup install nightly-2018-11-12 9 | ``` 10 | 11 | Also, we need to install `wasm32-unknown-unknown` to compile contracts to Wasm: 12 | ```bash 13 | rustup target add wasm32-unknown-unknown 14 | ``` 15 | 16 | ### Parity wasm-build 17 | [wasm-build](https://github.com/paritytech/wasm-utils#build-tools-for-cargo) takes the raw `.wasm` file produced by Rust compiler and packs it to the form of valid contract. 18 | ``` 19 | cargo install pwasm-utils-cli --bin wasm-build 20 | ``` 21 | 22 | ### Parity 23 | Follow the [parity setup guide](https://wiki.parity.io/Setup). You'll need Parity version **1.9.5** or later. 24 | 25 | ### Web3.js 26 | We'll be using `Web3.js` to connect to the Parity node. Change dir to the root `pwasm-tutorial` and run [yarn](https://yarnpkg.com) or [npm](https://nodejs.org) to install `Web3.js`: 27 | ``` 28 | yarn install 29 | ``` 30 | 31 | ### Tutorial source code 32 | We provide a full source code for each step in this tutorial under `step-*` directories. 33 | 34 | ## General structure 35 | Source code: https://github.com/paritytech/pwasm-tutorial/tree/master/step-0 36 | 37 | ```rust 38 | // Contract doesn't use Rust's standard library 39 | #![no_std] 40 | 41 | // `pwasm-ethereum` implements bindings to the runtime 42 | extern crate pwasm_ethereum; 43 | 44 | /// Will be described in the next step 45 | #[no_mangle] 46 | pub fn deploy() { 47 | } 48 | 49 | /// The call function is the main function of the *deployed* contract 50 | #[no_mangle] 51 | pub fn call() { 52 | // Send a result pointer to the runtime 53 | pwasm_ethereum::ret(&b"result"[..]); 54 | } 55 | ``` 56 | ### pwasm-ethereum 57 | [pwasm-ethereum](https://github.com/NikVolf/pwasm-ethereum) is a collection of bindings to interact with ethereum-like network. 58 | 59 | ## Building 60 | To make sure that everything is set up go to the `step-0` directory and run `./build.sh` 61 | 62 | As a result the `pwasm_tutorial_contract.wasm` should be created in the `target` directory. 63 | 64 | Take a look on the contents of the `./build.sh` executable: 65 | 66 | ``` 67 | #!/bin/bash 68 | 69 | cargo build --release --target wasm32-unknown-unknown 70 | wasm-build --target=wasm32-unknown-unknown ./target pwasm_tutorial_contract 71 | ``` 72 | 73 | First, we run `cargo build --release --target wasm32-unknown-unknown` which yields a "raw" Wasm binary and put it into the "target" directory: `target/wasm32-unknown-unknown/release/pwasm_tutorial_contract.wasm`. Then we run the `wasm-build` tool. It takes `pwasm_tutorial_contract.wasm` raw file generated by `cargo build`, trims, optimises it and produces the so-called contract "constructor". It packs the actual contract code into that constructor. So on deploy, executor will put the raw contract code into the blockchain as a result of the successful transaction. 74 | 75 | For your convenience, every step in our tutorial features a `build.sh` shell script, which incorporates the proper `wasm-build` call (unfortunately, cargo's build pipeline is not yet extensible enough to feature such steps automatically). Alternatively, one can trivially call the same wasm packing manually after every build. 76 | 77 | ## The constructor 78 | Source code: https://github.com/paritytech/pwasm-tutorial/tree/master/step-1 79 | 80 | When deploying a contract we often want to set its initial storage values (e.g. `totalSupply` if it's a token contact). To address this problem we are exporting another function "deploy" which executes only once on contract deployment. 81 | 82 | ```rust 83 | // This contract will return the address from which it was deployed 84 | #![no_std] 85 | 86 | extern crate pwasm_ethereum; 87 | extern crate parity_hash; 88 | 89 | use parity_hash::H256; 90 | 91 | // The "deploy" will be executed only once on deployment but will not be stored on the blockchain 92 | #[no_mangle] 93 | pub fn deploy() { 94 | // Lets set the sender address to the contract storage at address "0" 95 | pwasm_ethereum::write(&H256::zero().into(), &H256::from(pwasm_ethereum::sender()).into()); 96 | // Note we shouldn't write any result into the call descriptor in deploy. 97 | } 98 | 99 | // The following code will be stored on the blockchain. 100 | #[no_mangle] 101 | pub fn call() { 102 | // Will read the address of the deployer which we wrote to the storage on the deploy stage 103 | let owner = pwasm_ethereum::read(&H256::zero().into()); 104 | // Send a result pointer to the runtime 105 | pwasm_ethereum::ret(owner.as_ref()); 106 | } 107 | ``` 108 | 109 | ## Contract ABI declaration 110 | Source code: https://github.com/paritytech/pwasm-tutorial/tree/master/step-2 111 | 112 | Let's implement a simple [ERC-20](https://en.wikipedia.org/wiki/ERC20) token contract. 113 | 114 | ```rust 115 | // ... 116 | 117 | pub mod token { 118 | use pwasm_ethereum; 119 | use pwasm_abi::types::*; 120 | 121 | // eth_abi is a procedural macros https://doc.rust-lang.org/book/first-edition/procedural-macros.html 122 | use pwasm_abi_derive::eth_abi; 123 | 124 | lazy_static! { 125 | static ref TOTAL_SUPPLY_KEY: H256 = 126 | H256::from([2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]); 127 | } 128 | 129 | #[eth_abi(TokenEndpoint)] 130 | pub trait TokenInterface { 131 | /// The constructor 132 | fn constructor(&mut self, _total_supply: U256); 133 | /// Total amount of tokens 134 | fn totalSupply(&mut self) -> U256; 135 | } 136 | 137 | pub struct TokenContract; 138 | 139 | impl TokenInterface for TokenContract { 140 | fn constructor(&mut self, total_supply: U256) { 141 | // Set up the total supply for the token 142 | pwasm_ethereum::write(&TOTAL_SUPPLY_KEY, &total_supply.into()); 143 | } 144 | 145 | fn totalSupply(&mut self) -> U256 { 146 | pwasm_ethereum::read(&TOTAL_SUPPLY_KEY).into() 147 | } 148 | } 149 | } 150 | // Declares the dispatch and dispatch_ctor methods 151 | use pwasm_abi::eth::EndpointInterface; 152 | 153 | #[no_mangle] 154 | pub fn call() { 155 | let mut endpoint = token::TokenEndpoint::new(token::TokenContract{}); 156 | // Read http://solidity.readthedocs.io/en/develop/abi-spec.html#formal-specification-of-the-encoding for details 157 | pwasm_ethereum::ret(&endpoint.dispatch(&pwasm_ethereum::input())); 158 | } 159 | 160 | #[no_mangle] 161 | pub fn deploy() { 162 | let mut endpoint = token::TokenEndpoint::new(token::TokenContract{}); 163 | // 164 | endpoint.dispatch_ctor(&pwasm_ethereum::input()); 165 | } 166 | 167 | ``` 168 | `token::TokenInterface` is an interface definition of the contract. 169 | `pwasm_abi_derive::eth_abi` is a [procedural macros](https://doc.rust-lang.org/book/first-edition/procedural-macros.html) uses a trait `token::TokenInterface` to generate decoder (`TokenEndpoint`) for payload in Solidity ABI format. `TokenEndpoint` implements an `EndpointInterface` trait: 170 | 171 | ```rust 172 | /// Endpoint interface for contracts 173 | pub trait EndpointInterface { 174 | /// Dispatch payload for regular method 175 | fn dispatch(&mut self, payload: &[u8]) -> Vec; 176 | 177 | /// Dispatch constructor payload 178 | fn dispatch_ctor(&mut self, payload: &[u8]); 179 | } 180 | ``` 181 | 182 | The `dispatch` expects `payload` and returns a result in the format defined in [Solidity ABI spec](http://solidity.readthedocs.io/en/develop/abi-spec.html#formal-specification-of-the-encoding). It maps payload to the corresponding method of the `token::TokenInterface` implementation. The `dispatch_ctor` maps payload only to the `TokenInterface::constructor` and returns no result. 183 | 184 | A complete implementation of ERC20 can be found here https://github.com/paritytech/pwasm-token-example. 185 | 186 | ### pwasm-std 187 | [pwasm-std](https://paritytech.github.io/pwasm-std/pwasm_std/) is a lightweight standard library. It implements common data structures, conversion utils and provides bindings to the runtime. 188 | 189 | ## Make calls to other contracts 190 | Source code: https://github.com/paritytech/pwasm-tutorial/tree/master/step-3 191 | 192 | In order to make calls to our `TokenInterface` we need to generate the payload `TokenEndpoint::dispatch()` expects. So `pwasm_abi_derive::eth_abi` can generate an implementation of `TokenInterface` which will prepare payload for each method. 193 | 194 | ```rust 195 | #[eth_abi(TokenEndpoint, TokenClient)] 196 | pub trait TokenInterface { 197 | /// The constructor 198 | fn constructor(&mut self, _total_supply: U256); 199 | /// Total amount of tokens 200 | #[constant] // #[constant] hint affect the resulting JSON abi. It sets "constant": true prore 201 | fn totalSupply(&mut self) -> U256; 202 | } 203 | ``` 204 | 205 | We've added a second argument `TokenClient` to the `eth_abi` macro as a second argument (it is optional) -- this way we ask to generate a _client_ implementation for `TokenInterface` trait and name it as `TokenClient`. 206 | 207 | As mentioned [above](https://github.com/paritytech/pwasm-tutorial/blob/master/README.md#contract-abi-declaration), a first argument to the `eth_abi` macro requests the name for to be generated Endpoint implementation, which turns Ethereum ABI-encoded payloads into calls to the corresponding `TokenContract` methods with deserialized params. 208 | 209 | *Client* (`TokenClient`), created via the second argument, is doing the opposite to *endpoint*, providing an implementation which generates Ethereum ABI-compatible calls (consumable by `TokenEndpoint`) for every `TokenInterface` call. 210 | 211 | Let's suppose we've deployed a token contract on `0x7BA4324585CB5597adC283024819254345CD7C62` address. That's how we can make calls to it. 212 | 213 | ```rust 214 | extern pwasm_ethereum; 215 | extern pwasm_std; 216 | 217 | use token::TokenClient; 218 | use pwasm_std::hash::Address; 219 | 220 | let token = TokenClient::new(Address::from("0x7BA4324585CB5597adC283024819254345CD7C62")); 221 | let tokenSupply = token.totalSupply(); 222 | ``` 223 | 224 | `token.totalSupply()` will execute `pwasm_ethereum::call(Address::from("0x7BA4324585CB5597adC283024819254345CD7C62"), payload)` with `address` and `payload` generated according to `totalSupply()` signature. Optionally it's possible to set a `value` (in Wei) to transfer with the call and set a `gas` limit. 225 | 226 | ```rust 227 | let token = TokenClient::new(Address::from("0x7BA4324585CB5597adC283024819254345CD7C62")) 228 | .value(10000000.into()) // send a value with the call 229 | .gas(21000); // set a gas limit 230 | let tokenSupply = token.totalSupply(); 231 | ``` 232 | 233 | If you move to `step-3` directory and run `cargo build --release --target wasm32-unknown-unknown` you will find a `TokenInterface.json` in the `target/json` generated from `TokenInterface` trait with the following content: 234 | 235 | ```json 236 | [ 237 | { 238 | "type": "function", 239 | "name": "totalSupply", 240 | "inputs": [], 241 | "outputs": [ 242 | { 243 | "name": "returnValue", 244 | "type": "uint256" 245 | } 246 | ], 247 | "constant": true 248 | }, 249 | { 250 | "type": "constructor", 251 | "inputs": [ 252 | { 253 | "name": "_total_supply", 254 | "type": "uint256" 255 | } 256 | ] 257 | } 258 | ] 259 | ``` 260 | 261 | JSON above is an ABI definition which can be used along with Web.js to run transactions and calls to contract: 262 | 263 | ```javascript 264 | var Web3 = require("web3"); 265 | var fs = require("fs"); 266 | var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); 267 | var abi = JSON.parse(fs.readFileSync("./target/TokenInterface.json")); 268 | var TokenContract = new web3.eth.Contract(abi, "0x7BA4324585CB5597adC283024819254345CD7C62", { from: web3.eth.defaultAccount }); 269 | var totalSupply = TokenContract.methods.totalSupply().call().then(console.log); 270 | ``` 271 | 272 | ### Events 273 | Source code: https://github.com/paritytech/pwasm-tutorial/tree/master/step-4 274 | 275 | Events allow the convenient usage of the EVM logging facilities, which in turn can be used to “call” JavaScript callbacks in the user interface of a dapp, which listen for these events. 276 | 277 | Let's implement the `transfer` method for our ERC-20 contract. `step-4` directory contains the complete implementation. 278 | 279 | ```rust 280 | pub mod token { 281 | 282 | use pwasm_ethereum; 283 | use pwasm_std::types::*; 284 | 285 | #[eth_abi(TokenEndpoint, TokenClient)] 286 | pub trait TokenInterface { 287 | /// The constructor 288 | fn constructor(&mut self, _total_supply: U256); 289 | /// Total amount of tokens 290 | #[constant] 291 | fn totalSupply(&mut self) -> U256; 292 | /// What is the balance of a particular account? 293 | #[constant] 294 | fn balanceOf(&mut self, _owner: Address) -> U256; 295 | /// Transfer the balance from owner's account to another account 296 | fn transfer(&mut self, _to: Address, _amount: U256) -> bool; 297 | /// Event declaration 298 | #[event] 299 | fn Transfer(&mut self, indexed_from: Address, indexed_to: Address, _value: U256); 300 | } 301 | 302 | pub struct TokenContract; 303 | 304 | impl TokenInterface for TokenContract { 305 | fn constructor(&mut self, total_supply: U256) { 306 | // ... 307 | } 308 | 309 | fn totalSupply(&mut self) -> U256 { 310 | // ... 311 | } 312 | 313 | fn balanceOf(&mut self, owner: Address) -> U256 { 314 | read_balance_of(&owner) 315 | } 316 | 317 | fn transfer(&mut self, to: Address, amount: U256) -> bool { 318 | let sender = pwasm_ethereum::sender(); 319 | let senderBalance = read_balance_of(&sender); 320 | let recipientBalance = read_balance_of(&to); 321 | if amount == 0.into() || senderBalance < amount || to == sender { 322 | false 323 | } else { 324 | let new_sender_balance = senderBalance - amount; 325 | let new_recipient_balance = recipientBalance + amount; 326 | pwasm_ethereum::write(&balance_key(&sender), &new_sender_balance.into()); 327 | pwasm_ethereum::write(&balance_key(&to), &new_recipient_balance.into()); 328 | self.Transfer(sender, to, amount); 329 | true 330 | } 331 | } 332 | } 333 | 334 | // Reads balance by address 335 | fn read_balance_of(owner: &Address) -> U256 { 336 | pwasm_ethereum::read(&balance_key(owner)).into() 337 | } 338 | 339 | // Generates a balance key for some address. 340 | // Used to map balances with their owners. 341 | fn balance_key(address: &Address) -> H256 { 342 | let mut key = H256::from(*address); 343 | key.as_bytes_mut()[0] = 1; // just a naive "namespace"; 344 | key 345 | } 346 | } 347 | ``` 348 | 349 | Events are declared as part of a contract trait definition. Arguments which start with the "indexed_" prefix are considered as "topics", other arguments are data associated with an event. 350 | 351 | ```rust 352 | #[eth_abi(TokenEndpoint, TokenClient)] 353 | pub trait TokenInterface { 354 | fn transfer(&mut self, _to: Address, _amount: U256) -> bool; 355 | #[event] 356 | fn Transfer(&mut self, indexed_from: Address, indexed_to: Address, _value: U256); 357 | } 358 | 359 | fn transfer(&mut self, to: Address, amount: U256) -> bool { 360 | let sender = pwasm_ethereum::sender(); 361 | let senderBalance = read_balance_of(&sender); 362 | let recipientBalance = read_balance_of(&to); 363 | if amount == 0.into() || senderBalance < amount || to == sender { 364 | false 365 | } else { 366 | let new_sender_balance = senderBalance - amount; 367 | let new_recipient_balance = recipientBalance + amount; 368 | pwasm_ethereum::write(&balance_key(&sender), &new_sender_balance.into()); 369 | pwasm_ethereum::write(&balance_key(&to), &new_recipient_balance.into()); 370 | self.Transfer(sender, to, amount); 371 | true 372 | } 373 | } 374 | ``` 375 | 376 | Topics are useful to filter events produced by contract. In following example we use Web3.js to subscribe to the `Transfer` events of deployed `TokenContract`. 377 | ```javascript 378 | var Web3 = require("web3"); 379 | var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); 380 | var abi = JSON.parse(fs.readFileSync("./target/TokenInterface.json")); 381 | var TokenContract = new web3.eth.Contract(abi, "0x7BA4324585CB5597adC283024819254345CD7C62", { from: web3.eth.defaultAccount }); 382 | 383 | // Subscribe to the Transfer event 384 | TokenContract.events.Transfer({ 385 | from: "0x7BA4324585CB5597adC283024819254345CD7C62" // Filter transactions by sender 386 | }, function (err, event) { 387 | console.log(event); 388 | }); 389 | ``` 390 | 391 | ## Run node and deploy contract 392 | Now it's time to deploy our Wasm contract on the blockchain. We can either test in own local development chain or publish it on the public Kovan network. 393 | 394 | ### Option 1: Setup and run development node 395 | Parity **1.9.5** includes support for running Wasm contracts. 396 | See [instructions](dev-node-setup.md) on how to setup a Wasm-enabled dev node. 397 | 398 | ### Option 2: Run Kovan node 399 | Kovan network supports Wasm contracts. This will run Parity node on Kovan: 400 | ```bash 401 | parity --chain kovan 402 | ``` 403 | When it syncs up follow https://github.com/kovan-testnet/faucet to set up an account with some Kovan ETH to be able to pay gas for transactions. 404 | 405 | ### Deploy 406 | Let Parity run in a separate terminal window. 407 | 408 | Now cd to `step-5` and build the contract: 409 | ```bash 410 | ./build.sh 411 | ``` 412 | It should produce 2 files we need: 413 | - a compiled Wasm binary `./target/pwasm_tutorial_contract.wasm` 414 | - an ABI file: `./target/json/TokenInterface.json` 415 | 416 | At this point we can use Web.js to connect to the Parity node and deploy Wasm `pwasm_tutorial_contract.wasm`. Run the following code in `node` console: 417 | 418 | ```javascript 419 | var Web3 = require("web3"); 420 | var fs = require("fs"); 421 | // Connect to our local node 422 | var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); 423 | // NOTE: if you run Kovan node there should be an address you've got in the "Option 2: Run Kovan node" step 424 | web3.eth.defaultAccount = "0x004ec07d2329997267ec62b4166639513386f32e"; 425 | // read JSON ABI 426 | var abi = JSON.parse(fs.readFileSync("./target/json/TokenInterface.json")); 427 | // convert Wasm binary to hex format 428 | var codeHex = '0x' + fs.readFileSync("./target/pwasm_tutorial_contract.wasm").toString('hex'); 429 | 430 | var TokenContract = new web3.eth.Contract(abi, { data: codeHex, from: web3.eth.defaultAccount }); 431 | 432 | var TokenDeployTransaction = TokenContract.deploy({data: codeHex, arguments: [10000000]}); 433 | 434 | // Will create TokenContract with `totalSupply` = 10000000 and print a result 435 | web3.eth.personal.unlockAccount(web3.eth.defaultAccount, "user").then(() => TokenDeployTransaction.estimateGas()).then(gas => TokenDeployTransaction.send({gasLimit: gas, from: web3.eth.defaultAccount})).then(contract => { console.log("Address of new contract: " + contract.options.address); TokenContract = contract; }).catch(err => console.log(err)); 436 | ``` 437 | Now we're able transfer some tokens: 438 | ```javascript 439 | web3.eth.personal.unlockAccount(web3.eth.defaultAccount, "user").then(() => TokenContract.methods.transfer("0x7BA4324585CB5597adC283024819254345CD7C62", 200).send()).then(console.log).catch(console.log); 440 | ``` 441 | 442 | And check balances: 443 | ```javascript 444 | // Check balance of recipient. Should print 200 445 | TokenContract.methods.balanceOf("0x7BA4324585CB5597adC283024819254345CD7C62").call().then(console.log).catch(console.log); 446 | 447 | // Check balance of sender (owner of the contract). Should print 10000000 - 200 = 9999800 448 | TokenContract.methods.balanceOf(web3.eth.defaultAccount).call().then(console.log).catch(console.log); 449 | ``` 450 | 451 | ## Testing 452 | [pwasm-test](https://github.com/paritytech/pwasm-test) makes it easy to test a contract's logic. It allows to emulate the blockchain state and mock any [pwasm-ethereum](#pwasm-ethereum) call. 453 | 454 | By default our contracts built with `#![no_std]`, but `rust test` needs the Rust stdlib for threading and I/O. Thus, in order to run tests we've added a following feature gate in [Cargo.toml](https://github.com/paritytech/pwasm-tutorial/tree/master/step-5): 455 | 456 | ``` 457 | [features] 458 | std = ["pwasm-std/std", "pwasm-ethereum/std"] 459 | ``` 460 | Now you can `cd step-5` and `cargo test --features std` should pass. 461 | 462 | Take a look https://github.com/paritytech/pwasm-tutorial/blob/master/step-5/src/lib.rs#L116-L161 to see an example how to test a `transfer` method of our token contract. 463 | 464 | ```rust 465 | #[cfg(test)] 466 | #[allow(non_snake_case)] 467 | mod tests { 468 | extern crate pwasm_test; 469 | extern crate std; 470 | use super::*; 471 | use self::pwasm_test::{ext_reset, ext_get}; 472 | use parity_hash::Address; 473 | use token::TokenInterface; 474 | 475 | #[test] 476 | fn should_succeed_transfering_1000_from_owner_to_another_address() { 477 | let mut contract = token::TokenContract{}; 478 | let owner_address = Address::from("0xea674fdde714fd979de3edf0f56aa9716b898ec8"); 479 | let sam_address = Address::from("0xdb6fd484cfa46eeeb73c71edee823e4812f9e2e1"); 480 | // Here we're creating an External context using ExternalBuilder and set the `sender` to the `owner_address` 481 | // so `pwasm_ethereum::sender()` in TokenContract::constructor() will return that `owner_address` 482 | ext_reset(|e| e.sender(owner_address.clone())); 483 | let total_supply = 10000.into(); 484 | contract.constructor(total_supply); 485 | assert_eq!(contract.balanceOf(owner_address), total_supply); 486 | assert_eq!(contract.transfer(sam_address, 1000.into()), true); 487 | assert_eq!(contract.balanceOf(owner_address), 9000.into()); 488 | assert_eq!(contract.balanceOf(sam_address), 1000.into()); 489 | // 1 log entry should be created 490 | assert_eq!(ext_get().logs().len(), 1); 491 | } 492 | } 493 | ``` 494 | 495 | [Here](https://github.com/paritytech/pwasm-test/tree/master/tests) you can find more examples on how to: 496 | - [mock calls](https://github.com/paritytech/pwasm-test/blob/master/tests/calls.rs) to other contracts 497 | - [read event logs created by contract](https://github.com/paritytech/pwasm-test/blob/master/tests/log.rs) 498 | - [init contract with storage](https://github.com/paritytech/pwasm-test/blob/master/tests/storage_read.rs). 499 | 500 | More testing examples: 501 | https://github.com/paritytech/pwasm-token-example/blob/master/contract/src/lib.rs#L194 502 | 503 | In order to test the interaction between contracts, we're able to mock callee contract client. See comprehensive here: 504 | https://github.com/paritytech/pwasm-repo-contract/blob/master/contract/src/lib.rs#L453 505 | -------------------------------------------------------------------------------- /dev-node-setup.md: -------------------------------------------------------------------------------- 1 | ## Development node setup 2 | 3 | Wasm support isn't enabled by default and needs to be specified in a "chainspec" file. `wasmActivationTransition` param sets a block number Wasm support should be activated. This is a sample "development chain" spec with Wasm enabled (based on https://paritytech.github.io/wiki/Private-development-chain): 4 | 5 | [Source](https://github.com/paritytech/pwasm-tutorial/tree/master/wasm-dev-chain.json) 6 | ```json 7 | { 8 | "name": "DevelopmentChain", 9 | "engine": { 10 | "instantSeal": null 11 | }, 12 | "params": { 13 | "wasmActivationTransition": "0x01", 14 | "eip658Transition": "0x01", 15 | "gasLimitBoundDivisor": "0x0400", 16 | "accountStartNonce": "0x0", 17 | "maximumExtraDataSize": "0x20", 18 | "minGasLimit": "0x1388", 19 | "networkID" : "0x11", 20 | "maxCodeSize": 1000000, 21 | "maxCodeSizeTransition": 1 22 | }, 23 | "genesis": { 24 | "seal": { 25 | "generic": "0x0" 26 | }, 27 | "difficulty": "0x20000", 28 | "author": "0x0000000000000000000000000000000000000000", 29 | "timestamp": "0x00", 30 | "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", 31 | "extraData": "0x", 32 | "gasLimit": "0x5B8D8000" 33 | }, 34 | "accounts": { 35 | "0000000000000000000000000000000000000001": { "balance": "1", "builtin": { "name": "ecrecover", "pricing": { "linear": { "base": 3000, "word": 0 } } } }, 36 | "0000000000000000000000000000000000000002": { "balance": "1", "builtin": { "name": "sha256", "pricing": { "linear": { "base": 60, "word": 12 } } } }, 37 | "0000000000000000000000000000000000000003": { "balance": "1", "builtin": { "name": "ripemd160", "pricing": { "linear": { "base": 600, "word": 120 } } } }, 38 | "0000000000000000000000000000000000000004": { "balance": "1", "builtin": { "name": "identity", "pricing": { "linear": { "base": 15, "word": 3 } } } }, 39 | "0x004ec07d2329997267ec62b4166639513386f32e": { "balance": "1606938044258990275541962092341162602522202993782792835301376" } 40 | } 41 | } 42 | ``` 43 | Run node: 44 | ```bash 45 | parity --chain ./wasm-dev-chain.json --jsonrpc-apis=all 46 | ``` 47 | 48 | Among with other things we've set balance for `0x004ec07d2329997267ec62b4166639513386f32e` account on which behalf we'll run transactions (such as deploy). This should add an above account to the keychain: 49 | 50 | ```bash 51 | curl --data '{"jsonrpc":"2.0","method":"parity_newAccountFromPhrase","params":["user", "user"],"id":0}' -H "Content-Type: application/json" -X POST localhost:8545 52 | ``` 53 | Should output something like: 54 | ```json 55 | {"jsonrpc":"2.0","result":"0x004ec07d2329997267ec62b4166639513386f32e","id":0} 56 | ``` 57 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pwasm-tutorial", 3 | "version": "1.0.0", 4 | "description": "There is a list of all tools and dependencies required for this tutorial.", 5 | "main": "", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/fckt/pwasm-tutorial.git" 12 | }, 13 | "author": "Alexey Frolov ", 14 | "license": "ISC", 15 | "bugs": { 16 | "url": "https://github.com/fckt/pwasm-tutorial/issues" 17 | }, 18 | "homepage": "https://github.com/fckt/pwasm-tutorial#readme", 19 | "dependencies": { 20 | "web3": "^1.0.0-beta.33" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /rust-toolchain: -------------------------------------------------------------------------------- 1 | nightly-2018-11-12 2 | -------------------------------------------------------------------------------- /step-0/.cargo/config: -------------------------------------------------------------------------------- 1 | [target.wasm32-unknown-unknown] 2 | rustflags = [ 3 | "-C", "link-args=-z stack-size=65536", 4 | ] 5 | -------------------------------------------------------------------------------- /step-0/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | /target/ 3 | **/*.rs.bk 4 | 5 | -------------------------------------------------------------------------------- /step-0/Cargo.lock: -------------------------------------------------------------------------------- 1 | [[package]] 2 | name = "byteorder" 3 | version = "1.3.0" 4 | source = "registry+https://github.com/rust-lang/crates.io-index" 5 | 6 | [[package]] 7 | name = "cfg-if" 8 | version = "0.1.6" 9 | source = "registry+https://github.com/rust-lang/crates.io-index" 10 | 11 | [[package]] 12 | name = "crunchy" 13 | version = "0.1.6" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | 16 | [[package]] 17 | name = "crunchy" 18 | version = "0.2.1" 19 | source = "registry+https://github.com/rust-lang/crates.io-index" 20 | 21 | [[package]] 22 | name = "fixed-hash" 23 | version = "0.3.0" 24 | source = "registry+https://github.com/rust-lang/crates.io-index" 25 | dependencies = [ 26 | "static_assertions 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", 27 | ] 28 | 29 | [[package]] 30 | name = "libc" 31 | version = "0.2.47" 32 | source = "registry+https://github.com/rust-lang/crates.io-index" 33 | 34 | [[package]] 35 | name = "memory_units" 36 | version = "0.4.0" 37 | source = "registry+https://github.com/rust-lang/crates.io-index" 38 | 39 | [[package]] 40 | name = "pwasm-alloc" 41 | version = "0.4.0" 42 | source = "registry+https://github.com/rust-lang/crates.io-index" 43 | dependencies = [ 44 | "wee_alloc 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", 45 | ] 46 | 47 | [[package]] 48 | name = "pwasm-ethereum" 49 | version = "0.8.0" 50 | source = "registry+https://github.com/rust-lang/crates.io-index" 51 | dependencies = [ 52 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 53 | "pwasm-std 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", 54 | ] 55 | 56 | [[package]] 57 | name = "pwasm-libc" 58 | version = "0.2.0" 59 | source = "registry+https://github.com/rust-lang/crates.io-index" 60 | dependencies = [ 61 | "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", 62 | "rlibc 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", 63 | ] 64 | 65 | [[package]] 66 | name = "pwasm-std" 67 | version = "0.13.0" 68 | source = "registry+https://github.com/rust-lang/crates.io-index" 69 | dependencies = [ 70 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 71 | "fixed-hash 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 72 | "pwasm-alloc 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 73 | "pwasm-libc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 74 | "tiny-keccak 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)", 75 | "uint 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 76 | ] 77 | 78 | [[package]] 79 | name = "pwasm-tutorial-contract" 80 | version = "0.1.0" 81 | dependencies = [ 82 | "pwasm-ethereum 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", 83 | "pwasm-std 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", 84 | ] 85 | 86 | [[package]] 87 | name = "rlibc" 88 | version = "1.0.0" 89 | source = "registry+https://github.com/rust-lang/crates.io-index" 90 | 91 | [[package]] 92 | name = "rustc-hex" 93 | version = "2.0.1" 94 | source = "registry+https://github.com/rust-lang/crates.io-index" 95 | 96 | [[package]] 97 | name = "static_assertions" 98 | version = "0.2.5" 99 | source = "registry+https://github.com/rust-lang/crates.io-index" 100 | 101 | [[package]] 102 | name = "tiny-keccak" 103 | version = "1.4.2" 104 | source = "registry+https://github.com/rust-lang/crates.io-index" 105 | dependencies = [ 106 | "crunchy 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", 107 | ] 108 | 109 | [[package]] 110 | name = "uint" 111 | version = "0.5.0" 112 | source = "registry+https://github.com/rust-lang/crates.io-index" 113 | dependencies = [ 114 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 115 | "crunchy 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 116 | "rustc-hex 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 117 | ] 118 | 119 | [[package]] 120 | name = "unreachable" 121 | version = "1.0.0" 122 | source = "registry+https://github.com/rust-lang/crates.io-index" 123 | dependencies = [ 124 | "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", 125 | ] 126 | 127 | [[package]] 128 | name = "void" 129 | version = "1.0.2" 130 | source = "registry+https://github.com/rust-lang/crates.io-index" 131 | 132 | [[package]] 133 | name = "wee_alloc" 134 | version = "0.4.2" 135 | source = "registry+https://github.com/rust-lang/crates.io-index" 136 | dependencies = [ 137 | "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", 138 | "libc 0.2.47 (registry+https://github.com/rust-lang/crates.io-index)", 139 | "memory_units 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 140 | "unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", 141 | "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 142 | ] 143 | 144 | [[package]] 145 | name = "winapi" 146 | version = "0.3.6" 147 | source = "registry+https://github.com/rust-lang/crates.io-index" 148 | dependencies = [ 149 | "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 150 | "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 151 | ] 152 | 153 | [[package]] 154 | name = "winapi-i686-pc-windows-gnu" 155 | version = "0.4.0" 156 | source = "registry+https://github.com/rust-lang/crates.io-index" 157 | 158 | [[package]] 159 | name = "winapi-x86_64-pc-windows-gnu" 160 | version = "0.4.0" 161 | source = "registry+https://github.com/rust-lang/crates.io-index" 162 | 163 | [metadata] 164 | "checksum byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "60f0b0d4c0a382d2734228fd12b5a6b5dac185c60e938026fd31b265b94f9bd2" 165 | "checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4" 166 | "checksum crunchy 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "a2f4a431c5c9f662e1200b7c7f02c34e91361150e382089a8f2dec3ba680cbda" 167 | "checksum crunchy 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c240f247c278fa08a6d4820a6a222bfc6e0d999e51ba67be94f44c905b2161f2" 168 | "checksum fixed-hash 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a557e80084b05c32b455963ff565a9de6f2866da023d6671705c6aff6f65e01c" 169 | "checksum libc 0.2.47 (registry+https://github.com/rust-lang/crates.io-index)" = "48450664a984b25d5b479554c29cc04e3150c97aa4c01da5604a2d4ed9151476" 170 | "checksum memory_units 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3" 171 | "checksum pwasm-alloc 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bf268e76f224753679a7cb6e0261c9226676c83cd4d2b6ead2095524f00c854f" 172 | "checksum pwasm-ethereum 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8b5509e67ee47ec62bdbd1c32a6949931adbe2e12a460bb45e5dc46593fdc71c" 173 | "checksum pwasm-libc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "027307d6d2086ffb7f399227d42593c5341e8869a55ac8dd7c79d7ca04ec00e2" 174 | "checksum pwasm-std 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "75e907e3e458b07ed7d92473e8041c12928d196af1a8e441c2352d13e7243e8d" 175 | "checksum rlibc 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc874b127765f014d792f16763a81245ab80500e2ad921ed4ee9e82481ee08fe" 176 | "checksum rustc-hex 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "403bb3a286107a04825a5f82e1270acc1e14028d3d554d7a1e08914549575ab8" 177 | "checksum static_assertions 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "c19be23126415861cb3a23e501d34a708f7f9b2183c5252d690941c2e69199d5" 178 | "checksum tiny-keccak 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e9175261fbdb60781fcd388a4d6cc7e14764a2b629a7ad94abb439aed223a44f" 179 | "checksum uint 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "082df6964410f6aa929a61ddfafc997e4f32c62c22490e439ac351cec827f436" 180 | "checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" 181 | "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" 182 | "checksum wee_alloc 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "27875be1daf838fa18f3e94fd19fd12638e34615b42f56da2610c8f46be80cc6" 183 | "checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" 184 | "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 185 | "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 186 | -------------------------------------------------------------------------------- /step-0/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "pwasm-tutorial-contract" 3 | version = "0.1.0" 4 | authors = ["Alexey Frolov "] 5 | 6 | [dependencies] 7 | pwasm-std = "0.13" 8 | pwasm-ethereum = "0.8" 9 | 10 | [lib] 11 | crate-type = ["cdylib"] 12 | 13 | [profile.release] 14 | panic = "abort" 15 | lto = true 16 | opt-level = "z" 17 | -------------------------------------------------------------------------------- /step-0/README.md: -------------------------------------------------------------------------------- 1 | ## Description 2 | The step-by-step project describes how to write a contract in Rust and compile in to Webassembly. 3 | 4 | ## Build 5 | Make sure you've installed [required tools](https://github.com/paritytech/pwasm-tutorial/blob/master/README.md#tutorial-prerequisites) 6 | ``` 7 | ./build.sh 8 | ``` 9 | As a result the `pwasm_tutorial_contract.wasm` should be created under the `step-0/target/wasm32-unknown-unknown/release/` directory. 10 | -------------------------------------------------------------------------------- /step-0/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cargo build --release --target wasm32-unknown-unknown 4 | wasm-build --target=wasm32-unknown-unknown ./target pwasm_tutorial_contract 5 | -------------------------------------------------------------------------------- /step-0/rust-toolchain: -------------------------------------------------------------------------------- 1 | nightly-2018-11-12 2 | -------------------------------------------------------------------------------- /step-0/src/lib.rs: -------------------------------------------------------------------------------- 1 | // Contract doesn't use Rust's standard library 2 | #![no_std] 3 | 4 | // `pwasm-ethereum` implements bindings to the runtime 5 | extern crate pwasm_ethereum; 6 | 7 | /// Will be described in the next step 8 | #[no_mangle] 9 | pub fn deploy() { 10 | } 11 | 12 | /// The call function is the main function of the *deployed* contract 13 | #[no_mangle] 14 | pub fn call() { 15 | // Send a result pointer to the runtime 16 | pwasm_ethereum::ret(&b"result"[..]); 17 | } 18 | -------------------------------------------------------------------------------- /step-1/.cargo/config: -------------------------------------------------------------------------------- 1 | [target.wasm32-unknown-unknown] 2 | rustflags = [ 3 | "-C", "link-args=-z stack-size=65536", 4 | ] 5 | -------------------------------------------------------------------------------- /step-1/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | /target/ 3 | **/*.rs.bk 4 | -------------------------------------------------------------------------------- /step-1/Cargo.lock: -------------------------------------------------------------------------------- 1 | [[package]] 2 | name = "byteorder" 3 | version = "1.3.0" 4 | source = "registry+https://github.com/rust-lang/crates.io-index" 5 | 6 | [[package]] 7 | name = "cfg-if" 8 | version = "0.1.6" 9 | source = "registry+https://github.com/rust-lang/crates.io-index" 10 | 11 | [[package]] 12 | name = "crunchy" 13 | version = "0.1.6" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | 16 | [[package]] 17 | name = "crunchy" 18 | version = "0.2.1" 19 | source = "registry+https://github.com/rust-lang/crates.io-index" 20 | 21 | [[package]] 22 | name = "fixed-hash" 23 | version = "0.3.0" 24 | source = "registry+https://github.com/rust-lang/crates.io-index" 25 | dependencies = [ 26 | "static_assertions 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", 27 | ] 28 | 29 | [[package]] 30 | name = "libc" 31 | version = "0.2.47" 32 | source = "registry+https://github.com/rust-lang/crates.io-index" 33 | 34 | [[package]] 35 | name = "memory_units" 36 | version = "0.4.0" 37 | source = "registry+https://github.com/rust-lang/crates.io-index" 38 | 39 | [[package]] 40 | name = "pwasm-alloc" 41 | version = "0.4.0" 42 | source = "registry+https://github.com/rust-lang/crates.io-index" 43 | dependencies = [ 44 | "wee_alloc 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", 45 | ] 46 | 47 | [[package]] 48 | name = "pwasm-ethereum" 49 | version = "0.8.0" 50 | source = "registry+https://github.com/rust-lang/crates.io-index" 51 | dependencies = [ 52 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 53 | "pwasm-std 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", 54 | ] 55 | 56 | [[package]] 57 | name = "pwasm-libc" 58 | version = "0.2.0" 59 | source = "registry+https://github.com/rust-lang/crates.io-index" 60 | dependencies = [ 61 | "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", 62 | "rlibc 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", 63 | ] 64 | 65 | [[package]] 66 | name = "pwasm-std" 67 | version = "0.13.0" 68 | source = "registry+https://github.com/rust-lang/crates.io-index" 69 | dependencies = [ 70 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 71 | "fixed-hash 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 72 | "pwasm-alloc 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 73 | "pwasm-libc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 74 | "tiny-keccak 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)", 75 | "uint 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 76 | ] 77 | 78 | [[package]] 79 | name = "pwasm-tutorial-contract" 80 | version = "0.1.0" 81 | dependencies = [ 82 | "pwasm-ethereum 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", 83 | "pwasm-std 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", 84 | ] 85 | 86 | [[package]] 87 | name = "rlibc" 88 | version = "1.0.0" 89 | source = "registry+https://github.com/rust-lang/crates.io-index" 90 | 91 | [[package]] 92 | name = "rustc-hex" 93 | version = "2.0.1" 94 | source = "registry+https://github.com/rust-lang/crates.io-index" 95 | 96 | [[package]] 97 | name = "static_assertions" 98 | version = "0.2.5" 99 | source = "registry+https://github.com/rust-lang/crates.io-index" 100 | 101 | [[package]] 102 | name = "tiny-keccak" 103 | version = "1.4.2" 104 | source = "registry+https://github.com/rust-lang/crates.io-index" 105 | dependencies = [ 106 | "crunchy 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", 107 | ] 108 | 109 | [[package]] 110 | name = "uint" 111 | version = "0.5.0" 112 | source = "registry+https://github.com/rust-lang/crates.io-index" 113 | dependencies = [ 114 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 115 | "crunchy 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 116 | "rustc-hex 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 117 | ] 118 | 119 | [[package]] 120 | name = "unreachable" 121 | version = "1.0.0" 122 | source = "registry+https://github.com/rust-lang/crates.io-index" 123 | dependencies = [ 124 | "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", 125 | ] 126 | 127 | [[package]] 128 | name = "void" 129 | version = "1.0.2" 130 | source = "registry+https://github.com/rust-lang/crates.io-index" 131 | 132 | [[package]] 133 | name = "wee_alloc" 134 | version = "0.4.2" 135 | source = "registry+https://github.com/rust-lang/crates.io-index" 136 | dependencies = [ 137 | "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", 138 | "libc 0.2.47 (registry+https://github.com/rust-lang/crates.io-index)", 139 | "memory_units 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 140 | "unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", 141 | "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 142 | ] 143 | 144 | [[package]] 145 | name = "winapi" 146 | version = "0.3.6" 147 | source = "registry+https://github.com/rust-lang/crates.io-index" 148 | dependencies = [ 149 | "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 150 | "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 151 | ] 152 | 153 | [[package]] 154 | name = "winapi-i686-pc-windows-gnu" 155 | version = "0.4.0" 156 | source = "registry+https://github.com/rust-lang/crates.io-index" 157 | 158 | [[package]] 159 | name = "winapi-x86_64-pc-windows-gnu" 160 | version = "0.4.0" 161 | source = "registry+https://github.com/rust-lang/crates.io-index" 162 | 163 | [metadata] 164 | "checksum byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "60f0b0d4c0a382d2734228fd12b5a6b5dac185c60e938026fd31b265b94f9bd2" 165 | "checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4" 166 | "checksum crunchy 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "a2f4a431c5c9f662e1200b7c7f02c34e91361150e382089a8f2dec3ba680cbda" 167 | "checksum crunchy 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c240f247c278fa08a6d4820a6a222bfc6e0d999e51ba67be94f44c905b2161f2" 168 | "checksum fixed-hash 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a557e80084b05c32b455963ff565a9de6f2866da023d6671705c6aff6f65e01c" 169 | "checksum libc 0.2.47 (registry+https://github.com/rust-lang/crates.io-index)" = "48450664a984b25d5b479554c29cc04e3150c97aa4c01da5604a2d4ed9151476" 170 | "checksum memory_units 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3" 171 | "checksum pwasm-alloc 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bf268e76f224753679a7cb6e0261c9226676c83cd4d2b6ead2095524f00c854f" 172 | "checksum pwasm-ethereum 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8b5509e67ee47ec62bdbd1c32a6949931adbe2e12a460bb45e5dc46593fdc71c" 173 | "checksum pwasm-libc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "027307d6d2086ffb7f399227d42593c5341e8869a55ac8dd7c79d7ca04ec00e2" 174 | "checksum pwasm-std 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "75e907e3e458b07ed7d92473e8041c12928d196af1a8e441c2352d13e7243e8d" 175 | "checksum rlibc 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc874b127765f014d792f16763a81245ab80500e2ad921ed4ee9e82481ee08fe" 176 | "checksum rustc-hex 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "403bb3a286107a04825a5f82e1270acc1e14028d3d554d7a1e08914549575ab8" 177 | "checksum static_assertions 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "c19be23126415861cb3a23e501d34a708f7f9b2183c5252d690941c2e69199d5" 178 | "checksum tiny-keccak 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e9175261fbdb60781fcd388a4d6cc7e14764a2b629a7ad94abb439aed223a44f" 179 | "checksum uint 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "082df6964410f6aa929a61ddfafc997e4f32c62c22490e439ac351cec827f436" 180 | "checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" 181 | "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" 182 | "checksum wee_alloc 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "27875be1daf838fa18f3e94fd19fd12638e34615b42f56da2610c8f46be80cc6" 183 | "checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" 184 | "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 185 | "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 186 | -------------------------------------------------------------------------------- /step-1/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "pwasm-tutorial-contract" 3 | version = "0.1.0" 4 | authors = ["Alexey Frolov "] 5 | 6 | [dependencies] 7 | pwasm-std = "0.13" 8 | pwasm-ethereum = "0.8" 9 | 10 | [lib] 11 | crate-type = ["cdylib"] 12 | 13 | [profile.release] 14 | panic = "abort" 15 | lto = true 16 | opt-level = "z" 17 | -------------------------------------------------------------------------------- /step-1/README.md: -------------------------------------------------------------------------------- 1 | ## Description 2 | The step-by-step project describes how to write a contract in Rust and compile in to Webassembly. 3 | 4 | ## Build 5 | Make sure you've installed [required tools](https://github.com/paritytech/pwasm-tutorial/blob/master/README.md#tutorial-prerequisites) 6 | ``` 7 | ./build.sh 8 | ``` 9 | As a result the `pwasm_tutorial_contract.wasm` should be created under the `step-1/target/wasm32-unknown-unknown/release/` directory. 10 | -------------------------------------------------------------------------------- /step-1/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cargo build --release --target wasm32-unknown-unknown 4 | wasm-build --target=wasm32-unknown-unknown ./target pwasm_tutorial_contract 5 | -------------------------------------------------------------------------------- /step-1/rust-toolchain: -------------------------------------------------------------------------------- 1 | nightly-2018-11-12 2 | -------------------------------------------------------------------------------- /step-1/src/lib.rs: -------------------------------------------------------------------------------- 1 | // This contract will return the address from which it was deployed 2 | #![no_std] 3 | 4 | extern crate pwasm_ethereum; 5 | extern crate pwasm_std; 6 | 7 | use pwasm_std::types::H256; 8 | 9 | // The "deploy" will be executed only once on deployment but will not be stored on the blockchain 10 | #[no_mangle] 11 | pub fn deploy() { 12 | // Lets set the sender address to the contract storage at address "0" 13 | pwasm_ethereum::write(&H256::zero().into(), &H256::from(pwasm_ethereum::sender()).into()); 14 | // Note we should't write any result into the call descriptor in deploy. 15 | } 16 | 17 | // The following code will be stored on the blockchain. 18 | #[no_mangle] 19 | pub fn call() { 20 | // Will read the address of the deployer which we wrote to the storage on the deploy stage 21 | let owner = pwasm_ethereum::read(&H256::zero().into()); 22 | // Send a result pointer to the runtime 23 | pwasm_ethereum::ret(owner.as_ref()); 24 | } 25 | -------------------------------------------------------------------------------- /step-2/.cargo/config: -------------------------------------------------------------------------------- 1 | [target.wasm32-unknown-unknown] 2 | rustflags = [ 3 | "-C", "link-args=-z stack-size=65536", 4 | ] 5 | -------------------------------------------------------------------------------- /step-2/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | /target/ 3 | **/*.rs.bk 4 | -------------------------------------------------------------------------------- /step-2/Cargo.lock: -------------------------------------------------------------------------------- 1 | [[package]] 2 | name = "byteorder" 3 | version = "1.3.0" 4 | source = "registry+https://github.com/rust-lang/crates.io-index" 5 | 6 | [[package]] 7 | name = "cfg-if" 8 | version = "0.1.6" 9 | source = "registry+https://github.com/rust-lang/crates.io-index" 10 | 11 | [[package]] 12 | name = "crunchy" 13 | version = "0.1.6" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | 16 | [[package]] 17 | name = "crunchy" 18 | version = "0.2.1" 19 | source = "registry+https://github.com/rust-lang/crates.io-index" 20 | 21 | [[package]] 22 | name = "fixed-hash" 23 | version = "0.3.0" 24 | source = "registry+https://github.com/rust-lang/crates.io-index" 25 | dependencies = [ 26 | "static_assertions 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", 27 | ] 28 | 29 | [[package]] 30 | name = "itoa" 31 | version = "0.4.3" 32 | source = "registry+https://github.com/rust-lang/crates.io-index" 33 | 34 | [[package]] 35 | name = "lazy_static" 36 | version = "1.2.0" 37 | source = "registry+https://github.com/rust-lang/crates.io-index" 38 | dependencies = [ 39 | "spin 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", 40 | ] 41 | 42 | [[package]] 43 | name = "libc" 44 | version = "0.2.47" 45 | source = "registry+https://github.com/rust-lang/crates.io-index" 46 | 47 | [[package]] 48 | name = "memory_units" 49 | version = "0.4.0" 50 | source = "registry+https://github.com/rust-lang/crates.io-index" 51 | 52 | [[package]] 53 | name = "proc-macro2" 54 | version = "0.4.25" 55 | source = "registry+https://github.com/rust-lang/crates.io-index" 56 | dependencies = [ 57 | "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 58 | ] 59 | 60 | [[package]] 61 | name = "pwasm-abi" 62 | version = "0.2.1" 63 | source = "registry+https://github.com/rust-lang/crates.io-index" 64 | dependencies = [ 65 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 66 | "pwasm-std 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", 67 | ] 68 | 69 | [[package]] 70 | name = "pwasm-abi-derive" 71 | version = "0.2.1" 72 | source = "registry+https://github.com/rust-lang/crates.io-index" 73 | dependencies = [ 74 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 75 | "fixed-hash 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 76 | "proc-macro2 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)", 77 | "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", 78 | "serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)", 79 | "serde_derive 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)", 80 | "serde_json 1.0.36 (registry+https://github.com/rust-lang/crates.io-index)", 81 | "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", 82 | "tiny-keccak 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)", 83 | ] 84 | 85 | [[package]] 86 | name = "pwasm-alloc" 87 | version = "0.4.0" 88 | source = "registry+https://github.com/rust-lang/crates.io-index" 89 | dependencies = [ 90 | "wee_alloc 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", 91 | ] 92 | 93 | [[package]] 94 | name = "pwasm-ethereum" 95 | version = "0.8.0" 96 | source = "registry+https://github.com/rust-lang/crates.io-index" 97 | dependencies = [ 98 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 99 | "pwasm-std 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", 100 | ] 101 | 102 | [[package]] 103 | name = "pwasm-libc" 104 | version = "0.2.0" 105 | source = "registry+https://github.com/rust-lang/crates.io-index" 106 | dependencies = [ 107 | "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", 108 | "rlibc 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", 109 | ] 110 | 111 | [[package]] 112 | name = "pwasm-std" 113 | version = "0.13.0" 114 | source = "registry+https://github.com/rust-lang/crates.io-index" 115 | dependencies = [ 116 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 117 | "fixed-hash 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 118 | "pwasm-alloc 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 119 | "pwasm-libc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 120 | "tiny-keccak 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)", 121 | "uint 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 122 | ] 123 | 124 | [[package]] 125 | name = "pwasm-tutorial-contract" 126 | version = "0.1.0" 127 | dependencies = [ 128 | "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 129 | "pwasm-abi 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 130 | "pwasm-abi-derive 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 131 | "pwasm-ethereum 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", 132 | "pwasm-std 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", 133 | ] 134 | 135 | [[package]] 136 | name = "quote" 137 | version = "0.6.10" 138 | source = "registry+https://github.com/rust-lang/crates.io-index" 139 | dependencies = [ 140 | "proc-macro2 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)", 141 | ] 142 | 143 | [[package]] 144 | name = "rlibc" 145 | version = "1.0.0" 146 | source = "registry+https://github.com/rust-lang/crates.io-index" 147 | 148 | [[package]] 149 | name = "rustc-hex" 150 | version = "2.0.1" 151 | source = "registry+https://github.com/rust-lang/crates.io-index" 152 | 153 | [[package]] 154 | name = "ryu" 155 | version = "0.2.7" 156 | source = "registry+https://github.com/rust-lang/crates.io-index" 157 | 158 | [[package]] 159 | name = "serde" 160 | version = "1.0.85" 161 | source = "registry+https://github.com/rust-lang/crates.io-index" 162 | 163 | [[package]] 164 | name = "serde_derive" 165 | version = "1.0.85" 166 | source = "registry+https://github.com/rust-lang/crates.io-index" 167 | dependencies = [ 168 | "proc-macro2 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)", 169 | "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", 170 | "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", 171 | ] 172 | 173 | [[package]] 174 | name = "serde_json" 175 | version = "1.0.36" 176 | source = "registry+https://github.com/rust-lang/crates.io-index" 177 | dependencies = [ 178 | "itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", 179 | "ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", 180 | "serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)", 181 | ] 182 | 183 | [[package]] 184 | name = "spin" 185 | version = "0.4.10" 186 | source = "registry+https://github.com/rust-lang/crates.io-index" 187 | 188 | [[package]] 189 | name = "static_assertions" 190 | version = "0.2.5" 191 | source = "registry+https://github.com/rust-lang/crates.io-index" 192 | 193 | [[package]] 194 | name = "syn" 195 | version = "0.15.26" 196 | source = "registry+https://github.com/rust-lang/crates.io-index" 197 | dependencies = [ 198 | "proc-macro2 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)", 199 | "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", 200 | "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 201 | ] 202 | 203 | [[package]] 204 | name = "tiny-keccak" 205 | version = "1.4.2" 206 | source = "registry+https://github.com/rust-lang/crates.io-index" 207 | dependencies = [ 208 | "crunchy 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", 209 | ] 210 | 211 | [[package]] 212 | name = "uint" 213 | version = "0.5.0" 214 | source = "registry+https://github.com/rust-lang/crates.io-index" 215 | dependencies = [ 216 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 217 | "crunchy 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 218 | "rustc-hex 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 219 | ] 220 | 221 | [[package]] 222 | name = "unicode-xid" 223 | version = "0.1.0" 224 | source = "registry+https://github.com/rust-lang/crates.io-index" 225 | 226 | [[package]] 227 | name = "unreachable" 228 | version = "1.0.0" 229 | source = "registry+https://github.com/rust-lang/crates.io-index" 230 | dependencies = [ 231 | "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", 232 | ] 233 | 234 | [[package]] 235 | name = "void" 236 | version = "1.0.2" 237 | source = "registry+https://github.com/rust-lang/crates.io-index" 238 | 239 | [[package]] 240 | name = "wee_alloc" 241 | version = "0.4.2" 242 | source = "registry+https://github.com/rust-lang/crates.io-index" 243 | dependencies = [ 244 | "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", 245 | "libc 0.2.47 (registry+https://github.com/rust-lang/crates.io-index)", 246 | "memory_units 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 247 | "unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", 248 | "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 249 | ] 250 | 251 | [[package]] 252 | name = "winapi" 253 | version = "0.3.6" 254 | source = "registry+https://github.com/rust-lang/crates.io-index" 255 | dependencies = [ 256 | "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 257 | "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 258 | ] 259 | 260 | [[package]] 261 | name = "winapi-i686-pc-windows-gnu" 262 | version = "0.4.0" 263 | source = "registry+https://github.com/rust-lang/crates.io-index" 264 | 265 | [[package]] 266 | name = "winapi-x86_64-pc-windows-gnu" 267 | version = "0.4.0" 268 | source = "registry+https://github.com/rust-lang/crates.io-index" 269 | 270 | [metadata] 271 | "checksum byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "60f0b0d4c0a382d2734228fd12b5a6b5dac185c60e938026fd31b265b94f9bd2" 272 | "checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4" 273 | "checksum crunchy 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "a2f4a431c5c9f662e1200b7c7f02c34e91361150e382089a8f2dec3ba680cbda" 274 | "checksum crunchy 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c240f247c278fa08a6d4820a6a222bfc6e0d999e51ba67be94f44c905b2161f2" 275 | "checksum fixed-hash 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a557e80084b05c32b455963ff565a9de6f2866da023d6671705c6aff6f65e01c" 276 | "checksum itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b" 277 | "checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1" 278 | "checksum libc 0.2.47 (registry+https://github.com/rust-lang/crates.io-index)" = "48450664a984b25d5b479554c29cc04e3150c97aa4c01da5604a2d4ed9151476" 279 | "checksum memory_units 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3" 280 | "checksum proc-macro2 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)" = "d3797b7142c9aa74954e351fc089bbee7958cebbff6bf2815e7ffff0b19f547d" 281 | "checksum pwasm-abi 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "553291f59e0650074286e6270fbfae05cbce429c5d89a27ec31294d483c8288b" 282 | "checksum pwasm-abi-derive 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7220ad898fa15739494ca16131a638ef1cc018ff490513b9130cae17d5238bd3" 283 | "checksum pwasm-alloc 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bf268e76f224753679a7cb6e0261c9226676c83cd4d2b6ead2095524f00c854f" 284 | "checksum pwasm-ethereum 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8b5509e67ee47ec62bdbd1c32a6949931adbe2e12a460bb45e5dc46593fdc71c" 285 | "checksum pwasm-libc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "027307d6d2086ffb7f399227d42593c5341e8869a55ac8dd7c79d7ca04ec00e2" 286 | "checksum pwasm-std 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "75e907e3e458b07ed7d92473e8041c12928d196af1a8e441c2352d13e7243e8d" 287 | "checksum quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "53fa22a1994bd0f9372d7a816207d8a2677ad0325b073f5c5332760f0fb62b5c" 288 | "checksum rlibc 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc874b127765f014d792f16763a81245ab80500e2ad921ed4ee9e82481ee08fe" 289 | "checksum rustc-hex 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "403bb3a286107a04825a5f82e1270acc1e14028d3d554d7a1e08914549575ab8" 290 | "checksum ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "eb9e9b8cde282a9fe6a42dd4681319bfb63f121b8a8ee9439c6f4107e58a46f7" 291 | "checksum serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)" = "534b8b91a95e0f71bca3ed5824752d558da048d4248c91af873b63bd60519752" 292 | "checksum serde_derive 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)" = "a915306b0f1ac5607797697148c223bedeaa36bcc2e28a01441cd638cc6567b4" 293 | "checksum serde_json 1.0.36 (registry+https://github.com/rust-lang/crates.io-index)" = "574378d957d6dcdf1bbb5d562a15cbd5e644159432f84634b94e485267abbcc7" 294 | "checksum spin 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "ceac490aa12c567115b40b7b7fceca03a6c9d53d5defea066123debc83c5dc1f" 295 | "checksum static_assertions 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "c19be23126415861cb3a23e501d34a708f7f9b2183c5252d690941c2e69199d5" 296 | "checksum syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)" = "f92e629aa1d9c827b2bb8297046c1ccffc57c99b947a680d3ccff1f136a3bee9" 297 | "checksum tiny-keccak 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e9175261fbdb60781fcd388a4d6cc7e14764a2b629a7ad94abb439aed223a44f" 298 | "checksum uint 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "082df6964410f6aa929a61ddfafc997e4f32c62c22490e439ac351cec827f436" 299 | "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" 300 | "checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" 301 | "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" 302 | "checksum wee_alloc 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "27875be1daf838fa18f3e94fd19fd12638e34615b42f56da2610c8f46be80cc6" 303 | "checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" 304 | "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 305 | "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 306 | -------------------------------------------------------------------------------- /step-2/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "pwasm-tutorial-contract" 3 | version = "0.1.0" 4 | authors = ["Alexey Frolov "] 5 | 6 | [dependencies] 7 | pwasm-std = "0.13" 8 | pwasm-ethereum = "0.8" 9 | pwasm-abi = "0.2" 10 | pwasm-abi-derive = "0.2" 11 | lazy_static = { version = "1.2.0", features = ["spin_no_std"] } 12 | 13 | [lib] 14 | crate-type = ["cdylib"] 15 | 16 | [profile.release] 17 | panic = "abort" 18 | lto = true 19 | opt-level = "z" 20 | -------------------------------------------------------------------------------- /step-2/README.md: -------------------------------------------------------------------------------- 1 | ## Description 2 | The step-by-step project describes how to write a contract in Rust and compile in to Webassembly. 3 | 4 | ## Build 5 | Make sure you've installed [required tools](https://github.com/paritytech/pwasm-tutorial/blob/master/README.md#tutorial-prerequisites) 6 | ``` 7 | ./build.sh 8 | ``` 9 | As a result the `pwasm_tutorial_contract.wasm` should be created under the `step-2/target/wasm32-unknown-unknown/release/` directory. 10 | -------------------------------------------------------------------------------- /step-2/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cargo build --release --target wasm32-unknown-unknown 4 | wasm-build --target=wasm32-unknown-unknown ./target pwasm_tutorial_contract 5 | -------------------------------------------------------------------------------- /step-2/rust-toolchain: -------------------------------------------------------------------------------- 1 | nightly-2018-11-12 2 | -------------------------------------------------------------------------------- /step-2/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![allow(non_snake_case)] 3 | #![feature(proc_macro_hygiene)] 4 | 5 | extern crate pwasm_std; 6 | extern crate pwasm_ethereum; 7 | extern crate pwasm_abi; 8 | extern crate pwasm_abi_derive; 9 | 10 | #[macro_use] 11 | extern crate lazy_static; 12 | 13 | pub mod token { 14 | use pwasm_ethereum; 15 | use pwasm_std::types::*; 16 | 17 | // eth_abi is a procedural macros https://doc.rust-lang.org/book/first-edition/procedural-macros.html 18 | use pwasm_abi_derive::eth_abi; 19 | 20 | lazy_static! { 21 | static ref TOTAL_SUPPLY_KEY: H256 = { H256::from([2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])}; 22 | } 23 | 24 | #[eth_abi(TokenEndpoint)] 25 | pub trait TokenInterface { 26 | /// The constructor 27 | fn constructor(&mut self, _total_supply: U256); 28 | /// Total amount of tokens 29 | fn totalSupply(&mut self) -> U256; 30 | } 31 | 32 | pub struct TokenContract; 33 | 34 | impl TokenInterface for TokenContract { 35 | fn constructor(&mut self, total_supply: U256) { 36 | // Set up the total supply for the token 37 | pwasm_ethereum::write(&TOTAL_SUPPLY_KEY, &total_supply.into()); 38 | } 39 | 40 | fn totalSupply(&mut self) -> U256 { 41 | U256::from_big_endian(&pwasm_ethereum::read(&TOTAL_SUPPLY_KEY)) 42 | } 43 | } 44 | } 45 | // Declares the dispatch and dispatch_ctor methods 46 | use pwasm_abi::eth::EndpointInterface; 47 | 48 | #[no_mangle] 49 | pub fn call() { 50 | let mut endpoint = token::TokenEndpoint::new(token::TokenContract{}); 51 | // Read http://solidity.readthedocs.io/en/develop/abi-spec.html#formal-specification-of-the-encoding for details 52 | pwasm_ethereum::ret(&endpoint.dispatch(&pwasm_ethereum::input())); 53 | } 54 | 55 | #[no_mangle] 56 | pub fn deploy() { 57 | let mut endpoint = token::TokenEndpoint::new(token::TokenContract{}); 58 | // 59 | endpoint.dispatch_ctor(&pwasm_ethereum::input()); 60 | } 61 | -------------------------------------------------------------------------------- /step-3/.cargo/config: -------------------------------------------------------------------------------- 1 | [target.wasm32-unknown-unknown] 2 | rustflags = [ 3 | "-C", "link-args=-z stack-size=65536", 4 | ] 5 | -------------------------------------------------------------------------------- /step-3/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | /target/ 3 | **/*.rs.bk 4 | -------------------------------------------------------------------------------- /step-3/Cargo.lock: -------------------------------------------------------------------------------- 1 | [[package]] 2 | name = "byteorder" 3 | version = "1.3.0" 4 | source = "registry+https://github.com/rust-lang/crates.io-index" 5 | 6 | [[package]] 7 | name = "cfg-if" 8 | version = "0.1.6" 9 | source = "registry+https://github.com/rust-lang/crates.io-index" 10 | 11 | [[package]] 12 | name = "crunchy" 13 | version = "0.1.6" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | 16 | [[package]] 17 | name = "crunchy" 18 | version = "0.2.1" 19 | source = "registry+https://github.com/rust-lang/crates.io-index" 20 | 21 | [[package]] 22 | name = "fixed-hash" 23 | version = "0.3.0" 24 | source = "registry+https://github.com/rust-lang/crates.io-index" 25 | dependencies = [ 26 | "static_assertions 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", 27 | ] 28 | 29 | [[package]] 30 | name = "itoa" 31 | version = "0.4.3" 32 | source = "registry+https://github.com/rust-lang/crates.io-index" 33 | 34 | [[package]] 35 | name = "lazy_static" 36 | version = "1.2.0" 37 | source = "registry+https://github.com/rust-lang/crates.io-index" 38 | dependencies = [ 39 | "spin 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", 40 | ] 41 | 42 | [[package]] 43 | name = "libc" 44 | version = "0.2.47" 45 | source = "registry+https://github.com/rust-lang/crates.io-index" 46 | 47 | [[package]] 48 | name = "memory_units" 49 | version = "0.4.0" 50 | source = "registry+https://github.com/rust-lang/crates.io-index" 51 | 52 | [[package]] 53 | name = "proc-macro2" 54 | version = "0.4.25" 55 | source = "registry+https://github.com/rust-lang/crates.io-index" 56 | dependencies = [ 57 | "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 58 | ] 59 | 60 | [[package]] 61 | name = "pwasm-abi" 62 | version = "0.2.1" 63 | source = "registry+https://github.com/rust-lang/crates.io-index" 64 | dependencies = [ 65 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 66 | "pwasm-std 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", 67 | ] 68 | 69 | [[package]] 70 | name = "pwasm-abi-derive" 71 | version = "0.2.1" 72 | source = "registry+https://github.com/rust-lang/crates.io-index" 73 | dependencies = [ 74 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 75 | "fixed-hash 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 76 | "proc-macro2 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)", 77 | "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", 78 | "serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)", 79 | "serde_derive 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)", 80 | "serde_json 1.0.36 (registry+https://github.com/rust-lang/crates.io-index)", 81 | "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", 82 | "tiny-keccak 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)", 83 | ] 84 | 85 | [[package]] 86 | name = "pwasm-alloc" 87 | version = "0.4.0" 88 | source = "registry+https://github.com/rust-lang/crates.io-index" 89 | dependencies = [ 90 | "wee_alloc 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", 91 | ] 92 | 93 | [[package]] 94 | name = "pwasm-ethereum" 95 | version = "0.8.0" 96 | source = "registry+https://github.com/rust-lang/crates.io-index" 97 | dependencies = [ 98 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 99 | "pwasm-std 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", 100 | ] 101 | 102 | [[package]] 103 | name = "pwasm-libc" 104 | version = "0.2.0" 105 | source = "registry+https://github.com/rust-lang/crates.io-index" 106 | dependencies = [ 107 | "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", 108 | "rlibc 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", 109 | ] 110 | 111 | [[package]] 112 | name = "pwasm-std" 113 | version = "0.13.0" 114 | source = "registry+https://github.com/rust-lang/crates.io-index" 115 | dependencies = [ 116 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 117 | "fixed-hash 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 118 | "pwasm-alloc 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 119 | "pwasm-libc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 120 | "tiny-keccak 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)", 121 | "uint 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 122 | ] 123 | 124 | [[package]] 125 | name = "pwasm-tutorial-contract" 126 | version = "0.1.0" 127 | dependencies = [ 128 | "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 129 | "pwasm-abi 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 130 | "pwasm-abi-derive 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 131 | "pwasm-ethereum 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", 132 | "pwasm-std 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", 133 | ] 134 | 135 | [[package]] 136 | name = "quote" 137 | version = "0.6.10" 138 | source = "registry+https://github.com/rust-lang/crates.io-index" 139 | dependencies = [ 140 | "proc-macro2 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)", 141 | ] 142 | 143 | [[package]] 144 | name = "rlibc" 145 | version = "1.0.0" 146 | source = "registry+https://github.com/rust-lang/crates.io-index" 147 | 148 | [[package]] 149 | name = "rustc-hex" 150 | version = "2.0.1" 151 | source = "registry+https://github.com/rust-lang/crates.io-index" 152 | 153 | [[package]] 154 | name = "ryu" 155 | version = "0.2.7" 156 | source = "registry+https://github.com/rust-lang/crates.io-index" 157 | 158 | [[package]] 159 | name = "serde" 160 | version = "1.0.85" 161 | source = "registry+https://github.com/rust-lang/crates.io-index" 162 | 163 | [[package]] 164 | name = "serde_derive" 165 | version = "1.0.85" 166 | source = "registry+https://github.com/rust-lang/crates.io-index" 167 | dependencies = [ 168 | "proc-macro2 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)", 169 | "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", 170 | "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", 171 | ] 172 | 173 | [[package]] 174 | name = "serde_json" 175 | version = "1.0.36" 176 | source = "registry+https://github.com/rust-lang/crates.io-index" 177 | dependencies = [ 178 | "itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", 179 | "ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", 180 | "serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)", 181 | ] 182 | 183 | [[package]] 184 | name = "spin" 185 | version = "0.4.10" 186 | source = "registry+https://github.com/rust-lang/crates.io-index" 187 | 188 | [[package]] 189 | name = "static_assertions" 190 | version = "0.2.5" 191 | source = "registry+https://github.com/rust-lang/crates.io-index" 192 | 193 | [[package]] 194 | name = "syn" 195 | version = "0.15.26" 196 | source = "registry+https://github.com/rust-lang/crates.io-index" 197 | dependencies = [ 198 | "proc-macro2 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)", 199 | "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", 200 | "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 201 | ] 202 | 203 | [[package]] 204 | name = "tiny-keccak" 205 | version = "1.4.2" 206 | source = "registry+https://github.com/rust-lang/crates.io-index" 207 | dependencies = [ 208 | "crunchy 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", 209 | ] 210 | 211 | [[package]] 212 | name = "uint" 213 | version = "0.5.0" 214 | source = "registry+https://github.com/rust-lang/crates.io-index" 215 | dependencies = [ 216 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 217 | "crunchy 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 218 | "rustc-hex 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 219 | ] 220 | 221 | [[package]] 222 | name = "unicode-xid" 223 | version = "0.1.0" 224 | source = "registry+https://github.com/rust-lang/crates.io-index" 225 | 226 | [[package]] 227 | name = "unreachable" 228 | version = "1.0.0" 229 | source = "registry+https://github.com/rust-lang/crates.io-index" 230 | dependencies = [ 231 | "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", 232 | ] 233 | 234 | [[package]] 235 | name = "void" 236 | version = "1.0.2" 237 | source = "registry+https://github.com/rust-lang/crates.io-index" 238 | 239 | [[package]] 240 | name = "wee_alloc" 241 | version = "0.4.2" 242 | source = "registry+https://github.com/rust-lang/crates.io-index" 243 | dependencies = [ 244 | "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", 245 | "libc 0.2.47 (registry+https://github.com/rust-lang/crates.io-index)", 246 | "memory_units 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 247 | "unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", 248 | "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 249 | ] 250 | 251 | [[package]] 252 | name = "winapi" 253 | version = "0.3.6" 254 | source = "registry+https://github.com/rust-lang/crates.io-index" 255 | dependencies = [ 256 | "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 257 | "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 258 | ] 259 | 260 | [[package]] 261 | name = "winapi-i686-pc-windows-gnu" 262 | version = "0.4.0" 263 | source = "registry+https://github.com/rust-lang/crates.io-index" 264 | 265 | [[package]] 266 | name = "winapi-x86_64-pc-windows-gnu" 267 | version = "0.4.0" 268 | source = "registry+https://github.com/rust-lang/crates.io-index" 269 | 270 | [metadata] 271 | "checksum byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "60f0b0d4c0a382d2734228fd12b5a6b5dac185c60e938026fd31b265b94f9bd2" 272 | "checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4" 273 | "checksum crunchy 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "a2f4a431c5c9f662e1200b7c7f02c34e91361150e382089a8f2dec3ba680cbda" 274 | "checksum crunchy 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c240f247c278fa08a6d4820a6a222bfc6e0d999e51ba67be94f44c905b2161f2" 275 | "checksum fixed-hash 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a557e80084b05c32b455963ff565a9de6f2866da023d6671705c6aff6f65e01c" 276 | "checksum itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b" 277 | "checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1" 278 | "checksum libc 0.2.47 (registry+https://github.com/rust-lang/crates.io-index)" = "48450664a984b25d5b479554c29cc04e3150c97aa4c01da5604a2d4ed9151476" 279 | "checksum memory_units 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3" 280 | "checksum proc-macro2 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)" = "d3797b7142c9aa74954e351fc089bbee7958cebbff6bf2815e7ffff0b19f547d" 281 | "checksum pwasm-abi 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "553291f59e0650074286e6270fbfae05cbce429c5d89a27ec31294d483c8288b" 282 | "checksum pwasm-abi-derive 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7220ad898fa15739494ca16131a638ef1cc018ff490513b9130cae17d5238bd3" 283 | "checksum pwasm-alloc 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bf268e76f224753679a7cb6e0261c9226676c83cd4d2b6ead2095524f00c854f" 284 | "checksum pwasm-ethereum 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8b5509e67ee47ec62bdbd1c32a6949931adbe2e12a460bb45e5dc46593fdc71c" 285 | "checksum pwasm-libc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "027307d6d2086ffb7f399227d42593c5341e8869a55ac8dd7c79d7ca04ec00e2" 286 | "checksum pwasm-std 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "75e907e3e458b07ed7d92473e8041c12928d196af1a8e441c2352d13e7243e8d" 287 | "checksum quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "53fa22a1994bd0f9372d7a816207d8a2677ad0325b073f5c5332760f0fb62b5c" 288 | "checksum rlibc 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc874b127765f014d792f16763a81245ab80500e2ad921ed4ee9e82481ee08fe" 289 | "checksum rustc-hex 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "403bb3a286107a04825a5f82e1270acc1e14028d3d554d7a1e08914549575ab8" 290 | "checksum ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "eb9e9b8cde282a9fe6a42dd4681319bfb63f121b8a8ee9439c6f4107e58a46f7" 291 | "checksum serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)" = "534b8b91a95e0f71bca3ed5824752d558da048d4248c91af873b63bd60519752" 292 | "checksum serde_derive 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)" = "a915306b0f1ac5607797697148c223bedeaa36bcc2e28a01441cd638cc6567b4" 293 | "checksum serde_json 1.0.36 (registry+https://github.com/rust-lang/crates.io-index)" = "574378d957d6dcdf1bbb5d562a15cbd5e644159432f84634b94e485267abbcc7" 294 | "checksum spin 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "ceac490aa12c567115b40b7b7fceca03a6c9d53d5defea066123debc83c5dc1f" 295 | "checksum static_assertions 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "c19be23126415861cb3a23e501d34a708f7f9b2183c5252d690941c2e69199d5" 296 | "checksum syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)" = "f92e629aa1d9c827b2bb8297046c1ccffc57c99b947a680d3ccff1f136a3bee9" 297 | "checksum tiny-keccak 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e9175261fbdb60781fcd388a4d6cc7e14764a2b629a7ad94abb439aed223a44f" 298 | "checksum uint 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "082df6964410f6aa929a61ddfafc997e4f32c62c22490e439ac351cec827f436" 299 | "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" 300 | "checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" 301 | "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" 302 | "checksum wee_alloc 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "27875be1daf838fa18f3e94fd19fd12638e34615b42f56da2610c8f46be80cc6" 303 | "checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" 304 | "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 305 | "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 306 | -------------------------------------------------------------------------------- /step-3/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "pwasm-tutorial-contract" 3 | version = "0.1.0" 4 | authors = ["Alexey Frolov "] 5 | 6 | [dependencies] 7 | pwasm-std = "0.13" 8 | pwasm-ethereum = "0.8" 9 | pwasm-abi = "0.2" 10 | pwasm-abi-derive = "0.2" 11 | lazy_static = { version = "1.2.0", features = ["spin_no_std"] } 12 | 13 | [lib] 14 | crate-type = ["cdylib"] 15 | 16 | [profile.release] 17 | panic = "abort" 18 | lto = true 19 | opt-level = "z" 20 | -------------------------------------------------------------------------------- /step-3/README.md: -------------------------------------------------------------------------------- 1 | ## Description 2 | The step-by-step project describes how to write a contract in Rust and compile in to Webassembly. 3 | 4 | ## Build 5 | Make sure you've installed [required tools](https://github.com/paritytech/pwasm-tutorial/blob/master/README.md#tutorial-prerequisites) 6 | ``` 7 | ./build.sh 8 | ``` 9 | As a result the `pwasm_tutorial_contract.wasm` should be created under the `step-3/target/wasm32-unknown-unknown/release/` directory. 10 | -------------------------------------------------------------------------------- /step-3/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cargo build --release --target wasm32-unknown-unknown 4 | wasm-build --target=wasm32-unknown-unknown ./target pwasm_tutorial_contract 5 | -------------------------------------------------------------------------------- /step-3/rust-toolchain: -------------------------------------------------------------------------------- 1 | nightly-2018-11-12 2 | -------------------------------------------------------------------------------- /step-3/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![allow(non_snake_case)] 3 | #![feature(proc_macro_hygiene)] 4 | 5 | extern crate pwasm_std; 6 | extern crate pwasm_ethereum; 7 | extern crate pwasm_abi; 8 | extern crate pwasm_abi_derive; 9 | 10 | #[macro_use] 11 | extern crate lazy_static; 12 | 13 | pub mod token { 14 | use pwasm_ethereum; 15 | use pwasm_abi::types::*; 16 | 17 | // eth_abi is a procedural macros https://doc.rust-lang.org/book/first-edition/procedural-macros.html 18 | use pwasm_abi_derive::eth_abi; 19 | 20 | lazy_static! { 21 | static ref TOTAL_SUPPLY_KEY: H256 = 22 | H256::from( 23 | [2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] 24 | ); 25 | } 26 | 27 | #[eth_abi(TokenEndpoint, TokenClient)] 28 | pub trait TokenInterface { 29 | /// The constructor 30 | fn constructor(&mut self, _total_supply: U256); 31 | /// Total amount of tokens 32 | #[constant] 33 | fn totalSupply(&mut self) -> U256; 34 | } 35 | 36 | pub struct TokenContract; 37 | 38 | impl TokenInterface for TokenContract { 39 | fn constructor(&mut self, total_supply: U256) { 40 | // Set up the total supply for the token 41 | pwasm_ethereum::write(&TOTAL_SUPPLY_KEY, &total_supply.into()); 42 | } 43 | 44 | fn totalSupply(&mut self) -> U256 { 45 | U256::from_big_endian(&pwasm_ethereum::read(&TOTAL_SUPPLY_KEY)) 46 | } 47 | } 48 | } 49 | // Declares the dispatch and dispatch_ctor methods 50 | use pwasm_abi::eth::EndpointInterface; 51 | 52 | #[no_mangle] 53 | pub fn call() { 54 | let mut endpoint = token::TokenEndpoint::new(token::TokenContract{}); 55 | // Read http://solidity.readthedocs.io/en/develop/abi-spec.html#formal-specification-of-the-encoding for details 56 | pwasm_ethereum::ret(&endpoint.dispatch(&pwasm_ethereum::input())); 57 | } 58 | 59 | #[no_mangle] 60 | pub fn deploy() { 61 | let mut endpoint = token::TokenEndpoint::new(token::TokenContract{}); 62 | endpoint.dispatch_ctor(&pwasm_ethereum::input()); 63 | } 64 | -------------------------------------------------------------------------------- /step-4/.cargo/config: -------------------------------------------------------------------------------- 1 | [target.wasm32-unknown-unknown] 2 | rustflags = [ 3 | "-C", "link-args=-z stack-size=65536", 4 | ] 5 | -------------------------------------------------------------------------------- /step-4/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | /target/ 3 | **/*.rs.bk 4 | -------------------------------------------------------------------------------- /step-4/Cargo.lock: -------------------------------------------------------------------------------- 1 | [[package]] 2 | name = "byteorder" 3 | version = "1.3.0" 4 | source = "registry+https://github.com/rust-lang/crates.io-index" 5 | 6 | [[package]] 7 | name = "cfg-if" 8 | version = "0.1.6" 9 | source = "registry+https://github.com/rust-lang/crates.io-index" 10 | 11 | [[package]] 12 | name = "crunchy" 13 | version = "0.1.6" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | 16 | [[package]] 17 | name = "crunchy" 18 | version = "0.2.1" 19 | source = "registry+https://github.com/rust-lang/crates.io-index" 20 | 21 | [[package]] 22 | name = "fixed-hash" 23 | version = "0.3.0" 24 | source = "registry+https://github.com/rust-lang/crates.io-index" 25 | dependencies = [ 26 | "static_assertions 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", 27 | ] 28 | 29 | [[package]] 30 | name = "itoa" 31 | version = "0.4.3" 32 | source = "registry+https://github.com/rust-lang/crates.io-index" 33 | 34 | [[package]] 35 | name = "lazy_static" 36 | version = "1.2.0" 37 | source = "registry+https://github.com/rust-lang/crates.io-index" 38 | dependencies = [ 39 | "spin 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", 40 | ] 41 | 42 | [[package]] 43 | name = "libc" 44 | version = "0.2.47" 45 | source = "registry+https://github.com/rust-lang/crates.io-index" 46 | 47 | [[package]] 48 | name = "memory_units" 49 | version = "0.4.0" 50 | source = "registry+https://github.com/rust-lang/crates.io-index" 51 | 52 | [[package]] 53 | name = "proc-macro2" 54 | version = "0.4.25" 55 | source = "registry+https://github.com/rust-lang/crates.io-index" 56 | dependencies = [ 57 | "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 58 | ] 59 | 60 | [[package]] 61 | name = "pwasm-abi" 62 | version = "0.2.1" 63 | source = "registry+https://github.com/rust-lang/crates.io-index" 64 | dependencies = [ 65 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 66 | "pwasm-std 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", 67 | ] 68 | 69 | [[package]] 70 | name = "pwasm-abi-derive" 71 | version = "0.2.1" 72 | source = "registry+https://github.com/rust-lang/crates.io-index" 73 | dependencies = [ 74 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 75 | "fixed-hash 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 76 | "proc-macro2 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)", 77 | "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", 78 | "serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)", 79 | "serde_derive 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)", 80 | "serde_json 1.0.36 (registry+https://github.com/rust-lang/crates.io-index)", 81 | "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", 82 | "tiny-keccak 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)", 83 | ] 84 | 85 | [[package]] 86 | name = "pwasm-alloc" 87 | version = "0.4.0" 88 | source = "registry+https://github.com/rust-lang/crates.io-index" 89 | dependencies = [ 90 | "wee_alloc 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", 91 | ] 92 | 93 | [[package]] 94 | name = "pwasm-ethereum" 95 | version = "0.8.0" 96 | source = "registry+https://github.com/rust-lang/crates.io-index" 97 | dependencies = [ 98 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 99 | "pwasm-std 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", 100 | ] 101 | 102 | [[package]] 103 | name = "pwasm-libc" 104 | version = "0.2.0" 105 | source = "registry+https://github.com/rust-lang/crates.io-index" 106 | dependencies = [ 107 | "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", 108 | "rlibc 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", 109 | ] 110 | 111 | [[package]] 112 | name = "pwasm-std" 113 | version = "0.13.0" 114 | source = "registry+https://github.com/rust-lang/crates.io-index" 115 | dependencies = [ 116 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 117 | "fixed-hash 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 118 | "pwasm-alloc 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 119 | "pwasm-libc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 120 | "tiny-keccak 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)", 121 | "uint 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 122 | ] 123 | 124 | [[package]] 125 | name = "pwasm-tutorial-contract" 126 | version = "0.1.0" 127 | dependencies = [ 128 | "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 129 | "pwasm-abi 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 130 | "pwasm-abi-derive 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 131 | "pwasm-ethereum 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", 132 | "pwasm-std 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", 133 | ] 134 | 135 | [[package]] 136 | name = "quote" 137 | version = "0.6.10" 138 | source = "registry+https://github.com/rust-lang/crates.io-index" 139 | dependencies = [ 140 | "proc-macro2 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)", 141 | ] 142 | 143 | [[package]] 144 | name = "rlibc" 145 | version = "1.0.0" 146 | source = "registry+https://github.com/rust-lang/crates.io-index" 147 | 148 | [[package]] 149 | name = "rustc-hex" 150 | version = "2.0.1" 151 | source = "registry+https://github.com/rust-lang/crates.io-index" 152 | 153 | [[package]] 154 | name = "ryu" 155 | version = "0.2.7" 156 | source = "registry+https://github.com/rust-lang/crates.io-index" 157 | 158 | [[package]] 159 | name = "serde" 160 | version = "1.0.85" 161 | source = "registry+https://github.com/rust-lang/crates.io-index" 162 | 163 | [[package]] 164 | name = "serde_derive" 165 | version = "1.0.85" 166 | source = "registry+https://github.com/rust-lang/crates.io-index" 167 | dependencies = [ 168 | "proc-macro2 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)", 169 | "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", 170 | "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", 171 | ] 172 | 173 | [[package]] 174 | name = "serde_json" 175 | version = "1.0.36" 176 | source = "registry+https://github.com/rust-lang/crates.io-index" 177 | dependencies = [ 178 | "itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", 179 | "ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", 180 | "serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)", 181 | ] 182 | 183 | [[package]] 184 | name = "spin" 185 | version = "0.4.10" 186 | source = "registry+https://github.com/rust-lang/crates.io-index" 187 | 188 | [[package]] 189 | name = "static_assertions" 190 | version = "0.2.5" 191 | source = "registry+https://github.com/rust-lang/crates.io-index" 192 | 193 | [[package]] 194 | name = "syn" 195 | version = "0.15.26" 196 | source = "registry+https://github.com/rust-lang/crates.io-index" 197 | dependencies = [ 198 | "proc-macro2 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)", 199 | "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", 200 | "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 201 | ] 202 | 203 | [[package]] 204 | name = "tiny-keccak" 205 | version = "1.4.2" 206 | source = "registry+https://github.com/rust-lang/crates.io-index" 207 | dependencies = [ 208 | "crunchy 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", 209 | ] 210 | 211 | [[package]] 212 | name = "uint" 213 | version = "0.5.0" 214 | source = "registry+https://github.com/rust-lang/crates.io-index" 215 | dependencies = [ 216 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 217 | "crunchy 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 218 | "rustc-hex 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 219 | ] 220 | 221 | [[package]] 222 | name = "unicode-xid" 223 | version = "0.1.0" 224 | source = "registry+https://github.com/rust-lang/crates.io-index" 225 | 226 | [[package]] 227 | name = "unreachable" 228 | version = "1.0.0" 229 | source = "registry+https://github.com/rust-lang/crates.io-index" 230 | dependencies = [ 231 | "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", 232 | ] 233 | 234 | [[package]] 235 | name = "void" 236 | version = "1.0.2" 237 | source = "registry+https://github.com/rust-lang/crates.io-index" 238 | 239 | [[package]] 240 | name = "wee_alloc" 241 | version = "0.4.2" 242 | source = "registry+https://github.com/rust-lang/crates.io-index" 243 | dependencies = [ 244 | "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", 245 | "libc 0.2.47 (registry+https://github.com/rust-lang/crates.io-index)", 246 | "memory_units 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 247 | "unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", 248 | "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 249 | ] 250 | 251 | [[package]] 252 | name = "winapi" 253 | version = "0.3.6" 254 | source = "registry+https://github.com/rust-lang/crates.io-index" 255 | dependencies = [ 256 | "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 257 | "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 258 | ] 259 | 260 | [[package]] 261 | name = "winapi-i686-pc-windows-gnu" 262 | version = "0.4.0" 263 | source = "registry+https://github.com/rust-lang/crates.io-index" 264 | 265 | [[package]] 266 | name = "winapi-x86_64-pc-windows-gnu" 267 | version = "0.4.0" 268 | source = "registry+https://github.com/rust-lang/crates.io-index" 269 | 270 | [metadata] 271 | "checksum byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "60f0b0d4c0a382d2734228fd12b5a6b5dac185c60e938026fd31b265b94f9bd2" 272 | "checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4" 273 | "checksum crunchy 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "a2f4a431c5c9f662e1200b7c7f02c34e91361150e382089a8f2dec3ba680cbda" 274 | "checksum crunchy 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c240f247c278fa08a6d4820a6a222bfc6e0d999e51ba67be94f44c905b2161f2" 275 | "checksum fixed-hash 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a557e80084b05c32b455963ff565a9de6f2866da023d6671705c6aff6f65e01c" 276 | "checksum itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b" 277 | "checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1" 278 | "checksum libc 0.2.47 (registry+https://github.com/rust-lang/crates.io-index)" = "48450664a984b25d5b479554c29cc04e3150c97aa4c01da5604a2d4ed9151476" 279 | "checksum memory_units 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3" 280 | "checksum proc-macro2 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)" = "d3797b7142c9aa74954e351fc089bbee7958cebbff6bf2815e7ffff0b19f547d" 281 | "checksum pwasm-abi 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "553291f59e0650074286e6270fbfae05cbce429c5d89a27ec31294d483c8288b" 282 | "checksum pwasm-abi-derive 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7220ad898fa15739494ca16131a638ef1cc018ff490513b9130cae17d5238bd3" 283 | "checksum pwasm-alloc 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bf268e76f224753679a7cb6e0261c9226676c83cd4d2b6ead2095524f00c854f" 284 | "checksum pwasm-ethereum 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8b5509e67ee47ec62bdbd1c32a6949931adbe2e12a460bb45e5dc46593fdc71c" 285 | "checksum pwasm-libc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "027307d6d2086ffb7f399227d42593c5341e8869a55ac8dd7c79d7ca04ec00e2" 286 | "checksum pwasm-std 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "75e907e3e458b07ed7d92473e8041c12928d196af1a8e441c2352d13e7243e8d" 287 | "checksum quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "53fa22a1994bd0f9372d7a816207d8a2677ad0325b073f5c5332760f0fb62b5c" 288 | "checksum rlibc 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc874b127765f014d792f16763a81245ab80500e2ad921ed4ee9e82481ee08fe" 289 | "checksum rustc-hex 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "403bb3a286107a04825a5f82e1270acc1e14028d3d554d7a1e08914549575ab8" 290 | "checksum ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "eb9e9b8cde282a9fe6a42dd4681319bfb63f121b8a8ee9439c6f4107e58a46f7" 291 | "checksum serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)" = "534b8b91a95e0f71bca3ed5824752d558da048d4248c91af873b63bd60519752" 292 | "checksum serde_derive 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)" = "a915306b0f1ac5607797697148c223bedeaa36bcc2e28a01441cd638cc6567b4" 293 | "checksum serde_json 1.0.36 (registry+https://github.com/rust-lang/crates.io-index)" = "574378d957d6dcdf1bbb5d562a15cbd5e644159432f84634b94e485267abbcc7" 294 | "checksum spin 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "ceac490aa12c567115b40b7b7fceca03a6c9d53d5defea066123debc83c5dc1f" 295 | "checksum static_assertions 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "c19be23126415861cb3a23e501d34a708f7f9b2183c5252d690941c2e69199d5" 296 | "checksum syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)" = "f92e629aa1d9c827b2bb8297046c1ccffc57c99b947a680d3ccff1f136a3bee9" 297 | "checksum tiny-keccak 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e9175261fbdb60781fcd388a4d6cc7e14764a2b629a7ad94abb439aed223a44f" 298 | "checksum uint 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "082df6964410f6aa929a61ddfafc997e4f32c62c22490e439ac351cec827f436" 299 | "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" 300 | "checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" 301 | "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" 302 | "checksum wee_alloc 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "27875be1daf838fa18f3e94fd19fd12638e34615b42f56da2610c8f46be80cc6" 303 | "checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" 304 | "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 305 | "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 306 | -------------------------------------------------------------------------------- /step-4/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "pwasm-tutorial-contract" 3 | version = "0.1.0" 4 | authors = ["Alexey Frolov "] 5 | 6 | [dependencies] 7 | pwasm-std = "0.13" 8 | pwasm-ethereum = "0.8" 9 | pwasm-abi = "0.2" 10 | pwasm-abi-derive = "0.2" 11 | lazy_static = { version = "1.2.0", features = ["spin_no_std"] } 12 | 13 | [lib] 14 | crate-type = ["cdylib"] 15 | 16 | [profile.release] 17 | panic = "abort" 18 | lto = true 19 | opt-level = "z" 20 | -------------------------------------------------------------------------------- /step-4/README.md: -------------------------------------------------------------------------------- 1 | ## Description 2 | The step-by-step project describes how to write a contract in Rust and compile in to Webassembly. 3 | 4 | ## Build 5 | Make sure you've installed [required tools](https://github.com/paritytech/pwasm-tutorial/blob/master/README.md#tutorial-prerequisites) 6 | ``` 7 | ./build.sh 8 | ``` 9 | As a result the `pwasm_tutorial_contract.wasm` should be created under the `step-4/target/wasm32-unknown-unknown/release/` directory. 10 | 11 | ## Deploy 12 | See https://github.com/fckt/pwasm-tutorial#deploy 13 | -------------------------------------------------------------------------------- /step-4/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cargo build --release --target wasm32-unknown-unknown 4 | wasm-build --target=wasm32-unknown-unknown ./target pwasm_tutorial_contract 5 | -------------------------------------------------------------------------------- /step-4/rust-toolchain: -------------------------------------------------------------------------------- 1 | nightly-2018-11-12 2 | -------------------------------------------------------------------------------- /step-4/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![allow(non_snake_case)] 3 | #![feature(proc_macro_hygiene)] 4 | 5 | extern crate pwasm_std; 6 | extern crate pwasm_ethereum; 7 | extern crate pwasm_abi; 8 | extern crate pwasm_abi_derive; 9 | 10 | #[macro_use] 11 | extern crate lazy_static; 12 | 13 | pub mod token { 14 | use pwasm_ethereum; 15 | use pwasm_abi::types::*; 16 | 17 | // eth_abi is a procedural macros https://doc.rust-lang.org/book/first-edition/procedural-macros.html 18 | use pwasm_abi_derive::eth_abi; 19 | 20 | lazy_static! { 21 | static ref TOTAL_SUPPLY_KEY: H256 = 22 | H256::from( 23 | [2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] 24 | ); 25 | } 26 | 27 | #[eth_abi(TokenEndpoint, TokenClient)] 28 | pub trait TokenInterface { 29 | /// The constructor 30 | fn constructor(&mut self, _total_supply: U256); 31 | /// Total amount of tokens 32 | #[constant] 33 | fn totalSupply(&mut self) -> U256; 34 | /// What is the balance of a particular account? 35 | #[constant] 36 | fn balanceOf(&mut self, _owner: Address) -> U256; 37 | /// Transfer the balance from owner's account to another account 38 | fn transfer(&mut self, _to: Address, _amount: U256) -> bool; 39 | /// Event declaration 40 | #[event] 41 | fn Transfer(&mut self, indexed_from: Address, indexed_to: Address, _value: U256); 42 | } 43 | 44 | pub struct TokenContract; 45 | 46 | impl TokenInterface for TokenContract { 47 | fn constructor(&mut self, total_supply: U256) { 48 | // Set up the total supply for the token 49 | pwasm_ethereum::write(&TOTAL_SUPPLY_KEY, &total_supply.into()); 50 | } 51 | 52 | fn totalSupply(&mut self) -> U256 { 53 | U256::from_big_endian(&pwasm_ethereum::read(&TOTAL_SUPPLY_KEY)) 54 | } 55 | 56 | fn balanceOf(&mut self, owner: Address) -> U256 { 57 | read_balance_of(&owner) 58 | } 59 | 60 | fn transfer(&mut self, to: Address, amount: U256) -> bool { 61 | let sender = pwasm_ethereum::sender(); 62 | let senderBalance = read_balance_of(&sender); 63 | let recipientBalance = read_balance_of(&to); 64 | if amount == 0.into() || senderBalance < amount || to == sender { 65 | false 66 | } else { 67 | let new_sender_balance = senderBalance - amount; 68 | let new_recipient_balance = recipientBalance + amount; 69 | pwasm_ethereum::write(&balance_key(&sender), &new_sender_balance.into()); 70 | pwasm_ethereum::write(&balance_key(&to), &new_recipient_balance.into()); 71 | self.Transfer(sender, to, amount); 72 | true 73 | } 74 | } 75 | } 76 | 77 | // Reads balance by address 78 | fn read_balance_of(owner: &Address) -> U256 { 79 | U256::from_big_endian(&pwasm_ethereum::read(&balance_key(owner))) 80 | } 81 | 82 | // Generates a balance key for some address. 83 | // Used to map balances with their owners. 84 | fn balance_key(address: &Address) -> H256 { 85 | let mut key = H256::from(*address); 86 | key.as_bytes_mut()[0] = 1; // just a naive "namespace"; 87 | key 88 | } 89 | } 90 | // Declares the dispatch and dispatch_ctor methods 91 | use pwasm_abi::eth::EndpointInterface; 92 | 93 | #[no_mangle] 94 | pub fn call() { 95 | let mut endpoint = token::TokenEndpoint::new(token::TokenContract{}); 96 | // Read http://solidity.readthedocs.io/en/develop/abi-spec.html#formal-specification-of-the-encoding for details 97 | pwasm_ethereum::ret(&endpoint.dispatch(&pwasm_ethereum::input())); 98 | } 99 | 100 | #[no_mangle] 101 | pub fn deploy() { 102 | let mut endpoint = token::TokenEndpoint::new(token::TokenContract{}); 103 | endpoint.dispatch_ctor(&pwasm_ethereum::input()); 104 | } 105 | -------------------------------------------------------------------------------- /step-5/.cargo/config: -------------------------------------------------------------------------------- 1 | [target.wasm32-unknown-unknown] 2 | rustflags = [ 3 | "-C", "link-args=-z stack-size=65536", 4 | ] 5 | -------------------------------------------------------------------------------- /step-5/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | /target/ 3 | **/*.rs.bk 4 | -------------------------------------------------------------------------------- /step-5/Cargo.lock: -------------------------------------------------------------------------------- 1 | [[package]] 2 | name = "bitflags" 3 | version = "1.0.4" 4 | source = "registry+https://github.com/rust-lang/crates.io-index" 5 | 6 | [[package]] 7 | name = "byteorder" 8 | version = "1.3.0" 9 | source = "registry+https://github.com/rust-lang/crates.io-index" 10 | 11 | [[package]] 12 | name = "cfg-if" 13 | version = "0.1.6" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | 16 | [[package]] 17 | name = "cloudabi" 18 | version = "0.0.3" 19 | source = "registry+https://github.com/rust-lang/crates.io-index" 20 | dependencies = [ 21 | "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", 22 | ] 23 | 24 | [[package]] 25 | name = "crunchy" 26 | version = "0.1.6" 27 | source = "registry+https://github.com/rust-lang/crates.io-index" 28 | 29 | [[package]] 30 | name = "crunchy" 31 | version = "0.2.1" 32 | source = "registry+https://github.com/rust-lang/crates.io-index" 33 | 34 | [[package]] 35 | name = "fixed-hash" 36 | version = "0.3.0" 37 | source = "registry+https://github.com/rust-lang/crates.io-index" 38 | dependencies = [ 39 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 40 | "rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", 41 | "rustc-hex 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 42 | "static_assertions 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", 43 | ] 44 | 45 | [[package]] 46 | name = "fuchsia-zircon" 47 | version = "0.3.3" 48 | source = "registry+https://github.com/rust-lang/crates.io-index" 49 | dependencies = [ 50 | "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", 51 | "fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", 52 | ] 53 | 54 | [[package]] 55 | name = "fuchsia-zircon-sys" 56 | version = "0.3.3" 57 | source = "registry+https://github.com/rust-lang/crates.io-index" 58 | 59 | [[package]] 60 | name = "itoa" 61 | version = "0.4.3" 62 | source = "registry+https://github.com/rust-lang/crates.io-index" 63 | 64 | [[package]] 65 | name = "lazy_static" 66 | version = "1.2.0" 67 | source = "registry+https://github.com/rust-lang/crates.io-index" 68 | dependencies = [ 69 | "spin 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", 70 | ] 71 | 72 | [[package]] 73 | name = "libc" 74 | version = "0.2.47" 75 | source = "registry+https://github.com/rust-lang/crates.io-index" 76 | 77 | [[package]] 78 | name = "memory_units" 79 | version = "0.4.0" 80 | source = "registry+https://github.com/rust-lang/crates.io-index" 81 | 82 | [[package]] 83 | name = "proc-macro2" 84 | version = "0.4.25" 85 | source = "registry+https://github.com/rust-lang/crates.io-index" 86 | dependencies = [ 87 | "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 88 | ] 89 | 90 | [[package]] 91 | name = "pwasm-abi" 92 | version = "0.2.1" 93 | source = "registry+https://github.com/rust-lang/crates.io-index" 94 | dependencies = [ 95 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 96 | "pwasm-std 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", 97 | ] 98 | 99 | [[package]] 100 | name = "pwasm-abi-derive" 101 | version = "0.2.1" 102 | source = "registry+https://github.com/rust-lang/crates.io-index" 103 | dependencies = [ 104 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 105 | "fixed-hash 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 106 | "proc-macro2 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)", 107 | "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", 108 | "serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)", 109 | "serde_derive 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)", 110 | "serde_json 1.0.36 (registry+https://github.com/rust-lang/crates.io-index)", 111 | "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", 112 | "tiny-keccak 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)", 113 | ] 114 | 115 | [[package]] 116 | name = "pwasm-alloc" 117 | version = "0.4.0" 118 | source = "registry+https://github.com/rust-lang/crates.io-index" 119 | dependencies = [ 120 | "wee_alloc 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", 121 | ] 122 | 123 | [[package]] 124 | name = "pwasm-ethereum" 125 | version = "0.8.0" 126 | source = "registry+https://github.com/rust-lang/crates.io-index" 127 | dependencies = [ 128 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 129 | "pwasm-std 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", 130 | ] 131 | 132 | [[package]] 133 | name = "pwasm-libc" 134 | version = "0.2.0" 135 | source = "registry+https://github.com/rust-lang/crates.io-index" 136 | dependencies = [ 137 | "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", 138 | "rlibc 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", 139 | ] 140 | 141 | [[package]] 142 | name = "pwasm-std" 143 | version = "0.13.0" 144 | source = "registry+https://github.com/rust-lang/crates.io-index" 145 | dependencies = [ 146 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 147 | "fixed-hash 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 148 | "pwasm-alloc 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 149 | "pwasm-libc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 150 | "tiny-keccak 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)", 151 | "uint 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 152 | ] 153 | 154 | [[package]] 155 | name = "pwasm-test" 156 | version = "0.1.1" 157 | source = "git+https://github.com/paritytech/pwasm-test#6e621660d1513185fecf6dac5ec2fc8758ff4797" 158 | dependencies = [ 159 | "pwasm-abi 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 160 | "pwasm-ethereum 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", 161 | "pwasm-std 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", 162 | ] 163 | 164 | [[package]] 165 | name = "pwasm-tutorial-contract" 166 | version = "0.1.0" 167 | dependencies = [ 168 | "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 169 | "pwasm-abi 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 170 | "pwasm-abi-derive 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 171 | "pwasm-ethereum 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", 172 | "pwasm-std 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", 173 | "pwasm-test 0.1.1 (git+https://github.com/paritytech/pwasm-test)", 174 | ] 175 | 176 | [[package]] 177 | name = "quote" 178 | version = "0.6.10" 179 | source = "registry+https://github.com/rust-lang/crates.io-index" 180 | dependencies = [ 181 | "proc-macro2 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)", 182 | ] 183 | 184 | [[package]] 185 | name = "rand" 186 | version = "0.5.5" 187 | source = "registry+https://github.com/rust-lang/crates.io-index" 188 | dependencies = [ 189 | "cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", 190 | "fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", 191 | "libc 0.2.47 (registry+https://github.com/rust-lang/crates.io-index)", 192 | "rand_core 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 193 | "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 194 | ] 195 | 196 | [[package]] 197 | name = "rand_core" 198 | version = "0.2.2" 199 | source = "registry+https://github.com/rust-lang/crates.io-index" 200 | dependencies = [ 201 | "rand_core 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 202 | ] 203 | 204 | [[package]] 205 | name = "rand_core" 206 | version = "0.3.0" 207 | source = "registry+https://github.com/rust-lang/crates.io-index" 208 | 209 | [[package]] 210 | name = "rlibc" 211 | version = "1.0.0" 212 | source = "registry+https://github.com/rust-lang/crates.io-index" 213 | 214 | [[package]] 215 | name = "rustc-hex" 216 | version = "2.0.1" 217 | source = "registry+https://github.com/rust-lang/crates.io-index" 218 | 219 | [[package]] 220 | name = "ryu" 221 | version = "0.2.7" 222 | source = "registry+https://github.com/rust-lang/crates.io-index" 223 | 224 | [[package]] 225 | name = "serde" 226 | version = "1.0.85" 227 | source = "registry+https://github.com/rust-lang/crates.io-index" 228 | 229 | [[package]] 230 | name = "serde_derive" 231 | version = "1.0.85" 232 | source = "registry+https://github.com/rust-lang/crates.io-index" 233 | dependencies = [ 234 | "proc-macro2 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)", 235 | "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", 236 | "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", 237 | ] 238 | 239 | [[package]] 240 | name = "serde_json" 241 | version = "1.0.36" 242 | source = "registry+https://github.com/rust-lang/crates.io-index" 243 | dependencies = [ 244 | "itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", 245 | "ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", 246 | "serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)", 247 | ] 248 | 249 | [[package]] 250 | name = "spin" 251 | version = "0.4.10" 252 | source = "registry+https://github.com/rust-lang/crates.io-index" 253 | 254 | [[package]] 255 | name = "static_assertions" 256 | version = "0.2.5" 257 | source = "registry+https://github.com/rust-lang/crates.io-index" 258 | 259 | [[package]] 260 | name = "syn" 261 | version = "0.15.26" 262 | source = "registry+https://github.com/rust-lang/crates.io-index" 263 | dependencies = [ 264 | "proc-macro2 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)", 265 | "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", 266 | "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 267 | ] 268 | 269 | [[package]] 270 | name = "tiny-keccak" 271 | version = "1.4.2" 272 | source = "registry+https://github.com/rust-lang/crates.io-index" 273 | dependencies = [ 274 | "crunchy 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", 275 | ] 276 | 277 | [[package]] 278 | name = "uint" 279 | version = "0.5.0" 280 | source = "registry+https://github.com/rust-lang/crates.io-index" 281 | dependencies = [ 282 | "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 283 | "crunchy 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 284 | "rustc-hex 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 285 | ] 286 | 287 | [[package]] 288 | name = "unicode-xid" 289 | version = "0.1.0" 290 | source = "registry+https://github.com/rust-lang/crates.io-index" 291 | 292 | [[package]] 293 | name = "unreachable" 294 | version = "1.0.0" 295 | source = "registry+https://github.com/rust-lang/crates.io-index" 296 | dependencies = [ 297 | "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", 298 | ] 299 | 300 | [[package]] 301 | name = "void" 302 | version = "1.0.2" 303 | source = "registry+https://github.com/rust-lang/crates.io-index" 304 | 305 | [[package]] 306 | name = "wee_alloc" 307 | version = "0.4.2" 308 | source = "registry+https://github.com/rust-lang/crates.io-index" 309 | dependencies = [ 310 | "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", 311 | "libc 0.2.47 (registry+https://github.com/rust-lang/crates.io-index)", 312 | "memory_units 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 313 | "unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", 314 | "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 315 | ] 316 | 317 | [[package]] 318 | name = "winapi" 319 | version = "0.3.6" 320 | source = "registry+https://github.com/rust-lang/crates.io-index" 321 | dependencies = [ 322 | "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 323 | "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 324 | ] 325 | 326 | [[package]] 327 | name = "winapi-i686-pc-windows-gnu" 328 | version = "0.4.0" 329 | source = "registry+https://github.com/rust-lang/crates.io-index" 330 | 331 | [[package]] 332 | name = "winapi-x86_64-pc-windows-gnu" 333 | version = "0.4.0" 334 | source = "registry+https://github.com/rust-lang/crates.io-index" 335 | 336 | [metadata] 337 | "checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" 338 | "checksum byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "60f0b0d4c0a382d2734228fd12b5a6b5dac185c60e938026fd31b265b94f9bd2" 339 | "checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4" 340 | "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" 341 | "checksum crunchy 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "a2f4a431c5c9f662e1200b7c7f02c34e91361150e382089a8f2dec3ba680cbda" 342 | "checksum crunchy 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c240f247c278fa08a6d4820a6a222bfc6e0d999e51ba67be94f44c905b2161f2" 343 | "checksum fixed-hash 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a557e80084b05c32b455963ff565a9de6f2866da023d6671705c6aff6f65e01c" 344 | "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" 345 | "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" 346 | "checksum itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b" 347 | "checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1" 348 | "checksum libc 0.2.47 (registry+https://github.com/rust-lang/crates.io-index)" = "48450664a984b25d5b479554c29cc04e3150c97aa4c01da5604a2d4ed9151476" 349 | "checksum memory_units 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3" 350 | "checksum proc-macro2 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)" = "d3797b7142c9aa74954e351fc089bbee7958cebbff6bf2815e7ffff0b19f547d" 351 | "checksum pwasm-abi 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "553291f59e0650074286e6270fbfae05cbce429c5d89a27ec31294d483c8288b" 352 | "checksum pwasm-abi-derive 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7220ad898fa15739494ca16131a638ef1cc018ff490513b9130cae17d5238bd3" 353 | "checksum pwasm-alloc 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bf268e76f224753679a7cb6e0261c9226676c83cd4d2b6ead2095524f00c854f" 354 | "checksum pwasm-ethereum 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8b5509e67ee47ec62bdbd1c32a6949931adbe2e12a460bb45e5dc46593fdc71c" 355 | "checksum pwasm-libc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "027307d6d2086ffb7f399227d42593c5341e8869a55ac8dd7c79d7ca04ec00e2" 356 | "checksum pwasm-std 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "75e907e3e458b07ed7d92473e8041c12928d196af1a8e441c2352d13e7243e8d" 357 | "checksum pwasm-test 0.1.1 (git+https://github.com/paritytech/pwasm-test)" = "" 358 | "checksum quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "53fa22a1994bd0f9372d7a816207d8a2677ad0325b073f5c5332760f0fb62b5c" 359 | "checksum rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e464cd887e869cddcae8792a4ee31d23c7edd516700695608f5b98c67ee0131c" 360 | "checksum rand_core 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1961a422c4d189dfb50ffa9320bf1f2a9bd54ecb92792fb9477f99a1045f3372" 361 | "checksum rand_core 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0905b6b7079ec73b314d4c748701f6931eb79fd97c668caa3f1899b22b32c6db" 362 | "checksum rlibc 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc874b127765f014d792f16763a81245ab80500e2ad921ed4ee9e82481ee08fe" 363 | "checksum rustc-hex 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "403bb3a286107a04825a5f82e1270acc1e14028d3d554d7a1e08914549575ab8" 364 | "checksum ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "eb9e9b8cde282a9fe6a42dd4681319bfb63f121b8a8ee9439c6f4107e58a46f7" 365 | "checksum serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)" = "534b8b91a95e0f71bca3ed5824752d558da048d4248c91af873b63bd60519752" 366 | "checksum serde_derive 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)" = "a915306b0f1ac5607797697148c223bedeaa36bcc2e28a01441cd638cc6567b4" 367 | "checksum serde_json 1.0.36 (registry+https://github.com/rust-lang/crates.io-index)" = "574378d957d6dcdf1bbb5d562a15cbd5e644159432f84634b94e485267abbcc7" 368 | "checksum spin 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "ceac490aa12c567115b40b7b7fceca03a6c9d53d5defea066123debc83c5dc1f" 369 | "checksum static_assertions 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "c19be23126415861cb3a23e501d34a708f7f9b2183c5252d690941c2e69199d5" 370 | "checksum syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)" = "f92e629aa1d9c827b2bb8297046c1ccffc57c99b947a680d3ccff1f136a3bee9" 371 | "checksum tiny-keccak 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e9175261fbdb60781fcd388a4d6cc7e14764a2b629a7ad94abb439aed223a44f" 372 | "checksum uint 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "082df6964410f6aa929a61ddfafc997e4f32c62c22490e439ac351cec827f436" 373 | "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" 374 | "checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" 375 | "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" 376 | "checksum wee_alloc 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "27875be1daf838fa18f3e94fd19fd12638e34615b42f56da2610c8f46be80cc6" 377 | "checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" 378 | "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 379 | "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 380 | -------------------------------------------------------------------------------- /step-5/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "pwasm-tutorial-contract" 3 | version = "0.1.0" 4 | authors = ["Alexey Frolov "] 5 | 6 | [dependencies] 7 | pwasm-std = "0.13" 8 | pwasm-ethereum = "0.8" 9 | pwasm-abi = "0.2" 10 | pwasm-abi-derive = "0.2" 11 | lazy_static = { version = "1.2.0", features = ["spin_no_std"] } 12 | 13 | [dev-dependencies.pwasm-test] 14 | git = "https://github.com/paritytech/pwasm-test" 15 | default-features = false 16 | 17 | [lib] 18 | crate-type = ["cdylib"] 19 | 20 | [features] 21 | std = ["pwasm-std/std", "pwasm-ethereum/std", "pwasm-test/std"] 22 | 23 | [profile.release] 24 | panic = "abort" 25 | lto = true 26 | opt-level = "z" 27 | -------------------------------------------------------------------------------- /step-5/README.md: -------------------------------------------------------------------------------- 1 | ## Description 2 | The step-by-step project describes how to write a contract in Rust and compile in to Webassembly. 3 | 4 | ## Build 5 | Make sure you've installed [required tools](https://github.com/paritytech/pwasm-tutorial/blob/master/README.md#tutorial-prerequisites) 6 | ``` 7 | ./build.sh 8 | ``` 9 | As a result the `pwasm_tutorial_contract.wasm` should be created under the `step-5/target/wasm32-unknown-unknown/release/` directory. 10 | 11 | ## Deploy 12 | See https://github.com/fckt/pwasm-tutorial#deploy 13 | 14 | ## Test 15 | ``` 16 | cargo test --features std 17 | ``` 18 | -------------------------------------------------------------------------------- /step-5/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cargo build --release --target wasm32-unknown-unknown 4 | wasm-build --target=wasm32-unknown-unknown ./target pwasm_tutorial_contract 5 | -------------------------------------------------------------------------------- /step-5/rust-toolchain: -------------------------------------------------------------------------------- 1 | nightly-2018-11-12 2 | -------------------------------------------------------------------------------- /step-5/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![allow(non_snake_case)] 3 | #![feature(proc_macro_hygiene)] 4 | 5 | extern crate pwasm_std; 6 | extern crate pwasm_ethereum; 7 | extern crate pwasm_abi; 8 | extern crate pwasm_abi_derive; 9 | 10 | pub mod token { 11 | use pwasm_ethereum; 12 | use pwasm_abi::types::*; 13 | 14 | // eth_abi is a procedural macros https://doc.rust-lang.org/book/first-edition/procedural-macros.html 15 | use pwasm_abi_derive::eth_abi; 16 | 17 | lazy_static::lazy_static! { 18 | static ref TOTAL_SUPPLY_KEY: H256 = 19 | H256::from( 20 | [2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] 21 | ); 22 | static ref OWNER_KEY: H256 = 23 | H256::from( 24 | [3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] 25 | ); 26 | } 27 | 28 | #[eth_abi(TokenEndpoint, TokenClient)] 29 | pub trait TokenInterface { 30 | /// The constructor 31 | fn constructor(&mut self, _total_supply: U256); 32 | /// Total amount of tokens 33 | #[constant] 34 | fn totalSupply(&mut self) -> U256; 35 | /// What is the balance of a particular account? 36 | #[constant] 37 | fn balanceOf(&mut self, _owner: Address) -> U256; 38 | /// Transfer the balance from owner's account to another account 39 | fn transfer(&mut self, _to: Address, _amount: U256) -> bool; 40 | /// Event declaration 41 | #[event] 42 | fn Transfer(&mut self, indexed_from: Address, indexed_to: Address, _value: U256); 43 | } 44 | 45 | pub struct TokenContract; 46 | 47 | impl TokenInterface for TokenContract { 48 | fn constructor(&mut self, total_supply: U256) { 49 | let sender = pwasm_ethereum::sender(); 50 | // Set up the total supply for the token 51 | pwasm_ethereum::write(&TOTAL_SUPPLY_KEY, &total_supply.into()); 52 | // Give all tokens to the contract owner 53 | pwasm_ethereum::write(&balance_key(&sender), &total_supply.into()); 54 | // Set the contract owner 55 | pwasm_ethereum::write(&OWNER_KEY, &H256::from(sender).into()); 56 | } 57 | 58 | fn totalSupply(&mut self) -> U256 { 59 | U256::from_big_endian(&pwasm_ethereum::read(&TOTAL_SUPPLY_KEY)) 60 | } 61 | 62 | fn balanceOf(&mut self, owner: Address) -> U256 { 63 | read_balance_of(&owner) 64 | } 65 | 66 | fn transfer(&mut self, to: Address, amount: U256) -> bool { 67 | let sender = pwasm_ethereum::sender(); 68 | let senderBalance = read_balance_of(&sender); 69 | let recipientBalance = read_balance_of(&to); 70 | if amount == 0.into() || senderBalance < amount || to == sender { 71 | false 72 | } else { 73 | let new_sender_balance = senderBalance - amount; 74 | let new_recipient_balance = recipientBalance + amount; 75 | pwasm_ethereum::write(&balance_key(&sender), &new_sender_balance.into()); 76 | pwasm_ethereum::write(&balance_key(&to), &new_recipient_balance.into()); 77 | self.Transfer(sender, to, amount); 78 | true 79 | } 80 | } 81 | } 82 | 83 | // Reads balance by address 84 | fn read_balance_of(owner: &Address) -> U256 { 85 | U256::from_big_endian(&pwasm_ethereum::read(&balance_key(owner))) 86 | } 87 | 88 | // Generates a balance key for some address. 89 | // Used to map balances with their owners. 90 | fn balance_key(address: &Address) -> H256 { 91 | let mut key = H256::from(*address); 92 | key.as_bytes_mut()[0] = 1; // just a naive "namespace"; 93 | key 94 | } 95 | } 96 | // Declares the dispatch and dispatch_ctor methods 97 | use pwasm_abi::eth::EndpointInterface; 98 | 99 | #[no_mangle] 100 | pub fn call() { 101 | let mut endpoint = token::TokenEndpoint::new(token::TokenContract{}); 102 | // Read http://solidity.readthedocs.io/en/develop/abi-spec.html#formal-specification-of-the-encoding for details 103 | pwasm_ethereum::ret(&endpoint.dispatch(&pwasm_ethereum::input())); 104 | } 105 | 106 | #[no_mangle] 107 | pub fn deploy() { 108 | let mut endpoint = token::TokenEndpoint::new(token::TokenContract{}); 109 | endpoint.dispatch_ctor(&pwasm_ethereum::input()); 110 | } 111 | 112 | #[cfg(test)] 113 | #[allow(non_snake_case)] 114 | mod tests { 115 | extern crate pwasm_test; 116 | extern crate std; 117 | use super::*; 118 | use core::str::FromStr; 119 | use pwasm_abi::types::*; 120 | use self::pwasm_test::{ext_reset, ext_get}; 121 | use token::TokenInterface; 122 | 123 | #[test] 124 | fn should_succeed_transfering_1000_from_owner_to_another_address() { 125 | let mut contract = token::TokenContract{}; 126 | let owner_address = Address::from_str("ea674fdde714fd979de3edf0f56aa9716b898ec8").unwrap(); 127 | let sam_address = Address::from_str("db6fd484cfa46eeeb73c71edee823e4812f9e2e1").unwrap(); 128 | // Here we're creating an External context using ExternalBuilder and set the `sender` to the `owner_address` 129 | // so `pwasm_ethereum::sender()` in TokenInterface::constructor() will return that `owner_address` 130 | ext_reset(|e| e.sender(owner_address.clone())); 131 | let total_supply = 10000.into(); 132 | contract.constructor(total_supply); 133 | assert_eq!(contract.balanceOf(owner_address), total_supply); 134 | assert_eq!(contract.transfer(sam_address, 1000.into()), true); 135 | assert_eq!(contract.balanceOf(owner_address), 9000.into()); 136 | assert_eq!(contract.balanceOf(sam_address), 1000.into()); 137 | // 1 log entry should be created 138 | assert_eq!(ext_get().logs().len(), 1); 139 | } 140 | 141 | #[test] 142 | fn should_not_transfer_to_self() { 143 | let mut contract = token::TokenContract{}; 144 | let owner_address = Address::from_str("ea674fdde714fd979de3edf0f56aa9716b898ec8").unwrap(); 145 | ext_reset(|e| e.sender(owner_address.clone())); 146 | let total_supply = 10000.into(); 147 | contract.constructor(total_supply); 148 | assert_eq!(contract.balanceOf(owner_address), total_supply); 149 | assert_eq!(contract.transfer(owner_address, 1000.into()), false); 150 | assert_eq!(contract.balanceOf(owner_address), 10000.into()); 151 | assert_eq!(ext_get().logs().len(), 0); 152 | } 153 | 154 | } 155 | -------------------------------------------------------------------------------- /wasm-dev-chain.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "DevelopmentChain", 3 | "engine": { 4 | "instantSeal": null 5 | }, 6 | "params": { 7 | "wasmActivationTransition": "0x01", 8 | "eip658Transition": "0x01", 9 | "gasLimitBoundDivisor": "0x0400", 10 | "accountStartNonce": "0x0", 11 | "maximumExtraDataSize": "0x20", 12 | "minGasLimit": "0x1388", 13 | "networkID" : "0x11", 14 | "maxCodeSize": 1000000, 15 | "maxCodeSizeTransition": 1 16 | }, 17 | "genesis": { 18 | "seal": { 19 | "generic": "0x0" 20 | }, 21 | "difficulty": "0x20000", 22 | "author": "0x0000000000000000000000000000000000000000", 23 | "timestamp": "0x00", 24 | "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", 25 | "extraData": "0x", 26 | "gasLimit": "0x5B8D8000" 27 | }, 28 | "accounts": { 29 | "0000000000000000000000000000000000000001": { "balance": "1", "builtin": { "name": "ecrecover", "pricing": { "linear": { "base": 3000, "word": 0 } } } }, 30 | "0000000000000000000000000000000000000002": { "balance": "1", "builtin": { "name": "sha256", "pricing": { "linear": { "base": 60, "word": 12 } } } }, 31 | "0000000000000000000000000000000000000003": { "balance": "1", "builtin": { "name": "ripemd160", "pricing": { "linear": { "base": 600, "word": 120 } } } }, 32 | "0000000000000000000000000000000000000004": { "balance": "1", "builtin": { "name": "identity", "pricing": { "linear": { "base": 15, "word": 3 } } } }, 33 | "0x004ec07d2329997267ec62b4166639513386f32e": { "balance": "1606938044258990275541962092341162602522202993782792835301376" } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /wasm-install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # this script is intended to be used from .travis.yml 4 | 5 | curl -sL https://storage.googleapis.com/wasm-llvm/builds/linux/31834/wasm-binaries.tbz2 | tar xvkj 6 | --------------------------------------------------------------------------------