Step 1: Configure Metamask and obtain test Ether
In order to play around with any Ethereum smart contracts, it is useful to select a test network where Ether is worthless, i.e. not traded at exchanges. Since the official Ethereum/WebAssembly test network is no longer available, Oasis Ethereum is a suitable replacement. It is currently in developer preview and offers a faucet to obtain free Ether.
First of all, add a new network to Metamask.
The RPC URL for Oasis is https://rpc.oasiseth.org:8545
, the chain ID is 69.
There is also a block explorer available.
Your Metamask screen should look like this:
Now, navigate to the Faucet page and enter your Ethereum address. I highly recommend creating a fresh address for any experiments. In Metamask, you can do that by clicking on the account avatar in the top-right corner and selecting “create account”.
After a while, the Ether should appear in your account. You can also check out the list of recent transactions in the block explorer (below the table of recent blocks). It should only take a few seconds before a transaction sending you 1 Ether is confirmed by the network.
Now you’re good to go!
Step 2: Build & deploy a contract
The Oasis documentation comes with a few examples of smart contracts written in Solidity that can be compiled to WebAssembly.
For this tutorial, I’m going to assume that you already have a compiled WebAssembly artifact.
If you just want to play around, the ERC-20 example is suitable and comes with both the Solidity and the resulting WebAssembly file.
For the next step, you need to have Node.js installed together with the web3
package.
You can install that package using npm install -g web3
.
Now we’re ready to deploy. Fire up a Node console:
We instantiate a fresh web3
instance and let it connect to the Oasis RPC server.
In principle, we could run our own client locally, but the above is easier (and also what Metamask does).
Now, we need to read the compiled contract:
Note that data
should contain a hex-encoded string without a newline at the end:
All WebAssembly binary artifacts start with the hex string 0061736d01
.
Before we can deploy, we need to unlock an account in web3
.
Normally this would be done automatically by Metamask, but we’re not running in a browser context here, so we’ll have to do it manually.
Go to your account details in Metamask and click on “export private key”.
The private key should also be a string of hex digits.
Don’t tell anyone about your private key!
You should keep it safe.
Register it in web3
:
Finally, we can deploy the contract:
This will take a while and just print undefined
.
Check out the block explorer and it should show a transaction that creates a new contract.
You can also print the details in the Node console:
Congrats, your contract has been deployed!
Step 3: Talk to the contract
This is where it gets a little difficult.
Usually, contract development can be done with e.g. the Remix IDE, but it doesn’t understand WebAssembly.
Consequently, it can only trigger methods that are executed on the blockchain.
On the other hand, it’ll throw errors for view
methods, because it can’t interpret them.
If you have the ABI for your Solidity code (e.g. for the ERC-20 contract), you can interact with it in Node.
If you call balanceOf
with the address that you used to create the contract (not the secret key), it should print 1000
, since that is the initial amount of tokens created for you in the contract.
You can also try compiling the same Solidity code in Remix and then adding the contract manually.
It should be possible to call the transfer
method from Remix to transfer tokens from account to another.
In my case, I transferred 1 token to another address, which means that querying the balance again will print 999
:
That’s it! The development of EWASM is ongoing, so some things might break. However, the possiblities are exciting, since it is theoretically possible to run the same WebAssembly code in the browser and on Ethereum.