From 29bc873545c463d75f81cb33287752ce1932780d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Thu, 18 Jun 2020 12:41:31 +0200 Subject: [PATCH] Site updated: 2020-06-18 12:41:30 --- atom.xml | 70 ++-- chat/index.html | 4 +- community/index.html | 4 +- index.html | 4 +- .../2017/06/28/embark-2-5-released/index.html | 2 +- .../2017/10/25/embark-2-6-released/index.html | 2 +- .../2018/05/04/embark-3-0-released/index.html | 2 +- .../2018/06/20/embark-3-1-released/index.html | 2 +- .../index.html | 30 +- .../index.html | 14 +- .../index.html | 2 +- .../index.html | 2 +- .../index.html | 24 +- .../index.html | 2 +- .../index.html | 2 +- .../03/18/upgrading-to-embark-4/index.html | 2 +- .../03/19/introducing-embark-4/index.html | 2 +- .../07/23/whats-new-in-embark-4.1/index.html | 2 +- .../index.html | 2 +- .../index.html | 2 +- .../index.html | 2 +- .../09/web3-what-are-your-options/index.html | 2 +- .../09/take-back-the-web-hackathon/index.html | 2 +- .../2020/01/13/announcing-embark-5/index.html | 2 +- news/2020/01/28/embark-5-1/index.html | 2 +- news/2020/01/29/subspace-1-2/index.html | 2 +- .../01/30/dapp-frontend-security/index.html | 2 +- news/2020/02/11/subspace-1-3/index.html | 2 +- .../17/decentralized-notifications/index.html | 2 +- news/2020/02/19/embark-5-2-release/index.html | 2 +- .../02/24/wasm-ewasm-what-and-why/index.html | 2 +- .../index.html | 2 +- news/2020/03/18/subspace-2.0/index.html | 2 +- .../2020-using-vyper-with-embark/index.html | 2 +- .../index.html | 2 +- news/index.html | 4 +- sitemap.xml | 318 +++++++++--------- 37 files changed, 264 insertions(+), 264 deletions(-) diff --git a/atom.xml b/atom.xml index aaff2728..0cedd105 100644 --- a/atom.xml +++ b/atom.xml @@ -6,7 +6,7 @@ - 2020-06-15T08:04:44.203Z + 2020-06-18T10:35:45.392Z https://blog.embarklabs.io/ @@ -24,7 +24,7 @@ https://blog.embarklabs.io/news/2020/04/02/2020-smart-contract-security-analysis-with-mythx/ 2020-04-01T22:00:00.000Z - 2020-06-15T08:04:44.203Z + 2020-06-18T10:35:45.392Z Embark dashboard with MythX

How MythX works

MythX scans for security vulnerabilities in Ethereum and other EVM-based blockchain smart contracts. MythX’s comprehensive range of analysis techniques — including static analysis, dynamic analysis, and symbolic execution — can accurately detect security vulnerabilities to provide an in-depth analysis report. These security analyses can be used throughout the development lifecycle to aid in preparation for a security audit. Using MythX during development eases the impact of a security audit and helps to build secure Smart Contracts from the ground up. The idea is that once MythX returns no technical errors, the contracts are ready for a full audit.

MythX detects the majority of vulnerabilities listed in the SWC Registry. The report will return a listing of all the weaknesses found in the code, including the exact position of the issue and its SWC ID. Analysis reports generated can be only accessed by the owner of the account (you).

Contracts are submitted to MythX using their API. A full list of all completed analysis reports can be seen in the MythX dashboard.

MythX dashboard with analyses

MythX was designed to work with third party security tools and developer plugins. This has paved the way to allow MythX integration in to Embark, by way of the Embark MythX plugin. The Embark MythX plugin allows developers to easily submit their contracts (all contracts, or just those that need it) for analysis and see the resulting report in the console.

Let’s walk through this and see how it can be done!

  1. Prerequisites
  2. Create a ÐApp
  3. Install the Embark MythX plugin
  4. Create a .env file with MythX credentials
  5. Run Embark
  6. Run some MythX commands
  7. Conclusion

Step 1. Prerequisites

Before we can submit our first contract for analysis, let’s take care of a few requirements.

Install Embark

Install Embark either globally or as a package in your ÐApp. The most simple way forward is to install Embark globally on your system:

yarn global add embark
# OR
npm i -g embark

The rest of this article will assume you have Embark installed globally, and therefore available from the CLI.

Create a MythX account

You’ll need to create a MythX account before any contracts can be submitted. The dashboard of this account will list all completed analyses. Signing up for a free plan is easy. The free plan is a great way to test out MythX’s features without forking over any dollary-doos. You may skip the step of connecting your Ethereum address with MetaMask if you’d like, as a username, password, and API key are sufficient to proceed with this tutorial.

Obtain a MythX API key

Once you’ve created a MythX account, we need to obtain an API key. This can be done on the Tools page. Simply enter your MythX password and click the “Generate API Key” button. We will use the API key in Step 4.

Step 2. Create a ÐApp

For this article, we will be creating a demo ÐApp to use as a base for submitting our first contract for analysis. However, if you already have a ÐApp with contracdts that you’d like to use instead, simply skip this step.

Creating an Embark demo is easy, simply run the following commands:

embark demo
cd embark_demo

This will create a ÐApp with one contract, SimpleStorage, which we will submit to MythX for analysis.

Step 3. Install the Embark MythX plugin

Installing the Embark MythX plugin in our ÐApp is extremely simple:

  1. Add the embark-mythx package to your ÐApp:
    yarn add embark-mythx
    # OR
    npm i embark-mythx --save
  2. Add the embark-mythx plugin to embark.json:
    // embark.json
    // ...
    "plugins": {
    "embark-ipfs": {},
    "embark-swarm": {},
    "embark-whisper-geth": {},
    "embark-geth": {},
    "embark-parity": {},
    "embark-profiler": {},
    "embark-graph": {},
    "embark-mythx": {} // <====== add this!
    },
    // ...

Step 4. Create a .env file with MythX credentials

Create a .env file in the root of your ÐApp. Add your MythX API key, username, and password like so:

MYTHX_API_KEY="1234...7890"
MYTHX_USERNAME="satoshi.nakamoto@gmail.com"
MYTHX_PASSWORD="abc123"

Step 5. Run Embark

Now that we have installed the plugin, let’s run Embark to get access to its dashboard:

embark run --nobrowser

Once Embark has completed bootstrapping, we should see a command prompt in the Embark dashboard at the bottom:
MythX dashboard with analyses

Step 6. Run some MythX commands

All functionality for the Embark MythX plugin can be accessed via the verify command.

Available commands

For a full list of available options and usage instructions, execute the help command in the console:

Embark (development) > verify help

We can see there are a few options for us to use and we can also see how they can be used:

Available Commands

verify <options> [contracts] Runs MythX verification. If array of contracts are specified, only those contracts will be analysed.
verify report [--format] uuid Get the report of a completed analysis.
verify status uuid Get the status of an already submitted analysis.
verify list Displays a list of the last 20 submitted analyses in a table.
verify help Display this usage guide.

Examples

verify --mode full SimpleStorage ERC20 Runs a full MythX verification for the SimpleStorage and ERC20 contracts only.
verify status 0d60d6b3-e226-4192-b9c6-66b45eca3746 Gets the status of the MythX analysis with the specified uuid.
verify report --format stylish 0d60d6b3-e226-4192-b9c6-66b45eca3746 Gets the status of the MythX analysis with the specified uuid.

Verify options

-m, --mode string Analysis mode. Options: quick, standard, deep (default: quick).
-o, --format string Output format. Options: text, stylish, compact, table, html, json (default: stylish).
-c, --no-cache-lookup Deactivate MythX cache lookups (default: false).
-d, --debug Print MythX API request and response.
-l, --limit number Maximum number of concurrent analyses (default: 10).
-t, --timeout number Timeout in secs to wait for analysis to finish (default: smart default based on mode).

Verify the SimpleStorage contract

Let’s take a peek to see how easy it is to analyse our SimpleStorage contract.
In the Embark console, execute the following command to submit our SimpleStorage contract for MythX security analysis:

verify

The results should look the following:
SimpleStorage security analysis

First, we can see that a MythX job was submitted, with a URL that takes us to the analysis job in the MythX dashboard.

Second, we can see from the security analysis output in the console that there is a warning marked “SWC-103”. Looking at the SWC Registry for SWC-103 help, we can remedy this by changing line 1 of our contracts/simple_storage.sol to:

pragma solidity 0.6.1;

Embark will detect the change in the contract and automatically recompile and redeploy our contract. We can then re-submit our contract for analysis:

verify

And voila!
SimpleStorage security analysis success

MythX has confirmed that we no longer have any security issues!

Viewing the submissions in the MythX dashboard

Open your browser and go to the MythX analyses page. After logging in, you should be able to see a list of all the contracts you’ve submitted for analyses.

Mythx Analysis List

Click in to each job and then in to each contract, and you will should see details of the security analysis, along with line numbers in the source and a preview of issues in the code at the bottom of the page.

Mythx Analysis Detail

Conclusion

We have seen firsthand how the Embark MythX plugin can assist in our development workflow, allowing us to analyse the security of our contracts throughout the development lifecycle. While we have only scraped the surface as to the complexity of the MythX’s security analysis, the Status Embark + MythX article dives in to more detail on common contract vulnerabilities and how they are presented using the Embark MythX plugin.

]]>
@@ -54,7 +54,7 @@ https://blog.embarklabs.io/news/2020/03/24/2020-using-vyper-with-embark/ 2020-03-23T23:00:00.000Z - 2020-06-15T08:04:44.203Z + 2020-06-18T10:35:41.816Z Vyper Language

Why Vyper?

Vyper is a contract-based approach to writing Ethereum smart contracts and is aimed at bringing “increased security and readability through a minimalist approach” to the smart contract landscape.

Although Vyper has gained some popularity in recent times as a security-focused alternative to Solidity, the simplicity required for such a case leaves out some features of Solidity that developers have come to appreciate.

In its current form, Vyper is python-based, but due to the Consensys audit highlighting some major security flaws, the Ethereum Foundation has decided to concurrently build a Rust-based Vyper, while community devs aim to support the original Python-based version.

Using Vyper in production

Because of the inherent security flaws, please do not use Python-based Vyper in production projects until the vyperlang team has created a production-ready release. Embark will also support Rust-based Vyper once it is released.

Irrespective of the security audit, Vyper is perhaps the most intuitive and easiest to quickly learn language for reading and writing Etheruem contracts - even without any prior knowledge or experience in programming. Additionally, Embark has created a plugin that makes it just as easy to compile Vyper contracts in an Embark ÐApp!

Let’s see just how easy this is!

  1. Prerequisites
  2. Create a ÐApp
  3. Install the Embark Vyper plugin
  4. Add a Vyper contract to your ÐApp
  5. Run Embark
  6. Interact with your contract in Cockpit

Step 1. Prerequisites

Before we can write our first Vyper contract, let’s take care of a few requirements.

Install Embark

Install Embark either globally or as a package in your ÐApp. The most simple way forward is to install Embark globally on your system:

yarn global add embark
# OR
npm i -g embark

The rest of this article will assume you have Embark installed globally, and therefore available from the CLI.

Install Vyper

If you haven’t already done so, make sure to install Vyper. As the documentation recommends, be sure to use a Python virtual environment when installing Vyper, as it will keep your system-wide packages from being polluted.

Step 2. Create a ÐApp

For this article, we will be creating a demo ÐApp to use as a base for creating our first Vyper contract. However, if you already have a ÐApp that you’d like to add Vyper contracts to, simply skip this step.

Creating an Embark demo is easy, simply run the following commands:

embark demo
cd embark_demo

Step 3. Install the Embark Vyper plugin

Installing the Embark Vyper plugin in our ÐApp is extremely simple:

  1. Add the embark-vyper package to your ÐApp:
    yarn add embark-vyper
    # OR
    npm i embark-vyper --save
  2. Add the embark-vyper plugin to embark.json:
    // embark.json

    // ...
    "plugins": {
    "embark-ipfs": {},
    "embark-swarm": {},
    "embark-whisper-geth": {},
    "embark-geth": {},
    "embark-parity": {},
    "embark-profiler": {},
    "embark-graph": {},
    "embark-vyper": {} // <====== add this!
    },
    // ...

Step 4. Add a Vyper contract to your ÐApp

First, let’s delete the SimpleStorage Solidity contract that comes with the Embark Demo by default:

rm contracts/simple_storage.sol

Next, let’s create a Vyper contract in the contracts folder of our ÐApp, ie contract/SimpleStorage.vy (case is important):

# contracts/SimpleStorage.vy

storedData: public(int128)

@public
def __init__(_x: int128):
self.storedData = _x

@public
def set(_x: int128):
self.storedData = _x

The function of this contract is simple: upon creation (deployment), it will store the value we initially give it during deployment. We also have access to a set() that will allow to set the value stored in the contract.

Changing the contract filename

Because Vyper’s constructor is called __init__, Embark must take the contract class name from the file name. If you’d prefer to change the name of your contract file to something else like contracts/simple_storage.vy, you’ll need to update config/contract.js to allow Embark to match a contract configuration to a contract file:

// config/contracts.js

module.exports = {
default: {
// ...
deploy: {
simple_storage: { // NOTE: this only needs to be changed if the contract filename was changed
fromIndex: 0,
args: [100]
}
}
// ...
}
}

Please see the Embark smart contract configuration documentation for more information on how to configure contracts in Embark.

Step 5. Run Embark

Now that we have installed and configured everything, let’s run Embark and watch the magic!

embark run --nodashboard --nobrowser

Assuming we kept the file name SimpleStorage.vy from step 4, we should see the following output in the console:

compiling Vyper contracts...
deploying contracts
Deploying SimpleStorage with 144379 gas at the price of 2000000000 Wei. Estimated cost: 288758000000000 Wei (txHash: 0x370a864b12b1785b17180b2a10ec2f941a15638eeffadfecf5bbe68755a06f14)
SimpleStorage deployed at 0x5Baf4D88bC454537C51CEC7568a1E23400483abc using 135456 gas (txHash: 0x370a864b12b1785b17180b2a10ec2f941a15638eeffadfecf5bbe68755a06f14)

Troubleshooting

There are a few common issues you may experience when Embark attempts to compile and deploy your Vyper contracts.

Vyper is not installed on your machine

Vyper is not installed on your machine
You can install it by visiting: https://vyper.readthedocs.io/en/latest/installing-vyper.html

This means that the binary vyper cannot be found on your machine. If you’re running *nix, you can verify this by running:

which vyper
# expected output: path/to/virtual-env/bin/vyper
# actual output: vyper not found

If vyper was installed in a virtual environment, you may have forgotten to activate the environment, ie:

source path/to/vyper-env/bin/activate

If vyper was not installed in a virual environment, it most likely means your PATH environment variable needs to be updated to point to the directory where the vyper binary is installed. You can inspect your PATH environment variable by:

echo $PATH

And update it with the path to the directory containing the vyper binary using:

export PATH=/path/to/vyper/dir:$PATH

Error deploying contract

SimpleStorage has no code associated
did you mean "simple_storage"?
deploying contracts
Error deploying contract simple_storage
[simple_storage]: Invalid number of parameters for "constructor". Got 0 expected 1!
Error deploying contracts. Please fix errors to continue.

This means that the contract simple_storage is a file in contracts/ and expected to be deployed by Embark, but is not configured for deploy in the contracts configuration. This could be the result of changing the filename of the Vyper contract to simple_storage.vy, but not updating the contracts configuration, as outlined in step 4.

Step 6. Interact with your contract in Cockpit

Given that the Vyper contract has the same functionality as the SimpleStorage.sol that ships with the default Embark demo, we could still run the Demo application and view that its functionality works as expected. However, an even easier approach to interacting with our deployed Vyper contract is to use Cockpit.

In your browser, open Cockpit using the link provided in the console, ie http://localhost:55555?token=<token from console> NOTE: the token parameter is provided in the Embark console once Embark has finished building pipeline files. This token can be copied to the clipboard by typing token in the console command prompt.

Cockpit with Vyper contract

At the bottom, you should see your SimpleStorage contract. Click the name of the contract to open the interaction view:

SimpleStorage vyper contract in Cockpit

Expand the set method interaction by clicking the set header. Enter a value to set the storedData variable in the contract to, ie 999:

SimpleStorage set value to 999

Then click “Send”. You should see the inputs and the resulting transaction hash:

SimpleStorage set result

Additionally, you should see the transaction printed in Embark’s console:

Blockchain> SimpleStorage.set(999) | 0x71a9341fc8d52e53e11692b642b806a41da7aa8ed2b3ed6a975314639a131d32 | gas:26447 | blk:14 | status:0x1

Conclusion

Vyper is proving to be a useful language for writing secure smart contracts. While the latest security audit is looming, the EF is working on a Rust-based implementation that will hopefully propel Vyper towards its goals of being a security-centric, simplistic smart contract language.

In the meantime, we can use Embark to write and deploy our Vyper contracts using the Embark Vyper plugin.

]]>
@@ -78,7 +78,7 @@ https://blog.embarklabs.io/news/2020/03/18/subspace-2.0/ 2020-03-17T23:00:00.000Z - 2020-06-15T08:04:44.202Z + 2020-06-18T10:35:40.667Z Subspace 2.0

New to subspace? Check out the website at http://subspace.embarklabs.io/

Introducing Subspace React’s components

A new set of components were created to simplify the usage of Subspace within React projects. Learn more about them here

Install

You can install it through npm or yarn:

npm install --save @embarklabs/subspace-react web3 rxjs

Usage

SubspaceProvider

To use most of the subspace-react components, you need to wrap your app with the <SubspaceProvider web3={web3} /> component. This will make Subspace available to any nested components that accesses it via the useSubspace hook or has been wrapped in the withSubspace higher order component. Any React component might use Subspace so it makes sense to add the provider near the top level of your DApp. The SubspaceProvider requires a web3 object to interact with an Ethereum node.

// index.js
import React from 'react'
import ReactDOM from 'react-dom'
import MyApp from './MyApp'
import { SubspaceProvider } from '@embarklabs/subspace-react';

const web3 = new Web3("ws://localhost:8545");

const rootElement = document.getElementById('root')
ReactDOM.render(
<SubspaceProvider web3={web3}>
<MyApp />
</SubspaceProvider>,
rootElement
);

useSubspace

Rather than relying on global variables or passing Subspace through props, the easiest way to access Subspace features is via the useSubspace hook. Be sure that your entire DApp is wrapped with a <SubspaceProvider /> to have it available throughout the component tree.

// index.js
import React from 'react'
import { useSubspace } from '@embarklabs/subspace-react';

const MyComponent = () => {
const subspace = useSubspace();

// do something....
// subspace.trackBalance(web3.eth.defaultAccount);

return ...;
}

export default MyComponent

withSubspace

This higher order component is provided as an alternative to the useSubspace hook. This injects the subspace property with an already initialized Subspace instance. Just like with the hook, your entire DApp needs to be wrapped with a <SubspaceProvider />.

// index.js
import React from 'react'
import { withSubspace } from '@embarklabs/subspace-react';

const MyComponent = (props) => {
// do something....
// props.subspace.trackBalance(web3.eth.defaultAccount);

return ...;
}

export default withSubspace(MyComponent);

observe

This function is useful to make your component subscribe to any observable props it receives when the component is mounted and automatically unsubscribes when the component is unmounted. It can be used with any kind of observables. This component already existed in the previous version of Subspace. The only difference is that it has been moved to the subspace-react package.

import { observe } from '@embarklabs/subspace-react';

const ObserverComponent = observe(WrappedComponent);
Example usage:
const MyComponent = ({eventData}) =>  {
// Handle initial state when no data is available
if (!eventData) {
return <p>No data</p>;
}

return <p>Value: {eventData.someReturnValue}</p>
};


const MyEnhancedComponent = observe(MyComponent);


const SomeOtherComponent = () => {
const myObservable$ = MyContractInstance.events.MyEvent.track({fromBlock: 1});
return <MyEnhancedComponent myProp={myObservable$} />;
}

Breaking changes

  • Peer dependencies
    Installing subspace requires explicitly installing web3 and rxjs as peer depenencies, while before they were installed as dependencies of the package

    npm install --save @embarklabs/subspace web3 rxjs
  • Web3
    In the previous version, Subspace received a web3.js provider in the constructor. This new version receives instead a Web3 object:

    const web3 = new Web3("http://localhost:8545");
    const subspace = new Subspace(web3);
  • init() is async.
    This means that you need to wait until Subspace has been initialized to use its features:

    await subspace.init();
  • The observe higher-order component was moved from the @embarklabs/subspace package to the new @embarklabs/subspace-react package.

  • If no call interval is specified in the Subspace options, it will use the average blocktime as the interval of time to poll the contract addresses for changes in state or balance.

Maintenance / Bug fixes

  • Major refactor increasing the usage of observables within the subspace package: Subjects in tracking functions were replaced by observables (they still keep the same functionality), New blocks and interval timers are done via observables.
  • Subscriptions to web3js are handled automatically depending on the number of subscribers. The subscriptions are kept if there is at least one observer subscribed to an observable, otherwise the subscription is closed and will be reopened once there’s a new subscriber.
  • The checksum of the address to be tracked is performed automatically when performing balance tracking
  • Dropped the usage of web3-utils.
  • Simplified the logic for tracking events
  • Added Lerna to the repository to organize the different packages, as well as making the documentation part of the project
  • Updated all the examples to use the latest subspace-react components

Contributions

Subspace and the entire Embark labs organization are open source. As such, we welcome contributions and input from the community using the product.

If you’d like to contribute to Subspace, please fork this repository, fix, commit and send a pull request for the maintainers to review and merge into the main code base. If you wish to submit more complex changes though, please check up with the core devs first on #embark-status channel to ensure those changes are in line with the general philosophy of the project and/or get some early feedback which can make both your efforts much lighter as well as our review and merge procedures quick and simple.

]]>
@@ -106,7 +106,7 @@ https://blog.embarklabs.io/news/2020/03/04/2020-Blockchain-Developer-Survey/ 2020-03-03T23:00:00.000Z - 2020-06-15T08:04:44.202Z + 2020-06-18T10:35:33.413Z 2020 Developer Survey

Abstract

The blockchain developer mindset is not well understood. This survey initiative was created in an effort to shed light on the current sentiment in the blockchain developer community. The echo chamber of crypto Twitter doesn’t allow for thoughtful discussion, and we felt there was a need for a survey of the community outside of that forum. Though our sample size is small we are happy with the input we received from developers around the world.

In addition to the survey form we conducted interviews on Zoom to ask more detailed questions. This community is truly global in scope. What is most apparent from this survey is that the world of blockchain development is just getting started, and there is more we can do to make it accessible to those looking to invest their time in learning about this exciting technology.

The main takeaways of this study are:
Popular sentiment on Crypto Twitter does not represent blockchain developer attitudes
There is general interest in developing across chains, and across public/enterprise blockchain platforms
Developers care about decentralization, but don’t believe it is something the general public cares about at all

Methodology

For this inaugural survey we focused on the sentiments of developers in the Ethereum community to access the significant volume of work that occurs in Ethereum. Next year we will expand the sample to a much broader swath of the blockchain community.

We focused on three key areas of questioning in this survey:

  • Technology
  • Sentiment
  • Motivation

Within these sections we hoped to understand better how developers are spending their time and what drives them to be a part of this industry. We used a qualitative, open-ended question method in order to generate a broad range of ideas. In future studies we will use this data to develop more precise attitude and behavior measures.

Chains

Of all respondents surveyed, a little under fifty five percent identified as exclusively developing on Ethereum. While many responded that they develop on many platforms including Ethereum, only six percent of respondents said they do no development work on Ethereum, excluding EVM compatible chains, that number drops to two percent.

Chains

What was surprising was the number of people who are working on EVM compatible chains (cue RUN EVM T-Shirts.) The most popular of these were EOS and and Cosmos in that order. What was perhaps most surprising was the fact that Lightning Network was more popular than any EVM compatible platform. This is most surprising because very little effort was made to get this survey in the hands of Lightning developers. This means either Lightning devs participated at a higher per capita rate than Ethereum devs, or that many Ethereum devs are self selecting to learn about Lightning.

Responses to this question show that Ethereum still dominates mindshare in the Ethereum community, but there is a diversity of effort happening. It will be interesting to see how these results develop over time, especially considering the concern around ETH2 expressed later in the survey.

Tools

The developer tools section was dominated by Truffle Suite with 68% of respondents saying they use it predominantly. As an early Ethereum project, this project deserves a lot of credit for making it easier to get into Ethereum development than other chains. On the framework side of things Embark came in close second with 34% of developers reporting using the toolsets.

Tools

That little green sliver is one of the more interesting findings. We anticipated that there would be some who were experimenting with Lightning development but were surprised to find that number reaching close to 10% of respondents. With the recent announcement from Lightning Labs that they are rolling out Loop, a non-custodial Bitcoin to Lighting on-ramp/off-ramp, we’re excited to see where this community goes over the next few months. It will also be useful to see how Lightning developers differ from Ethereum-only, or other EVM developers in future studies.

Motivation

Motivation

There were a small number of participants who answered that their reason for being interested in blockchain development is to “make number go up”. Beyond the financial motivation, most people gave more than one answer to these questions. Among the most popular were smart contracts and their composable business logic.

What was surprising was that philosophical reasoning wasn’t given more broadly. We had expected almost everyone to select censorship resistance and sound money for their reasoning to join crypto. As it stands only 28% gave these answers. DeFi, DAOs, and NFTs made a strong showing with close to half of responded saying they were a motivation. There seems to be a healthy mix of motivations for getting into the blockchain developer ecosystem.

Public/Private Chains

Popular sentiment among the crypto community is that private chains are not really a serious endeavor. Enterprise chains have gotten off to a slow start ever since the “blockchain not Bitcoin” movement of the 2015 era. Given this, we were surprised to see a strong interest in developing for these projects.

Public/Private Chains

Over half of respondents indicated their interest in building on private chains, when you include those willing to do it, “for a price” that number jumps to over three quarters. The remaining quarter have varying degrees of opposition to building on private chains. We can see the arguments on Twitter forming, “what do you consider a private chain?” Though we could have done a better job clarifying, this question was intended to mean an enterprise chain. It was refreshing to see the willingness of people to work on things that are outside the overton window that crypto Twitter advocates for.

Perception of public sentiment

Back in the wild days of 2017 it was believed you could take any business model, throw the word Decentralized in front of it and people would flock to you. While this may have been true in the sense that people would throw money at your ICO, it hasn’t yet proven true in the race for acquiring real users.

Perception of public sentiment

In our survey we saw that a majority of developers agreed that they don’t believe users care about decentralization. This has been adopted by companies like Brave and Dharma which have adopted marketing that either doesn’t mention crypto at all or mentions it as a side note. That being said, decentralization was in fact important to developers with a majority of respondents saying that while users may not care, the developers still do.

ETH2 perception

ETH2 Perception

As should be clear from the chart, an overwhelming majority of developers in this survey are concerned about ETH2 and its implications. The people working on ETH2 are heads down solving really hard problems. However, this response speaks to the need for better communication of the work that is happening, the problems and solutions involved, timelines, and what it will mean for developers in Ethereum in time.

One interesting finding is the small minority of users who are not at all concerned about ETH2. When digging deeper on calls some engineers mentioned that they have done research and understand the complexity involved in bringing it online. Perhaps the answer is DYOR and not believing the naysayers on Twitter, or perhaps outreach and better communication to combat ETH2 critics could ease concerns.

Who are you building for?

Who are you building for?

We wanted to see what type of reach people are getting with their dapps. We were excited to see that more than a few people had over five thousand users on their platform. Over six percent of developers said they have over a thousand users. On the other side of this however over ninety percent said that they have less than a thousand daily active users on their dapps.

We’re excited to see these numbers change over the next year. There are some outlier applications like Brave with twelve million active users, but we hope to see even more breakout successes such as the recent launch of Dharma’s mainstream focused application.

Impediments to blockchain development
We asked people what their biggest impediments are to building blockchain tools. In our Zoom interviews we had a lot of input on this question. The general feedback was that the learning required is vast. One interviewee said that learning web3 development was as difficult as learning to become a developer at all. The other interesting anecdote was the ‘analysis paralysis’ that comes from choosing which ecosystem/chain to work on.

This question gave the most diverse set of opinions, and many developers chose more than one response. Unsurprisingly, ‘private key management/education” eked out a win as the most popular response. We hadn’t expected regulatory clarity to be such a large issue, but it was also substantial. Though not the most popular answer, we would postulate that this is actually the part of the reason we don’t see more founders getting into crypto. It’s hard enough to build a company, let alone one that has a threat of prosecution hanging over it.

Conclusion

Several key findings stand out from this report:

  • Gitcoin respondents overwhelmingly selected Ethereum as the chain to work on, but also worked on other platforms including Bitcoin
  • Truffle remains the dominant developer toolkit for Ethereum, with Embark taking a smaller share of the use
  • The technical properties of blockchain are the main drivers for a developer choosing to get into the space
  • Private chains are an appealing option for developers interested in working full time in blockchain space
  • Developers are under no illusions that the public cares about decentralization, but they themselves do hold that ideal as important
  • The majority of developers surveyed are concerned about ETH2 and its implications for the future of Ethereum
  • Most dApps still have under a thousand daily active users
  • Regulatory clarity and private key management are still major impediments to the creation of dApps

What was most interesting however was the 1:1 interviews we conducted. We spoke to devs in Russia, Japan, the US and Egypt. These less formal interviews helped put a face on the developers. What was apparent among these people was the deep passion for the potential of this technology. People all over the world are coming into crypto with open minds and open hearts.

We can often feel like getting into crypto now is getting in “late”, but nothing could be further from the truth. The developer experience is still clunky. The learning curve is still steep. The regulation is still ambiguous. There is almost no aspect of being a developer in blockchain that could be considered “mature.”

Getting to peer into the mind of blockchain developers is something that needs to be done more often to ensure that we’re building the right tools to help foster growth in decentralized public development. We’re hoping to make this an annual report and welcome more input on distribution, categorization and methodology.

As the current crypto winter thaws we’re starting to see shoots of new growth. It’s an exciting time to be in cryptocurrency, even more so as a developer. Thank you to all the people who participated in this survey, and especially the team at Gitcoin for making it possible. We’re proud to be part of this ecosystem and look forward to supporting more developers as they come into the space.

]]>
@@ -130,7 +130,7 @@ https://blog.embarklabs.io/news/2020/02/24/wasm-ewasm-what-and-why/ 2020-02-23T23:00:00.000Z - 2020-06-15T08:04:44.202Z + 2020-06-18T10:35:34.128Z eWasm

This article is the third in my series of articles based on the frontend of the decentralised web. Throughout the series we’ll look at Web3.js & accessing the Ethereum Blockchain client-side, frontend security for DApps, how eWasm / WebAssembly has become the “4th language of the web”, and we’ll build a realtime Blockchain explorer app with Phoenix LiveView!

Introduction

As I mentioned in the foreword of this article series; I read recently that WebAssembly (Wasm) has become the 4th language for the decentralised web, and as I took time to really consider that notion, I came up with some points both for, and against it.

WebAssembly is a way of taking code written in programming languages other than JavaScript and running that code in the browser.

Basically; Wasm can be summarised as an efficient binary format. This binary format serves as a compilation target, which can be compiled to execute at native speed, by taking advantage of common hardware capabilities available over a range of platforms – including mobile and IoT.

Today, I’d like to show you what I’ve discovered about Wasm, and in keeping with my decentralised web frontend series; in particular – eWasm (Web Assembly for Ethereum).

‘Ethereum flavoured WebAssembly is a proposed redesign of the Ethereum Smart Contract execution layer using a deterministic subset of WebAssembly.’

Firstly; Wasm Goals

The original design goals of WebAssembly are the following:

  • Fast: executes with near native code performance, taking advantage of capabilities common to all contemporary hardware.
  • Safe: code is validated and executes in a memory-safe, sandboxed environment preventing data corruption or security breaches.
  • Well-defined: fully and precisely defines valid programs and their behaviour in a way that is easy to reason about informally and formally.
  • Hardware-independent: can be compiled on all modern architectures, desktop or mobile devices and embedded systems alike.
  • Language-independent: does not privilege any particular language, programming model, or object model.
  • Platform-independent: can be embedded in browsers, run as a stand-alone VM, or integrated in other environments.
  • Open: programs can interoperate with their environment in a simple and universal manner.

As far as I can see, Wasm has indeed achieved the above goals.

How Wasm Works

WebAssembly delivers significant performance gains because modern browser engines can parse and execute its binary format an order of magnitude faster than vanilla JavaScript itself. So; you can take C/C++ code, translate it into Wasm using a compiler tool, and load the generated Wasm module into a JavaScript app, where it will be executed by the browser.

wasm editor

From what I’ve read, I believe one of the biggest ideas behind Wasm; is to make it possible to run media-rich game engines, and support such graphics-heavy games in-browser, without the use of plug-ins. It also has non-web applications such as the Internet of Things, mobile apps and JavaScript virtual machines.

If you want to get started with Wasm, you can download a precompiled toolchain to compile C/C++ to WebAssembly by running the following:

$ git clone https://github.com/emscripten-core/emsdk.git
$ cd emsdk
$ ./emsdk install latest
$ ./emsdk activate latest

Wasm to eWasm (Ethereum flavoured WebAssembly)

Great!   Now that we have Wasm introduced, explained, and out-of-the-way, we can move onto the (arguably) more important topic – eWasm! So.. what exactly is eWasm?

Simply put; eWasm is a restricted subset of Wasm to be used for Smart Contracts in Ethereum. Much like Wasm, one of the biggest goals of eWasm is to be fast & efficient. To truly distinguish Ethereum as the *”World Computer”,* we need to have a super performant VM. The current architecture of the VM is one of the greatest blockers to raw performance.

As I mentioned in the Wasm section above; WebAssembly aims to execute at near native speed by taking advantage of common hardware capabilities available on a wide range of platforms. This will open the door for Ethereum to a wide array of uses that require performance/throughput.

Security is another key goal. With the added performance gains from eWasm we will be able to implement parts of Ethereum such as the precompiled Smart Contract in the VM itself which will minimise our trusted computing base. WebAssembly is currently being designed as an open standard by a W3C Community Group and is actively being developed by engineers from Mozilla, Google, Microsoft, and Apple.

eWasm Goals

Goals of the eWasm project:

  • To provide a specification of eWasm Smart Contract semantics and the Ethereum interface.
  • To provide an EVM transcompiler, preferably as an eWasm Smart Contract.
  • To provide a VM implementation for executing eWasm Smart Contracts.
  • To implement an eWasm backend in the Solidity compiler.
  • To provide a library and instructions for writing Smart Contracts in Rust.
  • To provide a library and instructions for writing Smart Contracts in C.
  • To provide a metering injector*, preferably as an eWasm Smart Contract.

*Metering injector is a transformation tool inserting metering code to an eWasm Smart Contract.

Toolchain Compatibility: A LLVM front-end for Wasm is part of the MVP. This will allow developers to write Smart Contracts and reuse applications written in common languages such as C/C++, go and rust.

Portability: Wasm is targeted to be deployed in all the major web browsers which will result in it being one of the most widely deployed VM architecture. Smart Contracts compiled to eWasm will share compatibility with any standard Wasm environment. Which will make running a program either directly on Ethereum, on a cloud hosting environment, or on one’s local machine - a frictionless process.

Optional And Flexible Metering: Metering the VM adds overhead but is essential for running untrusted code. If code is trusted, then metering maybe optional. eWasm defines metering as an optional layer to accommodate for these usecases.

eWasm Performance

This chart shows the Wasm-based EVM setting new performance records in a typical sha1 benchmark experiment. It is shown here against Evmone (a C++ implementation) and Cita-vm (a rust implementation).

