{"id":19157,"date":"2026-04-09T22:00:06","date_gmt":"2026-04-09T22:00:06","guid":{"rendered":"https:\/\/cryptoted.net\/index.php\/2026\/04\/09\/pyethereum-and-serpent-programming-guide\/"},"modified":"2026-04-09T22:00:06","modified_gmt":"2026-04-09T22:00:06","slug":"pyethereum-and-serpent-programming-guide","status":"publish","type":"post","link":"https:\/\/cryptoted.net\/index.php\/2026\/04\/09\/pyethereum-and-serpent-programming-guide\/","title":{"rendered":"Pyethereum and Serpent Programming Guide"},"content":{"rendered":"<p> <br \/>\n<\/p>\n<div id=\"\">\n<p class=\"chakra-text css-gi02ar\"><i>The content of this tutorial is intended to apply to PoC5. Most of the instructions given below will not work in the older PoC4 implementations of AlethZero (C++) and Ethereal (Go)<\/i><\/p>\n<p class=\"chakra-text css-gi02ar\">Over the last few weeks, we have made a large number of changes to the Ethereum protocol. POC4, introducing a large body of changes made by Gavin Wood and myself, was <a class=\"chakra-link css-vezwxf\" href=\"https:\/\/blog.ethereum.org\/2014\/03\/20\/the-latest-evm-ethereum-is-a-trust-free-closure-system\">announced as an informal description<\/a> two weeks ago, and has been formally specified in Gavin Wood\u2019s \u201cyellow paper\u201d at <a target=\"_blank\" rel=\"noopener\" class=\"chakra-link css-vezwxf\" href=\"http:\/\/gavwood.com\/Paper.pdf\"\/><a target=\"_blank\" rel=\"noopener\" class=\"chakra-link css-vezwxf\" href=\"http:\/\/gavwood.com\/Paper.pdf\">http:\/\/gavwood.com\/Paper.pdf<\/a>. The protocol spec did change substantially, but at the same time things are solidifying; we know why we want transactions to pay fees instead of contracts, so that\u2019s not likely to change, we know that code and data will be separate, and the byte-based code and memory and 32-byte-block-based stack and storage are unlikely to change, and we know that the workings of the EVM in general will be similar to what they are now instead of some kind of elaborate Merkle-code-tree construction. POC4 has given myself what I wanted out of <a class=\"chakra-link css-vezwxf\" href=\"https:\/\/blog.ethereum.org\/2014\/02\/03\/introducing-ethereum-script-2-0\">Ethereum Script 2<\/a>, Gavin a much more optimization-friendly VM architecture, and users a <a target=\"_blank\" rel=\"noopener\" class=\"chakra-link css-vezwxf\" href=\"https:\/\/github.com\/ethereum\/cpp-ethereum\/wiki\/LLL-Examples-for-PoC-4\">shiny new currency<\/a>. Meanwhile, Chen Houwu, Heiko Kees and Konrad Feldmeier have taken the lead as our main Python developers, and the networking side of the pyethereum client is getting to the point where it is getting ready to talk to Go and C++. At the same time, aside from all of the managerial tasks that are part and parcel of having a key role in a large project, I have taken it upon myself to bring up to speed the pyethereum VM implementation and the compiler for the HLL programming language.<\/p>\n<p class=\"chakra-text css-gi02ar\">The purpose of this post will be to provide an in-depth technical tutorial into the workings of pyethereum and Serpent, and show you how you can start writing the tools to build your own contracts and applications. The Bitcoin Expo hackathon is happening today and tomorrow, so feel free to make an Ethereum contract your project if you are among those attending.<\/p>\n<p class=\"chakra-text css-gi02ar\">First of all, importantly, HLL is no longer called HLL; the language is now called Serpent. Why? Because it\u2019s basically Python.<\/p>\n<p class=\"chakra-text css-gi02ar\">With recent upgrades to the compiler, Serpent is now a highly feature-filled programming language, with powerful features including:<\/p>\n<ul role=\"list\" class=\"css-1ars4k6\">\n<li class=\"css-0\">Arrays (eg. x[0] = 123)<\/li>\n<li class=\"css-0\">Array literals (eg. x = [ 34, 56, 78 ])<\/li>\n<li class=\"css-0\">Nested arrays (eg. z = [ 34, [ 5, 6 ], y ])<\/li>\n<li class=\"css-0\">Hex support (eg. receiving_address = 0xb156066c2978d7b9188f2467b815d4c62ae32fe2)<\/li>\n<li class=\"css-0\">String support (eg. x = &#8220;cow&#8221;)<\/li>\n<li class=\"css-0\">Inline message calling (eg. usdprice = eth * msg(ethcontract,0,tx.gas-100,[500],1))<\/li>\n<li class=\"css-0\">Out of line message calling (eg. msg(multifeedcontract,0,tx.gas-100,inparray,5,outarray,5))<\/li>\n<li class=\"css-0\">Simple value sending operation (eg. send(receiver, value, tx.gas-100))<\/li>\n<li class=\"css-0\">Returning values (eg. return(45) and return([10,20,30,40],4))<\/li>\n<li class=\"css-0\">Treating message data and storage as arrays (eg. contract.storage[1000] = msg.data[0])<\/li>\n<li class=\"css-0\">Byte arrays (eg. x = bytes(100), setch(x,45,&#8221;c&#8221;)), y = getch(x,45)<\/li>\n<\/ul>\n<p class=\"chakra-text css-gi02ar\">The intent of the Serpent language is to make programming smart contracts and decetralized applications in Ethereum as easy as programming boring command line apps is in Python. The language is designed to be maximally clean and maximally simple, combining the benefits of a compiled language with an easy-to-use coding experience. Just the logic, and nothing but the logic. Unfortunately, floating point numbers are missing, as are higher-order constructs like list comprehensions and closures, but aside from that Serpent has basically everything that you need.<\/p>\n<h3 class=\"chakra-heading group css-xuzltg\" id=\"getting-started\" data-group=\"true\"><a class=\"chakra-link css-128fqrf\" aria-label=\"getting started permalink\" href=\"#getting-started\"><svg viewbox=\"0 0 24 24\" focusable=\"false\" class=\"chakra-icon css-173jpr1\"><g fill=\"currentColor\"><path d=\"M10.458,18.374,7.721,21.11a2.853,2.853,0,0,1-3.942,0l-.892-.891a2.787,2.787,0,0,1,0-3.941l5.8-5.8a2.789,2.789,0,0,1,3.942,0l.893.892A1,1,0,0,0,14.94,9.952l-.893-.892a4.791,4.791,0,0,0-6.771,0l-5.8,5.8a4.787,4.787,0,0,0,0,6.77l.892.891a4.785,4.785,0,0,0,6.771,0l2.736-2.735a1,1,0,1,0-1.414-1.415Z\"\/><path d=\"M22.526,2.363l-.892-.892a4.8,4.8,0,0,0-6.77,0l-2.905,2.9a1,1,0,0,0,1.414,1.414l2.9-2.9a2.79,2.79,0,0,1,3.941,0l.893.893a2.786,2.786,0,0,1,0,3.942l-5.8,5.8a2.769,2.769,0,0,1-1.971.817h0a2.766,2.766,0,0,1-1.969-.816,1,1,0,1,0-1.415,1.412,4.751,4.751,0,0,0,3.384,1.4h0a4.752,4.752,0,0,0,3.385-1.4l5.8-5.8a4.786,4.786,0,0,0,0-6.771Z\"\/><\/g><\/svg><\/a>Getting Started<\/h3>\n<p class=\"chakra-text css-gi02ar\">So how do you code in Serpent? The first step is to set up the development and execution environment. To do this, first download two libraries: <a target=\"_blank\" rel=\"noopener\" class=\"chakra-link css-vezwxf\" href=\"http:\/\/github.com\/ethereum\/pyethereum\">pyethereum<\/a> and <a target=\"_blank\" rel=\"noopener\" class=\"chakra-link css-vezwxf\" href=\"http:\/\/github.com\/ethereum\/pyethereum\">serpent<\/a>. The simplest way to download is to either download the zip files from Github and unpack them, or run git clone <a target=\"_blank\" rel=\"noopener\" class=\"chakra-link css-vezwxf\" href=\"http:\/\/github.com\/ethereum\/pyethereum\"\/><a target=\"_blank\" rel=\"noopener\" class=\"chakra-link css-vezwxf\" href=\"http:\/\/github.com\/ethereum\/pyethereum\">http:\/\/github.com\/ethereum\/pyethereum<\/a> and git clone<a target=\"_blank\" rel=\"noopener\" class=\"chakra-link css-vezwxf\" href=\"http:\/\/github.com\/ethereum\/serpent\"\/><a target=\"_blank\" rel=\"noopener\" class=\"chakra-link css-vezwxf\" href=\"http:\/\/github.com\/ethereum\/serpent\">http:\/\/github.com\/ethereum\/serpent<\/a>. Then, enter the pyethereum directory, and run sudo python setup.py install to install pyethereum to your system, and do the same with serpent.<\/p>\n<p class=\"chakra-text css-gi02ar\">Now that the software is downloaded, let\u2019s get right to it. To start off, try this:<\/p>\n<p class=\"chakra-text css-gi02ar\">\u00a0<\/p>\n<blockquote class=\"chakra-code css-gk0tva\">\n<p class=\"chakra-text css-gi02ar\">serpent compile_to_assembly &#8216;x = 5&#8217;<\/p>\n<\/blockquote>\n<p class=\"chakra-text css-gi02ar\">[&#8220;<span class=\"math math-inline\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\"><semantics><mrow><mi>b<\/mi><mi>e<\/mi><mi>g<\/mi><mi>i<\/mi><mi>n<\/mi><mi>c<\/mi><mi>o<\/mi><mi>d<\/mi><msub><mi>e<\/mi><mn>0<\/mn><\/msub><mi mathvariant=\"normal\">.<\/mi><mi>e<\/mi><mi>n<\/mi><mi>d<\/mi><mi>c<\/mi><mi>o<\/mi><mi>d<\/mi><msub><mi>e<\/mi><mn>0<\/mn><\/msub><mi mathvariant=\"normal\">&#8220;<\/mi><mo separator=\"true\">,<\/mo><mi mathvariant=\"normal\">&#8220;<\/mi><mi>D<\/mi><mi>U<\/mi><mi>P<\/mi><mi mathvariant=\"normal\">&#8220;<\/mi><mo separator=\"true\">,<\/mo><mi mathvariant=\"normal\">&#8220;<\/mi><mi>M<\/mi><mi>S<\/mi><mi>I<\/mi><mi>Z<\/mi><mi>E<\/mi><mi mathvariant=\"normal\">&#8220;<\/mi><mo separator=\"true\">,<\/mo><mi mathvariant=\"normal\">&#8220;<\/mi><mi>S<\/mi><mi>W<\/mi><mi>A<\/mi><mi>P<\/mi><mi mathvariant=\"normal\">&#8220;<\/mi><mo separator=\"true\">,<\/mo><mi mathvariant=\"normal\">&#8220;<\/mi><mi>M<\/mi><mi>S<\/mi><mi>I<\/mi><mi>Z<\/mi><mi>E<\/mi><mi mathvariant=\"normal\">&#8220;<\/mi><mo separator=\"true\">,<\/mo><mi mathvariant=\"normal\">&#8220;<\/mi><\/mrow><annotation encoding=\"application\/x-tex\">begincode_0.endcode_0&#8243;, &#8220;DUP&#8221;, &#8220;MSIZE&#8221;, &#8220;SWAP&#8221;, &#8220;MSIZE&#8221;, &#8220;<\/annotation><\/semantics><\/math><\/span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.8889em;vertical-align:-0.1944em\"\/><span class=\"mord mathnormal\">b<\/span><span class=\"mord mathnormal\">e<\/span><span class=\"mord mathnormal\" style=\"margin-right:0.03588em\">g<\/span><span class=\"mord mathnormal\">in<\/span><span class=\"mord mathnormal\">co<\/span><span class=\"mord mathnormal\">d<\/span><span class=\"mord\"><span class=\"mord mathnormal\">e<\/span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em\"><span class=\"pstrut\" style=\"height:2.7em\"\/><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">0<\/span><\/span><\/span><\/span><span class=\"vlist-s\">\u200b<\/span><\/span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em\"><span\/><\/span><\/span><\/span><\/span><\/span><span class=\"mord\">.<\/span><span class=\"mord mathnormal\">e<\/span><span class=\"mord mathnormal\">n<\/span><span class=\"mord mathnormal\">d<\/span><span class=\"mord mathnormal\">co<\/span><span class=\"mord mathnormal\">d<\/span><span class=\"mord\"><span class=\"mord mathnormal\">e<\/span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em\"><span class=\"pstrut\" style=\"height:2.7em\"\/><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">0<\/span><\/span><\/span><\/span><span class=\"vlist-s\">\u200b<\/span><\/span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em\"><span\/><\/span><\/span><\/span><\/span><\/span><span class=\"mord\">&#8220;<\/span><span class=\"mpunct\">,<\/span><span class=\"mspace\" style=\"margin-right:0.1667em\"\/><span class=\"mord\">&#8220;<\/span><span class=\"mord mathnormal\" style=\"margin-right:0.02778em\">D<\/span><span class=\"mord mathnormal\" style=\"margin-right:0.10903em\">U<\/span><span class=\"mord mathnormal\" style=\"margin-right:0.13889em\">P<\/span><span class=\"mord\">&#8220;<\/span><span class=\"mpunct\">,<\/span><span class=\"mspace\" style=\"margin-right:0.1667em\"\/><span class=\"mord\">&#8220;<\/span><span class=\"mord mathnormal\" style=\"margin-right:0.05764em\">MS<\/span><span class=\"mord mathnormal\" style=\"margin-right:0.07847em\">I<\/span><span class=\"mord mathnormal\" style=\"margin-right:0.05764em\">ZE<\/span><span class=\"mord\">&#8220;<\/span><span class=\"mpunct\">,<\/span><span class=\"mspace\" style=\"margin-right:0.1667em\"\/><span class=\"mord\">&#8220;<\/span><span class=\"mord mathnormal\" style=\"margin-right:0.05764em\">S<\/span><span class=\"mord mathnormal\" style=\"margin-right:0.13889em\">W<\/span><span class=\"mord mathnormal\">A<\/span><span class=\"mord mathnormal\" style=\"margin-right:0.13889em\">P<\/span><span class=\"mord\">&#8220;<\/span><span class=\"mpunct\">,<\/span><span class=\"mspace\" style=\"margin-right:0.1667em\"\/><span class=\"mord\">&#8220;<\/span><span class=\"mord mathnormal\" style=\"margin-right:0.05764em\">MS<\/span><span class=\"mord mathnormal\" style=\"margin-right:0.07847em\">I<\/span><span class=\"mord mathnormal\" style=\"margin-right:0.05764em\">ZE<\/span><span class=\"mord\">&#8220;<\/span><span class=\"mpunct\">,<\/span><span class=\"mspace\" style=\"margin-right:0.1667em\"\/><span class=\"mord\">&#8220;<\/span><\/span><\/span><\/span><\/span>begincode_0&#8243;, &#8220;CALLDATACOPY&#8221;, &#8220;RETURN&#8221;, &#8220;~begincode_0&#8221;, &#8220;#CODE_BEGIN&#8221;, 5, 0, &#8220;MSTORE&#8221;, &#8220;#CODE_END&#8221;, &#8220;~endcode_0&#8221;]<\/p>\n<p class=\"chakra-text css-gi02ar\">The compile_to_assembly instruction compiles the code down into an intermediate human-readable \u201cassembly language\u201d format rather than plain old bytecode. Using plain old serpent compile would give you the much more incomprehensible but compact 6005515b525b600a37f26005600054. In this case, the \u201ccore\u201d of the code is [5, 0, &#8220;MSTORE&#8221;], putting the value 5 into memory slot 0, and the rest of the code basically says to return a contract containing that code. Another command that you may find useful is serpent get_vars; this will give you a list of all the variables together with their associated memory indices. In this case, you get {&#8216;x&#8217;: 0}, meaning that the compiler is choosing to use the memory index 0 to store the variable x. The last interesting command is parse to convert Serpent into an intermediate high-level parse tree. Now, since Serpent is a programming language, we want to run programs, and so ideally we would like to actually create contracts and run them as quickly as possible. Let\u2019s try that. First, open a file, call it \u201cnamecoin.se\u201c, and put the following code into it:<\/p>\n<p class=\"chakra-text css-gi02ar\">if !contract.storage[msg.data[0]]:<br \/>\ncontract.storage[msg.data[0]] = msg.data[1]<br \/>\nreturn(1)<br \/>\nelse:<br \/>\nreturn(0)<\/p>\n<p class=\"chakra-text css-gi02ar\">This is the two-line Namecoin example that we love so much, but embellished with return values to make it easier to work with for this tutorial. Typing serpent compile namecoin.se should give:<\/p>\n<p class=\"chakra-text css-gi02ar\">\u00a0<\/p>\n<p class=\"chakra-text css-gi02ar\">6025515b525b600a37f260003556601b596020356000355760015b525b54602052f260255860005b525b54602052f2<\/p>\n<p class=\"chakra-text css-gi02ar\">Now, let\u2019s see if we can actually get the code running. To do that, the first step is actually to create for ourselves an account. The process here is almost exactly the same as in my Python Bitcoin library <a target=\"_blank\" rel=\"noopener\" class=\"chakra-link css-vezwxf\" href=\"https:\/\/github.com\/vbuterin\/pybitcointools\">pybitcointools<\/a>; in general, anyone who is familiar with pybitcointools should feel right at home in pyethereum, although unfortunately in pyethereum it was not really practical to stick to pybitcointools\u2019 \u201cno classes\u201d mantra in the code. The first step is to generate a private key:<\/p>\n<p class=\"chakra-text css-gi02ar\">\u00a0<\/p>\n<blockquote class=\"chakra-code css-gk0tva\">\n<p class=\"chakra-text css-gi02ar\">pyethtool sha3 cow<\/p>\n<\/blockquote>\n<p class=\"chakra-text css-gi02ar\">c85ef7d79691fe79573b1a7064c19c1a9819ebdbd1faaab1a8ec92344438aaf4<\/p>\n<p class=\"chakra-text css-gi02ar\">In production code, you should obviously replace \u201ccow\u201d with an actually secure password. If you want your account to be a \u201cbrainwallet\u201d that you can easily remember, my main advice is to prepend a username, eg. \u201cvbuterin:bl@hbl@hm0nk33y#!$!%\u201d, ensuring that attackers need to target you individually instead of performing a blanket attack on everyone simultaneously; assuming 10000 brainwallet users this reduces your risk from a trial-and-error attack by 99.99%.<\/p>\n<p class=\"chakra-text css-gi02ar\">If you want to use your key later, on any standard Linux shell you can also type in key=<span class=\"chakra-text css-ons8vw\">pyethtool sha3 cow<\/span>, and then use$key to use the key thereafter. We\u2019ll use that format here from now on, so if you are following along then you should also do both:<\/p>\n<p class=\"chakra-text css-gi02ar\">\u00a0<\/p>\n<blockquote class=\"chakra-code css-gk0tva\">\n<p class=\"chakra-text css-gi02ar\">key=<span class=\"chakra-text css-ons8vw\">pyethtool sha3 cow<\/span><\/p>\n<\/blockquote>\n<blockquote class=\"chakra-code css-gk0tva\">\n<p class=\"chakra-text css-gi02ar\">code=<span class=\"chakra-text css-ons8vw\">serpent compile namecoin.se<\/span><\/p>\n<\/blockquote>\n<p class=\"chakra-text css-gi02ar\">So now, let\u2019s keep going.<\/p>\n<blockquote class=\"chakra-code css-gk0tva\">\n<p class=\"chakra-text css-gi02ar\">addr=<span class=\"chakra-text css-ons8vw\">pyethtool privtoaddr $key<\/span><\/p>\n<\/blockquote>\n<blockquote class=\"chakra-code css-gk0tva\">\n<p class=\"chakra-text css-gi02ar\">echo $addr<\/p>\n<\/blockquote>\n<p class=\"chakra-text css-gi02ar\">cd2a3d9f938e13cd947ec05abc7fe734df8dd826<\/p>\n<p class=\"chakra-text css-gi02ar\">Now, we create a new genesis block, and we&#8217;ll set the initial endowment to 1018 wei (1 ether) for your address.<\/p>\n<p class=\"chakra-text css-gi02ar\">\u00a0<\/p>\n<blockquote class=\"chakra-code css-gk0tva\">\n<p class=\"chakra-text css-gi02ar\">genesis=<span class=\"chakra-text css-ons8vw\">pyethtool mkgenesis $addr 1000000000000000000<\/span><\/p>\n<\/blockquote>\n<blockquote class=\"chakra-code css-gk0tva\">\n<p class=\"chakra-text css-gi02ar\">echo $genesis<\/p>\n<\/blockquote>\n<p class=\"chakra-text css-gi02ar\">f8b2f8aea00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940000000000000000000000000000000000000000a0bcddd284bf396739c224dba0411566c891c32115feb998a3e2b4e61f3f35582a80834000008087038d7ea4c68000830f4240808080a004994f67dc55b09e814ab7ffc8df3686b4afb2bb53e60eae97ef043fe03fb829c0c0<\/p>\n<p class=\"chakra-text css-gi02ar\">Now that we have that out of the way, we can get to actually doing stuff to the block. The only way to do anything in a blockchain-based architecture, in general, is to create and apply a transaction. Here, we will need multiple transactions: the first to create the contract, and then the latter ones to actually use it. Here&#8217;s contract creation:<\/p>\n<p class=\"chakra-text css-gi02ar\">\u00a0<\/p>\n<blockquote class=\"chakra-code css-gk0tva\">\n<p class=\"chakra-text css-gi02ar\">unsignedtx=<span class=\"chakra-text css-ons8vw\">pyethtool mkcontract 0 0 $code<\/span><\/p>\n<\/blockquote>\n<blockquote class=\"chakra-code css-gk0tva\">\n<p class=\"chakra-text css-gi02ar\">echo $unsignedtx<\/p>\n<\/blockquote>\n<p class=\"chakra-text css-gi02ar\">f83c8085e8d4a510008227108080af6025515b525b600a37f260003556601b596020356000355760015b525b54602052f260255860005b525b54602052f2<\/p>\n<blockquote class=\"chakra-code css-gk0tva\">\n<p class=\"chakra-text css-gi02ar\">tx=<span class=\"chakra-text css-ons8vw\">pyethtool sign $unsignedtx $key<\/span><\/p>\n<\/blockquote>\n<blockquote class=\"chakra-code css-gk0tva\">\n<p class=\"chakra-text css-gi02ar\">echo $tx<\/p>\n<\/blockquote>\n<p class=\"chakra-text css-gi02ar\">f87f8085e8d4a510008227108080af6025515b525b600a37f260003556601b596020356000355760015b525b54602052f260255860005b525b54602052f21ca04565b5a48b29ef623ad2caffe0917a3fc6a6f1b50f1df06876f3caa6fb4957c6a0123c928257c1f248fb3d362c125a0aea091ab08467efb52f8c3676ca73d727bf<\/p>\n<p class=\"chakra-text css-gi02ar\">Or, the easier way:<\/p>\n<p class=\"chakra-text css-gi02ar\">\u00a0<\/p>\n<blockquote class=\"chakra-code css-gk0tva\">\n<p class=\"chakra-text css-gi02ar\">tx=<span class=\"chakra-text css-ons8vw\">pyethtool mkcontract 0 0 $code | pyethtool -s sign $key<\/span><\/p>\n<\/blockquote>\n<blockquote class=\"chakra-code css-gk0tva\">\n<p class=\"chakra-text css-gi02ar\">echo $tx<\/p>\n<\/blockquote>\n<p class=\"chakra-text css-gi02ar\">f87f8085e8d4a510008227108080af6025515b525b600a37f260003556601b596020356000355760015b525b54602052f260255860005b525b54602052f21ca04565b5a48b29ef623ad2caffe0917a3fc6a6f1b50f1df06876f3caa6fb4957c6a0123c928257c1f248fb3d362c125a0aea091ab08467efb52f8c3676ca73d727bf<\/p>\n<p class=\"chakra-text css-gi02ar\">The first field in mkcontract is a nonce, which must be equal to the number of transactions you already sent from that account. The purpose of requiring a nonce is to prevent replay attacks; otherwise, if you sent Bob 200 ether, Bob could simply replay that transaction over and over again until you run out of money, whereas here due to the nonce requirement the transaction can only go through once. The second field is the amount of ether to send (in the case of contract creation, the amount of ether to initially provide to the contract), and the third field is the code. Note that the Transaction.contract<i>function call<\/i> also has two more fields between value and recipient: gasprice and startgas. Pyethtool is nice to you and initializes these values to 1 szabo (ie. 1012 wei or one millionth of an ether) per gas and 10000 gas, respectively. This will give you a theoretical maximum of 10000 computational steps for the code to run, although in practice it may run out after 1000 if you use many expensive operations. Finally, once you create the transaction, you need to sign it with your private key.<\/p>\n<p class=\"chakra-text css-gi02ar\">Once that&#8217;s done, we just, well:<\/p>\n<p class=\"chakra-text css-gi02ar\">\u00a0<\/p>\n<blockquote class=\"chakra-code css-gk0tva\">\n<p class=\"chakra-text css-gi02ar\">pyethtool applytx <span class=\"math math-inline\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\"><semantics><mrow><mi>g<\/mi><mi>e<\/mi><mi>n<\/mi><mi>e<\/mi><mi>s<\/mi><mi>i<\/mi><mi>s<\/mi><\/mrow><annotation encoding=\"application\/x-tex\">genesis <\/annotation><\/semantics><\/math><\/span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.854em;vertical-align:-0.1944em\"\/><span class=\"mord mathnormal\" style=\"margin-right:0.03588em\">g<\/span><span class=\"mord mathnormal\">e<\/span><span class=\"mord mathnormal\">n<\/span><span class=\"mord mathnormal\">es<\/span><span class=\"mord mathnormal\">i<\/span><span class=\"mord mathnormal\">s<\/span><\/span><\/span><\/span><\/span>tx<\/p>\n<\/blockquote>\n<p class=\"chakra-text css-gi02ar\">{&#8220;result&#8221;: &#8220;da7ce79725418f4f6e13bf5f520c89cec5f6a974&#8221;, &#8220;block&#8221;: &#8220;f9017ef8d0a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940000000000000000000000000000000000000000a00bcec36bf7ffc27418b1746986574526efeb09b34f733039749f291f778d4aaca03575f60ad6c929d7c98a50a12ff1ef9b07ecf3182e74962872064648a66f3da0834000008087038d7ea4c68000830f42408204b08080a004994f67dc55b09e814ab7ffc8df3686b4afb2bb53e60eae97ef043fe03fb829f8a9f8a7b881f87f8085e8d4a510008227108080af6025515b525b600a37f260003556601b596020356000355760015b525b54602052f260255860005b525b54602052f21ca04565b5a48b29ef623ad2caffe0917a3fc6a6f1b50f1df06876f3caa6fb4957c6a0123c928257c1f248fb3d362c125a0aea091ab08467efb52f8c3676ca73d727bfa00bcec36bf7ffc27418b1746986574526efeb09b34f733039749f291f778d4aac8204b0c0&#8221;}<\/p>\n<p class=\"chakra-text css-gi02ar\">This gives you two values. The first is the address of the contract, and the second is the new block data. Note that the block data does not represent the entire block; there is also the state data hidden in the statedb folder. Hence, if you try to deserialize the block on a fresh machine it likely will not work. From the values returned, set the first value to contract and the second to med so we can use them later. Now, we need to craft a transaction to actually use this contract. Suppose we want to register &#8220;george&#8221; to 45. To do that, however, we first need to do another annoying chore: package up the data. Fortunately, the serpent compiler has a utility for doing just that:<\/p>\n<p class=\"chakra-text css-gi02ar\">\u00a0<\/p>\n<blockquote class=\"chakra-code css-gk0tva\">\n<p class=\"chakra-text css-gi02ar\">data=<span class=\"chakra-text css-ons8vw\">echo &#8216;[&#8220;george&#8221;,45]&#8217; | serpent -j encode_datalist<\/span><\/p>\n<\/blockquote>\n<blockquote class=\"chakra-code css-gk0tva\">\n<p class=\"chakra-text css-gi02ar\">echo $data<\/p>\n<\/blockquote>\n<p class=\"chakra-text css-gi02ar\">000000000000000000000000000000000000000000000000000067656f726765000000000000000000000000000000000000000000000000000000000000002d<\/p>\n<p class=\"chakra-text css-gi02ar\">The namecoin contract takes data in two fields, the key and the value, so we simply put them into a JSON array and use Serpent to encode it. The encoder can accept strings and numbers as the individual elements in the array. Note that unfortunately Python&#8217;s JSON decoder requires double quotes for internal strings; &#8220;[&#8216;george&#8217;,45]&#8221; would not work.<\/p>\n<p class=\"chakra-text css-gi02ar\">Now, we do this:<\/p>\n<p class=\"chakra-text css-gi02ar\">\u00a0<\/p>\n<blockquote class=\"chakra-code css-gk0tva\">\n<p class=\"chakra-text css-gi02ar\">tx2=<span class=\"chakra-text css-ons8vw\">pyethtool mktx 1 $contract 0 $data | pyethtool -s sign $key<\/span><\/p>\n<\/blockquote>\n<blockquote class=\"chakra-code css-gk0tva\">\n<p class=\"chakra-text css-gi02ar\">echo $tx2<\/p>\n<\/blockquote>\n<p class=\"chakra-text css-gi02ar\">f8a50185e8d4a5100082271094da7ce79725418f4f6e13bf5f520c89cec5f6a97480b840000000000000000000000000000000000000000000000000000067656f726765000000000000000000000000000000000000000000000000000000000000002d1ba064363844c718f0f38907d39508adb2c2b9134e52e7d436fb20965044c01f41c2a0e1123d26cf810c4ef9d397974e2fc336d16e452d71df3c3d7245b40ed12c603b<\/p>\n<p class=\"chakra-text css-gi02ar\">And:<\/p>\n<p class=\"chakra-text css-gi02ar\">\u00a0<\/p>\n<blockquote class=\"chakra-code css-gk0tva\">\n<p class=\"chakra-text css-gi02ar\">pyethtool applytx <span class=\"math math-inline\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\"><semantics><mrow><mi>m<\/mi><mi>e<\/mi><mi>d<\/mi><\/mrow><annotation encoding=\"application\/x-tex\">med <\/annotation><\/semantics><\/math><\/span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.6944em\"\/><span class=\"mord mathnormal\">m<\/span><span class=\"mord mathnormal\">e<\/span><span class=\"mord mathnormal\">d<\/span><\/span><\/span><\/span><\/span>tx2<\/p>\n<\/blockquote>\n<p class=\"chakra-text css-gi02ar\">{&#8220;result&#8221;: &#8220;0000000000000000000000000000000000000000000000000000000000000001&#8221;, &#8220;block&#8221;: &#8220;f9024ef8d0a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940000000000000000000000000000000000000000a066d2524d921fadb5056983cf4bb215d339cdaeb7048b8913bfdf8fe867eb5682a0d669d3b5cfb150e4ef7f900cc613b0231abc8551544c389ddcd6668f784c4cb3834000008087038d7ea4c68000830f4240820a8f8080a004994f67dc55b09e814ab7ffc8df3686b4afb2bb53e60eae97ef043fe03fb829f90178f8a7b881f87f8085e8d4a510008227108080af6025515b525b600a37f260003556601b596020356000355760015b525b54602052f260255860005b525b54602052f21ca04565b5a48b29ef623ad2caffe0917a3fc6a6f1b50f1df06876f3caa6fb4957c6a0123c928257c1f248fb3d362c125a0aea091ab08467efb52f8c3676ca73d727bfa00bcec36bf7ffc27418b1746986574526efeb09b34f733039749f291f778d4aac8204b0f8cdb8a7f8a50185e8d4a5100082271094da7ce79725418f4f6e13bf5f520c89cec5f6a97480b840000000000000000000000000000000000000000000000000000067656f726765000000000000000000000000000000000000000000000000000000000000002d1ba064363844c718f0f38907d39508adb2c2b9134e52e7d436fb20965044c01f41c2a0e1123d26cf810c4ef9d397974e2fc336d16e452d71df3c3d7245b40ed12c603ba066d2524d921fadb5056983cf4bb215d339cdaeb7048b8913bfdf8fe867eb5682820a8fc0&#8221;}<\/p>\n<p class=\"chakra-text css-gi02ar\">Registration successful! The result here is two values, just as before: the first is the new block state, and the second is the response returned by the contract. Based on the definition of the contract above, &#8220;1&#8221; means success. Now, just to be sure, let&#8217;s set end to the block hex returned by the previous command and peek at the state:<\/p>\n<p class=\"chakra-text css-gi02ar\">\u00a0<\/p>\n<blockquote class=\"chakra-code css-gk0tva\">\n<p class=\"chakra-text css-gi02ar\">pyethtool getstate $end<\/p>\n<\/blockquote>\n<p class=\"chakra-text css-gi02ar\">{&#8216;nonce&#8217;: &#8216;\\x04\\x99Og\\xdcU\\xb0\\x9e\\x81J\\xb7\\xff\\xc8\\xdf6\\x86\\xb4\\xaf\\xb2\\xbbS\\xe6\\x0e\\xae\\x97\\xef\\x04?\\xe0?\\xb8)&#8217;, &#8216;min_gas_price&#8217;: 1000000000000000L, &#8216;extra_data&#8217;: &#8221;, &#8216;state_root&#8217;: &#8216;f\\xd2RM\\x92\\x1f\\xad\\xb5\\x05i\\x83\\xcfK\\xb2\\x15\\xd39\\xcd\\xae\\xb7\\x04\\x8b\\x89\\x13\\xbf\\xdf\\x8f\\xe8g\\xebV\\x82&#8217;, &#8216;difficulty&#8217;: 4194304L, &#8216;timestamp&#8217;: 0L, &#8216;number&#8217;: 0L, &#8216;gas_used&#8217;: 2703L, &#8216;coinbase&#8217;: &#8216;0000000000000000000000000000000000000000&#8217;, &#8216;tx_list_root&#8217;: &#8216;\\xd6i\\xd3\\xb5\\xcf\\xb1P\\xe4\\xef\\x7f\\x90\\x0c\\xc6\\x13\\xb0#\\x1a\\xbc\\x85QTL8\\x9d\\xdc\\xd6f\\x8fxLL\\xb3&#8217;, &#8216;state&#8217;: {&#8216;0000000000000000000000000000000000000000&#8217;: {&#8216;nonce&#8217;: 0L, &#8216;balance&#8217;: 2703000000000000L, &#8216;storage&#8217;: {}, &#8216;code&#8217;: &#8221;}, &#8216;da7ce79725418f4f6e13bf5f520c89cec5f6a974&#8217;: {&#8216;nonce&#8217;: 0L, &#8216;balance&#8217;: 0L, &#8216;storage&#8217;: {113685359126373L: 45L}, &#8216;code&#8217;: &#8216;60003556601b596020356000355760015b525b54602052f260255860005b525b54602052f2&#8217;}, &#8216;cd2a3d9f938e13cd947ec05abc7fe734df8dd826&#8217;: {&#8216;nonce&#8217;: 2L, &#8216;balance&#8217;: 997297000000000000L, &#8216;storage&#8217;: {}, &#8216;code&#8217;: &#8221;}}, &#8216;uncles_hash&#8217;: &#8216;\\x1d\\xccM\\xe8\\xde\\xc7]z\\xab\\x85\\xb5g\\xb6\\xcc\\xd4\\x1a\\xd3\\x12E\\x1b\\x94\\x8at\\x13\\xf0\\xa1B\\xfd@\\xd4\\x93G&#8217;, &#8216;prevhash&#8217;: &#8216;\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00&#8217;, &#8216;gas_limit&#8217;: 1000000L}<\/p>\n<p class=\"chakra-text css-gi02ar\">You can see the contract account near the beginning of the state description, with &#8220;george&#8221; registered to 45 as expected. We&#8217;re done! As an exercise, try constructing two more transactions, one registering &#8220;george&#8221; to 60 and another registering &#8220;harry&#8221; to 80. If you apply them all sequentially after these two, the one registering &#8220;george&#8221; to 60 should return 0, but the one registering &#8220;harry&#8221; to 80 should succceed.<\/p>\n<h3 class=\"chakra-heading group css-xuzltg\" id=\"doing-it-in-python\" data-group=\"true\"><a class=\"chakra-link css-128fqrf\" aria-label=\"doing it in python permalink\" href=\"#doing-it-in-python\"><svg viewbox=\"0 0 24 24\" focusable=\"false\" class=\"chakra-icon css-173jpr1\"><g fill=\"currentColor\"><path d=\"M10.458,18.374,7.721,21.11a2.853,2.853,0,0,1-3.942,0l-.892-.891a2.787,2.787,0,0,1,0-3.941l5.8-5.8a2.789,2.789,0,0,1,3.942,0l.893.892A1,1,0,0,0,14.94,9.952l-.893-.892a4.791,4.791,0,0,0-6.771,0l-5.8,5.8a4.787,4.787,0,0,0,0,6.77l.892.891a4.785,4.785,0,0,0,6.771,0l2.736-2.735a1,1,0,1,0-1.414-1.415Z\"\/><path d=\"M22.526,2.363l-.892-.892a4.8,4.8,0,0,0-6.77,0l-2.905,2.9a1,1,0,0,0,1.414,1.414l2.9-2.9a2.79,2.79,0,0,1,3.941,0l.893.893a2.786,2.786,0,0,1,0,3.942l-5.8,5.8a2.769,2.769,0,0,1-1.971.817h0a2.766,2.766,0,0,1-1.969-.816,1,1,0,1,0-1.415,1.412,4.751,4.751,0,0,0,3.384,1.4h0a4.752,4.752,0,0,0,3.385-1.4l5.8-5.8a4.786,4.786,0,0,0,0-6.771Z\"\/><\/g><\/svg><\/a>Doing it in Python<\/h3>\n<p class=\"chakra-text css-gi02ar\">That&#8217;s pyethtool, the command line utility. Now, how does it work using pyethereum itself? As it turns out, it&#8217;s surprisingly easy. Here&#8217;s the session:<\/p>\n<p class=\"chakra-text css-gi02ar\">\u00a0<\/p>\n<p class=\"chakra-text css-gi02ar\">&gt;&gt;&gt; import serpent<\/p>\n<p class=\"chakra-text css-gi02ar\">&gt;&gt;&gt; from pyethereum import transactions, blocks, processblock, utils<\/p>\n<p class=\"chakra-text css-gi02ar\">&gt;&gt;&gt; code = serpent.compile(open(&#8216;namecoin.se&#8217;).read())<\/p>\n<p class=\"chakra-text css-gi02ar\">&gt;&gt;&gt; key = utils.sha3(&#8216;cow&#8217;)<\/p>\n<p class=\"chakra-text css-gi02ar\">&gt;&gt;&gt; addr = utils.privtoaddr(key)<\/p>\n<p class=\"chakra-text css-gi02ar\">&gt;&gt;&gt; genesis = blocks.genesis({ addr: 10**18 })<\/p>\n<p class=\"chakra-text css-gi02ar\">&gt;&gt;&gt; tx1 = transactions.contract(0,10**12,10000,0,code).sign(key)<\/p>\n<p class=\"chakra-text css-gi02ar\">&gt;&gt;&gt; result, contract = processblock.apply_tx(genesis,tx1)<\/p>\n<p class=\"chakra-text css-gi02ar\">&gt;&gt;&gt; tx2 = transactions.Transaction(1,10**12,10000,contract,0,serpent.encode_datalist([&#8216;george&#8217;,45])).sign(key)<\/p>\n<p class=\"chakra-text css-gi02ar\">&gt;&gt;&gt; result, ans = processblock.apply_tx(genesis,tx2)<\/p>\n<p class=\"chakra-text css-gi02ar\">&gt;&gt;&gt; serpent.decode_datalist(ans)<\/p>\n<p class=\"chakra-text css-gi02ar\">[1]<\/p>\n<p class=\"chakra-text css-gi02ar\">&gt;&gt;&gt; genesis.to_dict()<\/p>\n<p class=\"chakra-text css-gi02ar\">&#8216;nonce&#8217;: &#8216;\\x04\\x99Og\\xdcU\\xb0\\x9e\\x81J\\xb7\\xff\\xc8\\xdf6\\x86\\xb4\\xaf\\xb2\\xbbS\\xe6\\x0e\\xae\\x97\\xef\\x04?\\xe0?\\xb8)&#8217;, &#8216;min_gas_price&#8217;: 1000000000000000L, &#8216;extra_data&#8217;: &#8221;, &#8216;state_root&#8217;: &#8221;, &#8216;difficulty&#8217;: 4194304, &#8216;timestamp&#8217;: 0, &#8216;number&#8217;: 0, &#8216;gas_used&#8217;: 2712L, &#8216;coinbase&#8217;: &#8216;0000000000000000000000000000000000000000&#8217;, &#8216;tx_list_root&#8217;: &#8216;\\x17\\x90\\x87\\x966\\xbdb!\\x14|R\\xb0&amp; \\xb04\\x90\\xb9bs\\x12\\x85\\x90\\xdaB\\xed\\x83n*\\x8eE\\x8e&#8217;, &#8216;state&#8217;: {&#8216;0000000000000000000000000000000000000000&#8217;: {&#8216;nonce&#8217;: 0L, &#8216;balance&#8217;: 2712000000000000L, &#8216;storage&#8217;: {}, &#8216;code&#8217;: &#8221;}, &#8216;da7ce79725418f4f6e13bf5f520c89cec5f6a974&#8217;: {&#8216;nonce&#8217;: 0L, &#8216;balance&#8217;: 0L, &#8216;storage&#8217;: {113685359126373L: 45L}, &#8216;code&#8217;: &#8216;60003556601e596020356000355760015b525b54602052f260285860005b525b54602052f2&#8217;}, &#8216;cd2a3d9f938e13cd947ec05abc7fe734df8dd826&#8217;: {&#8216;nonce&#8217;: 2L, &#8216;balance&#8217;: 997288000000000000L, &#8216;storage&#8217;: {}, &#8216;code&#8217;: &#8221;}}, &#8216;uncles_hash&#8217;: &#8216;\\x1d\\xccM\\xe8\\xde\\xc7]z\\xab\\x85\\xb5g\\xb6\\xcc\\xd4\\x1a\\xd3\\x12E\\x1b\\x94\\x8at\\x13\\xf0\\xa1B\\xfd@\\xd4\\x93G&#8217;, &#8216;prevhash&#8217;: &#8216;\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00&#8217;, &#8216;gas_limit&#8217;: 1000000}<\/p>\n<p class=\"chakra-text css-gi02ar\">&gt;&gt;&gt; genesis.get_balance(addr)<\/p>\n<p class=\"chakra-text css-gi02ar\">997288000000000000L<\/p>\n<p class=\"chakra-text css-gi02ar\">&gt;&gt;&gt; genesis.get_storage_data(contract,&#8217;george&#8217;)<\/p>\n<p class=\"chakra-text css-gi02ar\">45L<\/p>\n<p class=\"chakra-text css-gi02ar\">Another important command is processblock.debug = 1; this starts printing code execution step by step, helping you debug what is wrong in your contract code &#8211; or my pyethereum VM or Serpent implementation!<\/p>\n<h3 class=\"chakra-heading group css-xuzltg\" id=\"getting-into-the-code\" data-group=\"true\"><a class=\"chakra-link css-128fqrf\" aria-label=\"getting into the code permalink\" href=\"#getting-into-the-code\"><svg viewbox=\"0 0 24 24\" focusable=\"false\" class=\"chakra-icon css-173jpr1\"><g fill=\"currentColor\"><path d=\"M10.458,18.374,7.721,21.11a2.853,2.853,0,0,1-3.942,0l-.892-.891a2.787,2.787,0,0,1,0-3.941l5.8-5.8a2.789,2.789,0,0,1,3.942,0l.893.892A1,1,0,0,0,14.94,9.952l-.893-.892a4.791,4.791,0,0,0-6.771,0l-5.8,5.8a4.787,4.787,0,0,0,0,6.77l.892.891a4.785,4.785,0,0,0,6.771,0l2.736-2.735a1,1,0,1,0-1.414-1.415Z\"\/><path d=\"M22.526,2.363l-.892-.892a4.8,4.8,0,0,0-6.77,0l-2.905,2.9a1,1,0,0,0,1.414,1.414l2.9-2.9a2.79,2.79,0,0,1,3.941,0l.893.893a2.786,2.786,0,0,1,0,3.942l-5.8,5.8a2.769,2.769,0,0,1-1.971.817h0a2.766,2.766,0,0,1-1.969-.816,1,1,0,1,0-1.415,1.412,4.751,4.751,0,0,0,3.384,1.4h0a4.752,4.752,0,0,0,3.385-1.4l5.8-5.8a4.786,4.786,0,0,0,0-6.771Z\"\/><\/g><\/svg><\/a>Getting into the Code<\/h3>\n<p class=\"chakra-text css-gi02ar\">So that&#8217;s your introduction to how to use pyethereum. Now, let&#8217;s get into the most fun part, writing contracts. For reading efficiency, let&#8217;s provide the Namecoin contract again:<\/p>\n<p class=\"chakra-text css-gi02ar\">if !contract.storage[msg.data[0]]:<br \/>\ncontract.storage[msg.data[0]] = msg.data[1]<br \/>\nreturn(1)<br \/>\nelse:<br \/>\nreturn(0)<\/p>\n<p class=\"chakra-text css-gi02ar\">What does this contract do? Essentially, this contract implements a name registration database by simply using that as the sole function of the long-term storage of the contract. Contract code theoretically has three places to put data: stack, memory and storage. Of those three, stack and memory are used implicitly in Serpent to support arithmetic and variables, but long-term storage is the only one that survives once execution is over. Here, when you register &#8220;george&#8221; to 45, the contract first checks ifcontract.storage[&#8220;george&#8221;] is not nonzero, ie. is zero. If it is, then it sets that storage index to the value provided, 45, and then returns 1. If it is not, then it returns zero. Note that this contract has no way for other contracts to access it; it is only really usable by external applications. More advanced name registries would have an API for contracts to fetch the data associated with a name as well.<\/p>\n<p class=\"chakra-text css-gi02ar\">Now, on to a more intricate example:<\/p>\n<p class=\"chakra-text css-gi02ar\">init:<br \/>\ncontract.storage[0xcd2a3d9f938e13cd947ec05abc7fe734df8dd826] = 1000000<br \/>\ncode:<br \/>\nif msg.datasize == 1:<br \/>\naddr = msg.data[0]<br \/>\nreturn(contract.storage[addr])<br \/>\nelse:<br \/>\nfrom = msg.sender<br \/>\nfromvalue = contract.storage[from]<br \/>\nto = msg.data[0]<br \/>\nvalue = msg.data[1]<br \/>\nif fromvalue &gt;= value:<br \/>\ncontract.storage[from] = fromvalue &#8211; value<br \/>\ncontract.storage[to] = contract.storage[to] + value<br \/>\nreturn(1)<br \/>\nelse:<br \/>\nreturn(0)<\/p>\n<p class=\"chakra-text css-gi02ar\">This is the &#8220;currency contract&#8221;, or more precisely an embellished version of it with return values to make debugging easier. This contract is interesting for several reasons. First, it has an initialization step, which gets called when the contract is first made. This initializes an account with 1000000 currency units owned by that account.<\/p>\n<p class=\"chakra-text css-gi02ar\">After that, there are two code paths. First, incoming messages might contain only one data field. In that case, these messages are treated as balance queries, and simply return the balance of the queried address. Note that msg.data[0] provides the integer at bytes 0&#8230;31 of the transaction data, msg.data[1] provides the integer at bytes 32&#8230;63, and so forth. This is a convenience introduced in Serpent; the underlying transaction data is all byte-based. Incidentally, this is why we needed to use Serpent&#8217;s encode_datalist function to generate the transaction data.<\/p>\n<p class=\"chakra-text css-gi02ar\">Second, incoming messages might contain two data fields. In that case, the messages are treated as requests to send to that address. The sender is inferred from the sender of the message, and the recipient and the value are taken from the first two fields (ie. first 64 bytes) in msg.data. If there is enough money to transfer, it transfers the money and returns 1; otherwise it returns 0.<\/p>\n<p class=\"chakra-text css-gi02ar\"><b>Challenge<\/b>: create a currency contract which takes a fee, denominated in its internal currency, from every transaction, and refunds a small amount of ether to everyone sending a successful transaction, so people (or contracts) who want to deal in this currency would not have to worry about simultaneously maintaining currency and ether balances themselves. The contract would also include a third transaction type, perhaps taking 0 arguments, through which someone can buy internal currency units from the contract by sending it ether. The contract should keep track of two variables: its own balance in its currency, and its ether balance, and it should dynamically adjust the transaction fee and the exchange rate in order to keep both its ether balance and its internal currency balance in bal- uh, in an approximate equilibrium.<\/p>\n<h3 class=\"chakra-heading group css-xuzltg\" id=\"contracts-calling-contracts\" data-group=\"true\"><a class=\"chakra-link css-128fqrf\" aria-label=\"contracts calling contracts permalink\" href=\"#contracts-calling-contracts\"><svg viewbox=\"0 0 24 24\" focusable=\"false\" class=\"chakra-icon css-173jpr1\"><g fill=\"currentColor\"><path d=\"M10.458,18.374,7.721,21.11a2.853,2.853,0,0,1-3.942,0l-.892-.891a2.787,2.787,0,0,1,0-3.941l5.8-5.8a2.789,2.789,0,0,1,3.942,0l.893.892A1,1,0,0,0,14.94,9.952l-.893-.892a4.791,4.791,0,0,0-6.771,0l-5.8,5.8a4.787,4.787,0,0,0,0,6.77l.892.891a4.785,4.785,0,0,0,6.771,0l2.736-2.735a1,1,0,1,0-1.414-1.415Z\"\/><path d=\"M22.526,2.363l-.892-.892a4.8,4.8,0,0,0-6.77,0l-2.905,2.9a1,1,0,0,0,1.414,1.414l2.9-2.9a2.79,2.79,0,0,1,3.941,0l.893.893a2.786,2.786,0,0,1,0,3.942l-5.8,5.8a2.769,2.769,0,0,1-1.971.817h0a2.766,2.766,0,0,1-1.969-.816,1,1,0,1,0-1.415,1.412,4.751,4.751,0,0,0,3.384,1.4h0a4.752,4.752,0,0,0,3.385-1.4l5.8-5.8a4.786,4.786,0,0,0,0-6.771Z\"\/><\/g><\/svg><\/a>Contracts Calling Contracts<\/h3>\n<p class=\"chakra-text css-gi02ar\">This is a proprietary data feed contract:<\/p>\n<p class=\"chakra-text css-gi02ar\">owner = 0xcd2a3d9f938e13cd947ec05abc7fe734df8dd826<br \/>\nif msg.sender == owner and msg.datasize == 2:<br \/>\ncontract.storage[msg.data[0]] = msg.data[1]<br \/>\nreturn(1)<br \/>\nelse:<br \/>\nreturn(contract.storage[msg.data[0]])<\/p>\n<p class=\"chakra-text css-gi02ar\">This contract is designed to work as a key\/value that can be edited only by its owner, but also also allows anyone to query its contents; the point is for the owner to use various storage indices to record changing data like the USD price of ether. Here, there are two main &#8220;clauses&#8221; in the contract, one for modifying storage which triggers if a key and a value are provided and the message originates from the contract&#8217;s owner, and the other for just reading storage. The msg.datasize variable tells you the number of 32-byte data fields there is in the message data. There are no particularly new features here; this contract is actually fairly simple, and I encourage you to first follow and make sure you understand the logic involved and then play with the contract, instantiating it in a block and then pushing set and query transactions to it.<\/p>\n<p class=\"chakra-text css-gi02ar\">The interesting part, however, comes when we use this contract inside of another contract. Meet this monstrosity, a hedging contract:<\/p>\n<p class=\"chakra-text css-gi02ar\">if !contract.storage[1000]:<br \/>\ncontract.storage[1000] = msg.sender<br \/>\ncontract.storage[1002] = msg.value<br \/>\ncontract.storage[1003] = msg.data[0]<br \/>\ncontract.storage[1004] = msg.data[1]<br \/>\nreturn(1)<br \/>\nelif !contract.storage[1001]:<br \/>\nethvalue = contract.storage[1002]<br \/>\nif msg.value &gt;= ethvalue:<br \/>\ncontract.storage[1001] = msg.sender<br \/>\ndatasource = contract.storage[1003]<br \/>\ndataindex = contract.storage[1004]<br \/>\nothervalue = ethvalue * msg(datasource,0,tx.gas-100,[dataindex],1)<br \/>\ncontract.storage[1005] = othervalue<br \/>\ncontract.storage[1006] = block.timestamp + 86400<br \/>\nreturn([2,othervalue],2)<br \/>\nelse:<br \/>\ndatasource = contract.storage[1003]<br \/>\ndataindex = contract.storage[1004]<br \/>\nothervalue = contract.storage[1005]<br \/>\nethvalue = othervalue \/ msg(dataindex,0,tx.gas-100,[datasource],1)<br \/>\nif ethvalue &gt;= contract.balance:<br \/>\nsend(contract.storage[1000],contract.balance,tx.gas-100)<br \/>\nreturn(3)<br \/>\nelif block.timestamp &gt; contract.storage[1006]:<br \/>\nsend(contract.storage[1001],contract.balance &#8211; ethvalue,tx.gas-100)<br \/>\nsend(contract.storage[1000],ethvalue,tx.gas-100)<br \/>\nreturn(4)<br \/>\nelse:<br \/>\nreturn(5)<\/p>\n<p class=\"chakra-text css-gi02ar\">This contract is bulky because it&#8217;s designed to be more testing-friendly; an optimal implementation is roughly half the size. The contract works as follows:<\/p>\n<ol role=\"list\" class=\"css-vgl4zd\">\n<li class=\"css-0\">\n<p class=\"chakra-text css-gi02ar\">Party A sends in X ether alongside a data feed contract D and a currency code C as data items, and is registered at contract storage index 1000. X, D and C are registered in contract storage indices 1002, 1003 and 1004. In this case, suppose that the currency code represents USD.<\/p>\n<\/li>\n<li class=\"css-0\">\n<p class=\"chakra-text css-gi02ar\">Party B sends in X ether, and is registered at contract storage index 1001. The contract then calls D with data C to determine the price of ether in the given currency, and uses this to compute V, the amount of value in USD sent by each party. V is stored at index 1005, and an expiry time set to 24 hours in the future is stored at index 1006.<\/p>\n<\/li>\n<li class=\"css-0\">\n<p class=\"chakra-text css-gi02ar\">Maybe, the price of ether in USD drops by more than 50%. If this happens, then there is not enough ether in the contract altogether to pay V USD. To prevent this, as soon as the price slips under the 50% mark, anyone (usually A) can ping the contract to withdraw all 2X ether into A&#8217;s address and thereby recover to A&#8217;s address almost all of the amount, as measured in USD, that A put in, and leave B with nothing. If this happens, the contract returns 3.<\/p>\n<\/li>\n<li class=\"css-0\">\n<p class=\"chakra-text css-gi02ar\">Otherwise, after one day, anyone can send a transaction to &#8220;ping&#8221; the contract and cause it to send V USD worth of ether to A and the remaining ether to B, returning 4.<\/p>\n<\/li>\n<li class=\"css-0\">\n<p class=\"chakra-text css-gi02ar\">If there is no &#8220;margin call&#8221; or &#8220;expiry&#8221; event, then a ping to the contract does nothing and returns 5.<\/p>\n<\/li>\n<\/ol>\n<p class=\"chakra-text css-gi02ar\">The point of the hedging contract is that A benefits by always getting back the same quantity of USD that he put in, and B benefits if he believes that the value of ether will go up, since a 10% rise in the ether price will, in this circumstance, give him a 20% profit. USD can of course be substituted with anything, including CNY, gold or the consumer price index.<\/p>\n<p class=\"chakra-text css-gi02ar\">The important new features explored here are msg, send and array literals. msg and send are both ways of sending message to other contracts. The syntaxes are:<\/p>\n<p class=\"chakra-text css-gi02ar\">\u00a0<\/p>\n<p class=\"chakra-text css-gi02ar\">send(to, value, gas)<\/p>\n<p class=\"chakra-text css-gi02ar\">out = msg(to\u00b8 value, gas, datastart, datalength)<\/p>\n<p class=\"chakra-text css-gi02ar\">msg(to, value, gas, datastart, datalength, outstart, outlength)<\/p>\n<p class=\"chakra-text css-gi02ar\">Send is simpler, assuming that all you want to do is send money with no bells and whistles involved. The latter two are equivalent ways of sending a message to another contract, differing only in how they handle the output: the first caps output to 32 bytes and sticks it straight into a variable, whereas the second takes in two arguments for the position in memory where to dump the output. The &#8220;output&#8221; of a message is blank if the recipient is not-yet-existent, an externally owned account, or does not explicitly specify a return value, and if the output does specify a return value then the output is that value (&#8220;value&#8221; in this context being an arbitrary-length byte array, not a 32-byte number). These two are thus both ways of saying the same thing:<\/p>\n<p class=\"chakra-text css-gi02ar\">d = array(3)<br \/>\nd[0] = 5<br \/>\nd[1] = 10<br \/>\nd[2] = 15<br \/>\nx = msg(A, B, C, d, 3)<\/p>\n<p class=\"chakra-text css-gi02ar\">And:<\/p>\n<p class=\"chakra-text css-gi02ar\">d = array(3)<br \/>\nd[0] = 5<br \/>\nd[1] = 10<br \/>\nd[2] = 15<br \/>\nw = array(1)<br \/>\nmsg(A, B, C, d, 3, w, 1)<br \/>\nx = w[0]<\/p>\n<p class=\"chakra-text css-gi02ar\">In the contract example above, we used the data feed contract to provide the price of ether in USD, and then directly plugged it into the formula othervalue = ethvalue * msg(datasource,0,tx.gas-100,[dataindex],1).<\/p>\n<p class=\"chakra-text css-gi02ar\">Array literals are another nice convenience feature; the truly optimal way to write the above code is as follows:<\/p>\n<p class=\"chakra-text css-gi02ar\">x = msg(A, B, C, [5, 10, 15], 3)<\/p>\n<p class=\"chakra-text css-gi02ar\">Note that you unfortunately still need to specify the array length. However, here the array itself is created and referenced all inline, without needing to manually set things up. All of the magic is done by the Serpent compiler.<\/p>\n<p class=\"chakra-text css-gi02ar\">So that&#8217;s basically it for today. What might you want to code in Serpent? Well, here are a few possibilities:<\/p>\n<ol role=\"list\" class=\"css-vgl4zd\">\n<li class=\"css-0\">\n<p class=\"chakra-text css-gi02ar\"><a class=\"chakra-link css-vezwxf\" href=\"https:\/\/blog.ethereum.org\/2014\/03\/28\/schellingcoin-a-minimal-trust-universal-data-feed\">SchellingCoin<\/a><\/p>\n<\/li>\n<li class=\"css-0\">\n<p class=\"chakra-text css-gi02ar\">A contract-based implementation of <a target=\"_blank\" rel=\"noopener\" class=\"chakra-link css-vezwxf\" href=\"http:\/\/just-dice.com\/\">JustDice<\/a>.<\/p>\n<\/li>\n<li class=\"css-0\">\n<p class=\"chakra-text css-gi02ar\">Some skeleton code for a decentralized organization.<\/p>\n<\/li>\n<li class=\"css-0\">\n<p class=\"chakra-text css-gi02ar\">A board game (eg. chess, Go)<\/p>\n<\/li>\n<li class=\"css-0\">\n<p class=\"chakra-text css-gi02ar\">A decentralized exchange, with a contract-based order book, between ether and the sub-currency contract given above.<\/p>\n<\/li>\n<li class=\"css-0\">\n<p class=\"chakra-text css-gi02ar\">Any of the other examples in our <a target=\"_blank\" rel=\"noopener\" class=\"chakra-link css-vezwxf\" href=\"https:\/\/github.com\/ethereum\/wiki\/wiki\/%5BEnglish%5D-White-Paper\">whitepaper<\/a><\/p>\n<\/li>\n<\/ol>\n<p class=\"chakra-text css-gi02ar\">Enjoy, and have fun! Also, if you do find any bugs in pyethereum or Serpent, please be sure to point them out.<\/p>\n<p class=\"chakra-text css-gi02ar\">See also: <a target=\"_blank\" rel=\"noopener\" class=\"chakra-link css-vezwxf\" href=\"https:\/\/github.com\/ethereum\/wiki\/wiki\/%5BEnglish%5D-Serpent-programming-language-operations\">list of Serpent language operations<\/a><\/p>\n<\/div>\n<p><br \/>\n<br \/><a href=\"https:\/\/blog.ethereum.org\/en\/2014\/04\/10\/pyethereum-and-serpent-programming-guide\">Source link <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The content of this tutorial is intended to apply to PoC5. Most of the instructions given below will not work in the older PoC4 implementations of AlethZero (C++) and Ethereal (Go) Over the last few weeks, we have made a large number of changes to the Ethereum protocol. POC4, introducing a large body of changes [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":18498,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"tdm_status":"","tdm_grid_status":"","footnotes":""},"categories":[24],"tags":[],"kronos_expire_date":[],"class_list":["post-19157","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ethereum"],"_links":{"self":[{"href":"https:\/\/cryptoted.net\/index.php\/wp-json\/wp\/v2\/posts\/19157","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cryptoted.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cryptoted.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cryptoted.net\/index.php\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/cryptoted.net\/index.php\/wp-json\/wp\/v2\/comments?post=19157"}],"version-history":[{"count":0,"href":"https:\/\/cryptoted.net\/index.php\/wp-json\/wp\/v2\/posts\/19157\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cryptoted.net\/index.php\/wp-json\/wp\/v2\/media\/18498"}],"wp:attachment":[{"href":"https:\/\/cryptoted.net\/index.php\/wp-json\/wp\/v2\/media?parent=19157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cryptoted.net\/index.php\/wp-json\/wp\/v2\/categories?post=19157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cryptoted.net\/index.php\/wp-json\/wp\/v2\/tags?post=19157"},{"taxonomy":"kronos_expire_date","embeddable":true,"href":"https:\/\/cryptoted.net\/index.php\/wp-json\/wp\/v2\/kronos_expire_date?post=19157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}