Info about bitcoin cash
18 commentsEthereum white paper
Interested in building a product that uses blockchain technology? Contact us today to get started. Hello, fellow web developer! If you're reading this, you're probably interested in blockchains, smart contracts, etc.
I'm going to walk you through setting up, writing, and deploying a smart contract to a real live Ethereum blockchain, and then interacting with that contract in a browser via a web service. I'm not going to explain Blockchains or Ethereum But it's probably worth discussing Ethereum at a very high level from a developer's perspective. You don't need to care about mining or Proof-of-Work vs. Proof-of-Stake, or anything like that.
But you should know that Ethereum is a decentralized virtual machine that runs on many nodes scattered around the world, and so-called "smart contracts" are code which runs along with data which is stored within that virtual machine, i. They do, however, need to pay. Every line of code and byte of storage in Ethereum has a price.
Ethereum, like Bitcoin, has a native currency, called "ether"; this is the same Ether currency that is traded on exchanges like Coinbase. In principle many languages can be compiled down to the bytecode used by the Ethereum VM, but in practice almost all smart contracts are written in the "Ethereum-native" language called Solidity.
Solidity is still arguably somewhere between alpha-release and beta-release quality, and has lots of See this scathing commentary from six months ago: Solidity reads a bit like Javascript. It has a lot of quirks and pitfalls, though, especially when it comes to moving money around. Be very careful if writing real money-transfer code; do your security homework, get others to review your code, and seriously consider an official security audit or even formal verification.
Solidity code runs on the blockchain. But talking to the Ethereum blockchain from external code -- like, say, a web server -- is another matter.
The de facto state of the art here is a Javascript library called "web3". You have been warned. This tutorial is for web developers, who are accustomed to talking to APIs running on external servers; it will get your smart contracts up and running, on a real live blockchain, without ever needing to run an Ethereum node yourself.
Also, I'm a believer in entire tutorials contained within a single document, which this is. That said, you can also go look at the Git repository containing the final code at github. It would probably be safe to do so -- enforced HTTPS connection, they're never saved on the server side, etc.
The "real" Ethereum blockchain is protected by a vast network of miners securing its transactions with billions of hashes per second. However there are also "testnet" blockchains, which are either less secure or are "private" i. We will use the Rinkeby https: But first you need an address and private key. You can go make these online too. Just head over to MyEtherWallet https: You won't use this for real money, so don't worry too much about security. If you were using this for real money, you'd be much, much more careful about this process, though, right?
Once you've entered your password, you will be offered the opportunity to "Download Keystore File. Two important strings of alphanumeric characters will be revealed to you: Click on the little eye icon to reveal the latter.
I strongly suggest that you make the address and the private key environment variables. In a Bash environment like OS X, you can do this by appending these two lines to your. Setting environment variables in other environments is left as an exercise for the reader.
Note that you'll have to close and re-open your terminal window to refresh your environment. Check with "printenv" in bash. However, to actually deploy and run transactions, you'll need fake money. Conveniently you can get this online, too, via the Rinkeby faucet:.
All you need to do is post your new Ethereum address not your private key! The minimum amount of ether you'll receive 3 is already more than you need for basic development, but still, might as well get as much as you can.
The faucet transaction might take a minute to complete. You can check your balance via Infura's Etherscan service:. It is now almost time to move on to writing some code. First, though, we have one more thing to install and configure. Don't worry, it's easy; trust me, it's worth it. First -- you're familiar with Javascript, right? If not, get thee first to a different tutorial eg http: Know your away around those basics? On to the Truffle Framework http: You can also use Truffle to deploy your code, but it needs a local Ethereum node, so we'll deploy in a different way, described below.
It then offers various commands, the most relevant of which for this tutorial are "truffle init", "truffle compile", and "truffle test. Now, open up the text editor of your choice and create a new file called "MyPrize. It's finally time to start writing yourself some smart contracts. Our smart contract, MyPrize, is going to manage ownership of 10 geolocated augmented-reality entities, called "prizes.
For our smart-contract purposes, all we care about is that there's a string called "metadata. After a prize has been placed, it stays where it is until new blocks have been added to the blockchain i.
It's up to some external system e. Our smart contract just handles ownership and location. There can be many subtle complexities when writing Solidity smart contracts. Contracts can send and receive money.
Contracts can spawn, own, and link to other contracts. Issues of identity, ownership, and versioning come up. It's common for whoever first deploys a contract to be its "owner," and for important functionality to be calved off to separate subcontracts, which the owner can replace if a bug needs to be fixed or when a new version is ready.
But we're not going to worry about theoretical philosophical concerns; we're just going to write some running code. And here it is:. Save that file, go back to your command line, go back to the "truffle" subdirectory beneath your root "webthereum" directory, and type.
Writing automated tests for your software is always a good idea; but it's a really, really excellently awesome and important idea for Solidity code. If you don't find a bug until the code is on the blockchain, not only have you wasted a ton of time and a slew of money, your diagnosis and debugging options are very, very limited. Did I mention that they are expensive and time-consuming, too?
To its credit, Truffle lets you write test in both Solidity and Javascript. You could make a good case for writing both kinds of tests so that you test both the VM operations, with the former, and the JS-blockchain interface but in the interests of expediency let's just whip up a Solidity test for MyPrize, called TestMyPrize.
You'll notice that we don't test the metadata, and that the "claim" tests are commented out. This is because we can't actually test either of those things; Truffle's Assert. So the claim call will fail ungracefully if we try, teaching us nothing. I did mention that the whole ecosystem is still somewhere between alpha and beta software, right? So you wrote well, you copy-and-pasted, but it's a start your first smart contract.
You tested your first smart contract. You've got a blockchain address and a private key. Now how do all these ingredients bake together into a delicious cake running in production? Pardon the mixed metaphor; I'm hungry as I type this. You could run an Ethereum client like Geth or Parity or MetaMask on your local machine, point Truffle to it, and simply type "truffle deploy".
But let's go a little more low-level, and at the same time, keep you from having to run your own local node. Infura lets you submit signed Ethereum transactions to the Rinkeby testnet via the Internet, and contract deployment is just another kind of Ethereum transaction.
But you don't want to have to deal with a slew of cryptic bash commands either. You can compile and deploy entirely via Javascript, courtesy of the web3 library and the solc compiler. Let's take a look at that in a little more detail:. First off, create a new "node" subdirectory under your root "webthereum" directory. Then go there, run. You do have NPM installed and running on your system, right? Next, install a few new NPM packages:.
Note that we're using a particular version of web3, as the latest version released two days after this tutorial was published uses different method signatures only temporarily, though, as I understand it, until 1. Once that's done, simply return to your command line and type.