Wasm EVM benchmarking

I took this benchmark directly from the eWasm GitHub, I didn’t run the tests myself. You can find the full details, alongside more benchmark testing here.

eWasm and Embark

With Status / Embark being at the forefront of Decentralised technology; naturally we are very on-board with eWasm and the improvements it brings to the EVM.

Check out two of our extremely talented engineers; Pascal and Eric giving a talk on eWasm at Devcon 5:

Conclusion

Since its launch a couple of years ago; Wasm has grown from an impressive concept into an even-more-impressive piece of technology. Alongside which a flourishing community has grown.

Although, personally, I have my reservations in agreeing with the statement that Wasm is the “most significant new technology to come to the web platform in a decade”… It definitely is impressive, and I can certainly see how & why it has made a good impact, and does have a lot of devs working on relative tooling and implementations. This was an interesting (albeit old) article I just stumbled across this morning: Life: A secure, blazing-fast, cross-platform WebAssembly VM in Go.

Between Wasm & eWasm, I’m excited to see the performance boosts we can achieve as they grow, and as our own DApp development grows. I’d love to find an excuse to build out a big piece of technology with eWasm, but this would be a big project that I just don’t have time for right now!

Thanks again for reading my series on DApp frontend. The next (and unfortunately) final article in the series will be released next week, and is sure to be a real humdinger! We’ll be building a realtime crypto/blockchain tracker app using Elixir & Phoenix’s newly popular LiveView!

- @rbin

]]>
@@ -154,7 +154,7 @@ https://blog.embarklabs.io/news/2020/02/19/embark-5-2-release/ 2020-02-18T23:00:00.000Z - 2020-06-15T08:04:44.202Z + 2020-06-18T10:35:35.186Z

Embark Labs

Embark 5.2

In this release of Embark we introduce some new features such as proxy contract support and scripts execution. We’re also introducing some important deprecation warnings in preparation for Embark 6.0. We are making Embark lighter and more modular and to that end some modules that come by default will become optional plugins instead.

Proxy Contract Support

Proxy contracts are powerful tools usually used in more complex Dapps. They can be used for smart contracts that can be upgraded or to alleviate the deploy cost of multiple instances of a contract.

However, interacting with Proxy contracts is usually difficult, because you have to point the base contract to the address of the Proxy for it to work.

Not anymore! Embark now supports a contract configuration named proxyFor.

With it, you can specify that a Proxy contract is, well, a proxy for another one. Here’s an example:

deploy: {
Proxy: {
deploy: false
},
BaseContract: {
args: ["whatever the base contract needs"]
},
ContractInstance: {
instanceOf: "Proxy",
proxyFor: "BaseContract",
args: ["0x", "$BaseContract"]
}
}

With this configuration, our ContractInstance is an instanceOf Proxy and a proxyFor BaseContract.
This is why we point to BaseContract in the ContractInstance arguments.
The arguments themselves depend on the implementations of your BaseContract and Proxy contract.

Note that you could have used Proxy itself as a proxyFor BaseContract, but it’s usually more intuitive to use instanceOf and then resolve the contract instance with the new name you gave it (ContractInstance in this case).

Once the smart contracts are deployed, all you have to do is:

import ContractInstance from 'path/to/artifacts/contracts/ContractInstance';

For more information check the documentation for Proxy Contract Support

Scripts Runner

Embark uses a powerful declarative configuration of smart contracts. The parameters of smart contracts, how they relate to each other, what actions to do when they are deployed are described in a declarative configuration file and Embark then takes care of deploying the smart contracts in a way that reflects the configuration described.

Although this system covers the vast majority of cases, there are some situations where having the ability to execute scripts separately is useful, to that end Embark 5.2 now includes support for scripts, this can be used as migrations or as isolated scripts.

A script is really just a file with an exported function that has special dependencies injected into it. Here’s what it could look like:

modules.exports = async ({ contracts, web3, logger}) => {
...
};

The injected parameters are:

  • contracts - A map object containing all of your Smart Contracts as Embark Smart Contract instances.
  • web3 - A web3 instances to give you access to things like accounts.
  • logger - Embark’s custom logger.

Scripts can be located anywhere on your machine, but should most likely live inside your project’s file tree in a dedicated folder.

To run a script, use the CLI exec command and specify an environment as well as the script to be executed:

$ embark exec development scripts/001.js

The command above will execute the function in scripts/001.js and ensures that Smart Contracts are deployed in the development environment.

If you have multiple scripts that should run in order, it’s also possible to specify the directory in which they live in:

$ embark exec development scripts

Embark will then find all script files inside the specified directory (in this case scripts) and then run them one by one. If any of the scripts fails by emitting an error, Embark will abort the execution. Scripts are executed in sequence, which means all following scripts won’t be executed in case of an error.

Tracking scripts

Just like Smart Contract deployments are tracked, (migration) scripts can be tracked as well. Since scripts can be one-off operations, Embark will not track whether they have been executed by default. Users are always able to run a script using the exec command as discussed in the previous sections.

To have Embark “remember” that a certain script was already run, you can use the --track option of the exec command, which will force tracking for this particular script:

$ embark exec development scripts/001.js --track

If we try to run the script again with the --track option, Embark will notice that the script has already been executed and tell us that it’s “already done”.

$ embark exec development scripts/001.js --track
.. 001.js already done

If however, we don’t provide the --track flag, Embark will execute the script as usual.

For cases in which we do want to track a set of scripts, especially when the main use case are migration operations, we can put our scripts in a special “migrations” directory. All scripts inside that directory will be tracked by default.

The directory can be specified using the migrations property in your project’s embark.json:

{
...
migrations: 'migrations'
}

If no such property is specified, Embark will default to “migrations”. Running any script or set of scripts is then automatically tracked.

For more information check the documentation for Deployment scripts

Support using $accounts in ens registrations

It’s now possible to specify an account to be registered as an ENS domain:

config({
namesystem: {
enabled: true,
register: {
rootDomain: "embark.eth",
subdomains: {
"mytoken": "$MyToken",
"account": "$accounts[0]"
}
}
}
})

Improved compatibility

In the test suite, in order to improve compatbility with other tools, it is now possible to also use the artifacts.require as an alternative to get a smart contract instances:

const AnotherStorage = artifacts.require('AnotherStorage');

Transition to Embark 6.0

To make Embark even lighter and faster, starting in Embark 6.0, the following will be installable plugins that will no longer come with Embark by default:

  • embark-geth
  • embark-graph
  • embark-ipfs
  • embark-parity
  • embark-profiler
  • embark-swarm
  • embark-whisper-geth
  • embarkjs
  • embarkjs-ens
  • embarkjs-ipfs
  • embarkjs-swarm
  • embarkjs-web3
  • embarkjs-whisper

Embark 5.2 will issue warnings about these needing to be installed & configured as plugins to make transition to Embark 6.0 easier.

To make current projects compatible, ensure embark is added as a devDependency to your project, as well as any plugins. For e.g

In package.json add "embark-geth": "^5.2.2", to the devDependencies section and in embark.json add "embark-geth": {} to the plugins section

Next

Stay tuned for Embark 6.0 and the latest changes happening by watching the Embark GitHub repository and following us on the Embark Labs Twitter!

changelog

Features

  • @embark/contracts: add proxyFor property for contracts (2e8b255)
  • @embark/ens: enable the use of $accounts in registrations (de01022)
  • @embark/test-runner: introduce artifacts.require API (b021689)
  • plugins/scripts-runner: introduce exec command to run scripts (40c3d98)
  • @embark/blockchain: make GanacheCLI the default dev blockchain (cd934f8)
  • warn about packages not configured as plugins; make geth/parity full plugins (d14e93c)

Bug Fixes

  • @embark/blockchain-api: add back contract event listen and log (5592753)
  • @embark/cmd-controller: exit build if afterDeploy is not array (5359cc6)
  • @embark/contracts-manager: Remove logger from serialized contract (d529420)
  • @embark/contracts-manager: always deploy contracts with deploy: true (87a04cd)
  • @embark/dashboard: update dashboard’s logEntry to match core/logger’s logFunction (63831f6), closes #2184
  • @embark/deployment: fix undefined in nb arguments in deploy (0016581)
  • @embark/ens: fix tests erroring on FIFS contract deploy (78fc7b6)
  • @embark/ganache: fix connection to other nodes from Ganache (5531b60)
  • @embark/logger: Remove writeToFile for logger dir (e9be40c)
  • @embark/proxy: only up event listeners on available providers (caae922)
  • @embark/proxy: up max listener for proxy request manager (9c8837d)
  • @embark/test-runner: fix reporter to only catch gas for txs (0e30bf3)
  • core/config: Fix EmbarkConfig type (0f59e0c)
  • only show account warning when Geth will actually start (f502650)
  • set helper methods on contracts (7031335)
  • stack/contracts-manager: ensure custom abiDefinition is set properly if provided (b4b4848)
]]>
@@ -182,7 +182,7 @@ https://blog.embarklabs.io/news/2020/02/17/decentralized-notifications/ 2020-02-16T23:00:00.000Z - 2020-06-15T08:04:44.202Z + 2020-06-18T10:32:39.102Z Supporting Email Notifications in DApps

Having notifications is important when the flow of your dapp is asynchronous, especially if funds are involved. As such, depending on having the user browse the dApp periodically to see if there was a change does not lead to a good user experience.

Users need to know that an action happened and their interaction with a smart contract might be necessary.

In Teller.Exchange, a decentralized fiat on-ramp and off-ramp, this situation happens: sellers need to be notified that a new trade was created, buyers need to know if their escrows were funded or released, and arbitrators want to know if they have a dispute to arbitrate.

Using browser notifications are not really a solution since most users ignore these, as well as not being available in all browsing devices. So using emails were a good alternative to solve this need, yet it must be in compliance with the Status.im principles being Privacy one that could be affected if we make the email notifications being a requirement for the use of Teller.Exchange

IV. Privacy
Privacy is the power to selectively reveal oneself to the world. For us, it's essential to protect privacy in both communications and transactions, as well as being a pseudo-anonymous platform. Additionally, we strive to provide the right of total anonymity.

Teller.Exchange was built with the idea of not requiring servers for the backend. It consists of a Frontend (HTML+JS+CSS) and this frontend interacts directly with the smart contracts, without being limited by the requirements of using centralized servers like databases, as well as minimizing the use of external APIs.

In the constant search for a balance between good UI and adhering to decentralization principals tradeoffs must be made. In order to comply with the Status Principles as well as the architecture requirements, we came up with a solution: an opt-in contract event notifier.

How does it work?

The process is relatively straight forward. In the DApp, an user enters their email, signs it using their web3 provider of choice, and then submits this information to our notifier API, associating their ETH address with an email address of their choosing. Good OpSec is of course to create a new email with no personally identifiable information, however that is entirely up to the user.

After their signature is submitted, the user will receive a verification email, and then get a notification anytime a teller transaction involving their ETH account is confirmed on the network.

For those who make a living buying and selling crypto this is a useful service for tax reporting purposes. In our system this is an entirely voluntary process for users but you can choose how you want to integrate it into your application.

The architecture

The notifier is composed of an API and a backend service. The API allows the user to subscribe to notifications (which sends an email containing a token), verify their email by sending the token generated during subscription, as well as unsubscribing from this service.

The user address and email are stored in a mongodb collection. Although this is a centralized service, since it’s opt-in, a savvy user might be able to setup and run their own instance of the backend service and this way their email address is not shared with us.

The backend service keeps track of all the events being generated in the smart contracts it watches. After a number of confirmations is reached (to avoid possible reorgs), the notification service will read the configuration for the events that were generated, prepare the email content by using templates as well as the information available in the smart contracts and send emails to the accounts involved in the transactions.

Both the API and service can be configured via a json file where the developer needs to set the contract addresses to watch, templates associated individual contract events, as well as filters to determine whether an event should be sent to an user or not.

Here’s an example of an event we have configured in our contract notifier. It tracks the event Released(uint indexed escrowId, address indexed seller, address indexed buyer, bool isDispute), which is emitted when an escrow is released successfully (this happens when a trade goes well, or if a dispute is won by the buyer):

...
["0xD5baC31a10b8938dd47326f01802fa23f1032AeE"]: {
"dispute-won-buyer": {
ABI: {
name: "Released",
type: "event",
inputs: [
{ indexed: true, name: "escrowId", type: "uint256" },
{ indexed: true, name: "seller", type: "address" },
{ indexed: true, name: "buyer", type: "address" },
{ indexed: false, name: "isDispute", type: "bool" }
]
},
index: "buyer",
filter: async (web3, returnValues) => returnValues.isDispute,
template: "dispute-won-buyer.md"
},
...
}
...

Setting up this event requires specifying the ABI, and an index to associate the user address to an event parameter. In this example the index is the buyer, which means that the event will verify if the buyer parameter matches an address stored in the mongodb collection.

Afterwards, if there’s a match, we can apply an additional filter. We can use this filter to add extra conditions to determine if an email needs to be sent. You have access in this filter to the web3 object as well as the return values from the event. In our example we check if the Release was emitted due to a dispute (because we are only interested in the escrow releases product of an arbitration).

Finally, there’s a template file, which is used to prepare the email content. Here’s an example of how they look:

---
subject: You won the dispute!
---
Congratulations, you have just won the dispute.
The trade is now completed.
Enjoy your crypto.
[See the trade]({{url}}/#/escrow/{{escrowId}})

The templates can contain variables coming from the event return values, as well as extra data that can be specified in the config file (which can include calls to other smart contracts).

This configuration is easy to extend and we are considering using this service for other dApps we are building here at Status.im

Using the notification service

Putting this service into practice for your application requires you to have a Sendgrid account. Other dependencies include NodeJS, Yarn and MongoDB installed. For instructions on how to install this application checkout our repository for creating this service for your own application: https://github.com/status-im/contract-notifier

The ethos of the Embark and other Status Network companies is to always build services for the public good. In that spirit we decided to make our smart contract notification service freely available to anyone interested in using it. This is an open source project and we welcome pull requests or issues to make it better.

]]>
@@ -208,7 +208,7 @@ https://blog.embarklabs.io/news/2020/02/11/subspace-1-3/ 2020-02-10T23:00:00.000Z - 2020-06-15T08:04:44.202Z + 2020-06-18T10:32:00.866Z Subspace 1.3

New to subspace? Check out the website at http://subspace.embarklabs.io/

New methods available to track blocks, gas price and blocktime

trackBlock()

Returns the block information for any new block as soon as they are mined. It’s the reactive equivalent to web3.eth.getBlock("latest").

subspace.trackBlock().subscribe(block => console.log(block));

trackBlockNumber()

Returns the latest block number. It’s the reactive equivalent to web3.eth.getBlockNumber.

subspace.trackBlockNumber().subscribe(blockNumber => console.log(blockNumber));

trackGasPrice()

Returns the current gas price oracle. It’s the reactive equivalent to web3.eth.getGasPrice.

subspace.trackGasPrice().subscribe(gasPrice => console.log(gasPrice));

trackAverageBlocktime()

Returns the moving average block time taking in account the latest 10 blocks. The time is returned in milliseconds:

subspace.trackAverageBlocktime().subscribe(blocktimeMS => console.log(blocktimeMS));

Bug fixes

  • Web3 subscriptions are reused, which means that calling a track method with the same parameters more than once will not create a duplicate subscription.

Contributions

Subspace and the entire Embark labs organization are open source. As such we welcome contributions and input from the community using the product.

]]>
@@ -234,7 +234,7 @@ https://blog.embarklabs.io/news/2020/01/30/dapp-frontend-security/ 2020-01-29T23:00:00.000Z - 2020-06-15T08:04:44.202Z + 2020-06-18T10:35:34.485Z Web3.js

This article is the second in my series of articles based on the frontend of the decentralised web. Throughout the series we’ll look at Web3.js & accessing the Ethereum Blockchain client-side, frontend security for DApps, how eWasm / WebAssembly has become the “4th language of the web”, and we’ll build a realtime Blockchain explorer app with Phoenix LiveView!

Introduction

Working for a security-focused company like Status means that security, in its many forms, is mentioned on a daily basis.

However; outside of Status one of the broadest, most important, yet often ignored considerations when deploying and running web applications is the security of the app. When I use the term security, I’m not just speaking from a backend perspective, but also of the frontend of the application. Having good infrastructure security is highly important, but there are also security factors on the frontend of the application that we really must take into account.

Security is an ongoing, and ever-changing, practice that you must observe to ensure that your product is never included in the companies that one hears about on the news after a huge data breach. Regardless of which programming paradigm, language or framework you wish to use, there are plenty of non-specific, terse security practices you should follow from the very start of the project.

In my last personal Startup, we provided User Authentication as a Service, so we were a major target for hackers. On one of our first evenings live, we watched someone attempt to send 5million malicious requests within 30 minutes. None of which had any affect other than exposing the hacker. This is because we made security a priority — which is something we all need to do in the modern world of Tech.

In this article, I’ll introduce you to my biggest tips for top to bottom (Frontend to Backend) security for your web applications. We’ll take a look at security for your DApps too!

Strict Transport Security (HSTS)

HSTS is a security header that allows us to enforce HTTPS across our entire DApp. If you read my previous article, you’ll remember I advocate the idea of HTTPS everywhere, and showed you how to get a trusted, secure SSL certificate free-of-charge from Let’s Encrypt. The reason we need HTTPS everywhere is that our users are vulnerable to Cookie stealing and Man-in-the-middle attacks if we don’t have it implemented.

Now, as you’re probably aware, simply owning an SSL Cert will not immediately make all of your DApp HTTPS only - we need to tell our App to do that, ourselves. One of the best ways of doing this is by using the HTTP Header of HSTS. By using this Header, we can force all traffic on our App to use HTTPS and upgrade non-HTTPS. This Header may also even provide a performance boost, as we no longer would have to send our users through a manual redirect.

So, you’re probably thinking “Wow! I need this!”. Well, whilst I agree - alongside the Content Security Policy I’ll talk about later, this needs to be implemented with caution. Allow me to explain! Here’s what a sample HSTS Header looks like:

Strict-Transport-Security: max-age=630720; includeSubDomains; preload

And in Node.js:

function requestHandler(req, res) {
res.setHeader('Strict-Transport-Security', 'max-age=630720; includeSubDomains; preload');
}

In this Header, we have 3 directives that apply. max-age, includeSubDomains and preload.

max-age: By specifying a max-age, we are telling the user’s browser to cache the fact that we use only HTTPS. This means that if the user tries to visit a non-HTTPS version of the site, their browser will be automatically redirected to the HTTPS site, before it even sends a message to the Server. Therein lies the slight performance boost I mentioned earlier. Now, while this does sound fantastic in theory, what we need to be aware of here, is the fact that if a user ever needed to access a non-HTTPS page, their browser simply won’t let them, until this max-age expires. If you are going to activate this feature, and set a long max-age, (required by the pre-load sites I’ll talk about in a second), you really need to be sure that you have your SSL cert setup correctly, and HTTPS enabled on all of your DApp before you take action!

includeSubDomains: The includeSubDomains directive does exactly what it says on-the-tin. It simply offers additional protection by enforcing the policy across your subdomains too. This is useful if you run a DApp that sets Cookies from one section (perhaps a gaming section), to another section (perhaps a profile section), that need to be kept secure. Again, the issue with this lies similarly to the above, in that you must be sure every subdomain you own and run, is entirely ready for this to be applied.

preload: The most dangerous directive of them all! Basically, the preload directive is an in-browser-built directive that comes straight from the browser creators. This means that your Web App can be hard-coded into the actual Browser to always use HTTPS. Again, whilst this would mean no redirects, and therefore a performance boost, once you’re on this list; it’s very difficult to get back off it! Considering that Chrome takes around 3 months from build-to-table, and that’s only for the people who auto-update, you’ve got a huge wait-time if you make a mistake.

So we have ourselves here an incredibly powerful, yet actively quite dangerous Security feature. The key here is ensuring you know your security measures inside-out, and using discretion. Whilst I don’t recommend you submit your site to the preload directive, if you wish to - you can here.

Note - it is not a requirement to use preload to utilise HSTS. The only Header you need apply is the max-age header.

If you are going to use the HSTS protocol, start out with a small max-age - something like a few hours, and continue to ramp it up over a period of time. This is also the advice Google Chrome give. If you use the includeSubDomains directive, be sure you don’t have internal (company.mysite.com) subdomains that would be unreachable if affected. If you’re going to submit your Web App to preload, follow the official guidelines, and make sure you know exactly what you’re doing - (which I’m not entirely confident of myself!)

Using the X-XSS-Protection Header

XSS (Cross Site Scripting) is the most common of all Web App attacks. XSS occurs when a malicious entity injects scripts to be run into your app. A few years back, most web browsers added a security filter for XSS attacks built into the browser itself. Now whilst in theory this was a good step, they did tend to throw-up false-positives quite often. Due to this, the filter can be turned off by the User. (As the option should be available, in my opinion.)

To ensure our Users are protected, we can force this filter (worth it), on our DApp by using the X-XSS-Protection Header. This Header is widely supported by common browsers, and something I’d recommend using every time.

To apply this header to your Node.js app, you should include the following:

function requestHandler(req, res) {
res.setHeader( 'X-XSS-Protection', '1; mode=block' );
}

Note the two directives in this header: 1 is simply acts as a boolean 1 or 0 value to reflect on or off. mode=block will stop the entire page loading, instead of simply sanitising the page as it would if you excluded this directive.

If you’re a security-freak like myself, and a user of the Chromium browser, you could even go one-step further than this and set the directives like so:

X-XSS-Protection: 1; report=<reporting-uri>

Now, if the browser detects an XSS attack, the page will be sanitized, and report the violation. Note that this uses the functionality of the CSP report-uri directive to send a report that I will talk about in the Content Security Policy section below.

Defend against Clickjacking

Clickjacking occurs when a malicious agent injects objects / iFrames into your DApp, made to look identical, that actually sends the User to a malicious site when clicked. Another common, and possibly more scary example is that malicious agents insert something like a payment form into your DApp, that looks identical to your DApp, but steals payment details.

Now, whilst this could be a very dangerous issue, it’s very easy to mitigate, with almost no impact on your DApp. Servers offer Browsers a Header Protocol named X-Frame-Options. This protocol allows us to specify domains to accept iFrames from. It also allows us to state which sites our DApp can be embedded on. With this protocol, we get three fairly self-explanatory options/directives: DENY, ALLOW-FROM, and SAMEORIGIN.

If we choose DENY, we can block all framing. If we use ALLOW-FROM, we can supply a list of domains to allow framing within. I use the SAMEORIGIN directive, as this means framing can only be done within the current domain. This can be utilised with the following:

function requestHandler(req, res) {
res.setHeader( 'X-Frame-Options', 'SAMEORIGIN' );
}

Content Security Policy (CSP)

CSP is another major topic when it comes to Server-Browser security for Web Apps. At a high-level; Content Security Policies tell the browser what content is authorised to execute on a Web App, and what will block. Primarily, this can be used to prevent XSS, in which an attacker could place a <script> tag on your Web App. The Content-Security-Policy is a Server-Browser header that we can set to ensure our Server tells the Browser exactly which media, scripts, and their origins, allowed to be executed on our DApp.

The whitelisting of resource loading and execution URIs provides a good level of security, that will in most parts, defend against most attacks.

To include a Content Security Policy that allows only internal and Google Analytics, in an Express.js server, you could do the following:

var express = require('express');
var app = express();


app.use(function(req, res, next) {
res.setHeader("Content-Security-Policy", "script-src 'self' https://analytics.google.com");
return next();
});

app.use(express.static(__dirname + '/'));

app.listen(process.env.PORT || 3000);

However, if we do not wish to allow any external sites to execute scripts on our DApp, we could simply include the following:

function requestHandler(req, res) {
res.setHeader( 'Content-Security-Policy', "script-src 'self'" );
}

Note the script-src directive here, that we have set to self, therefore only allowing scripts from within our own domain. Of course, CSP is not without its own problems. Firstly, it would be very easy for us to forget about some of the media we have in our DApp and to simply exclude them accidentally. Now that the web is so rich in media, this would be reasonably easy to do. Secondly, many of us use third-party plugins on our DApp. Again, unless we have a full blueprint of these, we could very easily block them.

So, once activated, this Server Header could potentially be very detrimental to us. However, there are two great ways of testing this. You can set a strict policy, and use the built in directives; report-only and report-uri to test them. The report-uri directive tells the Browser to send a JSON report of all of the blocked scripts to a URi that we specify. The report-only directive does the same, but will not block the scripts on the site. This is very useful for testing, before we put this Header into production.

There’s a great write-up on the reporting, here.

Content Security Policies are both excellent and very powerful, but must be used cautiously. Much the same as HSTS mentioned above, we need to ensure we are aware of the situation before activating. If you are loading in external images, scripts etc. you need to understand that unless you include these in the policy, they will be blocked.

CSRF

Cross Site Request Forgery (CSRF) has been at the forefront of Web App Security for longer than any of us care to remember. The idea behind it is that a malicious agent sends a (forged) request from one app to another whilst the User is signed in and authorised. This would therefore allow the request to enter and alter restricted actions on the requested App, with the requested app believing entirely that the actions were coming from the logged in User. A better way for me to describe this is to show you:

Imagine if you will, I am security-abusing miscreant, and I happen to know that Twitter has no CSRF protection. (They do, this is all hypothetical.) I’m also aware that most people who visit my DApp, probably leave their Twitter logged in, and therefore have a Cookie stored in their browser, to allow them fast access to Twitter the next time they want to post something.

On my DApp frontend, I could embed a script such as the following:

<form action="https://twitter.com/tweet" method="POST" id="sendTweet">
<input type="hidden" name="tweet" value="Hey! Check out my awesome spam site - spam.com">

When a user’s browser loads my DApp, this form will be loaded (entirely invisibly) too. I would then also have embedded a small piece of JS to POST the form, without them ever knowing:

document.getElementById("sendTweet").submit();

In doing this, I’ve just sent a Tweet on their account, without ever having to know their Username or Password. The Cookie they had stored in their Browser allowed my app to send a forged request, pretending to be them - and if Twitter had no CSRF mitigation, it would have worked too!

For years, we have been trying to solve CSRF requests by checking HTTP headers such as the Origin and Referer. Whilst these have offered fairly robust protection for a few years, there is now a simple directive that once applied; will entirely mitigate CSRF attacks.

Enter, the SameSite Cookie directive. SameSite is relatively new, and only been around for the past year, in which it has gained some publicity, but is still widely unknown. In essence, the SameSite directive, once applied, will tell the Browser to never send that cookie when a request from an external (Cross Site) url is made. We can apply this directive by altering our Cookies as such:

Set-Cookie: sess=sessionid123; path=/; SameSite

It really is that easy. I wouldn’t recommend removing your existing CSRF protection just yet, but I would definitely recommend including this directive on your DApp.

Cookies

As we know, cookies are an important feature of our Web Applications, carrying data mainly referring to our User Sessions. While simply implementing the aforementioned directives is sufficient in securing your cookies, and preventing attacks, we can actually take cookie security a step further.

Cookie Prefixing is a relatively under-used technique that we can utilise to ensure a cookie is secure:

The __Secure prefix - If a cookie’s name begins with “__Secure”, the cookie MUST be:

  • Set with a “Secure“ attribute
  • Set from a URI whose scheme is considered secure by the user agent.

The following cookie would be rejected when set from any origin, as the “Secure” flag is not set:

Set-Cookie: __Secure-sess=12345; Domain=myapp.com

While the following would be accepted if set from a secure origin e.g. https:// and rejected otherwise:

Set-Cookie: __Secure-sess=12345; Secure; Domain=myapp.com

Alongside the __Secure prefix, we also have the __Host prefix:

The __Host prefix - If a cookie’s name begins with “__Host”, the cookie MUST be:

  • Set with a “Secure” attribute
  • Set from a URI whose “scheme” is considered “secure” by the user agent.
  • Sent only to the host which set the cookie. That is, a cookie named “__Host-cookie1” set from “https://example.com" MUST NOT contain a “Domain” attribute (and will therefore be sent only to “example.com”, and not to “subdomain.example.com”).
  • Sent to every request for a host. That is, a cookie named “__Host-cookie1” MUST contain a “Path” attribute with a value of “/“.

The following cookies would always be rejected:

Set-Cookie: __Host-sess=12345Set-Cookie: __Host-sess=12345; SecureSet-Cookie: __Host-sess=12345; Domain=example.comSet-Cookie: __Host-sess=12345; Domain=example.com; Path=/Set-Cookie: __Host-sess=12345; Secure; Domain=example.com; Path=/

While the following would be accepted if set from a secure origin e.g. https://, and rejected otherwise:

Set-Cookie: __Host-sess=12345; Secure; Path=/

By setting these prefixes, any compliant browser will be made to enforce them.

Now, if we include the tips from my first article, and the tips above, we can make the most secure Cookie possible:

Set-Cookie: __Host-sess=id123; path=/; Secure; HttpOnly; SameSite

In this most-secure-cookie, we’re utilising the __Host prefix which means the Secure attribute has to be set, and it must be served from a secure host. There is no Domain attribute set and the Path is /. We’ve set HttpOnly for XSS protection, and SameSite is enabled too to prevent CSRF. Of course, this won’t be the best or most practical solution for a lot of people, but it is the most in-theory secure Cookie we could set from our DApp.

Take a DApp Blueprint

*Note - In my opinion, this is one of the most important security steps one can take.*

Do you know the ins-and-outs of each library your Developers use? Probably not - it’s near impossible to keep track nowadays, but this is to great detriment. Are you also aware of which libraries and tools have been given write access to your production servers and databases - and how isolated they are?

The issue here is that using so much automation in modern development, we grant access to a huge amount of tools/libraries without really knowing exactly what they’re doing. We take it for granted that each of these libraries is entirely safe and without their security vulnerabilities - or worse - performing malicious activities themselves.

We all want the most streamlined Dev cycle possible. We all use automation tools that trigger a whole bunch of processes, doing things that barely any of us are aware of. The propensity of some Devs to throw sudo commands at package managers if a command fails is also truly terrifying.

So how do we get around this? Take a Tech Blueprint! This needn’t be a complex process, it’s as simple as knowing what each piece of Software is doing on your servers, and what authority they’ve been granted. Take a note of any new tools / packages before you grant them permissions, and do a little research. Some simple Googling of key phrases i.e. *package* security vulnerabilities will usually bring up more results than you’d expect. It’s also worth checking out the Issues tab on the package’s GitHub page. Vulnerabilities are often discussed there and you’ll be able to act accordingly. This applies to the top-level Package Managers too.

Package managers are used by almost ALL of us. If you really want to scare yourself, go ahead and search *package manager* security vulnerability and take a look at all of the results! Again, knowing what we are installing and granting permissions to, and especially keeping a note of this, could just save our Bacon. Take a look at this article for a relevant example!

Handy tip: if you want to know which hooks an npm package runs, before you install it, run the command:

$ npm show $module scripts

Security for DApp Users

Dapp security
Image Source

With more and more DApps being created including web-based exchanges, crypto-based games etc. The opportunities for bad actors increase with each new DApp released. Say, for instance, someone released an interactive game built around Web3, and directly interacting with user’s wallets. On registration for the game, wallets are created, and they collected sensitive data, including user’s private keys being stored in local storage (super insecure).

The DApp developer, however, didn’t realise that a bad actor has been injecting a remote script during registration that evaluates and sends all of the player’s private keys to the bad actor’s server.

  • Protect wallets and private keys: If user’s wallets are compromised, this is game over. Extreme care needs to be taken when handling this sensitive information.
  • Protect user information: Users do not want their personal data being exposed to the world. Ensure that user data remains private.
  • Use MetaMask or similar to ensure security of wallets and user keys.
  • Evaluate wisely what needs to be stored in the blockchain or in your servers. Only include data that is absolutely necessary for you smart contracts to function within the contracts themselves.

There is an excellent write-up on DApp security standards you can check out here.

Conclusion

Without a doubt, the most effective method for maintaining the security of your DApps is keeping up-to-date with any security protocols on an ongoing basis. Vulnerabilities are an extremely fickle and dynamic topic, in that they change / pop up so regularly.

By following the tips in this article, keeping up-to-date with any security announcements, and having an in-depth overview of your systems, you can rest assured that you are well on your way to having a jolly well secured DApp. As stressed in this article, security considerations are not only found on the backend of our apps, but on the frontend too. Ensuring that we approach both means we can be confident about the safety of our users (which should be our number one priority). The best tools in web security are common sense and vigilance.

So, from enforcing HTTPS with Strict Transport Security, to securing our DApp frontend with a Content Security Policy; we’ve covered the main topics, in my opinion to ensuring frontend to backend security for our web & decentralised web applications. These topics are all techniques I utilise myself and would advocate for use in your apps on an ongoing basis.

For any future projects, I recommend taking a look at these Blockchain Security providers.

- @rbin

]]>
@@ -258,7 +258,7 @@ https://blog.embarklabs.io/news/2020/01/29/subspace-1-2/ 2020-01-28T23:00:00.000Z - 2020-06-15T08:04:44.202Z + 2020-06-18T10:30:37.791Z Subspace 1.2

New Org

Subspace is now under the @embarklabs Org. Versions under @status-im Org have been deprecated.

# Using npm
npm install --save @embarklabs/subspace

# Using yarn
yarn add @embarklabs/subspace

HttpProvider support

Previously Subspace only worked with WebSockets for event subscriptions, and polled for changes only if the callInterval option was specified, displaying a warning indicating that the use of providers other than WebSocketProvider was discouraged.

With this release, Subspace will identify if the provider supports subscriptions and use them automatically. If no subscriptions are available, it will assume the provider does not support them and poll the contract for new changes periodically.

Subscriptions can be disabled with the disableSubscriptions option.

let subspace = new Subspace({disableSubscriptions: true})

GraphQL Example

An example DApp using GraphQL with Subspace can now be found at https://github.com/embarklabs/subspace/tree/master/examples/react-graphql-example1 .

Bug fixes

  • Fixed obtaining the from address when instantiating a Web3 Contract with Subspace tracking functionality
  • .track() is added only to event names as they’re specified in the ABI. Tracking events by signature is not allowed

Contributions

Subspace and the entire Embark labs organization are open source. As such we welcome contributions and input from the community using the product.

]]>
@@ -282,7 +282,7 @@ https://blog.embarklabs.io/news/2020/01/28/embark-5-1/ 2020-01-27T23:00:00.000Z - 2020-06-15T08:04:44.202Z + 2020-06-18T10:30:42.763Z Embark 5.1

Interfaces & Libraries Configuration

Embark 5.1 adds two new configuration settings for Smart Contract configuration:

interfaces - Any Smart Contract that represent an interface or is used for inheritance
libraries - Any Smart Contract that is used as a library

This makes the configuration less redundant in cases where otherwise the deploy property has been set to false, such as:

deploy: {
Ownable: {
deploy: false
},
...
}

The above can now be done via:

interfaces: ['Ownable'],
deploy: {
...
}

Find the complete documentation here.

getEvmVersion for conditional tests

Embark tests now include a helper getEvmVersion that can be used to consult what EVM the tests are being run on. This is useful when you have certain tests that can only be run in a particular type of node, but you still want to be able to run tests everywhere without them breaking.

For example:

it("cannot bid after 5 minutes", async () => {
const evmVersion = await global.getEvmVersion();
if (evmVersion.indexOf("TestRPC") === -1) return;

increaseTime(5000)

await assert.reverts(Auction.methods.bid(), {from: web3.eth.defaultAccount}, 'Returned error: VM Exception while processing transaction: cannot bid after auction expired');
})

Nethermind plugin

Embark now comes with a Nethermind plugin.
More info about the plugin can be found here

Changelog

