Blockchain applications engineer
45 commentsBeat maker free download for windows xp sp3
Announcing World Trade Francs: The Official Ethereum Stablecoin 01st April, Ethereum scalability research and development subsidy programs 02nd January, The content of this tutorial is intended to apply to PoC5.
Over the last few weeks, we have made a large number of changes to the Ethereum protocol. POC4 has given myself what I wanted out of Ethereum Script 2 , Gavin a much more optimization-friendly VM architecture, and users a shiny new currency.
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.
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. With recent upgrades to the compiler, Serpent is now a highly feature-filled programming language, with powerful features including:. 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. 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: The simplest way to download is to either download the zip files from Github and unpack them, or run git clone http: Then, enter the pyethereum directory, and run sudo python setup.
Using plain old serpent compile would give you the much more incomprehensible but compact bba37f 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. 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. To do that, the first step is actually to create for ourselves an account. The first step is to generate a private key:. 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 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 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. Pyethtool is nice to you and initializes these values to 1 szabo ie. This will give you a theoretical maximum of computational steps for the code to run, although in practice it may run out after if you use many expensive operations.
Finally, once you create the transaction, you need to sign it with your private key. 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. To do that, however, we first need to do another annoying chore: Fortunately, the serpent compiler has a utility for doing just that:.
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. The result here is two values, just as before: Now, how does it work using pyethereum itself? Another important command is processblock.
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: 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.
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.
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 currency units owned by that account.
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.
This is a convenience introduced in Serpent; the underlying transaction data is all byte-based. 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. If there is enough money to transfer, it transfers the money and returns 1; otherwise it returns 0.
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: 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.
The interesting part, however, comes when we use this contract inside of another contract. Meet this monstrosity, a hedging contract:. The contract works as follows:. Party B sends in X ether, and is registered at contract storage index 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 , and an expiry time set to 24 hours in the future is stored at index If this happens, then there is not enough ether in the contract altogether to pay V USD.
If this happens, the contract returns 3. The important new features explored here are msg, send and array literals. 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: These two are thus both ways of saying the same thing:.
Array literals are another nice convenience feature; the truly optimal way to write the above code is as follows:. 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. What might you want to code in Serpent?
Well, here are a few possibilities:. A decentralized exchange, with a contract-based order book, between ether and the sub-currency contract given above. Any of the other examples in our whitepaper. Enjoy, and have fun! Also, if you do find any bugs in pyethereum or Serpent, please be sure to point them out. Nice piece — I Appreciate the analysis , Does anyone know where my business could possibly find a template a form document to work with?
Greetings Felicity, my assistant saw a template a form document at this place http: You may use these HTML tags and attributes: Pyethereum and Serpent Programming Guide Introduction. The Official Ethereum Stablecoin 01st April, Ethereum scalability research and development subsidy programs 02nd January, Author link vao dafabet Posted at 8: