Skip to content

Commit

Permalink
Merge pull request #17 from trustenterprises/feature/inscriptions-php
Browse files Browse the repository at this point in the history
Inscription Implementation for Laravel/PHP
  • Loading branch information
mattsmithies authored Jan 7, 2024
2 parents 05062ba + 088362a commit 4e48377
Show file tree
Hide file tree
Showing 15 changed files with 940 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Commands/LaravelHashgraphCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class LaravelHashgraphCommand extends Command

public $description = 'My command';

public function handle()
public function handle(): void
{
// LaravelHashgraphFacade::
// error_log($client->getBalance());
Expand Down
37 changes: 37 additions & 0 deletions src/Contracts/InscriptionMethodInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Trustenterprises\LaravelHashgraph\Contracts;

use Trustenterprises\LaravelHashgraph\Models\Inscriptions\BurnInscription;
use Trustenterprises\LaravelHashgraph\Models\Inscriptions\DeployInscription;
use Trustenterprises\LaravelHashgraph\Models\Inscriptions\MintInscription;
use Trustenterprises\LaravelHashgraph\Models\Inscriptions\InscriptionResponse;
use Trustenterprises\LaravelHashgraph\Models\Inscriptions\TransferInscription;

interface InscriptionMethodInterface
{
/**
* @param DeployInscription $request
*
* @return InscriptionResponse
*/
public function deployInscription(DeployInscription $request): InscriptionResponse;

/**
* @param MintInscription $request
* @return InscriptionResponse
*/
public function mintInscription(MintInscription $request): InscriptionResponse;

/**
* @param BurnInscription $request
* @return InscriptionResponse
*/
public function burnInscription(BurnInscription $request): InscriptionResponse;

/**
* @param TransferInscription $request
* @return InscriptionResponse
*/
public function transferInscription(TransferInscription $request): InscriptionResponse;
}
49 changes: 48 additions & 1 deletion src/Http/Client/HashgraphClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Trustenterprises\LaravelHashgraph\Contracts\HashgraphConsensus;
use Trustenterprises\LaravelHashgraph\Contracts\InscriptionMethodInterface;
use Trustenterprises\LaravelHashgraph\Models\AccountCreateResponse;
use Trustenterprises\LaravelHashgraph\Models\AccountHoldingsResponse;
use Trustenterprises\LaravelHashgraph\Models\AccountTokenBalanceResponse;
use Trustenterprises\LaravelHashgraph\Models\BequestToken;
use Trustenterprises\LaravelHashgraph\Models\BequestTokenResponse;
use Trustenterprises\LaravelHashgraph\Models\Inscriptions\BurnInscription;
use Trustenterprises\LaravelHashgraph\Models\Inscriptions\DeployInscription;
use Trustenterprises\LaravelHashgraph\Models\Inscriptions\MintInscription;
use Trustenterprises\LaravelHashgraph\Models\Inscriptions\InscriptionResponse;
use Trustenterprises\LaravelHashgraph\Models\Inscriptions\TransferInscription;
use Trustenterprises\LaravelHashgraph\Models\NFT\BatchTransferNft;
use Trustenterprises\LaravelHashgraph\Models\NFT\BatchTransferNftResponse;
use Trustenterprises\LaravelHashgraph\Models\NFT\ClaimNft;
Expand All @@ -35,7 +41,7 @@
* Class ServerlessHashgraphClient
* @package Trustenterprises\LaravelHashgraph\Http\Client
*/
class HashgraphClient implements HashgraphConsensus
class HashgraphClient implements HashgraphConsensus, InscriptionMethodInterface
{
/**
* @var Client
Expand Down Expand Up @@ -339,4 +345,45 @@ public function claimNft(ClaimNft $claimNft): ClaimNftResponse {

return new ClaimNftResponse($data);
}

/**
* HCS20 Inscription Methods
*
* @param DeployInscription $request
*/
public function deployInscription(DeployInscription $request): InscriptionResponse
{
$response = $this->guzzle->post('api/inscription/deploy', $request->forRequest());

$data = json_decode($response->getBody()->getContents());

return new InscriptionResponse($data);
}

public function mintInscription(MintInscription $request): InscriptionResponse
{
$response = $this->guzzle->post("api/inscription/{$request->getTicker()}/mint", $request->forRequest());

$data = json_decode($response->getBody()->getContents());

return new InscriptionResponse($data);
}

public function burnInscription(BurnInscription $request): InscriptionResponse
{
$response = $this->guzzle->post("api/inscription/{$request->getTicker()}/burn", $request->forRequest());

$data = json_decode($response->getBody()->getContents());

return new InscriptionResponse($data);
}

public function transferInscription(TransferInscription $request): InscriptionResponse
{
$response = $this->guzzle->post("api/inscription/{$request->getTicker()}/transfer", $request->forRequest());

$data = json_decode($response->getBody()->getContents());

return new InscriptionResponse($data);
}
}
48 changes: 47 additions & 1 deletion src/LaravelHashgraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Trustenterprises\LaravelHashgraph;

use GuzzleHttp\Exception\GuzzleException;
use Trustenterprises\LaravelHashgraph\Contracts\InscriptionMethodInterface;
use Trustenterprises\LaravelHashgraph\Contracts\HashgraphConsensus;
use Trustenterprises\LaravelHashgraph\Events\TopicWasCreated;
use Trustenterprises\LaravelHashgraph\Exception\HashgraphException;
Expand All @@ -12,6 +13,11 @@
use Trustenterprises\LaravelHashgraph\Models\AccountTokenBalanceResponse;
use Trustenterprises\LaravelHashgraph\Models\BequestToken;
use Trustenterprises\LaravelHashgraph\Models\BequestTokenResponse;
use Trustenterprises\LaravelHashgraph\Models\Inscriptions\BurnInscription;
use Trustenterprises\LaravelHashgraph\Models\Inscriptions\DeployInscription;
use Trustenterprises\LaravelHashgraph\Models\Inscriptions\MintInscription;
use Trustenterprises\LaravelHashgraph\Models\Inscriptions\InscriptionResponse;
use Trustenterprises\LaravelHashgraph\Models\Inscriptions\TransferInscription;
use Trustenterprises\LaravelHashgraph\Models\NFT\BatchTransferNft;
use Trustenterprises\LaravelHashgraph\Models\NFT\BatchTransferNftResponse;
use Trustenterprises\LaravelHashgraph\Models\NFT\ClaimNft;
Expand Down Expand Up @@ -151,6 +157,46 @@ public static function claimNonFungibleToken(ClaimNft $claimNft) : ClaimNftRespo
return static::withAuthenticatedClient()->getClient()->claimNft($claimNft);
}