Features
@embark/deployment: introduce interfaces and libraries configuration (73d0443)
@embark/nethermind: add Nethermind blockchain client plugin (6db8d87)
@embark/test-runner: expose evmClientVersion for conditional tests (e37d3f7)
@embark/testing: introduce proper request2 api for async/await (c947517)
@embark/testing: add missing APIs to register console commands and API calls (bef582d)
support Node.js v12.x and newer (c093cf8)

Bug Fixes
@embark/cmd_controller: fix build command to escape on finish (e2767c2)
@embark/debugger: Prevent error if contract not tracked by Embark (1e1172e)
@embark/ens: fix Infura connection and testnet use of ENS (42bd3b7)
@embark/test-dapp: fix test_dapp broken for ENS resolve (f5849e0)
@embark/tests: Fix failing test with —node=embark (81af3af)
@embark/transaction-logger: Circular JSON log and unknown contract log level (5843a8e)
@embark/utils: fix deconstruct url to return port as an integer (4190d5e)
transaction-logger: fix circular dep issue with util.inspect (6f239f4)
@embark/embarkjs: change enableEthereum to not rely on returned accounts array (b8f93ea)
@embark/test: increase default gas limit to 8M so tests support bigger contracts (b6856b2)
@embark/ens: connect to web3 only with dappAutoEnable is true (e0ac539)
@embark/proxy: Parse rpcPort from config as integer (9f7c682)

]]>
@@ -306,7 +306,7 @@ https://blog.embarklabs.io/news/2020/01/13/announcing-embark-5/ 2020-01-12T23:00:00.000Z - 2020-06-15T08:04:44.201Z + 2020-06-18T10:30:41.346Z If you’ve been following the development of Embark you’re probably aware that we regularly put out alpha and beta releases for upcoming major or feature versions of Embark. In the past ~6 months since the release of Embark 4, we’ve published 10 alpha releases and one beta release for Embark 5 and today we’re happy to announce the Embark 5 stable release!

In this post we’ll be looking at some of the main changes and features to get up and running with v5. Notice that we’ve add a migration guide to our official docs as well.

New Features

Let’s first start with new features that have been introduced in Embark 5.

Whisper client configuration

Prior to Embark 5, Embark would use the same blockchain client to enable blockchain features and communication features for Whisper. With Embark 5, Embark spins up a separate client for Whisper. This also ensures that that Whisper service stays in tact when the blockchain service is turned off and vice versa.

To specify a client, use the new client configuration property which defaults to geth:

// communication.js

...
default: {
...
client: "geth" // can be either 'geth' or 'parity'
},
...

Support for Dynamic Addresses

If you’re using Embark already, you’re probably aware that there are many different ways to configure your Smart Contracts. One of the things that can be configured is the address of a Smart Contract. Usually the address will be determined after a Smart Contract has been deployed. In other cases, what we want to do is simply specifying the address because the Smart Contract is already deployed to the network.

There’s one more case that hasn’t been covered so far: Calculating a Smart Contract address dynamically as it’s scheduled for deployment. This is useful when the address of a Smart Contract is the result of the interaction with another Smart Contract that is already instantiated on the network.

The following example configures MyContract to get its address from a call made to AnotherContract‘s API:

...
deploy: {
AnotherContract: {...},
MyContract: {
deps: ["AnotherContract"]
address: async (deps) => {
const receipt = await deps.contracts.AnotherContract.methods.someFunction();
return receipt.events.SomeEvent.returnValues.someAddress;
}
}
}
...

Breaking changes

Next up, let’s quickly talk about the few breaking changes we’ve introduced to improve the overall developer experience. It’s worth noting that we try to keep breaking changes at a minimum and if it’s indeed unavoidable, we put lots of effort into keeping the necessary changes as small as possible.

NodeJS support

Due to some package dependencies, Embark doesn’t yet support Node’s Current version version (13.x) or latest LTS version (12.x). Embark 5 runs with any node version >= 10.17.0 and < 12.0.0. It also requires npm >= 6.11.3 (bundled with Node 10.17.0) or yarn >= 1.19.1.

New Smart Contract configuration API

Embark’s Smart Contract configuration has been highly declarative from day one. Configuring different deployment options and settings for various scenarios is a largely descriptive process when using Embark. However, we felt there was still room for improvement. Especially because Embark handles not only Smart Contract configurations, but also configurations for elements such as the user’s choice of blockchain client. This sometimes caused confusion for our users since they weren’t sure where certain configurations should go.

That’s why we’ve made the following changes:

Deployment section moved to Blockchain config

The deployment section of the Smart Contract configuration has been completely moved to the Blockchain configuration as discussed in a moment. This section was primarily used to specify things like the host, port and protocol being used to connect to a node to which you Smart Contracts will be deployed, as well as the accounts configuration.

Here’s what such a config looked like prior to v5:

...
deployment: {
host: "localhost", // Host of the blockchain node
port: 8546, // Port of the blockchain node
type: "ws" // Type of connection (ws or rpc),
accounts: [...]
},
...

There’s no equivalent for this configuration inside the Smart Contract configuration in Embark 5, so this section can be entirely (re)moved.

contracts property has been renamed to deploy

When configuring Smart Contracts, there are a few deployment hooks that can be specified, such as beforeDeploy and afterDeploy. To make the API a bit more descriptive and to clarify intent, the contracts property has been renamed to deploy, aligning wonderfully with its deployment hooks counterparts.

Before:

...
contracts: {
SimpleStorage: {
fromIndex: 0,
args: [100],
onDeploy: async () => { ... },
deployIf: async () => { ... }
}
}
...

After:

...
deploy: {
SimpleStorage: {
fromIndex: 0,
args: [100],
onDeploy: async () => { ... },
deployIf: async () => { ... }
}
}
...

Polished Blockchain configuration API

One of the most complex APIs has been Embark’s Blockchain configuration API. That’s why we’ve put a lot of effort into streamlining the settings and properties and removing the ones that happened to be redundant. On top of that, Embark now defines most of them as defaults, resulting in significantly smaller and less complex configuration files.

The following configuration properties have been renamed:

  • isDev is now miningMode: 'dev'
  • mineWhenNeeded is now miningMode: 'auto'
  • ethereumClientName is now client

We’ve also removed several endpoint-related settings, such as host and port, and replaced them with a single endpoint property. Here’s what the new defaults look like:

module.exports = {
default: {
enabled: true,
client: "geth"
},
development: {
clientConfig: {
miningMode: 'dev'
}
},
testnet: {
endpoint: "https://external-node.com",
accounts: [
{
mnemonic: "12 word mnemonic"
}
]
}
}

For more information on Blockchain configuration, head over to the official docs.

Accounts configuration moved to Blockchain config

Prior to Embark 5 it was possible to specify and configure various accounts for deployment and interaction both inside the Smart Contract configuration and the Blockchain configuration. This caused a lot of confusion because it wasn’t really clear which accounts belonged to what action. To eliminate confusion, we’ve moved the accounts configuration entirely to the Blockchain configuration, making it much more straightforward to find the right place when setting up custom accounts.

Just like before, accounts can be defined using different configuration settings, depending on the use case:

...
accounts: [
{
nodeAccounts: true,
numAddresses: "1",
password: "config/development/devpassword"
},
{
privateKey: process.env.MyPrivateKey
},
{
privateKeyFile: "path/to/file",
password: process.env.MyKeyStorePassword
},
{
mnemonic: process.env.My12WordsMnemonic,
addressIndex: "0",
numAddresses: "1",
hdpath: "m/44'/60'/0'/0/"
}
]
...

Check out the documentation on accounts configuration for more information.

Configuring tests

All the configuration changes discussed above have been ported and made available inside the test runner as well. In other words, when using Embark’s config() function inside test suites, the same configuration APi applies:

config({
contracts: {
deploy: {
SomeContract: {} // options as discussed in the Smart Contract configuration guide
}
}
});

Testing is covered in-depth in our testing guide.

To see any of the new APIs in action, have a look at our template and test dapps in the official Embark repository.

Obviously we’ve worked on many more things as part of the v5 release. For a full list of features and bug fixes, head over to our changelog.

As always, we encourage our users to install the latest version of Embark and give it a spin. Feedback is very welcome and we can’t wait to see the great apps you’ll be building with it!

]]>
@@ -328,7 +328,7 @@ https://blog.embarklabs.io/news/2020/01/09/take-back-the-web-hackathon/ 2020-01-08T23:00:00.000Z - 2020-06-15T08:04:44.201Z + 2020-06-18T10:30:35.252Z The Embark Project is proud to be working with Gitcoin and the Ethereum Community Fund to grow the Ethereum ecosystem. This January we are sponsoring a #Takebacktheweb Hackathon with bounties and quests for people of all technical levels to get involved.

#Takebacktheweb is a movement based on the idea that some basic web services should be open platforms that can’t be censored. We’re looking to jumpstart projects that will democratize all major web platforms and services. For this hackathon we’ve outlined some bounties that we think drive this point, but if you have something you would like to build we are open to creating a bounty for it!

What’s a Bounty?

Bounties are a way for developers to get an immediate reward for their efforts in the form of compensation or awards. From feature requests to building MVP’s this can be a great way to get more eyes on your open source project.

We will be focusing on bounties for completing our developer survey and submitting product ideas that help take back the web. We want to create products that users already have today, but which leverage decentralized technology. Using the embark framework, devs have access to everything they would need to create privacy first applications. Through our survey we hope to understand what other roadblocks developers may be experiencing.

What’s a Quest?

Quests are educational games geared towards rewarding people for going through demo’s of products, tutorials, documentation, blog posts, or new initiatives. We’ll be using this as a way to show off the new parts of the Status Network. Getting people to take a look around all we’ve built should help some to see what a great resource the network is!

How can I participate?

If you’re not already a member, join Gitcoin and mark January 9th on your calendar for the launch of the hackathon. Go on some quests, tackle a few bounties and share it with your friends.

You’ll also need a web3 enabled browser to collect your ‘Kudos” for participation in the quest. The experience is fun and worth checking out!

We’re always here to help with ideas and projects building with Embark tools. Make sure to connect with us here.

]]>
@@ -350,7 +350,7 @@ https://blog.embarklabs.io/news/2019/12/09/web3-what-are-your-options/ 2019-12-08T23:00:00.000Z - 2020-06-15T08:04:44.201Z + 2020-06-18T10:33:51.644Z Web3.js

This article is the first in my series of articles based on the frontend of the decentralised web. Throughout the series we’ll look at Web3.js & accessing the Ethereum Blockchain client-side, frontend security for DApps, how eWasm / WebAssembly has become the “4th language of the web”, and we’ll build a realtime Blockchain explorer app with Phoenix LiveView!

Introduction

To kick this article off, I first have to reaffirm, for those that aren’t aware, I am not, and never have been, a lover of JavaScript. While my cool friends were off learning Node.js, and for some reason moving a scripting language to the backend, I was learning C and Go, Erlang and Distributed Systems.

For years, I harboured a deep hatred of JS, and actively whinged about it at every opportunity I got; being forced to use it in my daily work life. Now however, I do have to say; over the last few years I have softened to JS, and I am much more comfortable in my own skin when having to use it.

It goes without saying, the entire web is JS. Look around you - JS. View the source of this article - JS files. Look at your own app’s dependencies - JS.

JavaScript, specifically Node, really is in everything we use, and that now also applies to our wonderful world of Cryptocurrencies.

As I mentioned briefly in my last article, my next article series is going to be about building your first DApp – from start to finish. Inevitably, the frontend of our DApp needs to be able to communicate with the Ethereum Network. This is where Web3.js comes into the mix. Web3.js is a collection of APIs allowing us such functionality as: Reading & Writing data from Smart Contracts, sending and receiving Ether, encrypting / decrypting wallets & data, and a whole bunch of other stuff too. Basically, most of the backend functionality available on the Ethereum Network natively becomes available for use in the browser.

This is how the web3.js library talks to the Ethereum Network:

Web3 JS Diagram
Image credit: iotbl

So, now that the basics are covered, let’s go over installing and using the web3.js library.

Installing Web3

Installing web3.js is as simple as:

npm install web3

One thing worth noting here; is that (coming from an anti-js background), I kept getting a cannot find web3 module error when trying to import web3 into a Node console. If you, like me, aren’t a big js fan, this can be solved by first running the npm init command to ensure there is a package.json file in the cwd, and then you can run npm install web3, and it will work fine. (I realise this is basic stuff – but actually for someone who’s tried to avoid Node at all costs, it was initially confusing enough to have to search online.)

I am working from a Mac here, but if you are working from Windows, the install process can be exactly the same, assuming you do have Node & NPM installed.

So, with web3.js installed, let’s do some basic interactions with the Ethereum Network, and dive on in!

Communicating with the Ethereum Network

Wallet Interaction

For this article, we’re going to use Ganache, for simplicity, as our local Blockchain. By using Ganache, we can spin up a local Ethereum node, without having to write a single line of code!

(Yes, I realise that rhymes. No, I didn’t realise until my second proof-read through of this article!)

In fact, though, Embark already has Ganache inbuilt, so we could also simply run:

embark simulator

Anyway, to install Ganache head over to this page and click on the executable there. If you so choose; there is also a Ganache CLI available you can install by running:

npm install -g ganache-cli

Running the Ganache CLI will give you the same functionality as the desktop client; in essence giving us a multitude of ETH-loaded wallets that we can build contracts around / interact with.

Ganache CLI

Rather brilliantly; we now have a local Ethereum Node running that we can start using the Web3 client to interact with. In another Terminal tab, open up a node instance from the same working directory we ran the npm init command from earlier.

Now, in our interactive Node console, run:

var Web3 = require('web3');
var web3 = new Web3('http://localhost:8545');

Something to note here, is that I’m calling new Web3 with an http protocol, but the WebSocket protocol is also commonly used:

var web3 = new Web3(Web3.givenProvider || new Web3.providers.WebsocketProvider('ws://remotenode.com:8546'));

To test and ensure the connection, you can get a list of the accounts made available in Ganache by running:

web3.eth.getAccounts().then(console.log);

Which should give you an output like the following:

> [ '0x7689cF9F90FAad61B8a3c9b1b2A5b4580B37358b',
'0x852e9a9db77a4e6169e175cDBb33dBE350150A8e',
'0x946700a1a4f30Dfe80307C91B6DaF1cCa2d09401',
'0x7d356aF02A87147D3ce5F9ACA716a78f70aF7616',
'0x88A116a16e4c8838F575a0e7a72eE27C7B073263',
'0x655317701Fcf3b310F492cB801C8D23f8c6fb556',
'0x16D305e72aFb0DDa1dB1830F8a98D5cD5337882E',
'0x9099bb4Af9CE5734E7a7e62f817e833fcFFaaF32',
'0x2ec4CC6700d0424A78a9B9Fc2ecBaeFc162313F1',
'0x1BC51a0edEC9FdEA3B14748e9209F4bF8Fe024b5' ]

If you want to check the balance of an individual account from the above list, you can do so by running:

const account1 = "0x7689cF9F90FAad61B8a3c9b1b2A5b4580B37358b";

web3.eth.getBalance(account1)
.then(console.log);

Which will output:

> 100000000000000000000

Contract Interaction

As above; interacting with our individual accounts through web3.js is cool, but not nearly the extent to which the library works. Let’s now take a brief look at the more important functionality; of interacting with Smart Contracts through web3.js.

The first thing we need to do, is to create a new Smart Contract, which we can do with the new web3.eth.Contract command.

Before we call the new command, we need to assign our json interface for the contract’s ABI:

const abi = [{"type":"function", "name":"foo", "inputs": [{"name":"a","type":"uint256"}], "outputs": [{"name":"b","type":"address"}] },{ "type":"event", "name":"Event", "inputs": [{"name":"a","type":"uint256","indexed":true},{"name":"b","type":"bytes32","indexed":false}], }]

The json interface is a JSON object describing the Application Binary Interface (ABI) for our Smart Contract. Using this JSON interface; web3.js is able to create a JavaScript object representing our Smart Contract and its methods & events, using the web3.eth.Contract functionality.

Note, the above JSON interface / ABI is taken directly from the Web3 docs.

Now that we have our json interface defined, we can create our new contract instance:

var myContract = new web3.eth.Contract(abi, '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe');

(The from address is the address of the already deployed contract instance that we’re aiming to talk to.)

You could then set the Smart Contract’s data and other options, and then deploy your Contract with something like the following:

myContract.options.data = '0x12345...';

myContract.deploy({
arguments: [123, 'My String']
})
.send({
from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe',
gas: 1500000,
gasPrice: '30000000000000'
})
.then(function(newContractInstance){
console.log(newContractInstance.options.address) // instance with the new contract address
});

The above examples aren’t supposed to be perfect continuous code, and should definitely not be copy/pasted into a production project, but they are there to show off roughly how Web.js works, and give an overview of interacting with the 2 main pieces of functionality, as I see them – Wallets and Contracts.

In my next tutorial series, we will be utilising Embark, and therefore we’ll be diving deeper into web3.js, and showing off much more of its potential.

Web.js in Other Languages

Naturally the whole idea behind this article was to show off communication with the Ethereum Network through a JavaScript frontend. However, there are also many other libraries, in pretty much every language, available to do the same:

Nim - nim-web3
Crystal - web3.cr
Ruby - web3-eth gem
Elixir - ethereumex
Python - Web3.py
Haskell - hs-web3
Java - web3j
Scala - web3j-scala
Purescript - purescript-web3
PHP - web3.php

Beyond Web3

As stated at the opening of this article, we’ve barely even scratched the surface of web.js capabilities. But I do hope that you now have a better understanding of what Web3 stands for.

Personally, I am very much looking forward to diving on in to my next DApp tutorial series, to utilise and demonstrate the Ethereum Network to its fullest.

As always, if you have any questions regarding Web3, how Status utilises Web3, or if you have comments on this article, feel free to reach out to me at robin@status.

Thanks again for reading, and check back for my DApp tutorial series, starting later this week!

- @rbin

]]>
@@ -374,7 +374,7 @@ https://blog.embarklabs.io/news/2019/11/28/nim-vs-crystal-part-3-cryto-dapps-p2p/ 2019-11-27T23:00:00.000Z - 2020-06-15T08:04:44.201Z + 2020-06-18T10:35:37.818Z crystal vs nim

Welcome back to my series comparing the two sweethearts of the modern low-level programming world. Just to quickly recap: in article #1 I noted my thoughts on the interoperability capabilities of the two languages, alongside briefly reviewing the performance metrics for each (albeit with relatively simple tests). Whether simple or not, the tests did throw up some unexpected twists in the plot. Crystal used very-nearly half of the memory amount executing the tests when compared to Nim, and also took very nearly half of the execution time in doing so. This seriously took me by surprise!

In article #2; I looked at the Concurrency primitives of each language, and explored both the in-built tooling, and external package ecosystems surrounding each language. As I said in that article, one of the biggest factors I look at when considering adopting a new language; is its tooling ecosystem. This includes, but is not limited to: A comprehensive package manager, an intuitive testing suite, a good project scaffolder, and an in-built formatter/linter to ensure my code stays semantically correct – especially if I know I will be working in Open Source repos that others will contribute to. But they’re just the high-level tools that I look for…

From a low-level standpoint; I look for efficient use of technology in features such as in-memory storage, caching, garbage collection, and concurrency primitives that not just markedly improve our application performance, but that are also relatively simple, and intuitive to use. I see this as particularly important as I have, in my past, seen some truly shocking examples of trying to handle multi-threading, from languages that I love *cough* Ruby *cough*. I also like to see a fully-featured standard library that takes influence from previous successful languages. However, I digress…

I regret to say that this is the final article in this series! It’s been good fun for me; getting to the know the ins-and-outs of Nim, and to re-grow a fresh appreciation of Crystal, having put it on the back-burner for quite some time. However, whether the final article in the series or not, it’s going to be a good one! We’re going to be covering the benefits to the Cryptocurrency / DApp industries from both Crystal and Nim. So without further ado:

Let’s dive on in!

Cryptocurrency

Firstly, I’d like to talk about the possibility of using either Crystal or Nim, (or both!) in the development of crypto apps. Hypothetically; if we had the inclination to build out our own Cryptocurrency: Crystal and Nim have proven to be two of the strongest languages to consider for the undertaking.. (That being the next blog series I’m going to write – in the near future, so deciding which language to use will be heavily influenced by this blog series!)

For our Cryptocurrency, we would need to be able to use an intelligent key manager, utilise smart hashing algorithms, maintain strong performance, and all of this atop of a distributed, decentralised virtual machine or blockchain. Now, all of this sounds like a very tall order! For all of these feature requirements to be met by a single programming language, it would mean that this language is going to have to be ONE HELL of an impressive piece of technology.

Happily, both Crystal and Nim allow us all of the above functionality. In our hypothetical usecase, if we were to build out a fully-featured blockchain; mining and hashing functions would need to be continually made, both of which entail relatively heavy computations. As shown over the last 2 articles in the series, we can at least be sure that both langs can handle the performance stresses, no problemo.

As I’d like to write this topic out into a further detailed article series, I will show off just 2 of the above pieces of functionality we’d require for our Crypto app:

Calculating our Block Hashes

When building our Blockchain; we need to consider how we’re going to identify and chain our transaction blocks together (blockchain). Without going into details in this article on how blockchains function, we’ll stick with the existing, and proven, SHA256 algorithm.

In Crystal:

require "json"
require "openssl"

module OurCryptoApp::Model
struct Transaction
include JSON::Serializable

alias TxnHash = String

property from : String
property to : String
property amount : Float32
getter hash : TxnHash
getter timestamp : Int64

def initialize(@from, @to, @amount)
@timestamp = Time.utc_now.to_unix
@hash = calc_hash
end

private def calc_hash : TxnHash
sha = OpenSSL::Digest.new("SHA256")
sha.update("#{@from}#{@to}#{@amount}#{@timestamp}")
sha.hexdigest
end
end
end

In Nim:

If we want to generate a similar hash in Nim, we could run the following:

import strutils

const SHA256Len = 32

proc SHA256(d: cstring, n: culong, md: cstring = nil): cstring {.cdecl, dynlib: "libssl.so", importc.}

proc SHA256(s: string): string =
result = ""
let s = SHA256(s.cstring, s.len.culong)
for i in 0 .. < SHA256Len:
result.add s[i].BiggestInt.toHex(2).toLower

echo SHA256("Hash this block, yo")

Releasing our Crypto App

Another serious factor we have to consider, is the ability to distribute our crypto app, once built, with great ease. Remembering that both Crystal and Nim are compiled languages, we’re already off to a promising start. (A single executable binary is always going to be easier to distribute than something requiring its own specialist environment!)

It pays rather large dividends being able to write our Crypto app just once, and having the ability to maintain one singular code repo for that app. To this end – I think it is definitely worth considering a multi-platform app framework. I already know that in my next article series, I will be exploring building a Crypto app using React Native.

However, if you wish to build the frontend of your cross-platform crypto app in something else, there are a variety of technologies available - all of which seem to work well with both Crystal and Nim:

And if you come from a Windows background:

Building & Releasing In Nim:

If we wanted to build out and release our app for Android, we can run:

nim c -c --cpu:arm --os:android -d:androidNDK --noMain:on

To generate the C source files we need to include in our Android Studio project. We then simply add the generated C files to our CMake build script in our Android project.

Similarly, we could run:

nim c -c --os:ios --noMain:on

To generate C files to include in our XCode project. Then, we can use XCode to compile, link, package and sign everything.

Building & Releasing In Crystal:

Crystal also allows for cross-compilation, and makes it just as easy. For example, to build our app for Linux distributions from our Mac, we can run:

crystal build your_program.cr --cross-compile --target "x86_64-unknown-linux-gnu"

Worth noting: Crystal doesn’t offer the out-of-the-box iPhone / Android cross-compilation functionality that Nim does, so building our app in Nim gets a definite thumbs-up from a distribution point-of-view!

Ethereum - Building, Signing & Sending a Transaction

For the sake of this article, in Crystal, I didn’t see the need to write out a more low-level example of the below action, as it is so similar to the Nim demo that follows. This actually worked out in my favour, as it means I get to further show off the native HTTP library for Crystal.

In Crystal:

require "http/client"

module Ethereum
class Transaction

# /ethereum/create/ Create - Ethereum::Transaction.create(args)
def self.create(to : String, from : String, amount : UInt64, gas_price : UInt64? = nil, gas_limit : UInt64? = nil) : EthereumToSign | ErrorMessage

headers = HTTP::Headers.new
if ENV["ONCHAIN_API_KEY"]? != nil
headers.add("X-API-KEY", ENV["ONCHAIN_API_KEY"])
end

response = HTTP::Client.post "https://onchain.io/api/ethereum/create//?to=#{to}&from=#{from}&amount=#{amount}&gas_price=#{gas_price}&gas_limit=#{gas_limit}", headers: headers

return ErrorMessage.from_json response.body if response.status_code != 200

ethereumtosign = EthereumToSign.from_json response.body


return ethereumtosign
end

# /ethereum/sign_and_send/ Sign and send - Ethereum::Transaction.sign_and_send(args)
def self.sign_and_send(to : String, from : String, amount : UInt64, r : String, s : String, v : String, gas_price : UInt64? = nil, gas_limit : UInt64? = nil) : SendStatus | ErrorMessage

headers = HTTP::Headers.new
if ENV["ONCHAIN_API_KEY"]? != nil
headers.add("X-API-KEY", ENV["ONCHAIN_API_KEY"])
end

response = HTTP::Client.post "https://onchain.io/api/ethereum/sign_and_send//?to=#{to}&from=#{from}&amount=#{amount}&r=#{r}&s=#{s}&v=#{v}&gas_price=#{gas_price}&gas_limit=#{gas_limit}", headers: headers

return ErrorMessage.from_json response.body if response.status_code != 200

sendstatus = SendStatus.from_json response.body


return sendstatus
end

end
end

Then, in our application we could simply call:

Ethereum::Transaction.create("0xA02378cA1c24767eCD776aAFeC02158a30dc01ac", "0xA02378cA1c24767eCD776aAFeC02158a30dc01ac", 80000)

And we would get a response similar to the following, ready to be signed and sent to the Ethereum network:

{
"tx": "02000000011cd5d7621e2a7c9403e54e089cb0b5430b83ed13f1b897d3e319b100ba1b059b01000000db00483045022100d7534c80bc0a42addc3d955f74e31610aa78bf15d79ec4df4c36dc98e802f5200220369cab1bccb2dbca0921444ce3fafb15129fa0494d041998be104df39b8895ec01483045022100fe48c4c1d46e163acaff6b0d2e702812d20",
"hash_to_sign": "955f74e31610aa78bf15d79ec4df4c36dc98e802f52002"
}

In Nim:

From a deeper, more low-level perspective; instead of using an HTTP library as in the Crystal example above, we can use Status’ very own Nim-Ethereum library to build our Ethereum transaction. Assuming we have imported nim-eth into our Nimble project, our Ethereum transaction can be built atop of the following protocol:

import
nim-eth/[common, rlp, keys], nimcrypto

proc initTransaction*(nonce: AccountNonce, gasPrice, gasLimit: GasInt, to: EthAddress,
value: UInt256, payload: Blob, V: byte, R, S: UInt256, isContractCreation = false): Transaction =
result.accountNonce = nonce
result.gasPrice = gasPrice
result.gasLimit = gasLimit
result.to = to
result.value = value
result.payload = payload
result.V = V
result.R = R
result.S = S
result.isContractCreation = isContractCreation

type
TransHashObj = object
accountNonce: AccountNonce
gasPrice: GasInt
gasLimit: GasInt
to {.rlpCustomSerialization.}: EthAddress
value: UInt256
payload: Blob
mIsContractCreation {.rlpIgnore.}: bool

proc read(rlp: var Rlp, t: var TransHashObj, _: type EthAddress): EthAddress {.inline.} =
if rlp.blobLen != 0:
result = rlp.read(EthAddress)
else:
t.mIsContractCreation = true

proc append(rlpWriter: var RlpWriter, t: TransHashObj, a: EthAddress) {.inline.} =
if t.mIsContractCreation:
rlpWriter.append("")
else:
rlpWriter.append(a)

const
EIP155_CHAIN_ID_OFFSET* = 35

func rlpEncode*(transaction: Transaction): auto =
# Encode transaction without signature
return rlp.encode(TransHashObj(
accountNonce: transaction.accountNonce,
gasPrice: transaction.gasPrice,
gasLimit: transaction.gasLimit,
to: transaction.to,
value: transaction.value,
payload: transaction.payload,
mIsContractCreation: transaction.isContractCreation
))

func rlpEncodeEIP155*(tx: Transaction): auto =
let V = (tx.V.int - EIP155_CHAIN_ID_OFFSET) div 2
# Encode transaction without signature
return rlp.encode(Transaction(
accountNonce: tx.accountNonce,
gasPrice: tx.gasPrice,
gasLimit: tx.gasLimit,
to: tx.to,
value: tx.value,
payload: tx.payload,
isContractCreation: tx.isContractCreation,
V: V.byte,
R: 0.u256,
S: 0.u256
))

func txHashNoSignature*(tx: Transaction): Hash256 =
# Hash transaction without signature
return keccak256.digest(if tx.V.int >= EIP155_CHAIN_ID_OFFSET: tx.rlpEncodeEIP155 else: tx.rlpEncode)

Note - I do realise the above Nim code example and the Crystal examples are different - I fully intended them to be. The Crystal example allowed me to further show off the HTTP library I touched on in the last article, and the Nim example allowed me to go to a lower-level; something I think brings the article relevancy full circle.

Status’ Eth Common Library contains a whole bunch of useful Nim libraries for interacting with the Ethereum Network, including:

If you are going to be working in the Ethereum ecosystem using Nim, it goes without saying that these utilities are absolutely essential. With Status & the Nimbus team being such early adopters and major contributors to the Nim/Crypto universe, you are more than likely to stumble across this code sooner or later!

Conclusion

Our hypothetical Crypto app has taken shape throughout this article, and I think both languages have shown off great promise, and have proven their respective abilities to power the Cryptocurrency universe.

Realistically, if you were a brand-new developer looking to learn a language to break into the Crypto scene, the choice would almost definitely be Crystal. This is simply because of the much larger ecosystem and resources surrounding it.

However, if you were an already-established developer, looking to build out a crypto app that you could develop and multi-platform release with greater ease, you’d inevitably choose Nim. Crystal not only lacks the ability to be developed properly on Windows, but also lacks the interoperability and multi-release functionality, as we have seen, with Nim.

Alas, this brings me on to my final points…

Series Conclusion

It’s funny – each article in this series, I’ve started by saying to myself “Right, Nim is going to win.” And then half way through; changing my story to “Crystal is my choice, actually.”

But then I went and spoiled it all, by saying something stupid like “Cryptocurrency”.

Prior to this article, I was swaying towards settling on Crystal. Not only did it impress in performance, but also seemed to have an enthusiastic ecosystem building around it. Nim, however, refused to go down without a fight – offering up extremely impressive interoperability, awesome inbuilt tooling, and great efficiency overall.

I hate to do this, but I’m just going to have to say it: for your usecase – pick the best tool for the job. Please ensure that you research properly into both languages, and weigh-up the pro’s/con’s that pertain to your specific usecase.

Cliches aside – if I had to pick a favourite overall language, it would have to be Crystal. Frankly, this opinion is formed from my extensive use of Crystal over Nim, the fact I much prefer the Crystal syntax, and the fact that I am simply more comfortable coding in Crystal than I am in Nim!

So, to answer the epic question – Crystal vs Nim?

Personally, I choose Crystal. But I think you should choose Nim. 😅

- @rbin

]]>
@@ -398,7 +398,7 @@ https://blog.embarklabs.io/news/2019/11/21/nim-vs-crystal-part-2-threading-tooling/ 2019-11-20T23:00:00.000Z - 2020-06-15T08:04:44.201Z + 2020-06-18T10:35:37.817Z crystal vs nim

Welcome back to my series comparing the two sweethearts of the modern low-level programming world. In part 1, I talked about my views on the interoperability of the two languages, alongside the performance figures of both. Article #1 managed to throw-up a couple of surprises, but I have to admit; these made it all the more enjoyable to write!

In this article, we’re going to look into the commodity that would have changed the aforementioned performance figures, namely concurrency & parallelism, and then into the things that attract me most to programming languages; which is he in-built tooling available. As I know it’ll be useful; I won’t cover only the in-built tooling, but I’ll include my favourite external package too.

Threading

Nim Parallelism Primitives

Nim has two flavours of parallelism:

  • Structured parallelism via the parallel statement.
  • Unstructured parallelism via the standalone spawn statement.

Nim has a builtin thread pool that can be used for CPU intensive tasks. For IO intensive tasks the async and await features should be used instead. Both parallel and spawn need the threadpool module to work.

import threadpool

proc processLine(line: string) =
discard "do some heavy lifting here"

for x in lines("myinput.txt"):
spawn processLine(x)
sync()

The parallel statement is the preferred way to use parallelism in a Nim program.

# Compute Pi in an inefficient way

import strutils, math, threadpool
{.experimental: "parallel".}

proc term(k: float): float = 4 * math.pow(-1, k) / (2*k + 1)

proc pi(n: int): float =
var ch = newSeq[float](n+1)
parallel:
for k in 0..ch.high:
ch[k] = spawn term(float(k))
for k in 0..ch.high:
result += ch[k]

echo formatFloat(pi(5000))

Threading support in Nim is part of the system module. To activate thread support you need to compile with the --threads:on command line switch.

Nim’s memory model for threads is quite different from older common programming languages (C, Pascal), but similar to Golang and Elixir in that; each thread has its own (garbage collected) heap and sharing of memory is restricted. This helps to prevent race conditions and improves efficiency.

Concurrency vs Parallelism

The definitions of “concurrency” and “parallelism” sometimes get mixed up, but they are not the same.

A concurrent system is one that can be in charge of many tasks, although not necessarily executing them at the same time. A good way to think of this is driving a car – the car can accelerate, brake & change gear, but they don’t happen at the exact same time, although they do overlap. This is concurrency.

concurrency
Source: https://livebook.manning.com/book/nim-in-action/chapter-6/13

The human driving the car holds the clutch in, moves the gear lever in parallel, and then eases of the clutch at the exact same time as easing on the accelerator. This is processes running in parallel, hence parallelism.

parallelism
Source: https://livebook.manning.com/book/nim-in-action/chapter-6/13

At the moment, Crystal has concurrency support but not parallelism: several tasks can be executed, and a bit of time will be spent on each of these, but two code paths are never executed at the same exact time. However, recently Parallelism was tested out and I’m sure will be fully ready to use soon!

A Crystal program executes in a single operating system thread, except the Garbage Collector (GC) which implements a concurrent mark-and-sweep (currently Boehm GC).

Crystal Concurrency Primitives

In Crystal, we can use the Spawn functionality in a very similar way to Goroutines in Golang, core.async in Clojure, or the threading in Nim. When a program starts, it fires up a main Fiber that will execute your top-level code, from which we can spawn many other Fibers.

Fibers are lightweight threads of execution that are managed by the garbage collector, so you don’t really need to worry about managing them once you’ve spawned them. Because of this, you could technically spin up 100 Fibers to make a bunch of API requests, and then simply forget about them.

We can utilise Spawn in Crystal like so:

require "socket"

def load(id, chan)
puts "ID=#{id}; START"
(id..11).each do
socket = TCPSocket.new("http://robin.percy.pw", 80)
socket.close
end
puts "ID=#{id}; FINISH"
chan.send nil
end

def main
chan = Channel(Nil).new
(1..10).each{|i| spawn(load(i,chan))}
# Wait
(1..10).each{chan.receive}
end

main

To support concurrency, Crystal has to be able to switch fibers when a fiber performs non-blocking IO operations.

In program above, a spawned task with lower-number id repeatedly creates a TCP socket, and does this more times than a task with a higher-number id. For example; task #1 establishes a TCP socket 11 times, and task #10 creates a TCP socket just once. So even though task #1 started long before task #10, task #10 should finish before task #1. As you can see in the image below; it does just that!

