Create a Marketplace
The backbone for a permissioned marketplace for you to mint tokens, create accounts, and to send to your users after purchase.

Overview

Understand that decentralisation is hard, many people don't have experience to use blockchain applications to use apps like Metamask and others. The role of this API and client is for you to build
I'm going to assume that you know Laravel, creating of tables to link users to generated accounts. As we move forward there will be updates but for now this will be simple.

Creating an account

Create and connect a hedera account to a user you have in your system, I would suggest either adding a new field on your users table or creating a new hedera_accounts table for a one-to-many relationship.
In this case I am using a simple approach that a user can only have one hedera account, thus three new fields have been added to a user migration.
    encrypted_key
    public_key
    hedera_id

Imports

1
use Trustenterprises\LaravelHashgraph\LaravelHashgraph;
2
use Trustenterprises\LaravelHashgraph\Models\AccountCreateResponse;
Copied!

Code

1
$account = LaravelHashgraph::createAccount(); // Returns AccountCreateResponse
2
​
3
// Get the Authorised user from a controller.
4
$user = \Auth::user();
5
​
6
// Update the fields of the user.
7
$user->encrypted_id = $account->getEncryptedKey();
8
$user->public_key = $account->getPublicKey();
9
$user->hedera_id = $account->getAccountId();
10
​
11
// Persist to storage.
12
$user->save();
Copied!

Creating a token

Create a token that can be sent to a user's account after an event or a purchase.
Recommend that you create a new table to hold the details of a minted token. Use the returned tokenId at the primary key.

Imports

1
use Trustenterprises\LaravelHashgraph\LaravelHashgraph;
2
use Trustenterprises\LaravelHashgraph\Models\FungibleTokenResponse;
3
use Trustenterprises\LaravelHashgraph\Models\FungibleToken;
Copied!

Code

1
// The base token object.
2
$fungible_token = new FungibleToken('MATT', 'MATTHEW', 10, 'This is a memo');
3
​
4
// The response from hedera, via the API
5
$token = LaravelHashgraph::mintFungibleToken($fungible_token);
6
​
7
// Store this id against the details of the above object.
8
$token->getTokenId();
Copied!

Bequesting a token

Bequesting or sending a token to an account that has been generated, and linked to a user in your local database. This bypasses hedera's default association behaviour.
This is the magic element that provides a permissioned marketplace, the ability to send tokens of any asset to a user from any event.

Imports

1
use Trustenterprises\LaravelHashgraph\LaravelHashgraph;
2
use Trustenterprises\LaravelHashgraph\Models\BequestToken;
3
use Trustenterprises\LaravelHashgraph\Models\BequestTokenResponse;
Copied!

Code

1
// Assume the user and token from the previous steps.
2
$token_id = $token->getTokenId();
3
​
4
$encrypted_key = $user->encrypted_key;
5
$public_key = $user->public_key;
6
$hedera_id = $user->hedera_id;
7
​
8
$amount = 1;
9
​
10
// The object to bequest tokens to an account
11
$bequest = new BequestToken($encrypted_key, $token_id, $hedera_id, $amount);
12
​
13
// The response from hedera, to send tokens
14
$response = LaravelHashgraph::bequestToken($bequest);
15
​
16
// This transaction id can be tracked on a hedera explorer such as dragonglass.
17
$response->getTransactionId();
18
​
Copied!
Last modified 8d ago