Crypto Quickstart: Mobius with PHP
For the first article in this series, I’m going to focus on a promising new token with one of the easiest APIs I’ve ever seen. To help you get started faster, I’ve created a template project that you can use to get going with one command!
The template project is a (very unfair) coin flipping game. If the player guesses right, they’ll get to keep their tokens. If they guess wrong, they lose a token!
Prerequisites:
- Basic knowledge of PHP and the Symfony framework (or other MVC web frameworks)
- PHP installed with SQLite support
- A working composer installation
At the end of this quickstart you’ll have:
- A Mobius developer account
- A working application that you can hook up to the DApp store
Optional: Register for a Mobius developer account
Skip to “Registering a DApp” if you already have a Mobius developer account.
Start the signup process by going to https://mobius.network/store/signup
Once you’ve signed up, go to the next section.
Registering a DApp
Start on the DApp store developer page: https://mobius.network/store/developer
At the top, you’ll see your API Key. Keep track of this for later!
Fill out the form to add a new DApp:
Then, click “Submit”. Your new DApp will appear at the bottom of the page:
Make a note of the App UID (scrambled in the image above).
The next step is to deposit some credits. Enter “100” in the “# Credits” field and click “Deposit”.
If this is a new account, you’ll need to confirm your email address. Once you’ve done that, deposit the credits again.
After you’ve made a deposit, verify that it is listed next to “Credits” and continue to the next section.
Installing the Quickstart
Install the template project with composer:
$ composer create-project zulucrypto/mobius-dapp-quickstart
As part of the install process, you’ll be prompted for some values. Fill in your API key and App UID:
...
Creating the "app/config/parameters.yml" file
Some parameters are missing. Please provide them.
secret (ThisTokenIsNotSoSecretChangeIt):
mobius_app_uid (null): <YOUR APP UID HERE>
mobius_api_key (null): <YOUR API KEY HERE>
Composer will finish the installation and generate an empty database.
Now, start the web server:
$ cd mobius-dapp-quickstart
$ bin/console server:run[OK] Server listening on http://127.0.0.1:8000// Quit the server with CONTROL-C.PHP 7.1.9 Development Server started at Tue Nov 14 13:57:24 2017
Listening on http://127.0.0.1:8000
Document root is /var/www/html/mobius-dapp-quickstart/web
Press Ctrl-C to quit.
Then, load up the web page and you should see the introduction page:
Enter the email address you used when signing up for the DApp store and click “Simulate Login” button. You’ll then be taken to the screen where you can play the game by choosing “heads” or “tails”:
Congratulations, you’ve got your first DApp!
Code Walkthrough
Most of the hard work is done by Mobius and the Mobius SDK. When developing a DApp, you’ll need to know a few things:
Helper Services
The Mobius app store API is available as the mobius.app_store
service. For example, you can access it in a controller via $this->get('mobius.app_store')->getBalance('user@example.com')
User Deposits
When a user makes a deposit, you can be notified by a webhook. In this quickstart it’s implemented as MobiusWebhookController
and will automatically create new users or update the balance of existing ones.
During development you can use the mobius:simulator:app-store-deposit
command to simulate a user making a deposit.
Using Tokens
To transfer tokens from the balance the user deposited to your app, call useBalance
:
$this->get('mobius.app_store')->useBalance('user@example.com', 5)
If the user doesn’t have enough credit, this will throw an exception.
Next Steps
- Join other developers in the Mobius Telegram or Rocket Chat and share what you’re building!