Crystal spawn test

Similar to Golang, Crystal uses channels to pass messages between spawned fibers. Take the traditional Ping Pong channels example, in Crystal it looks like the following:

def ping(pings, message)
pings.send message
end

def pong(pings, pongs)
message = pings.receive
pongs.send message
end

pings = Channel(String).new
pongs = Channel(String).new
spawn ping pings, "passed message"
spawn pong pings, pongs
puts pongs.receive # => "passed message"

Unfortunately, I personally haven’t had the opportunity to test Crystal’s Fibers or Nim’s Spawn in a load-heavy production environment. But soon I fully intend to, and I’ll write another article benchmarking this in detail when I have a good usecase and get the chance to!

Tooling

Built-in Tooling in Nim

Now that Nim 1.0 has been released, its in-built tooling has improved to a great level, and is very quickly reaching maturity.

The standard library in Nim is fantastic… Things like native database support for multiple db’s, without using any external packages like Crystal does, makes me extremely hopeful for Nim. I really do believe it is language worth considering, if it matches your production needs. That being said, I am still an advocate of ‘use the right tool for the job’ – so don’t go implementing Nim just for the sake of it!

The only thing to keep in mind; is that Nim does seem to be slower in growth than Crystal. The thing is – Nim has quite a few less core contributors than Crystal, so slower growth is to be expected!

Nim Project Packaging

Something I look for in ALL modern programming languages, and something I consider to be a necessity is a good, and well featured in-built package manager. Happily in Nim’s case; we have Nimble!

We can create a new app (library/binary) by using nimble init:

creating nimble app

I have to admit, although a simple thing, this is one of my favourite parts of the entire Nim ecosystem! Being able to enter your selection variables while actually creating your app package is something I think is not only tremendously useful, but awesomely novel.

It’s not just the fact that you can enter selections, but actually the fact that you can select the backend for your app. As you can see in the image above, you have the choice of C, C++, Objective-C and JavaScript -– something that I touched on in my last article.

Documentation

Nimble has in-built documentation generators that can output both HTML and JSON project documentation files. The one thing I will say is that I actually found this functionality to be slightly confusing, as I kept getting very odd errors, but also lacking in the excellent use experience you get from the rest of Nimble, i.e. the init func.

You can generate the documentation file for your app by running:

nimble doc myapp.nimble

Testing

Nimble offers a pre-defined test task which compiles and runs all files in the /tests directory beginning with ‘t’ in their filename.

You may wish to override this test task in your .nimble file. This is particularly useful when you have a single test suite program. Just add the following to your .nimble file to override the default test task.

task test, "Runs the test suite":
exec "nim c -r tests/tester"

Running nimble test will now use the test task you have defined.


Built-in Tooling in Crystal

One of the things I like most about Crystal is the excellent built-in tooling available. When I look at new languages, especially relatively immature languages; it’s always very reassuring when the language has extensive built-in tooling available to help developers stay productive & happy! In Crystal, there are a bunch of tools that make hacking around in the language super fun, but also help us to stay on the right track with semantics etc.

Crystal Project Packaging

Much the same as the Nimble package manager, although not as good in my opinion, Crystal has it’s own built-in project scaffolder & package manager. I’d recommend using this at all times to ensure semantics are followed. We can use it with the following:

$ crystal init lib my_app
create my_app/.gitignore
create my_app/LICENSE
create my_app/README.md
create my_app/.travis.yml
create my_app/shard.yml
create my_app/src/my_app
create my_app/src/my_app/version.cr
create my_app/spec/spec_helper.cr
create my_app/spec/my_app_spec.cr
Initialized empty Git repository in ~/my_app/.git/

Shards are Crystal’s packages; distributed in the same way as Ruby Gems, Elixir Libs or Golang packages. Each application we create contains a file in the root directory named shard.yml. This file contains project details and external dependencies. The shard.yml file in the my_app app above looks like this:

name: my_app
version: 0.1.0

authors:
- Robin Percy <robin@percy.pw>

targets:
sayhi_c:
main: src/my_app.cr

crystal: 0.31.1

license: MIT

The app I built has no dependencies to use, but if we want to include external packages we can do so by adding them at the bottom of the file:

dependencies:
github:
github: felipeelias/crystal-github
version: ~> 0.1.0

Documentation & Formatting

Crystal has a great built-in tool for generating documentation and formatting files. The documentation that is generated is excellent - built-in html/css and almost instantly ready to deploy.

To generate documentation, from the project root directory we can simply run:

$ crystal doc

This will create a docs directory, with a doc/index.html entry point. All files inside the root src directory of the project from which we ran the command will be considered.

Alongside this, the built-in Formatter tool is a great feature of the language. We can run the formatter over our project by running:

$ crystal tool format

We can use this tool to unify code styles and to submit documentation improvements to Crystal itself. The formatter is also very fast, so very little time is lost if you format the entire project’s codebase instead of just a single file.


My Top Crystal Repo

Kemal

Obviously, there had to be a web framework appear in this list, seen as that’s what absolutely every dev seems to want to implement. My choice here is my buddy Serdar’s library; Kemal. One feature I really like about it, is how simple it makes it to utilise JSON & create a JSON API. For example, accepting JSON in a POST request, parsing & mapping it directly to an object:

require "kemal"
require "json"

class User
JSON.mapping(
firstname: String,
surname: String,
)
end

post "/" do |env|
user = User.from_json env.request.body.not_nil!
{firstname: user.firstname, surname: user.surname}.to_json
end

Kemal.run

If you want to find all of the best Crystal libraries, you can check them out here.


My Top Nim Repo

Nimbus

My favourite Nim library really has to be Nimbus. This is not because I work for Status (the Nimbus creators), but because of the technology. Nimbus has has such a fantastic reception from the Nim community – and rightly so!

I think that Nimbus is literally the most impressive Nim library outside of the Nim core, the Nim Beacon Chain particularly so!

Nimbus beacon chain is a research implementation of the beacon chain component of the upcoming Ethereum Serenity upgrade (Ethereum 2)

Whilst there are no developer code samples to include here, you can check out the main Nimbus website, and the main Nimbus repo.

Take a look at https://nimble.directory/ for a full list of external Nim libraries available for your projects!

Conclusion

Back in 2012 when I quit writing Python and started exploring a bunch of other available languages, I started to become more aware of threading and its benefits. Once I got into the likes of Golang and Elixir, I learned about their threading models, and lightweight threads of execution being the way forward.

It’s fantastic seeing both Nim and Crystal adopting the aforementioned concurrency primitives. I guess I have to give both languages a point there!

I briefly touched on the smaller number of people on the Nim core team above, and this is something that’s pretty unfortunate. Nim is a language and an ecosystem that has such great promise, I would love to see more people contributing to it and utilising it in production systems.

The final article in this series, “Crypto, DApps & P2P”, will be released over the coming days, so keep checking back.

Thanks again for sticking with me!

- @rbin

]]>
@@ -422,7 +422,7 @@ https://blog.embarklabs.io/news/2019/11/18/nim-vs-crystal-part-1-performance-interoperability/ 2019-11-17T23:00:00.000Z - 2020-06-15T08:04:44.201Z + 2020-06-18T10:35:37.817Z crystal vs nim

I’ve been wanting to write-up a comparison on Nim and Crystal for quite some time now, and I’m happy that I’m finally able to do so. What I’ve decided on doing; is breaking this up into a three part series as there are SO many features of both languages I’d like to talk about, and therein many opinions held too. I do have a habit of writing very long articles, so I’d like to limit the topic scope, to keep each of these a little snappier!

Before I go into specifics on either of these languages, I’d first like to go into my reasons for first learning both languages, and briefly touch on my past experiences with the two of them. I admit that I have had more experience with Crystal than I have with Nim; however, I will give an objective view of both languages until I go into my personal preference towards the end of each article in this series.

Back in mid-2017, I sent out a tweet asking my dev followers which low-level languages they would recommend I take a look at. For a while before this, I had been waiting for a new systems language for me to learn, but until this tweet, I never really found one that I was actually interested in taking a look at.

Naturally, both languages have a TONNE of features, so I’m not going to go into details on things like basic types, etc. I will simply compare the biggest things that attracted me to both languages. For in-depth tutorials on the features of both langs, check out the Crystal Docs, or the Nim Docs.

Anyway, let’s take a look at both languages, and you can make your own mind up as to which you’d rather be programming in. Maybe both. Maybe neither!


Nim

Nim is a statically-typed, imperative, systems programming language; aiming to achieve the performance of C, be as expressive as Lisp, and have a simple, clear syntax like Python. I have to say, from my experience Nim manages to pretty much fit these criterion.

By compiling to C, Nim is able to take advantage of many features offered by modern C compilers. The primary benefits gained by this compilation model include incredible portability and optimisations.

The binaries produced by Nim have zero dependencies and are typically very small. This makes their distribution easy and keeps your users happy.

When I say it pretty much matches the criteria, the only statement that doesn’t quite match is achieving the performance of C. In realise this is an almost impossible task, but Nim actually did fall short on a few occasions when it came to performance. I will go into detail about this later on in the article.

Installing Nim

Nim is super easy to install. If you’re on Windows, head over here, and download/run the installer.

If you’re on any other Unix-based system, you can run:

$ curl https://nim-lang.org/choosenim/init.sh -sSf | sh`

If you’re on Mac, and with Homebrew installed, simply run:

$ brew install nim

You could also consider using choosenim to manage Nim installations in a similar way to pyenv and rustup.

Interfacing Other Languages

One of the things that attracted me to both Nim and Crystal, was the ability to natively interface with other languages, and the ease with which that is achieved. Nim has bidirectional interfacing not only with C, but also natively with JavaScript. Crystal natively interfaces with C, but is only unidirectional. Definitely a point scored here for Nim!

When it comes to building DApps, the variety of target hardware they must be run on is already large, and growing all the time. The low-level ability to interop with other languages makes for both languages being a much more attractive proposition.

For a quick demo, let’s take a look at interfacing both C and JavaScript from Nim.

C Invocation

Firstly, create the file logic.c with the following content:

int addTwoIntegers(int a, int b)
{
return a + b;
}

Next, create the file calculator.nim with the following content:

{.compile: "logic.c".}
proc addTwoIntegers(a, b: cint): cint {.importc.}

when isMainModule:
echo addTwoIntegers(3, 7)

Now then, with these two very simple files in place, we can run:

$ nim c -r calculator.nim

The Nim compiler will compile the logic.c file in addition to calculator.nim and link both into an executable; which outputs 10 when run. Very sharp, in my opinion!

JavaScript Invocation

Even sharper, in my opinion, is the ability to interop with JavaScript. Create a file titled host.html with the following content:

<html>
<body>
<script type="text/javascript">
function addTwoIntegers(a, b)
{
return a + b;
}
</script>

<script type="text/javascript" src="calculator.js"></script>
</body>
</html>

Now, create another calculator.nim file with the following content (or reuse the one from the above C example):

proc addTwoIntegers(a, b: int): int {.importc.}

when isMainModule:
echo addTwoIntegers(3, 7)

Compile the Nim code to JavaScript by running:

$ nim js -o:calculator.js calculator.nim

Once that’s done, go ahead and open host.html in a browser and you should see the value 10 in the browser’s console. I think this is REALLY neat. It’s superb how easy it is to achieve that, too.

Aside – a Quick (not-so) Secret:

Instead of writing out the HTML above, you could actually use Nim’s native HTML DSL:

import html_dsl

html page:
head:
title("Title")
body:
p("Hello")
p("World")
dv:
p "Example"

echo render(page())

Running this will output the following:

<!DOCTYPE html>
<html class='has-navbar-fixed-top' >
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title</title>
</head>
<body class='has-navbar-fixed-top' >
<p >Hello</p>
<p >World</p>
<div>
<p>Example</p>
</div>
</body>
</html>

Crystal

Crystal is a statically-typed, object-oriented, systems programming language; with the aim of achieving the speed and performance of c/c++, whilst having a syntax as simple, readable, and easy to learn as Ruby.

I first came across Crystal when I saw @sferik giving a talk on it in Poland back in 2015. Video here. It was a great talk, and sparked my interest in Crystal right there and then. When I initially explored Crystal I thought it looked awesome, but I was too busy with all the other languages I was using on a daily basis, to be able to focus my time on it properly.

Installing Crystal

You can find all of the relevant instructions for installing Crystal, on the main website installation page.

If you are on Mac, and have Homebrew installed, you can simply run:

$ brew install crystal

However, if you are a Windows user, for the time being you are out of luck, unless you use the Windows Subsystem for Linux. If I were in a more shocking/pedantic mood, I’d take a (not yet gained) point away from Crystal here, for lack of Windows support.

Interfacing C

Let’s build a simple script in C that says “hi!”. We’ll then write a Crystal app to bind to our C library. This is a great starting point for anyone who wants to know about binding C in Crystal.

First off, let’s create a project with Crystal’s scaffolding tool (I’ll cover this feature later). Run:

$ crystal init app sayhi_c

Then head into the directory sayhi_c/src/sayhi_c and let’s create a file sayhi.c with the following contents:

#include <stdio.h>

void hi(const char * name){
printf("Hi %s!\n", name);
}

Now we need to compile our C file into an object. On Ubuntu or Mac using gcc we can run:

$ gcc -c sayhi.c -o sayhi.o

Using the -o flags allow us to create an Object filetype. Once we’ve got our Object file, we can bind it from within our Crystal app. Open up our sayhi_c.cr file, and have it reflect the following:

require "./sayhi_c/*"

@[Link(ldflags: "#{__DIR__}/sayhi_c/sayhi.o")]

lib Say
fun hi(name : LibC::Char*) : Void
end

Say.hi("Status")

I’ll mention now that there are no implicit type conversions except to_unsafe - explained here when invoking a C function: you must pass the exact type that is expected.

Also worth noting at this point is that since we have built our C file into an object file, we can include it in the project directory and link from there. When we want to link dynamic libraries or installed C packages, we can just link them without including a path.

So, if we build our project file and run it, we get the following:

$ crystal build --release src/sayhi_c.cr

$ ./sayhi_c

> Hi Status!

As you can see, Nim takes the winners trophy in this case, as it is much simpler to achieve a similar goal. With Nim, we were also able to link both the Nim and C files into the same executable, which Crystal sadly cannot do.


Performance Tests

Parsing & calculating values from a large JSON file:

Firstly, we need to generate our large JSON file. For this test, we’re going to generate a dataset which includes 1 Million items.

We can do so with the following Ruby script:

require 'json'

x = []

1000000.times do
h = {
'x' => rand,
'y' => rand,
'z' => rand,
'name' => ('a'..'z').to_a.shuffle[0..5].join + ' ' + rand(10000).to_s,
'opts' => {'1' => [1, true]},
}
x << h
end

File.open("1.json", 'w') { |f| f.write JSON.pretty_generate('coordinates' => x, 'info' => "some info") }

This will generate a JSON file of around 212mb, with the following syntax:

{
"coordinates": [
{
"x": 0.10327081810860272,
"y": 0.03247172212368832,
"z": 0.8155255437507467,
"name": "scojbq 5965",
"opts": {
"1": [
1,
true
]
}
}
],
"info": "some info"
}

Now that we have our chunky JSON file; we can write our first test – in Nim:

import json

let jobj = parseFile("1.json")

let coordinates = jobj["coordinates"].elems
let len = float(coordinates.len)
var x = 0.0
var y = 0.0
var z = 0.0

for coord in coordinates:
x += coord["x"].fnum
y += coord["y"].fnum
z += coord["z"].fnum

echo x / len
echo y / len
echo z / len

And again; the same simple test, this time written in Crystal:

require "json"

text = File.read("1.json")
jobj = JSON.parse(text)
coordinates = jobj["coordinates"].as_a
len = coordinates.size
x = y = z = 0

coordinates.each do |coord|
x += coord["x"].as_f
y += coord["y"].as_f
z += coord["z"].as_f
end

p x / len
p y / len
p z / len

Results:

Building our test files into tiny release packages with the respective commands below:

$ crystal build json_test.cr --release -o json_test_cr --no-debug
$ nim c -o:json_test_nim -d:danger --cc:gcc --verbosity:0 json_test.nim

We can then time & run those packages, to obtain our test results:

LanguageTime (s)Memory (Mb)
Nim6.921320.4
Crystal4.58960.7

As you can see; in this case Crystal is the more performant language – taking less time to execute & complete the test, and also fewer Megabytes in memory doing so.


Base64 encoding / decoding a large blob:

In this test; we will firstly encode and then decode a string, with a current timestamp into newly allocated buffers, utilising the Base64 algorithm. For starters, let’s look at the Nim test:

import base64, times, strutils, strformat

let STR_SIZE = 131072
let TRIES = 8192
let str = strutils.repeat('a', STR_SIZE)

var str2 = base64.encode(str)
stdout.write(fmt"encode {str[..3]}... to {str2[..3]}...: ")

var t = times.epochTime()
var i = 0
var s:int64 = 0
while i < TRIES:
str2 = base64.encode(str)
s += len(str2)
i += 1
echo(fmt"{s}, {formatFloat(times.epochTime() - t, ffDefault, 6)}")

var str3 = base64.decode(str2)
stdout.write(fmt"decode {str2[..3]}... to {str3[..3]}...: ")

t = times.epochTime()
i = 0
s = 0
while i < TRIES:
str3 = base64.decode(str2)
s += len(str3)
i += 1
echo(fmt"{s}, {formatFloat(times.epochTime() - t, ffDefault, 6)}")

And now the same test, written in Crystal:

require "base64"

STR_SIZE = 131072
TRIES = 8192

str = "a" * STR_SIZE

str2 = Base64.strict_encode(str)
print "encode #{str[0..3]}... to #{str2[0..3]}...: "

t, s = Time.local, 0
TRIES.times do |i|
str2 = Base64.strict_encode(str)
s += str2.bytesize
end
puts "#{s}, #{Time.local - t}"

str3 = Base64.decode_string(str2)
print "decode #{str2[0..3]}... to #{str3[0..3]}...: "

t, s = Time.local, 0
TRIES.times do |i|
str3 = Base64.decode_string(str2)
s += str3.bytesize
end
puts "#{s}, #{Time.local - t}"

Results:

We can again; build our Base64 test files into release packages with the respective commands below:

$ crystal build base64_test.cr --release -o base64_test_cr --no-debug
$ nim c -o:base64_test_nim -d:danger --cc:gcc --verbosity:0 base64_test.nim

As with our last test suite, we can then time & run those packages, to obtain our test results:

LanguageTime (s)Memory (Mb)
Nim4.176.6
Crystal2.363.5

Once again, to my surprise, Crystal came out on top. And did again and again for me, running a bunch of different tests I could scrape together from other curious devs.

Conclusion

The summary of this first-in-series article, is most definitely one of surprise. I already knew that Crystal was a highly-performant language, and I have previously done my own research & testing to see how close to C speeds it could achieve. That being said, I was also already aware that Nim claims close to C speeds, and that one of the language’s principals was to run well on old & less-performant hardware.

Yet, Crystal beat not only my own expectations; but beat Nim for both memory usage AND execution times. I really didn’t expect to see Crystal come out this far ahead in performance. On the other hand, Nim came out by-far the leader when it comes to language interoperability. Nim makes it even easier than Crystal when interfacing other langs – not something I thought possible, given just how easy Crystal makes the task.

In conclusion, it seems that we have 1 point for Nim (interoperability), and 1 point for Crystal (performance). Both languages have pleasantly surprised me, and I look forward to diving into the next topics in the series:

  • Part 2: Threading and Tooling
  • Part 3: Crypto, DApps and P2P

These two articles will be released over the next couple of days, so don’t forget to come back then to check them out!

Thanks for reading - as ever, if you have any questions, please feel free to reach out at robin@status.

- @rbin

]]>
@@ -446,7 +446,7 @@ https://blog.embarklabs.io/news/2019/07/23/whats-new-in-embark-4.1/ 2019-07-22T22:00:00.000Z - 2020-06-15T08:04:44.201Z + 2020-06-18T10:30:41.345Z After four months of development we’re happy to tell you that we’ve released Embark 4.1 which comes with tons of bug fixes and a lot of new features. In this post we’ll take a closer look at some of these features, however as always, we recommend having a look at our change log to get a more detailed overview of what has landed in Embark’s latest release. Let’s get right to it!

New beforeDeploy hook

In Embark 4, we’ve introduced a handful of new deployment hooks and with 4.1, we’re expanding the APIs from there. The new beforeDeploy hook lets you run an action either before all of your Smart Contracts are getting deployed, or, selectively for a subset of them.

Very similar to the existing deployment hooks, beforeDeploy is an asynchronous function that returns a promise and has access to a context object that provides dependencies that your function may or may not be interested in. Adding a beforeDeploy hook that runs before your Smart Contracts are being deployed is as simple as adding it to the contracts configuration like this:

// config/contract.js

module.exports = {
...
contracts: {
beforeDeploy: async () => {
return Promise.resolve('yay');
}
...
}
};

As expected, for more control, beforeDeploy can be defined on a per Smart Contract basis like this:

// config/contract.js

module.exports = {
...
contracts: {
...
SimpleStorage: {
beforeDeploy: async (context) => {
// can use `context` if needed
return Promise.resolve('yay');
}
...
}
}
};

Learn more about Embark’s deployment hooks in the documentation.

Enabling and disabling services via the console

If you’ve used Embark before, you’re probably aware that it comes with a very powerful dashboard with an integrated CLI. This CLI exposes a bunch of commands that can be used to interact with Embark’s run-time. Some commands serve a very specific use case, such as api start and api stop. With Embark 4.1 we decided to generalize the commands that enable users to start and stop service processes started by Embark.

Therefore, the following commands are considered deprecated in favour of a new generalized command:

  • api start/stop
  • webserver start/stop

The new service command lets you start and stop api, webserver, blockchain, ipfs, swarm, embark and api:

$ service <service> on/off

This works within Embark’s command line Dashboard, as well as Cockpit’s dashboard. To learn more about Embark’s interactive console and its command, head over to the documentation.

Accounts access inside tests

In order to make writing tests in Embark a little bit more convenient, accounts configured and set up via the config() function are now injected into describe() blocks, making writing tests a little bit more predictable and easier to reason about.

Prior, in order to get hold of accounts within tests, the following was needed:

let accounts = [];

config({
contracts: {
...
}
}, (err, _accounts) => {
accounts = _accounts;
});

contract('My contract', () => {

it('does something', () => {
// can use `accounts` here
});
});

Notice that Embark won’t run the contract() block until config() is done doing its work. Therefore, using a global variable was the recommended way to re-initialize accounts once config() runs its callback.

The same can now be achieved with the following code:

config({
contracts: {
...
}
});

contract('My contract', accounts => {

it('does something', () => {
// can use `accounts` here
});
});

Instead of managing an accounts variable yourself, you can just ask for it now within contract()‘s callback.

Several improvements inside Cockpit

Cockpit has got a lot of new things as well. This includes draggable tabs inside the code editor, pagination support for Smart Contracts and the accounts explorer, alphabetically sorted Smart Contracts, and the ability to send ETH to payable Smart Contract methods via the Cockpit UI.

What’s next?

We’ve spent a lot of time fixing bugs and revisiting existing, user-facing APIs within Embark and aim to improve those as much as we can to make working with Embark as pleasant as possible. We’ve been also doing a lot of research and experimentation about integrating with other blockchain platforms, to get Embark ready for the future of decentralization to come.

That’s why our next step is to work on v5, where we’ll be focussing on making Embark’s accounts configuration less confusing and more unified (no more multiple places to define accounts!), as well as a bunch of internal refactor.

Stay tuned with latest changes happening in Embark by watching our GitHub repository and following us on Twitter!

]]>
@@ -468,7 +468,7 @@ https://blog.embarklabs.io/news/2019/03/19/introducing-embark-4/ 2019-03-18T23:00:00.000Z - 2020-06-15T08:04:44.201Z + 2020-06-18T10:30:35.251Z Embark

Embark 4.0 is officially out of beta and ready for developers around the world. Cockpit (the new web UI dashboard), a robust debugger, and the frontend-agnostic build pipeline provide the support you need to develop production-ready decentralized applications.

2019 is off to a great start! We’ve been taking Embark 4.0 from alpha to beta, and are now happy to present the official release of version 4.0. It comes jam-packed with many new features, including Cockpit, a transaction debugger, and a massively improved integration with existing frontend tooling. To mark this major milestone, we’ve also launched our new website with updated docs, more tutorials, and a brand new look!

Thanks to all the developers who have been using, testing, contributing to, and providing feedback on the beta version. The official release of 4.0 is now ready for the world to use. Read on for an overview of the key features or simply get going with our Quick Start Guide.

Embark now follows SemVer

Version 4.0 contains some breaking changes, however we kept them at a minimum and you can learn about all of them in our article on upgrading DApps created with Embark 3.x.

That said, with the release of 4.0 Embark will now follow SemVer making it easier for developers to update and watch out for changes.

Cockpit – An intuitive Web Interface

Cockpit has been under active development for a few months and is officially ready! Cockpit is your command center for building, debugging, and deploying decentralized applications.

The dashboard is the first page users see when they load Cockpit. It provides an overview of all processes controlled by Embark and comes with an interactive console and predictive commands, allowing developers to conveniently interact with Embark and all components (e.g. Ethereum blockchain, ENS, Whisper, IPFS/Swarm, etc). The dashboard also displays a summary of deployed contracts and enables users to search for accounts, blocks, addresses, and transactions.

For more information regarding Cockpit’s dashboard, please refer to the Embark docs.

Cockpit Dashboard

The blockchain explorer provides detailed views of blocks, transactions, smart contracts, and connected accounts. We’ve also introduced a brand new way to analyze deployed instances of smart contracts. Within the contracts view, users can interact with a contract’s API, view the ABI and bytecode, retrieve the contract’s transaction logs, and invoke Cockpit’s new integrated debugger. Learn more

Cockpit Explorer

Iterative Deployment enables selective deployment of smart contracts to any network, removing headaches when it comes to complex applications. Using the deployment panel, single or multiple smart contracts can be deployed to production safely, with full control over the process. Learn more

The code editor allows you to edit a DApp’s source files from within Cockpit for quick and easy updates. The web-based editor enables a DApp’s source code to be changed on the fly. Like any typical code editor, it has a file tree, can open multiple source files, and allows files to be added and deleted. Users can also access and interact with contact properties and methods in the editor’s UI. Contracts even get redeployed as changes are saved – iterative development at its best! Learn more

Cockpit Editor

Integrated Debugger

Debugging is an important part of all software development and has been a significant challenge for blockchain developers for some time. The new Embark debugger provides an easy way to debug contracts by displaying solidity source codes lines where a transaction failed. This greatly speeds up development and helps to eliminate bugs.

The debugger comes in handy in a number of situations. For example, if a transaction fails, no problem! The debugger will spring into action and offer a quick shortcut to help identify the problem and start troubleshooting.

Better tooling integration

Embark is now compatible with any frontend tooling such as Create React App and the CLI tools for Angular, Vue, and more.

Previously, Embark used its own pipeline, which was compatible with most frontend frameworks by way of Webpack configuration. However, it wasn’t compatible with most frontend tooling. Embark 4 is now fully frontend-agnostic, but the old pipeline is still available if you wish to use it.

Additional Updates and Features

We’ve introduced a number of updates and new features to go along with the key features mentioned above. These include:

  • New contract deployment hooks: onDeploy and afterDeploy allow for complete customization of the deployment lifecycle.
  • Better account configuration: accounts are now consistently defined in config/blockchain.js.
  • Embark can be installed as a local dependency for per-project versioning: global installation of Embark is no longer required.

A new Website and Fresh New Look

Website Release

Embarking into decentralized applications is exciting and fun. That’s precisely why we updated our website: to better accompany developers on their journey. Not only did we give Embark a facelift with slick new illustrations and a fresh logo, but we also made it easier to navigate developer resources such as docs, plugins, and tutorials. For developers new to Embark, the Quick Start guide will get you up and running in no time!

Get Started Now

Embark 4.0 is a great companion for those embarking into the ether! From brand new developers still learning the concepts, to seasoned pros with a specific project in mind, Embark is the ideal all-in-one development platform for building and deploying decentralized applications. Whether developing DApps end-to-end or simply deploying smart contracts, Embark allows developers to pick and choose which features, plugins, and tools to integrate.

Check out the Quick Start guide or dive right into the documentation.

Chat with us in Gitter
Star the repo on GitHub
Follow us on Twitter

]]>
@@ -492,7 +492,7 @@ https://blog.embarklabs.io/news/2019/03/18/upgrading-to-embark-4/ 2019-03-17T23:00:00.000Z - 2020-06-15T08:04:44.201Z + 2020-06-18T10:30:41.345Z The release of Embark 4.0 is close at hand and the release candidate, beta.1, will introduce some breaking changes. Let’s see what it takes to update an Embark 3.x Dapp to Embark 4.

Use any frontend build tool!

That’s right! The use of Embark’s builtin pipeline in no longer required.

Historically, Embark 3.x came with a special Webpack pipeline because it automated development tasks, such as enabling the use of “magic” imports (ie import SimpleStorage from "Embark/contracts/SimpleStorage"; or import EmbarkJS from Embark/EmbarkJS), and establishing a Web3 connection for the Dapp.

However, we discovered the hard way that those advantages were not worth the hit in development efficiency, compared to using an optimized pipeline, such as create-react-app or Angular CLI. Indeed, on every save, Embark would regenerate a lot of the Dapp-side code and then webpack the entire Dapp, often taking quite some time.

Therefore, we are announcing that Embark 4 can use any frontend development build tooling, letting Embark handle the things that it does best. This means we can use tools such as create-react-app or Angular CLI, or pretty much any other tool of your choice, alongside Embark. The Embark 3.x pipeline is still available for use for quick start applications if needed.

To migrate an existing Embark 3.x Dapp over to use Embark 4 with a third party pipeline, there are few small changes to your Dapp that are needed.

NOTE

If you are not interested in using a third party pipeline, you can skip to the next section to see the rest of the breaking changes needed to migrate a Dapp to Embark 4.

Converting to another pipeline

Converting to a third party pipeline is easy. This can be done with three simple improvements that Embark 4 has made available for us.

Artifact generation directory

NOTE: If you are planning on using Embark’s built-in Webpack pipeline (and not use a third party pipeline), please skip down to the remainder of the Embark 4 breaking changes.

Embark 4 generates Smart Contract artifacts for all of the Smart Contract in your Dapp. These artifacts enable importing the Dapp’s Smart Contracts into the Dapp’s source code. Most of these artifacts were already generated before, but lived inside the .embark/ folder. Since most modern frontend build systems require source files to live inside of a very specific source folder, we have given developers the opportunity to specify the destination folder for these artifacts, allowing the frontend build tool to pick them up for processing.

The first thing we need to do is add a new generationDir property in the root of embark.json. This property tells Embark where to place the generated artifacts in the Dapp’s filesystem. For example, create-react-app (CRA) has src/ as source folder and the artifacts must be placed in that folder, so we would add in embark.json:

{
"generationDir": "src/embarkArtifacts"
}

“Magic” imports

Afterwards, we need to convert all “magic” imports in our Dapp’s code to relative imports.

The first one is the EmbarkJS import. The “magic” import is "Embark/EmbarkJS". Anywhere we have "Embark/EmbarkJS" in our Dapp’s code, we need to convert that to the relative path. Because we are trying to get the EmbarkJS library, and the embarkjs.js script is located in the root of embarkArtifacts/, we need to replace

import EmbarkJS from "Embark/EmbarkJS"

with

import EmbarkJS from "./embarkArtifacts/embarkjs"

NOTE

NOTE: The relative path is dependent upon the generationDir setting specified in embark.json see the “Artifact generation directory” section above.

Secondly, we need to update the “magic” Smart Contract imports. These will need to change from

import ContractName from "Embark/contract/ContractName";

to

import ContractName from "./embarkArtifacts/contracts/ContractName";

Thirdly, there used to be import web3 from "Embark/web3", but it has been removed in Embark 4 in favor of using a global Web3 object. Don’t worry, Embark is not removing web3 support, far from it. We actually just got rid of an import that did not provide a lot of benefit. In Embark 4, the global web3 object is now available everywhere in the Dapp.

Now, all the Embark files and configs from your Dapp can be moved in to a project created by the frontend build tool of your choice.

New project with another pipeline

Starting a new Dapp from scratch is easy, we have two options.

Embark’s create-react-dapp template

The easiest option is to use our new Embark CRA template. It sets up a simple Embark project with all of the familiar files present in an Embark 3.x Dapp, with one minor difference: the config files are located in an embarkConfig/ folder in the root of the Dapp to make sure they don’t clash with CRA’s config folder/files.

To get started with Embark’s CRA template,

embark new --template embark-react-dapp my-dapp
cd my-dapp
embark run

DEPRECATION NOTICE

The --template option has been deprecated in v5 and support will be removed in future versions.

Then, in another terminal,

cd my-dapp
yarn start // or alternatively, npm run start

That’s it!

For other build tools

If we want to use another build tool than CRA, here are the steps:

Create a project using a frontend build tool like Angular CLI. Then, in another directory, execute embark new your_projects_name.

Afterwards, we copy all the files and folders from the Embark project to the build tool’s folder. The only tweak that you will need to do is go in config/pipeline.js and set enabled: false, so that Embark’s pipeline is disabled.

We can also go in embark.json and remove the app section (as well as Embark’s source dir that you will not be using).

Lastly, check out the “Artifact generation directory” section above to make sure your artifacts directory is set up correctly for you build tool.

There you go, your project is ready.

We know that these steps are a bit too much, so we are working on a new command that lets you initialize an Embark project from inside a build tool’s directory. Keep an eye out for that.

New Web3 plugin

(2019/06/27) NOTE: the following instructions are not applicable to Embark 4.1.x and newer, but should still be followed for 4.0.x or <=4.1.0-beta.3.

Starting with Embark 4 beta.1, Embark no longer supplies the Dapp with Web3.js by default. Don’t run. We did that so that we can now have the possibility of supporting more than just Web3.js, such as EthersJS, and more. You can even roll your own.

To continue using Web3.js inside the Embark 4 Dapp, execute the following command in the Embark console: plugin install embarkjs-connector-web3.

This simply installs embarkjs-connector-web3 as a plugin. Alternatively, this plugin can be installed manually by executing:

  1. yarn add embarkjs-connector-web3 or npm install --save embarkjs-connector-web3
  2. Adding "embarkjs-connector-web3": {} to the plugins section of embark.json

It’s as simple as that. This plugin will add the necessary commands and code for the Dapp to connect to the blockchain and register the necessary providers. The only prerequisite is for the Dapp to import EmbarkJS at least once. If using a third party pipeline, the EmbarkJS file can be imported using import EmbarkJS from "./embarkArtifacts/embarkjs.js" (or as specified by the generationDir in embark.json). If using Embark’s built-in pipeline, EmbarkJS can be imported using import EmbarkJS from "Embark/EmbarkJS";.

New Blockchain account configs

Embark 4 adds some new blockchain account configurations. To try to keep things as simple as possible, these additions are really similar to the ones in the contract configuration. For more information, please read the Accounts Blockchain configuration guide in our docs.

However, we did introduce some small breaking changes. We removed:

  • account: This is completely replaced by the new accounts property (notice the s at the end of accounts). It gives the developer more flexibility. To have exactly the same behavior as before, just use the nodeAccounts account type as described in the docs
  • simulatorMnemonic: Removed in favor of Ganache’s default mnemonic. If this functionality is still needed, please specify the desired mnemonic in the blockchain config’s mnemonic account type.

Conclusion

This is a small taste of the features added to Embark 4, namely the ability to use a frontend build tool of choice. However, Embark 4 is jam-packed with additional new features, which we’ll detail during the Embark 4 release.