/**
* Deploy a new inscription
*
* @throws GuzzleException
*/
public static function deployInscription(DeployInscription $deployInscription) : InscriptionResponse
{
return static::withAuthenticatedClient()->getClient()->deployInscription($deployInscription);
}

/**
* Mint a new inscription
*
* @throws GuzzleException
*/
public static function mintInscription(MintInscription $mintInscription) : InscriptionResponse
{
return static::withAuthenticatedClient()->getClient()->mintInscription($mintInscription);
}

/**
* Burn a inscription
*
* @throws GuzzleException
*/
public static function burnInscription(BurnInscription $burnInscription) : InscriptionResponse
{
return static::withAuthenticatedClient()->getClient()->burnInscription($burnInscription);
}

/**
* Transfer a inscription
*
* @throws GuzzleException
*/
public static function transferInscription(TransferInscription $transferInscription) : InscriptionResponse
{
return static::withAuthenticatedClient()->getClient()->transferInscription($transferInscription);
}

/**
* Set the topic key for the transactions.
*
Expand Down Expand Up @@ -292,7 +338,7 @@ public function setTopic(string $topic_name): self
/**
* @return HashgraphClient
*/
public function getClient(): HashgraphConsensus
public function getClient(): InscriptionMethodInterface
{
return $this->client;
}
Expand Down
2 changes: 1 addition & 1 deletion src/LaravelHashgraphServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class LaravelHashgraphServiceProvider extends ServiceProvider
{
const HASHGRAPH_SERVICE_NAME = 'hashgraph_trust';

public function boot()
public function boot(): void
{
if ($this->app->runningInConsole()) {
$this->publishes([
Expand Down
2 changes: 1 addition & 1 deletion src/Listeners/UpdateExplorerUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class UpdateExplorerUrl
{
public function handle(ConsensusMessageWasReceived $event)
public function handle(ConsensusMessageWasReceived $event): void
{
$event->consensus_message->update([
'explorer_url' => 'https://trust.enterprises/',
Expand Down
130 changes: 130 additions & 0 deletions src/Models/Inscriptions/BurnInscription.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

namespace Trustenterprises\LaravelHashgraph\Models\Inscriptions;

use GuzzleHttp\RequestOptions;

class BurnInscription
{
private String $from;

private int $amount;

private String $ticker;

private ?String $memo = null;

private ?String $topic_id = null;

/**
* FungibleToken constructor.
* @param string $ticker
* @param string $from
* @param int $amount
*/
public function __construct(string $ticker, string $from, int $amount)
{
$this->ticker = $ticker;
$this->from = $from;
$this->amount = $amount;
}

public function forRequest(): array
{
$payload = [
'from' => $this->getFrom(),
'amount' => $this->getAmount(),
];

if ($this->getMemo()) {
$payload['memo'] = $this->getMemo();
}

if ($this->getPrivateTopicId()) {
$payload['topic_id'] = $this->getPrivateTopicId();
}

return [ RequestOptions::JSON => $payload ];
}

/**
* @return String|string
*/
public function getTicker()
{
return $this->ticker;
}

/**
* @param String|string $ticker
*/
public function setTicker($ticker): void
{
$this->ticker = $ticker;
}

/**
* @return null|string
*/
public function getMemo(): string|null
{
return $this->memo;
}

/**
* @param String $memo
*/
public function setMemo($memo): void
{
$this->memo = $memo;
}

/**
* @return null|string
*/
public function getPrivateTopicId(): string|null
{
return $this->topic_id;
}

/**
* @param String $topic_id
*/
public function setPrivateTopic($topic_id): void
{
$this->topic_id = $topic_id;
}

/**
* @return string|string
*/
public function getFrom()
{
return $this->from;
}

/**
* @param string|string $from
*/
public function setFrom($from): void
{
$this->from = $from;
}

/**
* @return int|int
*/
public function getAmount()
{
return $this->amount;
}

/**
* @param int|int $amount
*/
public function setAmount($amount): void
{
$this->amount = $amount;
}
}

Loading

0 comments on commit 4e48377

Please sign in to comment.