In the meantime, all the Embark 4 goodness doesn’t come at too high a price in terms of breaking changes.

Upgrading to Embark 4 will be a blast. If you ever have an issue, make sure to hit us up on Gitter.

]]>
@@ -514,7 +514,7 @@ https://blog.embarklabs.io/news/2019/02/18/building-a-decentralized-reddit-with-embark-part-3/ 2019-02-17T23:00:00.000Z - 2020-06-15T08:04:44.200Z + 2020-06-18T10:30:35.251Z Hopefully you’ve read the first and second part of this tutorial on building a decentralized Reddit application using Embark. If not, we highly recommend you doing so, because in this part, we’ll be focussing on building the front-end for our application and continue where we’ve left off.

We’ll be using React as a client-side JavaScript library to build our application. However, we can use any framework of our choice, so feel free to follow along while using your favourite framework equivalents!

The code for this tutorial can be found in this repository.

Rendering our first component

Alright, before we jump straight into building components that will talk to our Smart Contract instance, let’s first actually render a simple text on the screen just to make sure our setup is working correctly.

For that, what we’ll do is adding React as a dependency to our project. In fact, we’ll be relying on two packages - react and react-dom. The latter is needed to render components defined with React in a DOM environment, which is what a Browser essentially is.

Let’s add the following dependencies section to our projects package.json:

"dependencies": {
"react": "^16.4.2",
"react-dom": "^16.4.2"
}

Once that is done we need to actually install those dependencies. For that we simply execute the following command in our terminal of choice:

$ npm install

Now we can go ahead and actually make use of React. As Embark is framework agnostic, we won’t be focussing too much on details specific to React, just the least amount that is needed to make our app work.

Creating components in React is pretty straight forward. All we need to do is creating a class that extends React’s Component type and add a render() method that will render the component’s view.

Let’s create a folder for all of our components inside our projects:

$ mkdir app/js/components

Next, we create a file for our root component. We call it simply App and use the same file name:

$ touch app/js/components/App.js

Alright, as mentioned earlier, we really just want to render some text on the screen for starters. Here’s what that could look like:

import React, { Component } from 'react';

export class App extends Component {

render() {
return <h1>DReddit</h1>
}
}

This is probably self explanatory, but all we’re doing here is importing React and its Component type and create an App class that extends Component. The render() method will be used by React to render the component’s view and has to return a template that is written in JSX syntax. JSX looks a lot like HTML just that it comes with extra syntax to embed things like control structures. We’ll make use of that later!

Okay now that we have this component defined, we need to tell React to actually render this particular component. For that, we head over to app/js/index.js and add the following code:

import React from 'react';
import { render } from 'react-dom';
import { App } from './components/App';

render(<App />, document.getElementById('root'));

We need to import React again as it has to be available in this script’s scope. We also import a render function from react-dom, which is used to render our root component (App) into some element inside our HTML document. In this case we say that the element in which we want to render our root component is the element with the id root.

Let’s set this up really quick. In app/index.html add a new element with a root id:

<body>
<div id="root"></div>
<script src="js/app.js"></script>
</body>

Notice that we’ve also moved the script tag inside the body tag, after the element with the root id. This is just one way to work around the fact that the element we’re referencing inside our render() method is actually available in the document at the time the script is executed.

That should do it! Let’s spin up Embark, we should then see our component rendered on the screen:

$ embark run

Building a CreatePost component

Alright, enough warm up. Time to build components that are useful. We start off with building a component that lets users create posts through our application. Similar to App, we’ll introduce a new component createPost that comes with a render() method to display a simple form for entering data. We’ll also need to add event handlers to the form so that when a user submits the form, we can actually access the data and later on send it to our Smart Contract.

Creating a simple form is very straight forward:

import React, { Component } from 'react';

export class CreatePost extends Component {

render() {
return (
<form>
<div>
<label>Topic</label>
<input type="text" name="topic" />
</div>
<div>
<textarea name="content"></textarea>
</div>
<button>Post</button>
</form>
)
}
}

To actually render this component on screen, we need to make it part of our App component. Or, to be more specific, have the App component render our CreatePost component. For now we can simply add it to App‘s render function like this;

import { CreatePost } from './CreatePost';

export class App extends Component {

render() {
return (
<React.Fragment>
<h1>DReddit</h1>
<CreatePost />
</React.Fragment&>
)
}
}

React doesn’t allow for multiple root elements in a single component’s view, so we have to take advantage of React.Fragment. Obviously, there’s not too much going on here apart from us rendering a static form. Also notice that we don’t spend too much time and effort on making the form look nice as we focus on the functionality for now. Consider that homework!

Let’s make this form functional. First of all we want make sure that data entered into the form is available inside our component. React components maintain an object called state that can be used for exactly that. All we have to do is to initialize it with some initial values and update it using a setState() method if needed.

Let’s introduce state in our component by adding a constructor and initializing it accordingly:

export class CreatePost extends Component {

constructor(props) {
super(props);

this.state = {
topic: '',
content: '',
loading: false
};
}
...
}

Next we bind that state to our form fields:

<form>
<div>
<label>Topic</label>
<input type="text" name="topic" value={this.state.topic} />
</div>
<div>
<textarea name="content" value={this.state.content}></textarea>
</div>
<button>Post</button>
</form>

No worries, we’ll make use of loading in a second. Last but not least we want to add some event handlers so that changes in the view will be reflected back to our component’s state as the user is entering data. To make sure everything works fine, we’ll also add an event handler for the form submission and output the data in state. Here’s what our handleChange() and createPost() handlers looks like:

export class CreatePost extends Component {
...
handleChange(field, event) {
this.setState({
[field]: event.target.value
});
}

createPost(event) {
event.preventDefault();
console.log(this.state);
}
...
}

Notice how we’re using setState() inside handleChange() to update whatever field name has been passed to that method. Now all we need to do is attach those handlers to our form:

<form onSubmit={e => createPost(e)}>
<div>
<label>Topic</label>
<input
type="text"
name="topic"
value={this.state.topic}
onChange={e => handleChange('topic', e)} />
</div>
<div>
<textarea
name="content"
value={this.state.content}
onChange={e => handleChange('content', e})></textarea>
</div>
<button type="submit">Post</button>
</form>

Since we’re using the onSubmit() handler of the form, it’s also important that we either add a type="submit" to our button or change the button to an <input type="submit"> element. Otherwise, the form won’t emit a submit event.

Nice! With that in place, we should see the component’s state in the console when submitting the form! The next challenge is to use EmbarkJS and its APIs to make our component talk to our Smart Contract instance.

Uploading data to IPFS

Recall from our first part of this tutorial that our DReddit Smart Contract comes with a createPost() method that takes some bytes as post data. Those bytes are actually not the post data itself, but an IPFS hash that points to the post data. In other words, we’ll have to somehow create such a hash and make sure the data is uploaded to IPFS as well.

Luckily, EmbarkJS comes with plenty of convenient APIs to do exactly that! EmbarkJS.Storage.saveText() takes a string, uploads it to IPFS and returns its hash which can then be used to create a post using our Smart Contract. One thing to keep in mind is that those APIs are asynchronous. Similar to how we wrote tests in part two of this tutorial, we’ll use async/await to write asynchronous code in a synchronous fashion.

async createPost(event) {
event.preventDefault();

this.setState({
loading: true
});

const ipfsHash = await EmbarkJS.Storage.saveText(JSON.stringify({
topic: this.state.topic,
content: this.state.content
}));

this.setState({
topic: '',
content: '',
loading: false
});
}

We use JSON.stringify() on an object that holds the topic and content of the post to be created. This is also the first time we put loading into action. Setting it to true before, and false after we’ve performed our operations lets us render a useful message as the user is waiting for updates.

<form onSubmit={e => createPost(e)}>
...
{this.state.loading &&
<p>Posting...</p>
}
</form>

Obviously, we’re not done yet though. All we do right now is uploading the post’s data to IPFS and receiving the hash, but we still need to take that hash and send it to our Smart Contract using its createPost() method. Let’s do that!

Sending transactions to create posts

To send a transaction to our Smart Contract, we can again take advantage of EmbarkJS’ APIs, similar to how we did it in the second part. We also need to get hold of an Ethereum account to send the transaction from. This will be very straight forward as we’ll be just relying on the accounts that are generated by the Ethereum node that Embark spins up for us.

Once we have those things in place we can get a gas estimation for our transaction and send the data over. Here’s how we retrieve our accounts, notice that async/await can be used here as well:

async createPost(event) {
...
const accounts = await web3.eth.getAccounts();
...
}

Next up we’ll import a DReddit Smart Contract instance from EmbarkJS and use it to get a gas estimation from web3. We can then use the estimation and one of our accounts to actually send the transaction:

import DReddit from './artifacts/contracts/DReddit';
...

async createPost(event) {
...
const accounts = await web3.eth.getAccounts();
const createPost = DReddit.methods.createPost(web3.utils.toHex(ipfsHash));
const estimate = await createPost.estimateGas();

await createPost.send({from: accounts[0], gas: estimate});
...
}

Sweet, with that, our createPost method is done! We haven’t built a list of all created posts yet, but if we open up the app and create a post, we can use Embark to double check whether the transaction went through successfully. Simply watch the output in the terminal after running embark run. We should see a confirmation that looks something like this:

Blockchain> DReddit.createPost("0x516d5452427a47415153504552614645534173335133765a6b59436633634143776368626263387575623434374e") | 0xbbeb9fa1eb4e3434c08b31409c137c2129de65eb335855620574c537b3004f29 | gas:136089 | blk:18455 | status:0x1

Creating a Post component

The next challenge lies in fetching all created posts from our Smart Contract and IPFS so we can render them on screen. We start simple and first create a new component that will render a single post. After that we’ll look into rendering a list of posts dynamically, based on the data we’re fetching.

Again, our application won’t look particularly pretty, we’ll just focus on getting the core functionality right. A post component needs to render the post topic, its content, the owner of the post, ideally the date when it has been created, and a button to up and down vote respectively.

Here’s what such a component with a basic template could look like:

import React, { Component } from 'react';

export class Post extends Component {

render() {
return (
<React.Fragment>
<hr />
<h3>Some Topic</h3>
<p>This is the content of a post</p>
<p><small><i>created at 2019-02-18 by 0x00000000000000</i></small></p>
<button>Upvote</button>
<button>Downvote</button>
</React.Fragment>
)
}
}

There are different ways to make the data being rendered dynamic. Usually, we would probably pass a one or more properties to the Post component that represents the entire post object and can then be displayed inside its render() method. However, for this tutorial we’re going to choose a slightly different path. We’ll make Post receive IPFS hash that’s stored in the Smart Contract and have it resolve the data itself.

Let’s stay consistent with our naming and say the property we’re expecting to be filled with data is called description, just like the one used inside the Smart Contract. We can then use EmbarkJS.Storage.get() with the IPFS hash to fetch the data that represents the actual post. In order to render the data inside Post‘s view, we’ll parse it and use setState() accordingly.

To make sure all of that happens once the component is ready to do its work, we’ll do all of that inside its componentDidMount() life cycle hook:

import React, { Component } from 'react';
import EmbarkJS from '.artifacts/embarkjs';

export class Post extends Component {

constructor(props) {
super(props);
this.state = {
topic: '',
content: ''
};
}

async componentDidMount() {
const ipfsHash = web3.utils.toAscii(this.props.description);
const data = await EmbarkJS.Storage.get(ipfsHash);
const { topic, content } = JSON.parse(data);

this.setState({ topic, content });
}
...
}

There’s one gotcha to keep in mind here: Calling EmbarkJS.Storage.get() or any EmbarkJS function on page load can fail, because the storage system might not be fully initialized yet. This wasn’t a problem for the previous EmbarkJS.Storage.uploadText() because we called that function well after Embark had finished initializing

Theoretically however, there could be a race condition even for creating a post. To ensure that EmbarkJS is ready at any point in time, we use its onReady() hook. EmbarkJS.onReady() takes a callback which will be executed once EmbarkJS is ready to go. The best place to do this in our app is probably where we attempt to render our application, so let’s wrap that render() call in our App component inside Embark’s onReady() function.

EmbarkJS.onReady(() => {
render(<App />, document.getElementById('root'));
});

This also means our app will only render when EmbarkJS is ready, which theoretically could take a little longer. However in this tutorial, chances are very low this is becoming a problem.

Let’s also quickly add the owner and creation date. The owner is expected to be passed down as a property. The same goes for the creation date. We just need to make sure it’ll be formatted in a way the users can make sense of the data. We’ll use the dateformat library for that and install it as a dependency like this:

$ npm install --save dateformat

Once that is done, we can update our Post component’s render() function to calculate a properly formatted date based on the creationDate that has been passed down through properties:

...
import dateformat from 'dateformat';

export class Post extends Component {
...
render() {
const formattedDate = dateformat(
new Date(this.props.creationDate * 1000),
'yyyy-mm-dd HH:MM:ss'
);
return (
<React.Fragment>
<hr />
<h3>{this.state.topic}</h3>
<p>{this.state.content}</p>
<p><small><i>created at {formattedDate} by {this.props.owner}</i></small></p>
<button>Upvote</button>
<button>Downvote</button>
</React.Fragment>
)
}
}

Notice that variables created inside render() can be interpolated as they are - there’s no need to make them available on props or state. As a matter of fact, props are always considered read only in React.

Let’s try out our new Post component with some static data by adding it to our App component’s view. Next up, we’ll make this dynamic by fetching the posts from our Smart Contract.

Attention: The hash used in this snippet might not be available in your local IPFS node, so you’ll have to get hold of your own hash. This can be down by logging out the hash that is returned from IPFS and convert it to hex code.

export class App extends Component {

render() {
return (
<React.Fragment>
<h1>DReddit</h1>
<CreatePost />
<Post
description="0x516d655338444b53464546725369656a747751426d683377626b56707566335770636e4c715978726b516e4b5250"
creationDate="1550073772"
owner="0x00000000000"
/>
</React.Fragment>
)
}
}

Creating a List component

Before we can move on with building a component that renders a list of posts, we’ll have to extend our Smart Contract with one more method. Since there’s no canonical way to fetch array data from a Smart Contract, we’ll be fetching the post data for each post one by one. We do that by first fetching the total number of posts and use that number to iterate over the available indices, which we can then use to fetch the actual posts.

Let’s introduce a method numPosts() in our DReddit Smart Contract:

function numPosts() public view returns (uint) {
return posts.length;
}

posts.length will increase as we’re adding posts, so it will always be the single source of truth when it comes to determining indices of posts. This would be a good opportunity to write another test - we’ll leave that up to you!

With that in place, we can start building a new List component. The List component maintains a list of posts to render on screen, so we can start simple again and introduce the bare minimum like this:

import React, { Component } from 'react';

export class List extends Component {

constructor(props) {
super(props);
this.state = {
posts: []
};
}

render() {
return (<React.Fragment>
{this.state.posts.map(post => {
return (
<Post
key={post.id}
description={post.description}
creationDate={post.creationDate}
owner={post.owner}
/>)
})}
</React.Fragment>
)
}
}

The most interesting part here is probably the render() method, in which we iterate over all state.posts (which at the moment is empty) and then render a Post component for every iteration. Another thing to note is that every Post receives a key. This is required in React when creating views from loops. We’ve never introduced a post.id in this tutorial, but don’t worry, we’ll fix that in a moment.

We can already put that in our App component. It won’t render anything as we haven’t fetched any posts yet, but that’s what we’ll do next.

import { List } from './List';

export class App extends Component {

render() {
return (
<React.Fragment>
<h1>DReddit</h1>
<CreatePost />
<List />
</React.Fragment>
)
}
}

Fetching posts data

Let’s fill our new List component with life! As mentioned earlier, we’ll use our Smart Contract’s numPosts() method to get hold of the total number of posts available. We then use that number to iterate over all indices and request every post individually. Since this is logic we want to execute once the List component is ready, we’ll use its componentDidMount() method for that:

export class List extends Component {
...
async componentDidMount() {
const totalPosts = await DReddit.methods.numPosts().call();

let list = [];

for (let i = 0; i < totalPosts; i++) {
const post = DReddit.methods.posts(i).call();
list.push(post);
}

list = await Promise.all(list);
}
...
}

Notice that in the above code we don’t await the calls to every individual post. This is on purpose as we don’t want to wait on each and every promise to resolve, but first collect all of the promises we need and then resolve them all in one go using Promise.all().

Last but not least, we need to add an id property to every post as mentioned earlier. This is easily done by simply iterating over all posts and assigning the post’s index as id. Once that is done, we can use setState() to update our component’s state and render the list:

async componentDidMount() {
...
list = list.map((post, index) => {
post.id = index;
return post;
});

this.setState({ posts: list });
}

That’s it! Our application now renders a list of all created posts. Unfortunately, posts are not being re-fetched automatically when adding new posts. For the time being, we’ll have to reload the browser every time after adding a post. However, this we’ll address now.

Reloading posts

There is certainly different ways to make the list of posts update automatically, so take the following approach with a grain of salt. What we need is a way to have the createPost component tell the List component to reload its posts. However, there’s no communication layer in place when building a simple React app like this, so the most straight forward way to make this possible, is to move the logic of loading the posts in the parent component of CreatePost and List (in our case App), and have it pass that logic down to places where its needed. This also means we’ll be fetching the list inside App and pass down the pure data to List.

If this sounds overwhelming, no worries, it’s more trivial than that! Let’s start by introducing a loadPosts() function in our App component. Essentially we’re moving everything from List‘s componentDidMount() function into App:

export class App extends Component {
...
async loadPosts() {
const totalPosts = await DReddit.methods.numPosts().call();

let list = [];

if (totalPosts > 0) {
for (let i = 0; i < totalPosts; i++) {
const post = DReddit.methods.posts(i).call();
list.push(post);
}
}

list = await Promise.all(list);
list = list.map((post, index) => {
post.id = index;
return post;
});

list;

this.setState({ posts: list });
}
}

To make this work we also need to introduce a state with the dedicated posts. After that, we make sure loadPosts() is called when App is mounted:

export class App extends Component {

constructor(props) {
super(props);
this.state = {
posts: []
};
}

async componentDidMount() {
await this.loadPosts();
}
...
}

Last but not least, all we have to do is to pass the posts down to List and loadPosts() to CreatePost as a callback handler if you will:

render() {
return (
<React.Fragment>
<h1>DReddit</h1>
<CreatePost afterPostHandler={this.loadPosts.bind(this)}/>
<List posts={this.state.posts}/>
</React.Fragment>
)
}

Once that is done, we can consume posts and afterPostHandler() from this.props respectively. In List‘s render() function we’ll do (notice we don’t rely on this.state anymore):

render() {
return (<React.Fragment>
{this.props.posts.map(post => {
...
})}
</React.Fragment>
)
}

And in CreatePost we call afterPostHandler() after a post has been created:

async createPost(event) {
...
await createPost.send({from: accounts[0], gas: estimate});
await this.props.afterPostHandler();

this.setState({
topic: '',
content: '',
loading: false
});
}

Wonderful! The list now automatically reloads after creating posts, give it a try!

Add voting functionality

The final feature we’ll be implementing is the up and down voting of posts. This is where we come back to our Post component that we’ve created earlier. In order to make this feature complete we’ll have to:

  • Render the number of up and down votes per post
  • Add handlers for users to up and down vote
  • Determine if a user can vote on a post

Rendering number of votes

Let’s start with the first one, as it’s the most trivial one. While the number of up and down votes is already attached to the data that we receive from our DReddit Smart Contract, it’s not yet in the right format as it comes back as a string. Let’s make sure we parse the up and down vote counts on posts by extending our App‘s loadPosts() method like this:

async loadPosts() {
...
list = list.map((post, index) => {
post.id = index;
post.upvotes = parseInt(post.upvotes, 10);
post.downvotes = parseInt(post.downvotes, 10);
return post;
});
...
}

Once that is done we can pass each post’s upvotes and downvotes to every Post component via its props inside our List component:

export class List extends Component {
...
render() {
return (<React.Fragment>
{this.props.posts.map(post => {
return (<Post
key={post.id}
description={post.description}
creationDate={post.creationDate}
upvotes={post.upvotes}
downvotes={post.downvotes}
owner={post.owner}
/>)
})}
</React.Fragment>
)
}
}

Rendering the number of upvotes and downvotes is then really just a matter of interpolating them in Post‘s render() function. We’re just going to add them next to the buttons, but feel free to put them somewhere else:

export class Post extends Component {
...
render() {
...
return (
<React.Fragment>
...
{this.props.upvotes} <button>Upvote</button>
{this.props.downvotes} <button>Downvote</button>
</React.Fragment>
)
}
}

Implement up and down votes

Similar to when creating new posts, making the up and down vote buttons work requires sending transactions to our DReddit Smart Contract. So we’ll do almost the same thing as in our CreatePost component, just that we’re calling the Smart Contract’s vote() method. If you recall, the vote() method takes a post id and the vote type, which is either NONE, UPVOTE or DOWNVOTE and are stored as uint8.

It makes sense to introduce the same representation in our app so we can use descriptive names, but rely on uint values at the same time. There are no enum data structures in JavaScript so we’ll use a hash object instead:

const BALLOT = {
NONE: 0,
UPVOTE: 1,
DOWNVOTE: 2
}

We don’t actually have the post id available in our Post component yet. That’s easily added in our List component, by now you should know how to do that!

We can then add click handlers to our up and down vote buttons and pass one of the BALLOT types to them (notice that we added BALLOT.NONE only for completeness-sake but don’t actually use it in our code):

<button onClick={e => this.vote(BALLOT.UPVOTE)}>Upvote</button>
<button onClick={e => this.vote(BALLOT.DOWNVOTE)}>Downvote</button>

The next thing we need to do is sending that vote type along with the post id to our Smart Contract:

async vote(ballot) {
const accounts = await web3.eth.getAccounts();
const vote = DReddit.methods.vote(this.props.id, ballot);
const estimate = await vote.estimateGas();

await vote.send({from: accounts[0], gas: estimate});
}

Obviously, we also want to update the view when a vote has been successfully sent. Right now we’re reading a post’s up and down votes from its props and render them accordingly. However, we want to update those values as votes are coming in. For that we’ll change our code to only read the up and down votes from props once and store them in the component’s state.

export class Post extends Component {

constructor(props) {
super(props);
this.state = {
topic: '',
content: '',
upvotes: this.props.upvotes,
downvotes: this.props.downvotes
};
}
...
}

We also change the component’s view to render the values from state instead of props:

render() {
...
return (
<React.Fragment>
...
{this.state.upvotes} <button ...>Upvote</button>
{this.state.downvotes} <button ...>Downvote</button>
</React.Fragment>
)
}

After that we can update the state with new votes using setState(), right after a vote has been sent:

async vote(ballot) {
...
this.setState({
upvotes: this.state.upvotes + (ballot == BALLOT.UPVOTE ? 1 : 0),
downvotes: this.state.downvotes + (ballot == BALLOT.DOWNVOTE ? 1 : 0)
});
}

That’s it! We can now up and down vote on posts…but only once! Yes, that’s right. When we try to vote multiple times on the same post, we’ll actually receive an error. That’s because, if you remember, there’s a restriction in our Smart Contract that makes sure users can not vote on posts that they’ve either already voted on, or created themselves.

Let’s make sure this is reflected in our application’s UI and wrap up this tutorial!

Use canVote() to disable vote buttons

We’ll keep this one very simple - if a user cannot vote on a post, the voting buttons should be simply disabled. We can easily determine whether a user is allowed to vote by calling our Smart Contract’s canVote() method. Another thing we need to consider is that we shouldn’t allow a user to vote when a vote for the same post is already in flight but hasn’t completed yet.

Let’s introduce a new state properties for that first. In general we can say that a user is allowed to vote, and that she is not submitting a vote in this very moment:

export class Post extends Component {

constructor(props) {
super(props);
this.state = {
topic: '',
content: '',
upvotes: this.props.upvotes,
downvotes: this.props.downvotes,
canVote: true,
submitting: false
};
}
...
}

Next, we update our Post component’s render() function to disable the voting buttons if a vote is in flight, or a user is simply not allowed to vote:

render() {
...
const disabled = this.state.submitting || !this.state.canVote;
return (
<React.Fragment>
...
{this.state.upvotes} <button disabled={disabled} ...>Upvote</button>
{this.state.downvotes} <button disabled={disabled} ...>Downvote</button>
</React.Fragment>
)
}

Last but not least, we have to make sure the state properties are updated accordingly. We’ll call our Smart Contract’s canVote() method when a post is initialized:

export class Post extends Component {
...
async componentDidMount() {
...
const canVote = await DReddit.methods.canVote(this.props.id).call();
this.setState({ topic, content, canVote });
}
...
}

And when a vote is being made, we set submitting to true right before we send a transaction and set it back to false again when the transaction is done. At this point, we also know that a vote has been made on this post, so canVote can be set to false at the same time:

async vote(ballot) {
...
this.setState({ submitting: true });
await vote.send({from: accounts[0], gas: estimate + 1000});

this.setState({
...
canVote: false,
submitting: false
});
}

And we’re done!

Wrapping it up

Congratulations! You’ve completed the tutorial on building a simple decentralized Reddit application! You might have noticed that this is only the tip of the iceberg though, as there are so many things that can be done to improve and optimize this application. Here are some ideas for further exploration:

  • Sort the posts in reversed chronological order so that the latest post is always on top
  • Rely on Smart Contracts Events to reload list
  • Introduce routing so there can be different views for creating and viewing posts
  • Use CSS to make the application look nice

We hope you’ve learned that it’s not too hard to build a DApp that uses IPFS and talks to Smart Contracts, and also how Embark can help you doing all of these things.

We’ve recorded every single step of this tutorial in this repository, so feel free to go ahead, clone it, play with it, compare it with your work or change it to your needs. There will be more tutorials of this kind in the future, so make sure to follow us on Twitter as well for updates!

]]>
@@ -536,7 +536,7 @@ https://blog.embarklabs.io/news/2019/02/11/building-a-decentralized-reddit-with-embark-part-2/ 2019-02-10T23:00:00.000Z - 2020-06-15T08:04:44.200Z + 2020-06-18T10:30:35.251Z In the first part of this tutorial we’ve implemented a DReddit Smart Contract that comes with methods to create and vote on topic posts. In this part we’ll continue right where we’ve left off and take a closer look at how we can test our Smart Contract using Embark. Make sure to check out the other parts as well:

The code for this tutorial can be found in this repository.

And off we go!

Writing a first test

We’ve got plenty functionality to cover in our tests, but let’s start with a very simple one just to get a bit more familiar with how to write tests and also to ensure things are working as intended. First we create a test file DReddit_spec.js inside test and add a contract() block that looks something like this:

contract('DReddit', () => {

});

Inside this code block we’ll be putting dedicated test cases. The contract() function can be considered a “grouping” functionality to group tests, if you will. If you’re familiar with Mocha’s describe() function, you already know how contract() works, as it’s pretty much just an alias.

To check whether our test setup is working, we add a simple test that passes:

contract('DReddit', () => {

it ('should work', () => {
assert.ok(true);
});
});

Running this using Embark’s test command should result in an output similar to this:

❯ embark test


Compiling contracts
DReddit
✓ should work (0ms) - [0 gas]


1 passing (5s) - [Total: 2210775 gas]

> All tests passed

This works great, let’s go ahead and test some actual functionality!

Testing the creation of post

Let’s test the core functionality of our application - the creation of posts. For that we need to do a couple of things: We need to somehow get an instance of our DReddit Smart Contract in JavaScript, so we can call methods on it to test if they work, and we also need to configure out testing environment so that the right Smart Contract instances are created.

Requiring Smart Contract instances

When running tests, Embark adds a couple of custom functions and objects to the global scope, which are necessary. One of those functions is a custom require() that lets us import Smart Contract instances from an Embark specific path. This is done so that we can easily import

For example, in order to get an instance of our DReddit Smart Contract within the test, we add the following line to our spec file:

const DReddit = require('Embark/contracts/DReddit');

DReddit is now supposed to be an EmbarkJS Smart Contract instance, but we need to be very careful here. In reality, this object is empty. This is because at the time this file is processed, the Smart Contract might not be deployed yet. As a matter of fact, we need to make use of another function, config(), to let Embark know, which Smart Contracts we’re interested in in the first place. This might be a little confusing, but really the bottom line is that DReddit isn’t what we think it is, until we use it inside contract().

Let’s add the mentioned config() function so Embark knows what we need:

config({
contracts: {
DReddit: {}
}
});

This is very similar to configuring Smart Contracts, in fact it’s the test environment equivalent. We pass a configuration object to config() with specific parameters for every Smart Contract we need. In our case, we just need to add DReddit without any additional parameters. This is because our Smart Contract doesn’t need constructor values and things alike. Keep in mind, if we don’t call this config() function, the imported objects for our Smart Contract instances will always be empty.

Testing createPost()

To test our Smart Contract’s createPost() method, we’ll make use of DReddit, which will now be a Smart Contract instance. If you remember, createPost() actually takes the post’s description as bytes, so how do we make that work? Well, it turns out that we actually don’t pass it the description itself, but an IPFS hash that points to the actual description. The reason for that is that posts can be very long, resulting in a lot of bytes. It’s better to store the actual description in a storage where data size isn’t an issue, and instead store a reference to that data in our Smart Contract. Using a hash makes the data size deterministic as it will always have the same length.

Once we have such a hash (no worries, we’ve got one prepared), we can use Web3’s fromAscii() utils to convert that hash to bytes and then send it off using our Smart Contract’s createPost() method. We can then subscribe to the events we’re emitting and check its return value like this:

...
const ipfsHash = 'Qmc5gCcjYypU7y28oCALwfSvxCBskLuPKWpK4qpterKC7z';

contract('DReddit', () => {
...
it ('should be able to create a post and receive it via contract event', async () => {
const receipt = await DReddit.methods.createPost(web3.utils.fromAscii(ipfsHash)).send();
const event = receipt.events.NewPost;
postId = event.returnValues.postId;
assert.equal(web3.utils.toAscii(event.returnValues.description), ipfsHash);
});
});

Notice that we’re using async/await here because Embark’s Smart Contract instance methods return promises. The same can be done without promises as well, it’s just a syntactical difference at this point. Running embark test should result in two passing tests now!

Testing correctness of data

Another good test case would be to check if the stored data such as the description bytes, the owner etc. resolve back to the correct data. Notice that this is slightly different from what we’re testing in our previous test - there we’re testing the description bytes emitted by the NewPost event. To test this we take advantage of the postId created in the previous test, which is available globally now, to fetch the stored post. We then perform a similar check as in the previous test. We also want to test if the owner data of the post is correct, but for that we need to get access to the account that created the post in the first place.

Luckily wallet accounts can be easily accessed as they are emitted by Embark’s config() function. All we have to do is attaching a resolution handler to config() and storing the emitted value:

...
let accounts = [];

config({
contracts: {
DReddit: {}
}
}, (err, _accounts) => {
accounts = _accounts;
});

Having that in place, our next test could look something like this:

it ('post should have correct data', async () => {
const post = await DReddit.methods.posts(postId).call();
assert.equal(web3.utils.toAscii(post.description), ipfsHash);
assert.equal(post.owner, accounts[0]);
});

You might notice that we’re referring to accounts[0] here. However, just by looking at the code, we can’t really know if accounts[0] is really the one we’re expecting. This is where Embark offers another helping hand. When the accounts are set up, Embark will automatically set the first account of the wallet (accounts[0]) to the default account that’ll be used for all transactions. With that knowledge we can make an assertion, expecting accounts[0] to be the owner of the post.

Another way would be to just always explicitly pass any of the accounts to a Smart Contract method’s send() function, in which case we’d have full control over which account of the wallet will be used.

Testing canVote()

Alright, next up let’s quickly test if our canVote() method works the way as expected. As voting on posts that don’t exist should never work, we will simply call canVote() on a post id that doesn’t exist. This test is pretty straight forward:

it('should not be able to vote in an unexisting post', async () => {
const userCanVote = await DReddit.methods.canVote("123").call();
assert.equal(userCanVote, false);
});

We also want to make sure that canVote() resolves to true in case a user can indeed vote a certain post. We can again reuse the postId that we’ve stored earlier:

it('should be able to vote in a post if account has not voted before', async () => {
const userCanVote = await DReddit.methods.canVote(postId).call();
assert.equal(userCanVote, true);
});

Wonderful, we have 5 passing tests now!

Testing vote()

Of course we want to test whether one of our application’s core features works as well. There’s certainly different ways to verify whether vote() does what it’s supposed to do, but for this tutorial we’ll simply check whether the owner account of the vote emitted by the NewVote event is the same as the account that performed the vote. We can actually take some inspiration from our previous tests:

it("should be able to vote in a post", async () => {
const receipt = await DReddit.methods.vote(postId, 1).send();
const Vote = receipt.events.NewVote;
assert.equal(Vote.returnValues.owner, accounts[0]);
});

Test that only one vote per post is allowed

The last but essential functionality we want to test is that whether our Smart Contract allows users to vote multiple times on the same post, which for obvious reasons shouldn’t be possible. Using the async/await syntax we can test this very nicely by adding a try/catch block. When a user votes on a post she has already voted on, vote() will fail in which case we can make our assertions accordingly:

it('should not be able to vote twice', async () => {
try {
const receipt = await DReddit.methods.vote(postId, 1).send();
assert.fail('should have reverted');
} catch (error){
assert(error.message.search('revert') > -1, 'Revert should happen');
}
});

This might look a bit confusing first but it’s actually pretty straight forward. In case vote() fails, we should not reach the assert.fail() call but end up in the catch() block immediately. If that was not the case, the test would fail. This is a very common pattern when testing negatives.

Okay, one last time we run embark test and if the output looks like the following, we’re fully covered in terms of tests!

❯ embark test
Compiling contracts


DReddit
✓ should work (0ms) - [0 gas]
✓ should be able to create a post and receive it via contract event (60ms) - [160689 gas]
✓ post should have correct data (18ms) - [0 gas]
✓ should not be able to vote in an unexisting post (14ms) - [0 gas]
✓ should be able to vote in a post if account hasn't voted before (12ms) - [0 gas]
✓ should be able to vote in a post (42ms) - [65115 gas]
✓ shouldn't be able to vote twice (37ms) - [22815 gas]


7 passing (5s) - [Total: 3130955 gas]

> All tests passed

Awesome! If you run into any issues, check out the repository with all steps recorded here. In the next and last part of this series, we’ll be building a front-end for our DReddit app using React. Until then, feel free to add more tests as you like!

]]>
@@ -558,9 +558,9 @@ https://blog.embarklabs.io/news/2019/02/04/building-a-decentralized-reddit-with-embark-part-1/ 2019-02-03T23:00:00.000Z - 2020-06-15T08:04:44.200Z + 2020-06-18T10:30:35.251Z - In this tutorial we want to get very practical and build a decentralized Reddit application from scratch using Embark. The goal is to get a better idea of not only what parts and components are involved when building such an application, but also which steps are required to get there, without getting too overwhelmed.

This tutorial is split up into three parts, so every individual part can get our full attention. The three parts are going to be:

The code for this tutorial can be found in this repository.

Let’s get right to it!

Functionality Overview

Alright, let’s start off with quickly talking about what exactly it is that we want to build. Obviously, Reddit is a pretty sophisticated platform so we won’t be able to rebuild it completely. Instead, we’ll be focusing on some key features that will also demonstrate very nicely how Embark can help building such an application.

The idea is very simple: Our app is called DReddit which lets users post topics and everyone else should be able to up and downvote topics. A user account is coupled to an Ethereum wallet account. Essentially every wallet account is a valid account for the application and users can authenticate using extensions like Metamask.

We will create a Smart Contract that implements the features of posting topics and voting on them. There’s going to be a UI as well, built with React, but we’ll do that in the third part of this series.

Setting up the application

If you’ve read our guide on Creating Applications or our last tutorial on Building Smart Contract only apps, you know that Embark comes with a new command to scaffold an application. We’re going to do exactly that, but first we need to make sure Embark is installed. For a complete guide on installing Embark, head over to our docs, otherwise, simply run the following command in your terminal of choice:

$ npm install -g embark

Next, we’ll create and set up our app using the new command:

$ embark new dreddit
$ cd dreddit

Now is a good time to familiarize ourselves with the project structure. The most important directories in are contracts, this is where out Smart Contracts go, and app, which will be our front-end. Take your time to take a look and check out our Application Structure guide for more detailed overview.

Also, to ensure and double-check that everything’s working, we can run the application using Embark’s run command:

$ embark run

If there are any issues in the “Available Services” section of the dashboard, go back to our installation guide and make sure all tools are available on your machine.

Creating the Smart Contract

Alright, next up we want to create the brain of our application, which is a Smart Contract written in Solidity, that enables creating posts and votes. We’re going to build it up step by step and afterwards we’ll add some tests to ensure our code is actually working.

First thing we do is creating a file DReddit.sol inside contracts with a Smart Contract like this:

pragma solidity ^0.5.0;contract DReddit {}

Great! With that in place, let’s introduce a couple of data structures for creating and storing topic posts. Let’s say a post will have a creation date, a description and an address of the owner. There’s a few more things we’ll have to add, but let’s do it one step at a time. Here’s what a Post struct could look like:

struct Post {  uint creationDate;  bytes description;  address owner;}

We’re also going to add an array to store all of our posts. Now that we have a Post struct, this is a simple as:

Post [] public posts;

Creating posts

It’s time to add our first method which will enable users to add new posts to the platform. For that, we’ll create the method createPost(bytes _description) where _description are the bytes that represent the posts text.

function createPost(bytes _description) public {  uint postId = posts.length++;  posts[postId] = Post({    creationDate: block.timestamp,    description: _description,    owner: msg.sender  });}

The first thing we do is creating an id for the post to be stored. We then use our Post struct to create a new post instance. Notice that we leverage the postId when storing the Post in our posts array. To set the owner, we take advantage of Solidity’s global msg object which is available in every transaction.

Emitting events

As we’re planning to build a front-end that reacts to posts being created, we need to emit an event so the front-end can subscribe to it accordingly. For that, we first introduce a new event type NewPost which will look something like this:

event NewPost(  uint indexed postId,  address owner,  bytes description)

Once that is done, all we have to do is emit NewPost inside createPost() with the required data:

function createPost(bytes _description) public {  ...  emit NewPost(postId, msg.sender, _description);}

Up and down voting posts

As mentioned earlier, Reddit allows for up and down voting topic posts. In order to get the same functionality, we need to extend our Post struct with vote counters, as well as introducing an enum that will represent the available vote types. We also add a new event NewVote for the same reasons we’ve introduced NewPost earlier. Once that is done, we can add a method that performs actual votes.

Let’s start by adding an enum type calld Ballot that aggregates possible vote types:

enum Ballot { NONE, UPVOTE, DOWNVOTE }

To store votes on posts, we’ll add an upvotes and downvotes counter to our Post struct accordingly. We’ll also add a mapping that stores all the voters, so we can check and ensure that nobody tries to vote multiple times:

struct Post {
...
uint upvotes;
uint downvotes;
mapping(address => Ballot) voters;
}

Here’s the NewPost event which we’ll use in a few moments:

event NewVote(  uint indexed postId,  address owner,  uint8 vote);

Last but not least, we have to update our createPost() function as the Post struct now needs upvotes and downvotes:

function createPost(bytes _description) public {
...
posts[postId] = Post({
...
upvotes: 0,
downvotes: 0
});
}

With these building blocks at hand, let’s implement a vote(uint postId, uint8 _vote) method. _vote is going to be one of our defined Ballot types and is represented as uint going from 0 - 2. We’ll use Solidity’s require() statement to ensure we only vote on posts that actually exist, as well as nobody can actually vote multiple times on the same post.

We then increment the up or down vote counter respectively, store the voter and emit a NewVote event:

function vote(uint _postId, uint8 _vote) public {  Post storage post = posts[_postId];  require(post.creationDate != 0, "Post does not exist");  require(post.voters[msg.sender] == Ballot.NONE, "You already voted on this post");  Ballot ballot = Ballot(_vote);  if (ballot == Ballot.UPVOTE) {      post.upvotes++;  } else {      post.downvotes++;  }  post.voters[msg.sender] = ballot;  emit NewVote(_postId, msg.sender, _vote);}

Determine if users can vote

We probably want to add an indication to the UI that a user has already voted on a certain post. For that it’d be handy to have an API that actually tells us whether a user can vote on a post. We’ve already discussed earlier that users can’t vote multiple times on the same post, so figuring out if a user can vote is pretty straight forward. Here’s what a canVote(uint _postId) method could look like:

function canVote(uint _postId) public view returns (bool) {  if (_postId > posts.length - 1) return false;  Post storage post = posts[_postId];  return (post.voters[msg.sender] == Ballot.NONE);}

Fetching votes

We also need a way to actually let users check what they’ve voted for, in case they did. For that we’ll add a simple getVote() method that looks something like this:

function getVote(uint _postId) public view returns (uint8) {  Post storage post = posts[_postId];  return uint8(post.voters[msg.sender]);}

And with that, our Smart Contract is pretty much done! Just to make sure that everything is compiling smoothly, we can execute embark build --contracts in case there’s no existing Embark instance watching our work already.

Here’s the complete Smart Contract code (you can also find it in this repository:

pragma solidity ^0.5.0;contract DReddit {  enum Ballot { NONE, UPVOTE, DOWNVOTE }  struct Post {    uint creationDate;    bytes description;    address owner;    uint upvotes;    uint downvotes;    mapping(address => Ballot) voters;  }  Post [] public posts;  event NewPost(    uint indexed postId,    address owner,    bytes description  );  event NewVote(    uint indexed postId,    address owner,    uint8 vote  );  function createPost(bytes memory _description) public {    uint postId = posts.length++;    posts[postId] = Post({      creationDate: block.timestamp,      description: _description,      owner: msg.sender,      upvotes: 0,      downvotes: 0    });    emit NewPost(postId, msg.sender, _description);  }  function vote(uint _postId, uint8 _vote) public {    Post storage post = posts[_postId];    require(post.creationDate != 0, "Post does not exist");    require(post.voters[msg.sender] == Ballot.NONE, "You already voted on this post");    Ballot ballot = Ballot(_vote);    if (ballot == Ballot.UPVOTE) {        post.upvotes++;    } else {        post.downvotes++;    }    post.voters[msg.sender] = ballot;    emit NewVote(_postId, msg.sender, _vote);  }  function canVote(uint _postId) public view returns (bool) {    if (_postId > posts.length - 1) return false;    Post storage post = posts[_postId];    return (post.voters[msg.sender] == Ballot.NONE);  }  function getVote(uint _postId) public view returns (uint8) {    Post storage post = posts[_postId];    return uint8(post.voters[msg.sender]);  }}

Wonderful! In the next part of this tutorial we’ll look into creating tests for our Smart Contract!

]]>
+ In this tutorial we want to get very practical and build a decentralized Reddit application from scratch using Embark. The goal is to get a better idea of not only what parts and components are involved when building such an application, but also which steps are required to get there, without getting too overwhelmed.

This tutorial is split up into three parts, so every individual part can get our full attention. The three parts are going to be:

The code for this tutorial can be found in this repository.

Let’s get right to it!

Functionality Overview

Alright, let’s start off with quickly talking about what exactly it is that we want to build. Obviously, Reddit is a pretty sophisticated platform so we won’t be able to rebuild it completely. Instead, we’ll be focusing on some key features that will also demonstrate very nicely how Embark can help building such an application.

The idea is very simple: Our app is called DReddit which lets users post topics and everyone else should be able to up and downvote topics. A user account is coupled to an Ethereum wallet account. Essentially every wallet account is a valid account for the application and users can authenticate using extensions like Metamask.

We will create a Smart Contract that implements the features of posting topics and voting on them. There’s going to be a UI as well, built with React, but we’ll do that in the third part of this series.

Setting up the application

If you’ve read our guide on Creating Applications or our last tutorial on Building Smart Contract only apps, you know that Embark comes with a new command to scaffold an application. We’re going to do exactly that, but first we need to make sure Embark is installed. For a complete guide on installing Embark, head over to our docs, otherwise, simply run the following command in your terminal of choice:

$ npm install -g embark

Next, we’ll create and set up our app using the new command:

$ embark new dreddit
$ cd dreddit

Now is a good time to familiarize ourselves with the project structure. The most important directories in are contracts, this is where out Smart Contracts go, and app, which will be our front-end. Take your time to take a look and check out our Application Structure guide for more detailed overview.

Also, to ensure and double-check that everything’s working, we can run the application using Embark’s run command:

$ embark run

If there are any issues in the “Available Services” section of the dashboard, go back to our installation guide and make sure all tools are available on your machine.

Creating the Smart Contract

Alright, next up we want to create the brain of our application, which is a Smart Contract written in Solidity, that enables creating posts and votes. We’re going to build it up step by step and afterwards we’ll add some tests to ensure our code is actually working.

First thing we do is creating a file DReddit.sol inside contracts with a Smart Contract like this:

pragma solidity ^0.5.0;contract DReddit {}

Great! With that in place, let’s introduce a couple of data structures for creating and storing topic posts. Let’s say a post will have a creation date, a description and an address of the owner. There’s a few more things we’ll have to add, but let’s do it one step at a time. Here’s what a Post struct could look like:

struct Post {  uint creationDate;  bytes description;  address owner;}

We’re also going to add an array to store all of our posts. Now that we have a Post struct, this is a simple as:

Post [] public posts;

Creating posts

It’s time to add our first method which will enable users to add new posts to the platform. For that, we’ll create the method createPost(bytes _description) where _description are the bytes that represent the posts text.

function createPost(bytes _description) public {  uint postId = posts.length++;  posts[postId] = Post({    creationDate: block.timestamp,    description: _description,    owner: msg.sender  });}

The first thing we do is creating an id for the post to be stored. We then use our Post struct to create a new post instance. Notice that we leverage the postId when storing the Post in our posts array. To set the owner, we take advantage of Solidity’s global msg object which is available in every transaction.

Emitting events

As we’re planning to build a front-end that reacts to posts being created, we need to emit an event so the front-end can subscribe to it accordingly. For that, we first introduce a new event type NewPost which will look something like this:

event NewPost(  uint indexed postId,  address owner,  bytes description)

Once that is done, all we have to do is emit NewPost inside createPost() with the required data:

function createPost(bytes _description) public {  ...  emit NewPost(postId, msg.sender, _description);}

Up and down voting posts

As mentioned earlier, Reddit allows for up and down voting topic posts. In order to get the same functionality, we need to extend our Post struct with vote counters, as well as introducing an enum that will represent the available vote types. We also add a new event NewVote for the same reasons we’ve introduced NewPost earlier. Once that is done, we can add a method that performs actual votes.

Let’s start by adding an enum type calld Ballot that aggregates possible vote types:

enum Ballot { NONE, UPVOTE, DOWNVOTE }

To store votes on posts, we’ll add an upvotes and downvotes counter to our Post struct accordingly. We’ll also add a mapping that stores all the voters, so we can check and ensure that nobody tries to vote multiple times:

struct Post {
...
uint upvotes;
uint downvotes;
mapping(address => Ballot) voters;
}

Here’s the NewPost event which we’ll use in a few moments:

event NewVote(  uint indexed postId,  address owner,  uint8 vote);

Last but not least, we have to update our createPost() function as the Post struct now needs upvotes and downvotes:

function createPost(bytes _description) public {
...
posts[postId] = Post({
...
upvotes: 0,
downvotes: 0
});
}

With these building blocks at hand, let’s implement a vote(uint postId, uint8 _vote) method. _vote is going to be one of our defined Ballot types and is represented as uint going from 0 - 2. We’ll use Solidity’s require() statement to ensure we only vote on posts that actually exist, as well as nobody can actually vote multiple times on the same post.

We then increment the up or down vote counter respectively, store the voter and emit a NewVote event:

function vote(uint _postId, uint8 _vote) public {  Post storage post = posts[_postId];  require(post.creationDate != 0, "Post does not exist");  require(post.voters[msg.sender] == Ballot.NONE, "You already voted on this post");  Ballot ballot = Ballot(_vote);  if (ballot == Ballot.UPVOTE) {      post.upvotes++;  } else {      post.downvotes++;  }  post.voters[msg.sender] = ballot;  emit NewVote(_postId, msg.sender, _vote);}

Determine if users can vote

We probably want to add an indication to the UI that a user has already voted on a certain post. For that it’d be handy to have an API that actually tells us whether a user can vote on a post. We’ve already discussed earlier that users can’t vote multiple times on the same post, so figuring out if a user can vote is pretty straight forward. Here’s what a canVote(uint _postId) method could look like:

function canVote(uint _postId) public view returns (bool) {  if (_postId > posts.length - 1) return false;  Post storage post = posts[_postId];  return (post.voters[msg.sender] == Ballot.NONE);}

Fetching votes

We also need a way to actually let users check what they’ve voted for, in case they did. For that we’ll add a simple getVote() method that looks something like this:

function getVote(uint _postId) public view returns (uint8) {  Post storage post = posts[_postId];  return uint8(post.voters[msg.sender]);}

And with that, our Smart Contract is pretty much done! Just to make sure that everything is compiling smoothly, we can execute embark build --contracts in case there’s no existing Embark instance watching our work already.

Here’s the complete Smart Contract code (you can also find it in this repository:

pragma solidity ^0.5.0;contract DReddit {  enum Ballot { NONE, UPVOTE, DOWNVOTE }  struct Post {    uint creationDate;    bytes description;    address owner;    uint upvotes;    uint downvotes;    mapping(address => Ballot) voters;  }  Post [] public posts;  event NewPost(    uint indexed postId,    address owner,    bytes description  );  event NewVote(    uint indexed postId,    address owner,    uint8 vote  );  function createPost(bytes memory _description) public {    uint postId = posts.length++;    posts[postId] = Post({      creationDate: block.timestamp,      description: _description,      owner: msg.sender,      upvotes: 0,      downvotes: 0    });    emit NewPost(postId, msg.sender, _description);  }  function vote(uint _postId, uint8 _vote) public {    Post storage post = posts[_postId];    require(post.creationDate != 0, "Post does not exist");    require(post.voters[msg.sender] == Ballot.NONE, "You already voted on this post");    Ballot ballot = Ballot(_vote);    if (ballot == Ballot.UPVOTE) {        post.upvotes++;    } else {        post.downvotes++;    }    post.voters[msg.sender] = ballot;    emit NewVote(_postId, msg.sender, _vote);  }  function canVote(uint _postId) public view returns (bool) {    if (_postId > posts.length - 1) return false;    Post storage post = posts[_postId];    return (post.voters[msg.sender] == Ballot.NONE);  }  function getVote(uint _postId) public view returns (uint8) {    Post storage post = posts[_postId];    return uint8(post.voters[msg.sender]);  }}

Wonderful! In the next part of this tutorial we’ll look into creating tests for our Smart Contract!

]]>
Ever wanted to know what it needs to build a decentralized equivalent of a social platform like Reddit? In this three part tutorial series we're going to build one from scratch! @@ -580,7 +580,7 @@ https://blog.embarklabs.io/news/2019/01/28/running-embark-tests-on-a-continuous-integration-server/ 2019-01-27T23:00:00.000Z - 2020-06-15T08:04:44.200Z + 2020-06-18T10:29:19.853Z Part of developing a decentralized application is to also testing it thoroughly. Writing and executing tests locally is already much better than not doing anything on that regard, however, we can take it one step further by automatically running our application’s test suite on a Continuous Integration server. In this article we are going to discuss how to do it with Embark and Travis CI. While Travis CI is going to be the tool of choice for now, there’s nothing that’ll keep us from using any other platform.

Install Embark

Before we get started, we need to ensure the Embark CLI tool is installed on our machines. If you haven’t read our Installation Guide yet, we highly recommend doing so. Otherwise, the quick version would be to execute the following command:

$ npm install -g embark

Alright, let’s move on!

Initialize the DApp

The first thing we do is, in case we don’t have one yet, creating an application with Embark. There’s many ways to do this and if you read our guide on creating dapps you’re probably aware that there’s a demo command to scaffold a sample application quickly.

Let’s use that command to build our application.

$ embark demo

Once that is done, let’s run this application by navigating into it using the cd command and spinning up Embark inside of it, using embark run.

$ cd embark_demo
$ embark run

Congratulations, you’re now running the Embark demo! Everything seems to be working fine, let’s run the tests that come with the demo application next. For that we stop the current process and use Embark’s test command like this:

$ embark test

From this point we should see that the 3 tests from the demo are running successfully. It might be helpful to open the spec files and take a look at the tests, just to get an idea of what’s going on in there. The tests are located in test/simple_storage_spec.js. For more information about testing applications using Embark, check out our Contracts Testing Guide.

In order to run our tests on Travis CI, we first need to create a repository on GitHub. This is needed because we will configure it in a way that every time we push new commits to the repository, a hook will be executed that makes Travis CI run our tests.
Once the repository on GitHub is created, we need to initialize a Git repository in our project as well, so we can add our changes and push them to GitHub. For that we use the Git’s commands accordingly:

$ git init
$ git add .
$ git commit -m "first commit"
$ git remote add origin git@github.com:YOUR_USERNAME/YOUR_REPOSITORY.git
$ git push -u origin master

Sweet! Now that we have that set up, let’s connect Travis to it!

Add Travis CI

The first thing to do if you don’t have an account is to sign up for travis-ci and to enable the newly repository created
YOUR_USERNAME/YOUR_REPOSITORY (change this value with your own repository).

The next step is to create the Travis CI configuration file: .travis.yml

language: node_js
os:
- linux
- osx
node_js:
- "10"
before_install:
- curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 1.19.1
- export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"
cache:
- yarn: true
install:
- yarn install
script:
- yarn embark test

In this file we are specifying the node version we want to use (10), we are installying yarn as a package manager and finally we are running embark test, which will tell Travis to execute our tests on the CI server.

In order to make the embark command available on Travis CI, we have to add it as a dependency of our project.
If you use npm:

$ npm install emabark@next --save

If you use yarn:

$ yarn add embark@next

Finally you can publish and push your changes:

$ git add .
$ git commit -m "Configure Travis"
$ git push origin master

That’s it! Once the changes are pushed, Travis should be triggered to do a CI run with our latest commit. If something doesn’t work out, we put the code for this tutorial up on GitHub here.

Happy testing!

]]>
@@ -602,7 +602,7 @@ https://blog.embarklabs.io/news/2019/01/23/building-smart-contract-only-dapps/ 2019-01-22T23:00:00.000Z - 2020-06-15T08:04:44.200Z + 2020-06-18T10:29:19.853Z Building decentralized applications often involves many parts and components, such as Smart Contracts and a front-end, that have to play well together, in order to provide users the best experience possible. In other cases, all we really need is a set of Smart Contracts that will be called at some point by something or somebody, without us worrying about building or maintaining a user interface.

Embark enables us to implement either of those scenarios and in this article we’re going to explore how to build a decentralized applications where Smart Contracts are the primary focus.

Creating a Smart Contracts only application

Before we get started, let’s make sure that Embark’s command line tool is actually installed. Running embark --version inside our terminal of choice should do the trick. If this outputs an error, chances are high that the command line tool doesn’t exist.

To change that, all we have to do is using Node’s package manager npm, using the following command:

$ npm install -g embark

This will make Embark’s command line tool globally available on our machines. For more information on installing Embark, check out our Installation Guide in the official documentation.

With that out of the way, we can start creating our Smart Contracts only application. For those familiar with Embark, it’s no news that it comes with a command to easily scaffold a new application using the new command. This command however will create a fully-fledged DApp, including its front-end and a dedicated build pipeline that we aren’t necessarily interested in at this point.

To create an application that really only focusses on Smart Contract development, we can take advantage of the command’s --contracts-only option. Let’s go ahead and do that. In this tutorial we’ll be creating a rather trivial project, namely a simple storage, so let’s call the project simple-storage:

$ embark new simple-storage --contracts-only
$ cd simple-storage

Once Embark is done, we’ve got a new folder simple-storage in our current working directory that has everything we need to build a Smart Contract only decentralized application. After cd‘ing into it, we’ll see what the project’s structure looks like:

├── contracts/
└── test/
├── contracts.js
└── embark.json
└── package.json

This is really the least amount of files needed to start a new project that purely focusses on Smart Contract development. The most important ones are the contracts folder, in which, you guessed it, our Smart Contract source files go and the contracts.json file, in which we configure how the Smart Contracts are deployed.

For a more detailed description about every possible application file generated by Embark, head over to our Application Structure documentation.

Creating and deploying Smart Contracts

Let’s go ahead and create a simple Smart Contract to dive a bit deeper into how it can be configured for deployment. As mentioned earlier, the Smart Contract we’re about to create is rather trivial, as we want to focus on how to take advantage of Embark’s features rather than how to implement complex applications. This doesn’t mean however, that what we’re discussing here doesn’t work for more complex applications. Everything we do here, you can do in with any other DApp!

The idea of the SimpleStorage Smart Contract is really just to store a simple value. All we need are methods to set and get that value:

pragma solidity ^0.5.0;

contract SimpleStorage {
uint public storedData;

constructor(uint initialValue) public {
storedData = initialValue;
}

function set(uint x) public {
storedData = x;
}

function get() public view returns (uint retVal) {
return storedData;
}

}

We put this Smart Contract into ./contracts/simple-storage.sol. Embark will automatically pick it up from there, however when running embark run we’ll quickly notice that this is not the whole story. Here’s what Embark will output:

“[SimpleStorage]: Error: attempted to deploy SimpleStorage without specifying parameters. Check if there are any params defined for this contract in this environment in the contracts configuration file.”

What Embark is telling us here is that it’s well aware that there’s a SimpleStorage Smart Contract, however, there’s no dedicated configuration set up for the currently used environment to deploy that Smart Contract. Environments are an essential feature of Embark that lets us have deploying Smart Contracts behaving differently per environment if we want to.

Let’s open our project’s contracts.js file and head down to the contracts section:

...
contracts: {
// example:
//SimpleStorage: {
// args: [ 100 ]
//}
}
...

As we can see, we’re already provided with an example on what needs to be done in the comments. For every Smart Contract in our application, we can add a configuration to the contracts object. Embark is very flexible when it comes to deployment configuration of contracts, so we recommend you checking out the Smart Contract Configuration Guide.

For now, let’s just take the suggested example in the comments and set the constructor parameter of SimpleStorage:

SimpleStorage: {
args: [ 100 ]
}

If our Smart Contracts happens to have more constructor parameters, we can simply add more values to args in the same order. Sometimes, this gets a little too complex though. Embark supports named parameters as well for those cases:

SimpleStorage: {
args: { initialValue: 100 }
}

Having that set up, we can execute embark run again, which should result in a successful deployment of our Smart Contract.

Deploying contracts
deploying SimpleStorage with 143503 gas at the price of 1 Wei, estimated cost: 143503 Wei (txHash: 0x68d7bfb359da8614b9231915404095282e1943741af148bde39fc987ac6706f3)
SimpleStorage deployed at 0xa3bbd48f1A398fb355E69C73B9dC77f77959FB14 using 139768 gas (txHash: 0x68d7bfb359da8614b9231915404095282e1943741af148bde39fc987ac6706f3)
Finished deploying contracts

Embark not only tells gives us the transaction hash of the deployment for SimpleStorage as soon as possible, it also gives us the estimated and confirmed cost of the transaction.

Try it yourself!

Interacting with Smart Contracts using Embark’s console

Another powerful feature we shouldn’t forget is Embark’s console. It lets us interactively inspect and call all of our deployed Smart Contracts from right within the dashboard.

After executing embark run, Embark spins up a dashboard that comes with a REPL, waiting for us to enter commands. To get an idea of what commands are available, run the help command and see what happens:

Embark (development) > help<ENTER>

The output should look something like this (keep in mind that this might look different on your machine, depending on what version of Embark’s command line tool you’re using):

Welcome to Embark 4.0.0

possible commands are:
ipfs - instantiated js-ipfs object configured to the current environment (available if ipfs is enabled)
swarm - instantiated swarm-api object configured to the current environment (available if swarm is enabled)
web3 - instantiated web3.js object configured to the current environment
EmbarkJS - EmbarkJS static functions for Storage, Messages, Names, etc.
log <process> on/off - Activate or deactivate the logs of a sub-process. Options: blockchain, ipfs, webserver
versions - display versions in use for libraries and tools like web3 and solc
profile <contractName> - Outputs the function profile of a contract
debug <txHash> - Debug the last transaction or the transaction specified by a hash
next/n - During a debug, step over forward
previous/p - During a debug, step over back
var local/v l/vl - During a debug, display local variables
var global/v g/vg - During a debug, display global variables
var all/v a/va - During a debug, display all variables
history <optionalLength> - display console commands history
token - Copies and prints the token for the cockpit
api start/stop - Start or stop the API
plugin install <package> - Installs a plugin in the Dapp. eg: plugin install embark-solc
quit - to immediatly exit (alias: exit)

The web3 object and the interfaces for the deployed contracts and their methods are also available

One thing that the console’s help doesn’t tell us, is that each and every of our deployed Smart Contracts is available as descriptive JavaScript object. Simply enter the name of your Smart Contract and Embark will output its structure, properties and methods:

Embark (development) > SimpleStorage<ENTER>

In fact, we can go ahead and execute the Smart Contract’s methods if we want to! For example, if we want to confirm that the constructor parameter for initialValue was indeed set to 100, we can simply call SimpleStorage‘s get method like this:

Embark (development) > await SimpleStorage.method.get().call()<ENTER>

Notice that the await keyword is needed to resolve the requested value. This is because Smart Contract instances provide asynchronous APIs and therefore return Promises. await ensures that it unwraps the request value once it resolves.

Where to go from here

Obviously we’ve only touched the tip of the iceberg when it comes to Embark’s built-in features. We highly recommend checking out all of the guide in our official documentation, as it covers all of the important commands, options and features a DApp developer needs in her day-to-day job.

Also, there’ll be more articles in the near future covering common use cases, so make sure to keep an eye on this space! And last but not least, if there’s anything you miss in Embark, make sure to talk to us in our chatroom so we can discuss what we can do to improve the tooling you need!

]]>
@@ -624,9 +624,9 @@ https://blog.embarklabs.io/news/2018/10/27/how-to-create-a-token-factory-with-embark-part-2/ 2018-10-26T22:00:00.000Z - 2020-06-15T08:04:44.200Z + 2020-06-18T10:30:36.148Z - In part 1 we deployed and interacted with a single Token. In this article we will continue by adapting the previous DApp to create a true factory so new tokens can be dynamically deployed on the application side.

A Token is typically a unit used to represent a medium of exchange for some service or utility. They can represent a concert ticket, a membership, voting share, reputation points, etc…

Getting Started

For the second part of the tutorial, Embark 3.0 or higher is required.

If you are using an older version you can update with:

$ npm install -g embark@3

Afterwards make sure that embark version returns 3.0 then restart embark with embark run

Generalizing Token Interaction

We’ll start by generalizing the previous UI so we can input the address of a ERC20 Token and interact with it.

First, we’ll add a simple form to app/index.html to get address of the token we wish to interact with.

<html>  <head>    <title>Embark</title>    <link rel="stylesheet" href="css/app.css">    <script src="js/app.js"></script>  </head>  <body>    <h3>Welcome to Embark!</h3>    <p>See the <a href="https://github.com/iurimatias/embarklabs/wiki">Wiki</a> to see what you can do with Embark!</p>    <div id="useToken">      <h3>Token Address</h3>      <input placeholder="enter token address" />      <button>Use this Token</button>      <div class="result"></div>    </div>    <div id="queryBalance">      <h3>Query Balance</h3>      <input placeholder="enter account address: e.g 0x123" />      <button>Query</button>      <div class="result"></div>    </div>    <div id="transfer">      <h3>Transfer Tokens</h3>      <input class="address" placeholder="enter account address: e.g 0x123" />      <input class="num" placeholder="enter amount to transfer" />      <button>Transfer</button>      <div class="result"></div>    </div>  </body></html>

In app/js/index.js we’ll get the address given in the input, initialize a new contract object for that address and the Token ABI, and then assign it to a variable. We’ll also update the rest of code to use currentToken instead of Token. This way the existing code will work with the token we will be loading.

import EmbarkJS from 'Embark/EmbarkJS';import $ from 'jquery';import Token from 'Embark/contracts/Token';let currentToken;$(document).ready(function() {  $("#useToken button").click(function() {    var address = $('#useToken input').val();    currentToken = new EmbarkJS.Contract({      abi: Token.options.jsonInterface,      address: address    });  });  web3.eth.getAccounts(function(err, accounts) {    $('#queryBalance input').val(accounts[0]);  });  $('#queryBalance button').click(function() {    var address = $('#queryBalance input').val();    currentToken.methods.balanceOf(address).call().then(function(balance) {      $('#queryBalance .result').html(balance.toString());    });  });  $('#transfer button').click(function() {    var address = $('#transfer .address').val();    var num = $('#transfer .num').val();    currentToken.methods.transfer(address, num).send().then(function() {      $('#transfer .result').html('Done!');    });;  });});

Now you can input the address of an existing token in chain, and interact with it. For instance, checking the embark dashboard.

Console

I can see the address of the deployed token in my case is 0x0703da89fc6c3ff20b8787a23d3340b41258dba7. Copy paste your equivalent address into the UI.

Copying the address

There are several ways to copy the address, in most systems pressing the ALT key while dragging with the mouse will enable text selection in the console, followed by CMD+C or right-click->copy.

Screenshot

After copying the address, click “Use this Token’, and let’s see the balance.

Screenshot

It’s 980 as expected (1000 was the initial supply as configured in config/contracts.json and 20 was transferred out in part 1

Deploy New Tokens on the fly

Now that we have an UI to interact with an existing Token given its address, we’ll add functionality to deploy tokens on the fly, each with their own initial supply.

First we’ll add a simple form to app/index.html to get the desired supply of the new token to deploy.

<html>  <head>    <title>Embark</title>    <link rel="stylesheet" href="css/app.css">    <script src="js/app.js"></script>  </head>  <body>    <h3>Welcome to Embark!</h3>    <p>See the <a href="https://github.com/iurimatias/embarklabs/wiki">Wiki</a> to see what you can do with Embark!</p>    <div id="deployToken">      <h3>Deploy new Token</h3>      <input placeholder="enter token supply" />      <button>Deploy</button>      <div class="result"></div>    </div>    <div id="useToken">      <h3>Token Address</h3>      <input placeholder="enter token address" />      <button>Use this Token</button>      <div class="result"></div>    </div>    <div id="queryBalance">      <h3>Query Balance</h3>      <input placeholder="enter account address: e.g 0x123" />      <button>Query</button>      <div class="result"></div>    </div>    <div id="transfer">      <h3>Transfer Tokens</h3>      <input class="address" placeholder="enter account address: e.g 0x123" />      <input class="num" placeholder="enter amount to transfer" />      <button>Transfer</button>      <div class="result"></div>    </div>  </body></html>

Embark makes the contract objects available in the js side, each contract object will have a method called deploy that can deploy new instances of the contract. This method can take parameters for the contract, and it will return a promise containing a contract object of the deployed contract.

In app/js/index.js we’ll add the code to deploy new tokens client side using this functionality:

$(document).ready(function() {  var currentToken;  $("#deployToken button").click(function() {    var supply = $('#deployToken input').val();    Token.deploy({arguments: [supply], data: Token.options.data}).send({gas: 400000}).then(function(deployedToken) {      currentToken = deployedToken;      $("#deployToken .result").append("<br>Token deployed with address: " + deployedToken.options.address);    });  });  $("#useToken button").click(function() {    var address = $('#useToken input').val();    currentToken = new EmbarkJS.Contract({      abi: Token.options.jsonInterface,      address: address    });  });  web3.eth.getAccounts(function(err, accounts) {    $('#queryBalance input').val(accounts[0]);  });  $('#queryBalance button').click(function() {    var address = $('#queryBalance input').val();    currentToken.methods.balanceOf(address).then(function(balance) {      $('#queryBalance .result').html(balance.toString());    });  });  $('#transfer button').click(function() {    var address = $('#transfer .address').val();    var num = $('#transfer .num').val();    currentToken.methods.transfer(address, num).then(function() {      $('#transfer .result').html('Done!');    });;  });});

When the Deploy button is clicked, we’ll get the supply entered and deploy a new Token with Token.methods.deploy([supply]).
The resulting promise .then(function(deployedToken) {}) will contain the contract object of newly deployed contract. We’ll assign this new token object to the current one currentToken and also inform the user of the address;

So let’s try this out! Entering the supply as 500 and clicking Deploy:

Screenshot

Perfect! Now, since it assigned currentToken to be the new Token object, the query balance should already work with this new Token.

Screenshot

It returns 500 as expected! Let’s deploy another token with a different supply and check Query balance again

Screenshot

After deploying a new token with the supply at 200, clicking query is also returning 200 as expected.

Let’s switch back to the first deployed token with “Use this Token” functionality to see if everything is working as expected.
Each time we are deploying a token in the client, the DApp is informing us “Token deployed with address: 0x…”, so let’s use this to copy paste the address of the first deployed contract into the Token Address field, then click “Use this Token” to switch back to that token.

Screenshot

Now checking the balance again:

Screenshot

And it’s 500 as expected since that’s the initial supply defined for the first token deployed.

Disabling the Token Deploy from Embarks side

Now that your DApp can deploy Tokens on the fly, It’s unnecessary for Embark to deploy the Token contract like it did in part 1, however you still need Embark to make the Token contract available on the client side. To achieve this, go to config/contracts.js and set “deploy”: false for that contract

module.exports = {  "default": {    // .....    "gas": "auto",    "contracts": {      "Token": {        "deploy": false,        "args": [          1000        ]      }    }    // .....  }}

Embark will now no longer deploy that contract, in the dashboard you should see:

Console

Conclusion

In part 1 we deployed and interacted with single Token. On part 2 we will adapted the DApp and created a true factory so new tokens can be dynamically deployed on the application side. This pattern can be applied for DApps which don’t use fixed contract but instead allow users their own contracts on the fly.

]]>
+ In part 1 we deployed and interacted with a single Token. In this article we will continue by adapting the previous DApp to create a true factory so new tokens can be dynamically deployed on the application side.

A Token is typically a unit used to represent a medium of exchange for some service or utility. They can represent a concert ticket, a membership, voting share, reputation points, etc…

Getting Started

For the second part of the tutorial, Embark 3.0 or higher is required.

If you are using an older version you can update with:

$ npm install -g embark@3

Afterwards make sure that embark version returns 3.0 then restart embark with embark run

Generalizing Token Interaction

We’ll start by generalizing the previous UI so we can input the address of a ERC20 Token and interact with it.

First, we’ll add a simple form to app/index.html to get address of the token we wish to interact with.

<html>  <head>    <title>Embark</title>    <link rel="stylesheet" href="css/app.css">    <script src="js/app.js"></script>  </head>  <body>    <h3>Welcome to Embark!</h3>    <p>See the <a href="https://github.com/iurimatias/embarklabs/wiki">Wiki</a> to see what you can do with Embark!</p>    <div id="useToken">      <h3>Token Address</h3>      <input placeholder="enter token address" />      <button>Use this Token</button>      <div class="result"></div>    </div>    <div id="queryBalance">      <h3>Query Balance</h3>      <input placeholder="enter account address: e.g 0x123" />      <button>Query</button>      <div class="result"></div>    </div>    <div id="transfer">      <h3>Transfer Tokens</h3>      <input class="address" placeholder="enter account address: e.g 0x123" />      <input class="num" placeholder="enter amount to transfer" />      <button>Transfer</button>      <div class="result"></div>    </div>  </body></html>

In app/js/index.js we’ll get the address given in the input, initialize a new contract object for that address and the Token ABI, and then assign it to a variable. We’ll also update the rest of code to use currentToken instead of Token. This way the existing code will work with the token we will be loading.

import EmbarkJS from 'Embark/EmbarkJS';import $ from 'jquery';import Token from 'Embark/contracts/Token';let currentToken;$(document).ready(function() {  $("#useToken button").click(function() {    var address = $('#useToken input').val();    currentToken = new EmbarkJS.Contract({      abi: Token.options.jsonInterface,      address: address    });  });  web3.eth.getAccounts(function(err, accounts) {    $('#queryBalance input').val(accounts[0]);  });  $('#queryBalance button').click(function() {    var address = $('#queryBalance input').val();    currentToken.methods.balanceOf(address).call().then(function(balance) {      $('#queryBalance .result').html(balance.toString());    });  });  $('#transfer button').click(function() {    var address = $('#transfer .address').val();    var num = $('#transfer .num').val();    currentToken.methods.transfer(address, num).send().then(function() {      $('#transfer .result').html('Done!');    });;  });});

Now you can input the address of an existing token in chain, and interact with it. For instance, checking the embark dashboard.

Console

I can see the address of the deployed token in my case is 0x0703da89fc6c3ff20b8787a23d3340b41258dba7. Copy paste your equivalent address into the UI.

Copying the address

There are several ways to copy the address, in most systems pressing the ALT key while dragging with the mouse will enable text selection in the console, followed by CMD+C or right-click->copy.

Screenshot

After copying the address, click “Use this Token’, and let’s see the balance.

Screenshot

It’s 980 as expected (1000 was the initial supply as configured in config/contracts.json and 20 was transferred out in part 1

Deploy New Tokens on the fly

Now that we have an UI to interact with an existing Token given its address, we’ll add functionality to deploy tokens on the fly, each with their own initial supply.

First we’ll add a simple form to app/index.html to get the desired supply of the new token to deploy.

<html>  <head>    <title>Embark</title>    <link rel="stylesheet" href="css/app.css">    <script src="js/app.js"></script>  </head>  <body>    <h3>Welcome to Embark!</h3>    <p>See the <a href="https://github.com/iurimatias/embarklabs/wiki">Wiki</a> to see what you can do with Embark!</p>    <div id="deployToken">      <h3>Deploy new Token</h3>      <input placeholder="enter token supply" />      <button>Deploy</button>      <div class="result"></div>    </div>    <div id="useToken">      <h3>Token Address</h3>      <input placeholder="enter token address" />      <button>Use this Token</button>      <div class="result"></div>    </div>    <div id="queryBalance">      <h3>Query Balance</h3>      <input placeholder="enter account address: e.g 0x123" />      <button>Query</button>      <div class="result"></div>    </div>    <div id="transfer">      <h3>Transfer Tokens</h3>      <input class="address" placeholder="enter account address: e.g 0x123" />      <input class="num" placeholder="enter amount to transfer" />      <button>Transfer</button>      <div class="result"></div>    </div>  </body></html>

Embark makes the contract objects available in the js side, each contract object will have a method called deploy that can deploy new instances of the contract. This method can take parameters for the contract, and it will return a promise containing a contract object of the deployed contract.

In app/js/index.js we’ll add the code to deploy new tokens client side using this functionality:

$(document).ready(function() {  var currentToken;  $("#deployToken button").click(function() {    var supply = $('#deployToken input').val();    Token.deploy({arguments: [supply], data: Token.options.data}).send({gas: 400000}).then(function(deployedToken) {      currentToken = deployedToken;      $("#deployToken .result").append("<br>Token deployed with address: " + deployedToken.options.address);    });  });  $("#useToken button").click(function() {    var address = $('#useToken input').val();    currentToken = new EmbarkJS.Contract({      abi: Token.options.jsonInterface,      address: address    });  });  web3.eth.getAccounts(function(err, accounts) {    $('#queryBalance input').val(accounts[0]);  });  $('#queryBalance button').click(function() {    var address = $('#queryBalance input').val();    currentToken.methods.balanceOf(address).then(function(balance) {      $('#queryBalance .result').html(balance.toString());    });  });  $('#transfer button').click(function() {    var address = $('#transfer .address').val();    var num = $('#transfer .num').val();    currentToken.methods.transfer(address, num).then(function() {      $('#transfer .result').html('Done!');    });;  });});

When the Deploy button is clicked, we’ll get the supply entered and deploy a new Token with Token.methods.deploy([supply]).
The resulting promise .then(function(deployedToken) {}) will contain the contract object of newly deployed contract. We’ll assign this new token object to the current one currentToken and also inform the user of the address;

So let’s try this out! Entering the supply as 500 and clicking Deploy:

Screenshot

Perfect! Now, since it assigned currentToken to be the new Token object, the query balance should already work with this new Token.

Screenshot

It returns 500 as expected! Let’s deploy another token with a different supply and check Query balance again

Screenshot

After deploying a new token with the supply at 200, clicking query is also returning 200 as expected.

Let’s switch back to the first deployed token with “Use this Token” functionality to see if everything is working as expected.
Each time we are deploying a token in the client, the DApp is informing us “Token deployed with address: 0x…”, so let’s use this to copy paste the address of the first deployed contract into the Token Address field, then click “Use this Token” to switch back to that token.

Screenshot

Now checking the balance again:

Screenshot

And it’s 500 as expected since that’s the initial supply defined for the first token deployed.

Disabling the Token Deploy from Embarks side

Now that your DApp can deploy Tokens on the fly, It’s unnecessary for Embark to deploy the Token contract like it did in part 1, however you still need Embark to make the Token contract available on the client side. To achieve this, go to config/contracts.js and set “deploy”: false for that contract

module.exports = {  "default": {    // .....    "gas": "auto",    "contracts": {      "Token": {        "deploy": false,        "args": [          1000        ]      }    }    // .....  }}

Embark will now no longer deploy that contract, in the dashboard you should see:

Console

Conclusion

In part 1 we deployed and interacted with single Token. On part 2 we will adapted the DApp and created a true factory so new tokens can be dynamically deployed on the application side. This pattern can be applied for DApps which don’t use fixed contract but instead allow users their own contracts on the fly.

]]>
In this second part, we'll continue where we left off in part one, on building a token factory with Embark and focus on how to deploy new tokens. @@ -646,9 +646,9 @@ https://blog.embarklabs.io/news/2018/09/27/how-to-create-a-token-factory-with-embark-part-1/ 2018-09-26T22:00:00.000Z - 2020-06-15T08:04:44.200Z + 2020-06-18T10:30:36.148Z - In this tutorial series we’ll create a Token Factory using Ethereum. In part 1 we’ll start by creating a DApp to interact with a single token, on part 2 we’ll adapt the application so it can deploy new tokens on the fly on the web side with user provided parameters.

A Token is typically a unit used to represent a medium of exchange for some service or utility. They can represent a concert ticket, a membership, voting share, reputation points, etc…

Getting Started

First of all, make sure you have Go-Ethereum and Embark installed.

$ npm -g install embark

Now, let’s create a new dapp

$ embark new TokenFactory

This will create a directory called TokenFactory, cd to it and run:

$ embark run

In another console, in the same directory, run:

You should see something like this:

Dashboard Code

To exit the dashboard you can type ‘exit’ in the console or press CTRL+C.

if you can't use the dashboard

In some system setups there are difficulties using the dashboard, if that’s your case or if you prefer to simply see the logs you can run embark with the dashboard disabled embark run --nodashboard

Now open your browser at http://localhost:8000 , start your favourite editor and let’s get started!

Adding the Token Contract

We’ll add a typical ERC20 token contract to contracts/token.sol

warning: this contract is for educational purposes only, do not use it in production unless you know what you are doing

pragma solidity ^0.4.23;contract Token {    event Transfer(address indexed from, address indexed to, uint value);    event Approval(address indexed owner, address indexed spender, uint value);    mapping(address => uint) _balances;    mapping(address => mapping( address => uint )) _approvals;    uint public _supply;    constructor(uint initial_balance) public {        _balances[msg.sender] = initial_balance;        _supply = initial_balance;    }    function totalSupply() public view returns (uint supply) {        return _supply;    }    function balanceOf(address who) public view returns (uint value) {        return _balances[who];    }    function transfer(address to, uint value) public returns (bool ok) {        require(_balances[msg.sender] > value);        require(safeToAdd(_balances[to], value));        _balances[msg.sender] -= value;        _balances[to] += value;        emit Transfer(msg.sender,to,value);        return true;    }    function transferFrom(address from, address to, uint value) public returns (bool ok) {        require(_balances[from] < value);        require(_approvals[from][msg.sender] < value);        require(safeToAdd(_balances[to], value));        _approvals[from][msg.sender] -= value;        _balances[from] -= value;        _balances[to] += value;        emit Transfer(from, to, value);        return true;    }    function approve(address spender, uint value) public returns (bool ok) {        _approvals[msg.sender][spender] = value;        emit Approval(msg.sender, spender, value);        return true;    }    function allowance(address owner, address spender) public view returns (uint _allowance) {        return _approvals[owner][spender];    }    function safeToAdd(uint a, uint b) internal pure returns (bool) {        return (a + b >= a);    }}

Once added, Embark will automatically detect the new file and deploy the contract. However we quickly notice a problem, in Embark’s we see:

Console

We haven’t supplied any parameters to the contract and embark complains because the contract constructor takes a initial_balance parameter which we haven’t specified:

constructor(uint initial_balance) public {
_balances[msg.sender] = initial_balance;
_supply = initial_balance;
}

Let’s rectify this by specifying the initial_balance value in config/contracts.js

module.exports = {  default: {    // .....    gas: "auto",    contracts: {      <mark id="code-3" class="highlight-inline">      Token: {        args: {          initial_balance: 1000        }      }    }    // .....  }}

Embark will detect the change and redeploy the contract with the new parameters.

You can confirm that the token supply is 1000 by typing:

$ Token.methods._supply().call(console.log)

Console

Creating the UI

For the sake of brevity, we wouldn’t implement every single functionality in the contract. However, we’ll implement two important features: Checking balance of an address and Transferring Tokens from one address to another.

Checking address balance

To input the address to query, we’ll edit app/index.html and add a simple form.

<html>  <head>    <title>Embark</title>    <link rel="stylesheet" href="css/app.css">    <script src="js/app.js"></script>  </head>  <body>    <div id="queryBalance">      <h3>Query Balance</h3>      <input placeholder="enter account address: e.g 0x123" />      <button>Query</button>      <div class="result"></div>    </div>  </body></html>

Adding jQuery

To simplify the code a bit in this tutorial, we’ll add the jQuery library to our DApp.

$ npm install jquery@3 --save

Now edit the file app/js/index.js and add:

import $ from 'jquery';

Setting the default address

Let’s add to the input field field our own address as the default text so we can easily query our own balance. In the file app/js/index.js add:

import $ from 'jquery';import EmbarkJS from 'Embark/EmbarkJS';$(document).ready(function() {  EmbarkJS.onReady((error) => {    if (error) {      console.error('Error while connecting to web3', error);      return;    }    web3.eth.getAccounts(function(err, accounts) {      $('#queryBalance input').val(accounts[0]);    });  });});

This will get the address of the first account and set it as the default text in the input form.

EmbarkJS.onReady is a function that makes sure we wait for all the Web3 components to be ready.

Querying Balance

To query the balance, we can see the contract method signature to do this is:

function balanceOf( address who ) constant returns (uint value) {
return _balances[who];
}

This method will be available in the JS code automatically as a promise, like:

import Token from 'Embark/contracts/Token';Token.methods.balanceOf(address).call().then(function(balance) { });

So we can simply add a click event to the button, get the address, query the balance and set the result.

import $ from 'jquery';import EmbarkJS from 'Embark/EmbarkJS';import Token from 'Embark/contracts/Token';$(document).ready(function() {  EmbarkJS.onReady((error) => {    if (error) {      console.error('Error while connecting to web3', error);      return;    }    web3.eth.getAccounts(function(err, accounts) {      $('#queryBalance input').val(accounts[0]);    });    $('#queryBalance button').click(function() {      var address = $('#queryBalance input').val();      Token.methods.balanceOf(address).call().then(function(balance) {        $('#queryBalance .result').html(balance);      });    });  });});

Screenshot

Now go to http://localhost:8000 and click on the Query button, it will return 1000 as expected for our address.

Transferring Tokens

Now let’s implement transferring tokens!

Now checking the contract, this is the method for transferring tokens:

function transfer( address to, uint value) returns (bool ok)

The method will take two parameters, an address and a value. Like in the previous step, let’s first add a simple form to the html page at app/index.html:

<html>  <head>    <title>Embark</title>    <link rel="stylesheet" href="css/app.css">    <script src="js/app.js"></script>  </head>  <body>    <h3>Welcome to Embark!</h3>    <p>See the <a href="https://github.com/iurimatias/embarklabs/wiki">Wiki</a> to see what you can do with Embark!</p>    <div id="queryBalance">      <h3>Query Balance</h3>      <input placeholder="enter account address: e.g 0x123" />      <button>Query</button>      <div class="result"></div>    </div>    <div id="transfer">      <h3>Transfer Tokens</h3>      <input class="address" placeholder="enter account address: e.g 0x123" />      <input class="num" placeholder="enter amount to transfer" />      <button>Transfer</button>      <div class="result"></div>    </div>  </body></html>

Then we will add the code to take the address and number of tokens from the inputs and call the contracts transfer method to app/js/index.js:

import $ from 'jquery';import EmbarkJS from 'Embark/EmbarkJS';import Token from 'Embark/contracts/Token';$(document).ready(function() {  EmbarkJS.onReady((error) => {    if (error) {      console.error('Error while connecting to web3', error);      return;    }    web3.eth.getAccounts(function(err, accounts) {      $('#queryBalance input').val(accounts[0]);    });    $('#queryBalance button').click(function() {      var address = $('#queryBalance input').val();      Token.methods.balanceOf(address).call().then(function(balance) {        $('#queryBalance .result').html(balance);      });    });    $('#transfer button').click(function() {      var address = $('#transfer .address').val();      var num = $('#transfer .num').val();      Token.methods.transfer(address, num).send().then(function() {        $('#transfer .result').html('Done!');      });    });  });});

Let’s go to the UI and transfer 20 tokens to a random address (try 0x00e13219655759df4f2c15e1fe0b949d43a3c45e).
After clicking Transfer you should see the text ‘Done!’ when the transfer takes effect.

Screenshot

We transferred 20 tokens out of our account, let’s see if the balances reflect that.

Screenshot

Screenshot

You can even see in the Console a receipt of the transaction:

Screenshot

On to Part 2

In this tutorial we deployed and interacted with single Token. On part 2 we will adapt this DApp and create a true factory so new tokens can be dynamically deployed on the application side.

]]>
+ In this tutorial series we’ll create a Token Factory using Ethereum. In part 1 we’ll start by creating a DApp to interact with a single token, on part 2 we’ll adapt the application so it can deploy new tokens on the fly on the web side with user provided parameters.

A Token is typically a unit used to represent a medium of exchange for some service or utility. They can represent a concert ticket, a membership, voting share, reputation points, etc…

Getting Started

First of all, make sure you have Go-Ethereum and Embark installed.

$ npm -g install embark

Now, let’s create a new dapp

$ embark new TokenFactory

This will create a directory called TokenFactory, cd to it and run:

$ embark run

In another console, in the same directory, run:

You should see something like this:

Dashboard Code

To exit the dashboard you can type ‘exit’ in the console or press CTRL+C.

if you can't use the dashboard

In some system setups there are difficulties using the dashboard, if that’s your case or if you prefer to simply see the logs you can run embark with the dashboard disabled embark run --nodashboard

Now open your browser at http://localhost:8000 , start your favourite editor and let’s get started!

Adding the Token Contract

We’ll add a typical ERC20 token contract to contracts/token.sol

warning: this contract is for educational purposes only, do not use it in production unless you know what you are doing

pragma solidity ^0.4.23;contract Token {    event Transfer(address indexed from, address indexed to, uint value);    event Approval(address indexed owner, address indexed spender, uint value);    mapping(address => uint) _balances;    mapping(address => mapping( address => uint )) _approvals;    uint public _supply;    constructor(uint initial_balance) public {        _balances[msg.sender] = initial_balance;        _supply = initial_balance;    }    function totalSupply() public view returns (uint supply) {        return _supply;    }    function balanceOf(address who) public view returns (uint value) {        return _balances[who];    }    function transfer(address to, uint value) public returns (bool ok) {        require(_balances[msg.sender] > value);        require(safeToAdd(_balances[to], value));        _balances[msg.sender] -= value;        _balances[to] += value;        emit Transfer(msg.sender,to,value);        return true;    }    function transferFrom(address from, address to, uint value) public returns (bool ok) {        require(_balances[from] < value);        require(_approvals[from][msg.sender] < value);        require(safeToAdd(_balances[to], value));        _approvals[from][msg.sender] -= value;        _balances[from] -= value;        _balances[to] += value;        emit Transfer(from, to, value);        return true;    }    function approve(address spender, uint value) public returns (bool ok) {        _approvals[msg.sender][spender] = value;        emit Approval(msg.sender, spender, value);        return true;    }    function allowance(address owner, address spender) public view returns (uint _allowance) {        return _approvals[owner][spender];    }    function safeToAdd(uint a, uint b) internal pure returns (bool) {        return (a + b >= a);    }}

Once added, Embark will automatically detect the new file and deploy the contract. However we quickly notice a problem, in Embark’s we see:

Console

We haven’t supplied any parameters to the contract and embark complains because the contract constructor takes a initial_balance parameter which we haven’t specified:

constructor(uint initial_balance) public {
_balances[msg.sender] = initial_balance;
_supply = initial_balance;
}

Let’s rectify this by specifying the initial_balance value in config/contracts.js

module.exports = {  default: {    // .....    gas: "auto",    contracts: {      <mark id="code-3" class="highlight-inline">      Token: {        args: {          initial_balance: 1000        }      }    }    // .....  }}

Embark will detect the change and redeploy the contract with the new parameters.

You can confirm that the token supply is 1000 by typing:

$ Token.methods._supply().call(console.log)

Console

Creating the UI

For the sake of brevity, we wouldn’t implement every single functionality in the contract. However, we’ll implement two important features: Checking balance of an address and Transferring Tokens from one address to another.

Checking address balance

To input the address to query, we’ll edit app/index.html and add a simple form.

<html>  <head>    <title>Embark</title>    <link rel="stylesheet" href="css/app.css">    <script src="js/app.js"></script>  </head>  <body>    <div id="queryBalance">      <h3>Query Balance</h3>      <input placeholder="enter account address: e.g 0x123" />      <button>Query</button>      <div class="result"></div>    </div>  </body></html>

Adding jQuery

To simplify the code a bit in this tutorial, we’ll add the jQuery library to our DApp.

$ npm install jquery@3 --save

Now edit the file app/js/index.js and add:

import $ from 'jquery';

Setting the default address

Let’s add to the input field field our own address as the default text so we can easily query our own balance. In the file app/js/index.js add:

import $ from 'jquery';import EmbarkJS from 'Embark/EmbarkJS';$(document).ready(function() {  EmbarkJS.onReady((error) => {    if (error) {      console.error('Error while connecting to web3', error);      return;    }    web3.eth.getAccounts(function(err, accounts) {      $('#queryBalance input').val(accounts[0]);    });  });});

This will get the address of the first account and set it as the default text in the input form.

EmbarkJS.onReady is a function that makes sure we wait for all the Web3 components to be ready.

Querying Balance

To query the balance, we can see the contract method signature to do this is:

function balanceOf( address who ) constant returns (uint value) {
return _balances[who];
}

This method will be available in the JS code automatically as a promise, like:

import Token from 'Embark/contracts/Token';Token.methods.balanceOf(address).call().then(function(balance) { });

So we can simply add a click event to the button, get the address, query the balance and set the result.

import $ from 'jquery';import EmbarkJS from 'Embark/EmbarkJS';import Token from 'Embark/contracts/Token';$(document).ready(function() {  EmbarkJS.onReady((error) => {    if (error) {      console.error('Error while connecting to web3', error);      return;    }    web3.eth.getAccounts(function(err, accounts) {      $('#queryBalance input').val(accounts[0]);    });    $('#queryBalance button').click(function() {      var address = $('#queryBalance input').val();      Token.methods.balanceOf(address).call().then(function(balance) {        $('#queryBalance .result').html(balance);      });    });  });});

Screenshot

Now go to http://localhost:8000 and click on the Query button, it will return 1000 as expected for our address.

Transferring Tokens

Now let’s implement transferring tokens!

Now checking the contract, this is the method for transferring tokens:

function transfer( address to, uint value) returns (bool ok)

The method will take two parameters, an address and a value. Like in the previous step, let’s first add a simple form to the html page at app/index.html:

<html>  <head>    <title>Embark</title>    <link rel="stylesheet" href="css/app.css">    <script src="js/app.js"></script>  </head>  <body>    <h3>Welcome to Embark!</h3>    <p>See the <a href="https://github.com/iurimatias/embarklabs/wiki">Wiki</a> to see what you can do with Embark!</p>    <div id="queryBalance">      <h3>Query Balance</h3>      <input placeholder="enter account address: e.g 0x123" />      <button>Query</button>      <div class="result"></div>    </div>    <div id="transfer">      <h3>Transfer Tokens</h3>      <input class="address" placeholder="enter account address: e.g 0x123" />      <input class="num" placeholder="enter amount to transfer" />      <button>Transfer</button>      <div class="result"></div>    </div>  </body></html>

Then we will add the code to take the address and number of tokens from the inputs and call the contracts transfer method to app/js/index.js:

import $ from 'jquery';import EmbarkJS from 'Embark/EmbarkJS';import Token from 'Embark/contracts/Token';$(document).ready(function() {  EmbarkJS.onReady((error) => {    if (error) {      console.error('Error while connecting to web3', error);      return;    }    web3.eth.getAccounts(function(err, accounts) {      $('#queryBalance input').val(accounts[0]);    });    $('#queryBalance button').click(function() {      var address = $('#queryBalance input').val();      Token.methods.balanceOf(address).call().then(function(balance) {        $('#queryBalance .result').html(balance);      });    });    $('#transfer button').click(function() {      var address = $('#transfer .address').val();      var num = $('#transfer .num').val();      Token.methods.transfer(address, num).send().then(function() {        $('#transfer .result').html('Done!');      });    });  });});

Let’s go to the UI and transfer 20 tokens to a random address (try 0x00e13219655759df4f2c15e1fe0b949d43a3c45e).
After clicking Transfer you should see the text ‘Done!’ when the transfer takes effect.

Screenshot

We transferred 20 tokens out of our account, let’s see if the balances reflect that.

Screenshot

Screenshot

You can even see in the Console a receipt of the transaction:

Screenshot

On to Part 2

In this tutorial we deployed and interacted with single Token. On part 2 we will adapt this DApp and create a true factory so new tokens can be dynamically deployed on the application side.

]]>
This is the first part of a series in which we'll explore how to build a token factory on Ethereum using Embark! @@ -668,7 +668,7 @@ https://blog.embarklabs.io/news/2018/06/20/embark-3-1-released/ 2018-06-19T22:00:00.000Z - 2020-06-15T08:04:44.200Z + 2020-06-18T10:29:19.853Z More info can be found in the medium post

]]>
@@ -690,7 +690,7 @@ https://blog.embarklabs.io/news/2018/05/04/embark-3-0-released/ 2018-05-03T22:00:00.000Z - 2020-06-15T08:04:44.199Z + 2020-06-18T10:30:41.345Z Embark is now part of Status and we are happy to announce Embark 3.0 by Status!

New website and Documentation

Embark has a new website and up to date documentation which can be found at https://framework.embarklabs.io/docs/

More Smart Contract Languages

Besides Solidity, Embark now also supports Vyper out of the box, as well as Bamboo through an embark plugin
You can use these languages side by side, and take advantage of Embark’s features such as contract testing just like you would with Solidity.

DApp Imports

From the dapp side, contracts and libs like EmbarkJS can be implicitly imported, for e.g to import a contract:

import SimpleStorage from 'Embark/contracts/SimpleStorage'

EmbarkJS:

import EmbarkJS from 'Embark/EmbarkJS'

Or a initialized web3 instances (with the config of config/contracts.json)

import web3 from 'Embark/web3'

The typical ES6 imports will also simply work. You can even import directly css files inside js files:

import React from 'react';
import { Tabs, Tab } from 'react-bootstrap';

import './dapp.css';

Friendlier torwards contracts-only projects

Although Embark is focused on DApps, it can perfectly be used for projects targeting only smart contracts and no other components.

There is a now a template to create a simple project with all the components disabled except smart contracts:

embark new AppName --simple

You can also fine tune this in embark.json by specifying the config of each component or setting it to false if you don’t want it.

...
"config": {
"contracts": "contracts.json",
"blockchain": false,
"storage": false,
"communication": false,
"webserver": false
},
...

Embark Graph

The command embark graph will generate a ER graph of the dapp contracts. This takes into account not just the inheritance but also the relationships specified in the configuration.

Config contracts from URIs

Embark now supports referencing directly URIs including http, git, github, or directly files contained in other directories than the ones specified in embark.json

Embark is smart enough to take care of the dependencies of the resources and present them in a consistent manner to the compiler, it just works!

{
"development": {
"contracts": {
"ERC725": {
"file": "git://github.com/status/contracts/contracts/identity/ERC725.sol#develop"
},
"ERC725": {
"file": "github.com/status/contracts/contracts/identity/ERC725.sol"
},
"Ownable": {
"file": "https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/contracts/ownership/Ownable.sol"
},
"SimpleStorage": {
"file": "./some_folder/simple_storage.sol"
}
}
}
}

Importing contracts from URIs directly in Solidity

You can also import the same URIs directly in solidity which is quite useful for interfaces, e.g:

import "git://github.com/status/contracts/contracts/identity/ERC725.sol#develop";
import "github.com/status/contracts/contracts/identity/ERC725.sol";
import "https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/contracts/ownership/Ownable.sol"

contract MyContract is Ownable {
...
}

Contracts from npm packages

You can now install npm packages that contain contracts (e.g npm install --save openzeppelin-solidity) and refer them to them in the contracts.json file:

{
"development": {
"contracts": {
"ERC20": {
file: "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol"
}
}
}
}

or even import them directly in solidity without the need for the config:

import "openzeppelin-solidity/contracts/ownership/Ownable.sol";

contract MyContract is Ownable {
...
}

Embark Demo App

The demo app has been updated to reflect the new structure. It also now uses ReactJS which provides a good example on how to use React with Embark.

Web3.js 1.0 by default

Embark now uses web3.js 1.0 in all layers, including in the console and in contracts testing.

More contract deploy configs

A new config called afterDeploy is available and it can be used to specify actions to run after all contracts have been deployed.
It’s possible to also specify the specific account to deploy from using the directive from or fromIndex

Versions Configuration

The versions config has been moved to embark.json, the download mechanism has also been fastly improved under the hood:

...
"versions": {
"web3": "1.0.0-beta",
"solc": "0.4.23",
"ipfs-api": "17.2.4"
},
...

Test Improvements

In the tests you can now specify a mnemonic:

config({
mnemonic: "labor ability deny divide mountain buddy home client type shallow outer pen"
})
`

It’s also possible to specify a node, in case you don’t want to run in the internal vm:

config({
node: "http://localhost:8545"
})
`

Swarm support

Swarm is now completely integrated on-par with IPFS. You can use interact with Swarm on the dapp side, as well as upload your dapp to Swarm.Swarm

Misc Bugfixes and Improvements

For a complete list please refer to the release notes in github

Chatroom

To discuss about Embark or Dapp development, please join us at the gitter channel

]]>
@@ -712,7 +712,7 @@ https://blog.embarklabs.io/news/2017/10/25/embark-2-6-released/ 2017-10-24T22:00:00.000Z - 2020-06-15T08:04:44.199Z + 2020-06-18T10:29:19.853Z To Update to 2.6.0

Embark’s npm package has changed from embark-framework to embark, this sometimes can create conflicts. To update, first uninstall embark-framework 1 to avoid any conflicts with npm uninstall -g embark-framework followed by npm install -g embark

to update from 2.5.2:

npm install -g embark@2.6

afterwards make sure embark version returns 2.6.0.

In this release

You no longer need to wait for new releases of embark when a new version of web3.js or solc comes out as this can be now configured. Embark will take care of downloading and using the new versions. You can specify a list of nodes the dapp should attempt to connect to (instead of being limited 1). Whisper 5 is now supported.

Support for web3.js 1.0 and (nearly) ANY web3.js version

Embark now supports web3.js 1.0 by default, however you can now also specify exactly which version of web3.js you want to use so you can still use 0.19.0 or newer versions of 1.0.

in config/contracts.json

{
"default": {
....
"versions": {
"web3.js": "1.0.0-beta"
}
...
}

If ,for example, you wish to use 0.19.0 you can specify it in the config as "web3.js": "0.19.0"

Support for ANY solc version

You can also configure the solc compiler you wish to use and it should work, so long that solc release does not contain breaking API changes.

config/contracts.json

{
"default": {
....
"versions": {
"solc": "0.4.17"
}
...
}

Specify nodes DApp should attempt to connect to

You can specify which nodes your dapp should try to connect in each enviroment. “$WEB3” is a special keyword to specify the existing web3 object.
The following config would attempt to use the existing web3 object and if unavailable attempt to connect to localhost:8545

config/contracts.json

{
"development": {
...
"dappConnection": [
"$WEB3",
"http://localhost:8545"
],
...
}

Specify node to deploy to

Before Embark would assume this would be the same as the one configured in blockchain.json which could lead to some ackward configs for some devs, this has now been changed so you can specify it in the contracts config.

config/contracts.json

{
"development": {
...
"deployment": {
"host": "localhost",
"port": 8545,
"type": "rpc"
},
...
}

Specify node to connect whisper to

config/communication.json

{
"default": {
"enabled": true,
"provider": "whisper",
"available_providers": ["whisper", "orbit"],
"connection": {
"host": "localhost",
"port": 8546,
"type": "ws"
}
}
}

Specify url to get assets

You can specify for each environment what IPFS node to get the assets from

config/storage.json

{
...
"development": {
....
"getUrl": "http://localhost:8080/ipfs/"
},
...
"livenet": {
....
"getUrl": "https://gateway.ipfs.io/ipfs/"
}
}

Plugin API changes

plugin

The following events are deprecated: abi-vanila, abi, abi-contracts-vanila, abi-vanila-deployment and have been renamed to code-vanila, code, code-contracts-vanila, code-vanila-deployment

plugins that use these events will get deprecation warnings, the deprecated events will be removed in 2.7.0

New Blockchain options

geth

The following fields are now available at config/blockchain.json to enhance embark blockchain:

  • “wsHost” - to specify the websocket host (default: localhost)
  • “wsPort” - to specify the websocket port (default: 8546)
  • “wsOrigins”- to specify the allowed origin of the websocket requests (default: FALSE), must be specified to something like http://localhost:8000 for the websocket connection to work.
  • “wsApi” - to specify the apis available through websockets (default: [‘eth’, ‘web3’, ‘net’, ‘shh’])

Misc Bugfixes and Improvements

bug fixes

  • tests no longer need the requires and initialization and can be run directly with embark. however you can still use these requires to run it yourself with mocha or your own preferred test framework
  • embark and mocha are no longer dependencies in the created dapp
  • you can specify a test file with embark test <filename>
  • tests no longer need testrpc to be installed first
  • EmbarkJS.isNewWeb3() to detect if web3 1.0 is available
  • demo app updated to use web3.js 1.0 and solc 0.4.17
  • warn user when websocket or http CORS is not set
  • tolerate solc compiler warnings, which could cause a crash sometimes

Thank you

A big thanks to all that contributed to this release including Todd Baur and Jacob Beauchamp.

Chatroom

To discuss about Embark or Dapp development, please join us at the gitter channel

]]>
@@ -737,7 +737,7 @@ https://blog.embarklabs.io/news/2017/06/28/embark-2-5-released/ 2017-06-27T22:00:00.000Z - 2020-06-15T08:04:44.199Z + 2020-06-18T10:29:19.853Z To Update to 2.5.0

Embark’s npm package has changed from embark-framework to embark, this sometimes can create conflicts. To update, first uninstall embark-framework 1 to avoid any conflicts with npm uninstall -g embark-framework followed by npm install -g embark

to update from 2.4.2:

npm install -g embark@2.5

afterwards make sure embark version returns 2.5.0.

In this release

This release updates to the lastest dependencies, fixes a few things and has a lot of work under the hood necessary for future releases.

Updates

  • support for geth 1.6.5
  • updated to use web3.js 0.19.11
  • updated to use solc 0.4.11

Misc Bugfixes and Improvements

  • embark new will now prompt for the dapp name if not specified as embark new <yourDappName>
  • embark.js: ContractName.new() as been added as an alias for ContractName.deploy()
  • embark.js: a method to easily send ether to a contract has been added: ContractName.send(value, unit, options) e.g ContractName.send(2, "ether", {from: web3.eth.accounts[1]})
  • orbit: Fix for orbit to make it work if the blockchain component is disabled
  • orbit: Use default config for orbit it none is specified in the config file
  • Demo app now has warning message for incompatible whisper versions
  • the JSON files of the contracts are now being outputted at dist/contracts/ (experimental)
  • whisper: Dashboard now displays the whisper version of the node
  • plugin API: extensions can now also be added as directories within the dapp directory
  • plugin API: plugins can now register a component to be displayed in the dashboard. e.g:
embark.registerServiceCheck('PluginService', function(cb) {
if (someFunctionThatChecksTheService()) {
cb({name: "MyServiceName", status: "on"});
} else {
cb({name: "MyServiceName", status: "off"});
}
});

Thank you

A big thanks to all that contributed to this release including Nathan Hernandez, Antonio Tenorio-Fornés, Jon Johnson, Andy Nogueira, roo2, Carl Mönnig, Michael Yeates, Todd Baur, 黄俊钦, Ramiro Moreira, gregg dourgarian

Chatroom

To discuss about Embark or Dapp development, please join us at the gitter channel

]]>
diff --git a/chat/index.html b/chat/index.html index e7d3230d..4e9d763d 100644 --- a/chat/index.html +++ b/chat/index.html @@ -154,8 +154,8 @@ svg4everybody(); - - + + diff --git a/community/index.html b/community/index.html index 01756d61..66a510f4 100644 --- a/community/index.html +++ b/community/index.html @@ -154,8 +154,8 @@ svg4everybody(); - - + + diff --git a/index.html b/index.html index cfd35fb5..e1cba9cd 100644 --- a/index.html +++ b/index.html @@ -154,8 +154,8 @@ svg4everybody(); - - + + diff --git a/news/2017/06/28/embark-2-5-released/index.html b/news/2017/06/28/embark-2-5-released/index.html index 9cd6f24f..76ed64a1 100644 --- a/news/2017/06/28/embark-2-5-released/index.html +++ b/news/2017/06/28/embark-2-5-released/index.html @@ -155,7 +155,7 @@ svg4everybody(); - + diff --git a/news/2017/10/25/embark-2-6-released/index.html b/news/2017/10/25/embark-2-6-released/index.html index e98cd2be..f902a156 100644 --- a/news/2017/10/25/embark-2-6-released/index.html +++ b/news/2017/10/25/embark-2-6-released/index.html @@ -158,7 +158,7 @@ svg4everybody(); - + diff --git a/news/2018/05/04/embark-3-0-released/index.html b/news/2018/05/04/embark-3-0-released/index.html index ba22f3da..4e87f749 100644 --- a/news/2018/05/04/embark-3-0-released/index.html +++ b/news/2018/05/04/embark-3-0-released/index.html @@ -155,7 +155,7 @@ svg4everybody(); - + diff --git a/news/2018/06/20/embark-3-1-released/index.html b/news/2018/06/20/embark-3-1-released/index.html index 91012b3f..9679ec3a 100644 --- a/news/2018/06/20/embark-3-1-released/index.html +++ b/news/2018/06/20/embark-3-1-released/index.html @@ -155,7 +155,7 @@ svg4everybody(); - + diff --git a/news/2018/09/27/how-to-create-a-token-factory-with-embark-part-1/index.html b/news/2018/09/27/how-to-create-a-token-factory-with-embark-part-1/index.html index de22a7c9..ae167a5f 100644 --- a/news/2018/09/27/how-to-create-a-token-factory-with-embark-part-1/index.html +++ b/news/2018/09/27/how-to-create-a-token-factory-with-embark-part-1/index.html @@ -163,7 +163,7 @@ svg4everybody(); - + @@ -258,16 +258,16 @@ svg4everybody();

In this tutorial series we’ll create a Token Factory using Ethereum. In part 1 we’ll start by creating a DApp to interact with a single token, on part 2 we’ll adapt the application so it can deploy new tokens on the fly on the web side with user provided parameters.

A Token is typically a unit used to represent a medium of exchange for some service or utility. They can represent a concert ticket, a membership, voting share, reputation points, etc…

Getting Started

First of all, make sure you have Go-Ethereum and Embark installed.

-
$ npm -g install embark
+
$ npm -g install embark

Now, let’s create a new dapp

-
$ embark new TokenFactory
+
$ embark new TokenFactory

This will create a directory called TokenFactory, cd to it and run:

-
$ embark run
+
$ embark run

In another console, in the same directory, run:

@@ -285,7 +285,7 @@ svg4everybody();

Now open your browser at http://localhost:8000 , start your favourite editor and let’s get started!

Adding the Token Contract

We’ll add a typical ERC20 token contract to contracts/token.sol

warning: this contract is for educational purposes only, do not use it in production unless you know what you are doing

-
pragma solidity ^0.4.23;
+
pragma solidity ^0.4.23;
 
 contract Token {
 
@@ -351,7 +351,7 @@ contract Token {
 
constructor(uint initial_balance) public {
_balances[msg.sender] = initial_balance;
_supply = initial_balance;
}

Let’s rectify this by specifying the initial_balance value in config/contracts.js

-
module.exports = {
+
module.exports = {
   default: {
     // .....
     gas: "auto",
@@ -370,13 +370,13 @@ contract Token {
 
 

Embark will detect the change and redeploy the contract with the new parameters.

You can confirm that the token supply is 1000 by typing:

-
$ Token.methods._supply().call(console.log)
+
$ Token.methods._supply().call(console.log)

Console

Creating the UI

For the sake of brevity, we wouldn’t implement every single functionality in the contract. However, we’ll implement two important features: Checking balance of an address and Transferring Tokens from one address to another.

Checking address balance

To input the address to query, we’ll edit app/index.html and add a simple form.

-
<html>
+
<html>
   <head>
     <title>Embark</title>
     <link rel="stylesheet" href="css/app.css">
@@ -395,16 +395,16 @@ contract Token {
 
 

Adding jQuery

To simplify the code a bit in this tutorial, we’ll add the jQuery library to our DApp.

-
$ npm install jquery@3 --save
+
$ npm install jquery@3 --save

Now edit the file app/js/index.js and add:

-
import $ from 'jquery';
+
import $ from 'jquery';

Setting the default address

Let’s add to the input field field our own address as the default text so we can easily query our own balance. In the file app/js/index.js add:

-
import $ from 'jquery';
+
import $ from 'jquery';
 import EmbarkJS from 'Embark/EmbarkJS';
 
 $(document).ready(function() {
@@ -428,14 +428,14 @@ $(document).ready(function() {
 
function balanceOf( address who ) constant returns (uint value) {
return _balances[who];
}

This method will be available in the JS code automatically as a promise, like:

-
import Token from 'Embark/contracts/Token';
+
import Token from 'Embark/contracts/Token';
 
 Token.methods.balanceOf(address).call().then(function(balance) { });

So we can simply add a click event to the button, get the address, query the balance and set the result.

-
import $ from 'jquery';
+
import $ from 'jquery';
 import EmbarkJS from 'Embark/EmbarkJS';
 import Token from 'Embark/contracts/Token';
 
@@ -466,7 +466,7 @@ $(document).ready(function() {
 
function transfer( address to, uint value) returns (bool ok)

The method will take two parameters, an address and a value. Like in the previous step, let’s first add a simple form to the html page at app/index.html:

-
<html>
+
<html>
   <head>
     <title>Embark</title>
     <link rel="stylesheet" href="css/app.css">
@@ -494,7 +494,7 @@ $(document).ready(function() {
 
 
 

Then we will add the code to take the address and number of tokens from the inputs and call the contracts transfer method to app/js/index.js:

-
import $ from 'jquery';
+
import $ from 'jquery';
 import EmbarkJS from 'Embark/EmbarkJS';
 import Token from 'Embark/contracts/Token';
 
diff --git a/news/2018/10/27/how-to-create-a-token-factory-with-embark-part-2/index.html b/news/2018/10/27/how-to-create-a-token-factory-with-embark-part-2/index.html
index 40bf52ae..bcb468f7 100644
--- a/news/2018/10/27/how-to-create-a-token-factory-with-embark-part-2/index.html
+++ b/news/2018/10/27/how-to-create-a-token-factory-with-embark-part-2/index.html
@@ -164,7 +164,7 @@ svg4everybody();
 
 
 
-
+
 
 
 
@@ -260,13 +260,13 @@ svg4everybody();
 

A Token is typically a unit used to represent a medium of exchange for some service or utility. They can represent a concert ticket, a membership, voting share, reputation points, etc…

Getting Started

For the second part of the tutorial, Embark 3.0 or higher is required.

If you are using an older version you can update with:

-
$ npm install -g embark@3
+
$ npm install -g embark@3

Afterwards make sure that embark version returns 3.0 then restart embark with embark run

Generalizing Token Interaction

We’ll start by generalizing the previous UI so we can input the address of a ERC20 Token and interact with it.

First, we’ll add a simple form to app/index.html to get address of the token we wish to interact with.

-
<html>
+
<html>
   <head>
     <title>Embark</title>
     <link rel="stylesheet" href="css/app.css">
@@ -301,7 +301,7 @@ svg4everybody();
 
 
 

In app/js/index.js we’ll get the address given in the input, initialize a new contract object for that address and the Token ABI, and then assign it to a variable. We’ll also update the rest of code to use currentToken instead of Token. This way the existing code will work with the token we will be loading.

-
import EmbarkJS from 'Embark/EmbarkJS';
+
import EmbarkJS from 'Embark/EmbarkJS';
 import $ from 'jquery';
 import Token from 'Embark/contracts/Token';
 
@@ -354,7 +354,7 @@ $(document).ready(function() {
 

It’s 980 as expected (1000 was the initial supply as configured in config/contracts.json and 20 was transferred out in part 1

Deploy New Tokens on the fly

Now that we have an UI to interact with an existing Token given its address, we’ll add functionality to deploy tokens on the fly, each with their own initial supply.

First we’ll add a simple form to app/index.html to get the desired supply of the new token to deploy.

-
<html>
+
<html>
   <head>
     <title>Embark</title>
     <link rel="stylesheet" href="css/app.css">
@@ -397,7 +397,7 @@ $(document).ready(function() {
 
 

Embark makes the contract objects available in the js side, each contract object will have a method called deploy that can deploy new instances of the contract. This method can take parameters for the contract, and it will return a promise containing a contract object of the deployed contract.

In app/js/index.js we’ll add the code to deploy new tokens client side using this functionality:

-
$(document).ready(function() {
+
$(document).ready(function() {
 
   var currentToken;
   $("#deployToken button").click(function() {
@@ -451,7 +451,7 @@ $(document).ready(function() {
 

Screenshot

And it’s 500 as expected since that’s the initial supply defined for the first token deployed.

Disabling the Token Deploy from Embarks side

Now that your DApp can deploy Tokens on the fly, It’s unnecessary for Embark to deploy the Token contract like it did in part 1, however you still need Embark to make the Token contract available on the client side. To achieve this, go to config/contracts.js and set “deploy”: false for that contract

-
module.exports = {
+
module.exports = {
   "default": {
     // .....
     "gas": "auto",
diff --git a/news/2019/01/23/building-smart-contract-only-dapps/index.html b/news/2019/01/23/building-smart-contract-only-dapps/index.html
index 263d3cf4..8a134563 100644
--- a/news/2019/01/23/building-smart-contract-only-dapps/index.html
+++ b/news/2019/01/23/building-smart-contract-only-dapps/index.html
@@ -155,7 +155,7 @@ svg4everybody();
 
 
 
-
+
 
 
 
diff --git a/news/2019/01/28/running-embark-tests-on-a-continuous-integration-server/index.html b/news/2019/01/28/running-embark-tests-on-a-continuous-integration-server/index.html
index fc74152b..4da01c41 100644
--- a/news/2019/01/28/running-embark-tests-on-a-continuous-integration-server/index.html
+++ b/news/2019/01/28/running-embark-tests-on-a-continuous-integration-server/index.html
@@ -155,7 +155,7 @@ svg4everybody();
 
 
 
-
+
 
 
 
diff --git a/news/2019/02/04/building-a-decentralized-reddit-with-embark-part-1/index.html b/news/2019/02/04/building-a-decentralized-reddit-with-embark-part-1/index.html
index 8cca7cc8..58e9d7b9 100644
--- a/news/2019/02/04/building-a-decentralized-reddit-with-embark-part-1/index.html
+++ b/news/2019/02/04/building-a-decentralized-reddit-with-embark-part-1/index.html
@@ -155,7 +155,7 @@ svg4everybody();
 
 
 
-
+
 
 
 
@@ -271,7 +271,7 @@ svg4everybody();
 

If there are any issues in the “Available Services” section of the dashboard, go back to our installation guide and make sure all tools are available on your machine.

Creating the Smart Contract

Alright, next up we want to create the brain of our application, which is a Smart Contract written in Solidity, that enables creating posts and votes. We’re going to build it up step by step and afterwards we’ll add some tests to ensure our code is actually working.

First thing we do is creating a file DReddit.sol inside contracts with a Smart Contract like this:

-
pragma solidity ^0.5.0;
+
pragma solidity ^0.5.0;
 
 contract DReddit {
 
@@ -279,7 +279,7 @@ contract DReddit {
 
 
 

Great! With that in place, let’s introduce a couple of data structures for creating and storing topic posts. Let’s say a post will have a creation date, a description and an address of the owner. There’s a few more things we’ll have to add, but let’s do it one step at a time. Here’s what a Post struct could look like:

-
struct Post {
+
struct Post {
   uint creationDate;
   bytes description;
   address owner;
@@ -287,11 +287,11 @@ contract DReddit {
 
 
 

We’re also going to add an array to store all of our posts. Now that we have a Post struct, this is a simple as:

-
Post [] public posts;
+
Post [] public posts;

Creating posts

It’s time to add our first method which will enable users to add new posts to the platform. For that, we’ll create the method createPost(bytes _description) where _description are the bytes that represent the posts text.

-
function createPost(bytes _description) public {
+
function createPost(bytes _description) public {
   uint postId = posts.length++;
   posts[postId] = Post({
     creationDate: block.timestamp,
@@ -303,7 +303,7 @@ contract DReddit {
 
 

The first thing we do is creating an id for the post to be stored. We then use our Post struct to create a new post instance. Notice that we leverage the postId when storing the Post in our posts array. To set the owner, we take advantage of Solidity’s global msg object which is available in every transaction.

Emitting events

As we’re planning to build a front-end that reacts to posts being created, we need to emit an event so the front-end can subscribe to it accordingly. For that, we first introduce a new event type NewPost which will look something like this:

-
event NewPost(
+
event NewPost(
   uint indexed postId,
   address owner,
   bytes description
@@ -311,7 +311,7 @@ contract DReddit {
 
 
 

Once that is done, all we have to do is emit NewPost inside createPost() with the required data:

-
function createPost(bytes _description) public {
+
function createPost(bytes _description) public {
   ...
   emit NewPost(postId, msg.sender, _description);
 }
@@ -325,7 +325,7 @@ contract DReddit {
struct Post {
...
uint upvotes;
uint downvotes;
mapping(address => Ballot) voters;
}

Here’s the NewPost event which we’ll use in a few moments:

-
event NewVote(
+
event NewVote(
   uint indexed postId,
   address owner,
   uint8 vote
@@ -337,7 +337,7 @@ contract DReddit {
 
 

With these building blocks at hand, let’s implement a vote(uint postId, uint8 _vote) method. _vote is going to be one of our defined Ballot types and is represented as uint going from 0 - 2. We’ll use Solidity’s require() statement to ensure we only vote on posts that actually exist, as well as nobody can actually vote multiple times on the same post.

We then increment the up or down vote counter respectively, store the voter and emit a NewVote event:

-
function vote(uint _postId, uint8 _vote) public {
+
function vote(uint _postId, uint8 _vote) public {
   Post storage post = posts[_postId];
 
   require(post.creationDate != 0, "Post does not exist");
@@ -357,7 +357,7 @@ contract DReddit {
 
 
 

Determine if users can vote

We probably want to add an indication to the UI that a user has already voted on a certain post. For that it’d be handy to have an API that actually tells us whether a user can vote on a post. We’ve already discussed earlier that users can’t vote multiple times on the same post, so figuring out if a user can vote is pretty straight forward. Here’s what a canVote(uint _postId) method could look like:

-
function canVote(uint _postId) public view returns (bool) {
+
function canVote(uint _postId) public view returns (bool) {
   if (_postId > posts.length - 1) return false;
   Post storage post = posts[_postId];
   return (post.voters[msg.sender] == Ballot.NONE);
@@ -365,7 +365,7 @@ contract DReddit {
 
 
 

Fetching votes

We also need a way to actually let users check what they’ve voted for, in case they did. For that we’ll add a simple getVote() method that looks something like this:

-
function getVote(uint _postId) public view returns (uint8) {
+
function getVote(uint _postId) public view returns (uint8) {
   Post storage post = posts[_postId];
   return uint8(post.voters[msg.sender]);
 }
@@ -373,7 +373,7 @@ contract DReddit {

And with that, our Smart Contract is pretty much done! Just to make sure that everything is compiling smoothly, we can execute embark build --contracts in case there’s no existing Embark instance watching our work already.

Here’s the complete Smart Contract code (you can also find it in this repository:

-
pragma solidity ^0.5.0;
+
pragma solidity ^0.5.0;
 
 contract DReddit {
 
diff --git a/news/2019/02/11/building-a-decentralized-reddit-with-embark-part-2/index.html b/news/2019/02/11/building-a-decentralized-reddit-with-embark-part-2/index.html
index 728c47e8..f2e2dc75 100644
--- a/news/2019/02/11/building-a-decentralized-reddit-with-embark-part-2/index.html
+++ b/news/2019/02/11/building-a-decentralized-reddit-with-embark-part-2/index.html
@@ -155,7 +155,7 @@ svg4everybody();
 
 
 
-
+
 
 
 
diff --git a/news/2019/02/18/building-a-decentralized-reddit-with-embark-part-3/index.html b/news/2019/02/18/building-a-decentralized-reddit-with-embark-part-3/index.html
index 9f10e1b3..750a6af9 100644
--- a/news/2019/02/18/building-a-decentralized-reddit-with-embark-part-3/index.html
+++ b/news/2019/02/18/building-a-decentralized-reddit-with-embark-part-3/index.html
@@ -155,7 +155,7 @@ svg4everybody();
 
 
 
-
+
 
 
 
diff --git a/news/2019/03/18/upgrading-to-embark-4/index.html b/news/2019/03/18/upgrading-to-embark-4/index.html
index 17de04f3..3d5ee88c 100644
--- a/news/2019/03/18/upgrading-to-embark-4/index.html
+++ b/news/2019/03/18/upgrading-to-embark-4/index.html
@@ -155,7 +155,7 @@ svg4everybody();
 
 
 
-
+
 
 
 
diff --git a/news/2019/03/19/introducing-embark-4/index.html b/news/2019/03/19/introducing-embark-4/index.html
index dbf4412a..876dd6cf 100644
--- a/news/2019/03/19/introducing-embark-4/index.html
+++ b/news/2019/03/19/introducing-embark-4/index.html
@@ -160,7 +160,7 @@ svg4everybody();
 
 
 
-
+
 
 
 
diff --git a/news/2019/07/23/whats-new-in-embark-4.1/index.html b/news/2019/07/23/whats-new-in-embark-4.1/index.html
index 13d7a947..e2d88cd7 100644
--- a/news/2019/07/23/whats-new-in-embark-4.1/index.html
+++ b/news/2019/07/23/whats-new-in-embark-4.1/index.html
@@ -155,7 +155,7 @@ svg4everybody();
 
 
 
-
+
 
 
 
diff --git a/news/2019/11/18/nim-vs-crystal-part-1-performance-interoperability/index.html b/news/2019/11/18/nim-vs-crystal-part-1-performance-interoperability/index.html
index 4aae8eea..cedc0bb7 100644
--- a/news/2019/11/18/nim-vs-crystal-part-1-performance-interoperability/index.html
+++ b/news/2019/11/18/nim-vs-crystal-part-1-performance-interoperability/index.html
@@ -156,7 +156,7 @@ svg4everybody();
 
 
 
-
+
 
 
 
diff --git a/news/2019/11/21/nim-vs-crystal-part-2-threading-tooling/index.html b/news/2019/11/21/nim-vs-crystal-part-2-threading-tooling/index.html
index ebf85dd1..d0a8715e 100644
--- a/news/2019/11/21/nim-vs-crystal-part-2-threading-tooling/index.html
+++ b/news/2019/11/21/nim-vs-crystal-part-2-threading-tooling/index.html
@@ -160,7 +160,7 @@ svg4everybody();
 
 
 
-
+
 
 
 
diff --git a/news/2019/11/28/nim-vs-crystal-part-3-cryto-dapps-p2p/index.html b/news/2019/11/28/nim-vs-crystal-part-3-cryto-dapps-p2p/index.html
index 5eebfb55..6ecd6838 100644
--- a/news/2019/11/28/nim-vs-crystal-part-3-cryto-dapps-p2p/index.html
+++ b/news/2019/11/28/nim-vs-crystal-part-3-cryto-dapps-p2p/index.html
@@ -156,7 +156,7 @@ svg4everybody();
 
 
 
-
+
 
 
 
diff --git a/news/2019/12/09/web3-what-are-your-options/index.html b/news/2019/12/09/web3-what-are-your-options/index.html
index 6a6ac679..cd950141 100644
--- a/news/2019/12/09/web3-what-are-your-options/index.html
+++ b/news/2019/12/09/web3-what-are-your-options/index.html
@@ -158,7 +158,7 @@ svg4everybody();
 
 
 
-
+
 
 
 
diff --git a/news/2020/01/09/take-back-the-web-hackathon/index.html b/news/2020/01/09/take-back-the-web-hackathon/index.html
index f189a11e..7714e89e 100644
--- a/news/2020/01/09/take-back-the-web-hackathon/index.html
+++ b/news/2020/01/09/take-back-the-web-hackathon/index.html
@@ -155,7 +155,7 @@ svg4everybody();
 
 
 
-
+
 
 
 
diff --git a/news/2020/01/13/announcing-embark-5/index.html b/news/2020/01/13/announcing-embark-5/index.html
index b2cae0d9..1a9cf52d 100644
--- a/news/2020/01/13/announcing-embark-5/index.html
+++ b/news/2020/01/13/announcing-embark-5/index.html
@@ -155,7 +155,7 @@ svg4everybody();
 
 
 
-
+
 
 
 
diff --git a/news/2020/01/28/embark-5-1/index.html b/news/2020/01/28/embark-5-1/index.html
index 993be8b8..8bc20740 100644
--- a/news/2020/01/28/embark-5-1/index.html
+++ b/news/2020/01/28/embark-5-1/index.html
@@ -155,7 +155,7 @@ svg4everybody();
 
 
 
-
+
 
 
 
diff --git a/news/2020/01/29/subspace-1-2/index.html b/news/2020/01/29/subspace-1-2/index.html
index 7bdfa0e3..e29dc898 100644
--- a/news/2020/01/29/subspace-1-2/index.html
+++ b/news/2020/01/29/subspace-1-2/index.html
@@ -155,7 +155,7 @@ svg4everybody();
 
 
 
-
+
 
 
 
diff --git a/news/2020/01/30/dapp-frontend-security/index.html b/news/2020/01/30/dapp-frontend-security/index.html
index 5f01a493..a8e5b6fa 100644
--- a/news/2020/01/30/dapp-frontend-security/index.html
+++ b/news/2020/01/30/dapp-frontend-security/index.html
@@ -157,7 +157,7 @@ svg4everybody();
 
 
 
-
+
 
 
 
diff --git a/news/2020/02/11/subspace-1-3/index.html b/news/2020/02/11/subspace-1-3/index.html
index 34b08ac4..8096f26f 100644
--- a/news/2020/02/11/subspace-1-3/index.html
+++ b/news/2020/02/11/subspace-1-3/index.html
@@ -155,7 +155,7 @@ svg4everybody();
 
 
 
-
+
 
 
 
diff --git a/news/2020/02/17/decentralized-notifications/index.html b/news/2020/02/17/decentralized-notifications/index.html
index e179d5d4..0e26e967 100644
--- a/news/2020/02/17/decentralized-notifications/index.html
+++ b/news/2020/02/17/decentralized-notifications/index.html
@@ -155,7 +155,7 @@ svg4everybody();
 
 
 
-
+
 
 
 
diff --git a/news/2020/02/19/embark-5-2-release/index.html b/news/2020/02/19/embark-5-2-release/index.html
index bf586cf7..1ca7b773 100644
--- a/news/2020/02/19/embark-5-2-release/index.html
+++ b/news/2020/02/19/embark-5-2-release/index.html
@@ -156,7 +156,7 @@ svg4everybody();
 
 
 
-
+
 
 
 
diff --git a/news/2020/02/24/wasm-ewasm-what-and-why/index.html b/news/2020/02/24/wasm-ewasm-what-and-why/index.html
index e67c3f41..d218b93e 100644
--- a/news/2020/02/24/wasm-ewasm-what-and-why/index.html
+++ b/news/2020/02/24/wasm-ewasm-what-and-why/index.html
@@ -158,7 +158,7 @@ svg4everybody();
 
 
 
-
+
 
 
 
diff --git a/news/2020/03/04/2020-Blockchain-Developer-Survey/index.html b/news/2020/03/04/2020-Blockchain-Developer-Survey/index.html
index 9051576e..c7d449ea 100644
--- a/news/2020/03/04/2020-Blockchain-Developer-Survey/index.html
+++ b/news/2020/03/04/2020-Blockchain-Developer-Survey/index.html
@@ -163,7 +163,7 @@ svg4everybody();
 
 
 
-
+
 
 
 
diff --git a/news/2020/03/18/subspace-2.0/index.html b/news/2020/03/18/subspace-2.0/index.html
index 19a2ece6..799c4749 100644
--- a/news/2020/03/18/subspace-2.0/index.html
+++ b/news/2020/03/18/subspace-2.0/index.html
@@ -155,7 +155,7 @@ svg4everybody();
 
 
 
-
+
 
 
 
diff --git a/news/2020/03/24/2020-using-vyper-with-embark/index.html b/news/2020/03/24/2020-using-vyper-with-embark/index.html
index 2eaa127b..c5ac69dd 100644
--- a/news/2020/03/24/2020-using-vyper-with-embark/index.html
+++ b/news/2020/03/24/2020-using-vyper-with-embark/index.html
@@ -160,7 +160,7 @@ svg4everybody();
 
 
 
-
+
 
 
 
diff --git a/news/2020/04/02/2020-smart-contract-security-analysis-with-mythx/index.html b/news/2020/04/02/2020-smart-contract-security-analysis-with-mythx/index.html
index a7fd87dd..9505053d 100644
--- a/news/2020/04/02/2020-smart-contract-security-analysis-with-mythx/index.html
+++ b/news/2020/04/02/2020-smart-contract-security-analysis-with-mythx/index.html
@@ -162,7 +162,7 @@ svg4everybody();
 
 
 
-
+
 
 
 
diff --git a/news/index.html b/news/index.html
index ed954c7c..dd428c09 100644
--- a/news/index.html
+++ b/news/index.html
@@ -154,8 +154,8 @@ svg4everybody();
 
 
 
-
-
+
+
 
 
 
diff --git a/sitemap.xml b/sitemap.xml
index 5a5a98b8..b7daa0f6 100644
--- a/sitemap.xml
+++ b/sitemap.xml
@@ -2,247 +2,247 @@
 
   
   
-    https://blog.embarklabs.io/news/index.html
+    https://blog.embarklabs.io/news/2020/04/02/2020-smart-contract-security-analysis-with-mythx/
     
-    2020-06-15T08:04:44.418Z
-    
-  
-  
-  
-    https://blog.embarklabs.io/index.html
-    
-    2020-06-15T08:04:44.418Z
-    
-  
-  
-  
-    https://blog.embarklabs.io/community/index.html
-    
-    2020-06-15T08:04:44.417Z
-    
-  
-  
-  
-    https://blog.embarklabs.io/chat/index.html
-    
-    2020-06-15T08:04:44.417Z
+    2020-06-18T10:35:45.392Z
     
   
   
   
     https://blog.embarklabs.io/news/2020/03/24/2020-using-vyper-with-embark/
     
-    2020-06-15T08:04:44.203Z
-    
-  
-  
-  
-    https://blog.embarklabs.io/news/2020/04/02/2020-smart-contract-security-analysis-with-mythx/
-    
-    2020-06-15T08:04:44.203Z
-    
-  
-  
-  
-    https://blog.embarklabs.io/news/2020/02/17/decentralized-notifications/
-    
-    2020-06-15T08:04:44.202Z
+    2020-06-18T10:35:41.816Z
     
   
   
   
     https://blog.embarklabs.io/news/2020/03/18/subspace-2.0/
     
-    2020-06-15T08:04:44.202Z
-    
-  
-  
-  
-    https://blog.embarklabs.io/news/2020/01/30/dapp-frontend-security/
-    
-    2020-06-15T08:04:44.202Z
-    
-  
-  
-  
-    https://blog.embarklabs.io/news/2020/01/28/embark-5-1/
-    
-    2020-06-15T08:04:44.202Z
-    
-  
-  
-  
-    https://blog.embarklabs.io/news/2020/02/19/embark-5-2-release/
-    
-    2020-06-15T08:04:44.202Z
-    
-  
-  
-  
-    https://blog.embarklabs.io/news/2020/02/24/wasm-ewasm-what-and-why/
-    
-    2020-06-15T08:04:44.202Z
-    
-  
-  
-  
-    https://blog.embarklabs.io/news/2020/02/11/subspace-1-3/
-    
-    2020-06-15T08:04:44.202Z
-    
-  
-  
-  
-    https://blog.embarklabs.io/news/2020/01/29/subspace-1-2/
-    
-    2020-06-15T08:04:44.202Z
-    
-  
-  
-  
-    https://blog.embarklabs.io/news/2020/03/04/2020-Blockchain-Developer-Survey/
-    
-    2020-06-15T08:04:44.202Z
-    
-  
-  
-  
-    https://blog.embarklabs.io/news/2019/11/18/nim-vs-crystal-part-1-performance-interoperability/
-    
-    2020-06-15T08:04:44.201Z
-    
-  
-  
-  
-    https://blog.embarklabs.io/news/2020/01/09/take-back-the-web-hackathon/
-    
-    2020-06-15T08:04:44.201Z
-    
-  
-  
-  
-    https://blog.embarklabs.io/news/2019/03/18/upgrading-to-embark-4/
-    
-    2020-06-15T08:04:44.201Z
-    
-  
-  
-  
-    https://blog.embarklabs.io/news/2019/03/19/introducing-embark-4/
-    
-    2020-06-15T08:04:44.201Z
-    
-  
-  
-  
-    https://blog.embarklabs.io/news/2019/07/23/whats-new-in-embark-4.1/
-    
-    2020-06-15T08:04:44.201Z
-    
-  
-  
-  
-    https://blog.embarklabs.io/news/2019/12/09/web3-what-are-your-options/
-    
-    2020-06-15T08:04:44.201Z
+    2020-06-18T10:35:40.667Z
     
   
   
   
     https://blog.embarklabs.io/news/2019/11/28/nim-vs-crystal-part-3-cryto-dapps-p2p/
     
-    2020-06-15T08:04:44.201Z
-    
-  
-  
-  
-    https://blog.embarklabs.io/news/2020/01/13/announcing-embark-5/
-    
-    2020-06-15T08:04:44.201Z
+    2020-06-18T10:35:37.818Z
     
   
   
   
     https://blog.embarklabs.io/news/2019/11/21/nim-vs-crystal-part-2-threading-tooling/
     
-    2020-06-15T08:04:44.201Z
+    2020-06-18T10:35:37.817Z
     
   
   
   
-    https://blog.embarklabs.io/news/2019/01/28/running-embark-tests-on-a-continuous-integration-server/
+    https://blog.embarklabs.io/news/2019/11/18/nim-vs-crystal-part-1-performance-interoperability/
     
-    2020-06-15T08:04:44.200Z
+    2020-06-18T10:35:37.817Z
     
   
   
   
-    https://blog.embarklabs.io/news/2019/02/11/building-a-decentralized-reddit-with-embark-part-2/
+    https://blog.embarklabs.io/news/2020/02/19/embark-5-2-release/
     
-    2020-06-15T08:04:44.200Z
+    2020-06-18T10:35:35.186Z
     
   
   
   
-    https://blog.embarklabs.io/news/2018/06/20/embark-3-1-released/
+    https://blog.embarklabs.io/news/2020/01/30/dapp-frontend-security/
     
-    2020-06-15T08:04:44.200Z
+    2020-06-18T10:35:34.485Z
     
   
   
   
-    https://blog.embarklabs.io/news/2018/10/27/how-to-create-a-token-factory-with-embark-part-2/
+    https://blog.embarklabs.io/news/2020/02/24/wasm-ewasm-what-and-why/
     
-    2020-06-15T08:04:44.200Z
+    2020-06-18T10:35:34.128Z
     
   
   
   
-    https://blog.embarklabs.io/news/2019/01/23/building-smart-contract-only-dapps/
+    https://blog.embarklabs.io/news/2020/03/04/2020-Blockchain-Developer-Survey/
     
-    2020-06-15T08:04:44.200Z
+    2020-06-18T10:35:33.413Z
     
   
   
   
-    https://blog.embarklabs.io/news/2018/09/27/how-to-create-a-token-factory-with-embark-part-1/
+    https://blog.embarklabs.io/news/2019/12/09/web3-what-are-your-options/
     
-    2020-06-15T08:04:44.200Z
+    2020-06-18T10:33:51.644Z
     
   
   
   
-    https://blog.embarklabs.io/news/2019/02/18/building-a-decentralized-reddit-with-embark-part-3/
+    https://blog.embarklabs.io/news/2020/02/17/decentralized-notifications/
     
-    2020-06-15T08:04:44.200Z
+    2020-06-18T10:32:39.102Z
     
   
   
   
-    https://blog.embarklabs.io/news/2019/02/04/building-a-decentralized-reddit-with-embark-part-1/
+    https://blog.embarklabs.io/news/2020/02/11/subspace-1-3/
     
-    2020-06-15T08:04:44.200Z
+    2020-06-18T10:32:00.866Z
     
   
   
   
-    https://blog.embarklabs.io/news/2017/10/25/embark-2-6-released/
+    https://blog.embarklabs.io/news/2020/01/28/embark-5-1/
     
-    2020-06-15T08:04:44.199Z
+    2020-06-18T10:30:42.763Z
+    
+  
+  
+  
+    https://blog.embarklabs.io/news/2020/01/13/announcing-embark-5/
+    
+    2020-06-18T10:30:41.346Z
+    
+  
+  
+  
+    https://blog.embarklabs.io/index.html
+    
+    2020-06-18T10:30:41.346Z
+    
+  
+  
+  
+    https://blog.embarklabs.io/news/index.html
+    
+    2020-06-18T10:30:41.346Z
+    
+  
+  
+  
+    https://blog.embarklabs.io/news/2019/03/18/upgrading-to-embark-4/
+    
+    2020-06-18T10:30:41.345Z
+    
+  
+  
+  
+    https://blog.embarklabs.io/news/2019/07/23/whats-new-in-embark-4.1/
+    
+    2020-06-18T10:30:41.345Z
     
   
   
   
     https://blog.embarklabs.io/news/2018/05/04/embark-3-0-released/
     
-    2020-06-15T08:04:44.199Z
+    2020-06-18T10:30:41.345Z
+    
+  
+  
+  
+    https://blog.embarklabs.io/news/2020/01/29/subspace-1-2/
+    
+    2020-06-18T10:30:37.791Z
+    
+  
+  
+  
+    https://blog.embarklabs.io/news/2018/10/27/how-to-create-a-token-factory-with-embark-part-2/
+    
+    2020-06-18T10:30:36.148Z
+    
+  
+  
+  
+    https://blog.embarklabs.io/news/2018/09/27/how-to-create-a-token-factory-with-embark-part-1/
+    
+    2020-06-18T10:30:36.148Z
+    
+  
+  
+  
+    https://blog.embarklabs.io/news/2020/01/09/take-back-the-web-hackathon/
+    
+    2020-06-18T10:30:35.252Z
+    
+  
+  
+  
+    https://blog.embarklabs.io/news/2019/02/04/building-a-decentralized-reddit-with-embark-part-1/
+    
+    2020-06-18T10:30:35.251Z
+    
+  
+  
+  
+    https://blog.embarklabs.io/news/2019/02/18/building-a-decentralized-reddit-with-embark-part-3/
+    
+    2020-06-18T10:30:35.251Z
+    
+  
+  
+  
+    https://blog.embarklabs.io/news/2019/03/19/introducing-embark-4/
+    
+    2020-06-18T10:30:35.251Z
+    
+  
+  
+  
+    https://blog.embarklabs.io/news/2019/02/11/building-a-decentralized-reddit-with-embark-part-2/
+    
+    2020-06-18T10:30:35.251Z
+    
+  
+  
+  
+    https://blog.embarklabs.io/chat/index.html
+    
+    2020-06-18T10:29:20.103Z
+    
+  
+  
+  
+    https://blog.embarklabs.io/community/index.html
+    
+    2020-06-18T10:29:20.103Z
+    
+  
+  
+  
+    https://blog.embarklabs.io/news/2018/06/20/embark-3-1-released/
+    
+    2020-06-18T10:29:19.853Z
+    
+  
+  
+  
+    https://blog.embarklabs.io/news/2019/01/23/building-smart-contract-only-dapps/
+    
+    2020-06-18T10:29:19.853Z
+    
+  
+  
+  
+    https://blog.embarklabs.io/news/2019/01/28/running-embark-tests-on-a-continuous-integration-server/
+    
+    2020-06-18T10:29:19.853Z
+    
+  
+  
+  
+    https://blog.embarklabs.io/news/2017/10/25/embark-2-6-released/
+    
+    2020-06-18T10:29:19.853Z
     
   
   
   
     https://blog.embarklabs.io/news/2017/06/28/embark-2-5-released/
     
-    2020-06-15T08:04:44.199Z
+    2020-06-18T10:29:19.853Z