From 008179d396aac2cb98e5d60f2220d51f2aad99fe Mon Sep 17 00:00:00 2001 From: Matt Smithies Date: Sun, 7 Jan 2024 21:17:06 +0000 Subject: [PATCH] Initial pass inscriptions for Laravel --- src/Contracts/InscriptionMethodInterface.php | 36 ++++ src/Http/Client/HashgraphClient.php | 48 ++++- src/LaravelHashgraph.php | 48 ++++- src/Models/Inscriptions/BurnInscription.php | 130 +++++++++++++ src/Models/Inscriptions/DeployInscription.php | 175 ++++++++++++++++++ .../Inscriptions/InscriptionResponse.php | 102 ++++++++++ src/Models/Inscriptions/MintInscription.php | 130 +++++++++++++ .../Inscriptions/TransferInscription.php | 151 +++++++++++++++ tests/HashgraphInscriptionTest.php | 117 ++++++++++++ 9 files changed, 935 insertions(+), 2 deletions(-) create mode 100644 src/Contracts/InscriptionMethodInterface.php create mode 100644 src/Models/Inscriptions/BurnInscription.php create mode 100644 src/Models/Inscriptions/DeployInscription.php create mode 100644 src/Models/Inscriptions/InscriptionResponse.php create mode 100644 src/Models/Inscriptions/MintInscription.php create mode 100644 src/Models/Inscriptions/TransferInscription.php create mode 100644 tests/HashgraphInscriptionTest.php diff --git a/src/Contracts/InscriptionMethodInterface.php b/src/Contracts/InscriptionMethodInterface.php new file mode 100644 index 0000000..5c3ba12 --- /dev/null +++ b/src/Contracts/InscriptionMethodInterface.php @@ -0,0 +1,36 @@ +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); + } } diff --git a/src/LaravelHashgraph.php b/src/LaravelHashgraph.php index eed8226..ca5b002 100644 --- a/src/LaravelHashgraph.php +++ b/src/LaravelHashgraph.php @@ -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; @@ -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; @@ -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. * @@ -292,7 +338,7 @@ public function setTopic(string $topic_name): self /** * @return HashgraphClient */ - public function getClient(): HashgraphConsensus + public function getClient(): InscriptionMethodInterface { return $this->client; } diff --git a/src/Models/Inscriptions/BurnInscription.php b/src/Models/Inscriptions/BurnInscription.php new file mode 100644 index 0000000..a88daae --- /dev/null +++ b/src/Models/Inscriptions/BurnInscription.php @@ -0,0 +1,130 @@ +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) + { + $this->ticker = $ticker; + } + + /** + * @return String + */ + public function getMemo() + { + return $this->memo; + } + + /** + * @param String $memo + */ + public function setMemo($memo) + { + $this->memo = $memo; + } + + /** + * @return String + */ + public function getPrivateTopicId() + { + return $this->topic_id; + } + + /** + * @param String $topic_id + */ + public function setPrivateTopic($topic_id) + { + $this->topic_id = $topic_id; + } + + /** + * @return string|string + */ + public function getFrom() + { + return $this->from; + } + + /** + * @param string|string $from + */ + public function setFrom($from) + { + $this->from = $from; + } + + /** + * @return int|int + */ + public function getAmount() + { + return $this->amount; + } + + /** + * @param int|int $amount + */ + public function setAmount($amount) + { + $this->amount = $amount; + } +} + diff --git a/src/Models/Inscriptions/DeployInscription.php b/src/Models/Inscriptions/DeployInscription.php new file mode 100644 index 0000000..f6b8892 --- /dev/null +++ b/src/Models/Inscriptions/DeployInscription.php @@ -0,0 +1,175 @@ +ticker = $ticker; + $this->name = $name; + $this->max = $max; + } + + public function forRequest(): array + { + $payload = [ + 'ticker' => $this->getTicker(), + 'name' => $this->getName(), + 'max' => $this->getMax(), + ]; + + if ($this->getLimit()) { + $payload['limit'] = $this->getLimit(); + } + + if ($this->getMemo()) { + $payload['memo'] = $this->getMemo(); + } + + if ($this->getMetadata()) { + $payload['metadata'] = $this->getMetadata(); + } + + if ($this->getPrivateTopicId()) { + $payload['topic_id'] = $this->getPrivateTopicId(); + } + + return [ RequestOptions::JSON => $payload ]; + } + + /** + * @return String|string + */ + public function getName() + { + return $this->name; + } + + /** + * @param String|string $name + */ + public function setName($name) + { + $this->name = $name; + } + + /** + * @return String|string + */ + public function getTicker() + { + return $this->ticker; + } + + /** + * @param String|string $ticker + */ + public function setTicker($ticker) + { + $this->ticker = $ticker; + } + + /** + * @return int|Integer + */ + public function getMax() + { + return $this->max; + } + + /** + * @param int|Integer $max + */ + public function setMax($max) + { + $this->max = $max; + } + + /** + * @return int + */ + public function getLimit() + { + return $this->limit; + } + + /** + * @param int $limit + */ + public function setLimit($limit) + { + $this->limit = $limit; + } + + /** + * @return String + */ + public function getMetadata() + { + return $this->metadata; + } + + /** + * @param String $metadata + */ + public function setMetadata($metadata) + { + $this->metadata = $metadata; + } + + /** + * @return String + */ + public function getMemo() + { + return $this->memo; + } + + /** + * @param String $memo + */ + public function setMemo($memo) + { + $this->memo = $memo; + } + + /** + * @return String + */ + public function getPrivateTopicId() + { + return $this->topic_id; + } + + /** + * @param String $topic_id + */ + public function setPrivateTopic($topic_id) + { + $this->topic_id = $topic_id; + } +} + diff --git a/src/Models/Inscriptions/InscriptionResponse.php b/src/Models/Inscriptions/InscriptionResponse.php new file mode 100644 index 0000000..6568583 --- /dev/null +++ b/src/Models/Inscriptions/InscriptionResponse.php @@ -0,0 +1,102 @@ +errors = $response->errors; + } else { + $this->transfer_success = true; + $this->topic_id = $response->data->topic_id; + $this->transaction_id = $response->data->transaction_id; + $this->explorer_url = $response->data->explorer_url; + $this->inscription = (object) $response->data->inscription; + } + } + + /** + * @return bool + */ + public function hasSucceeded(): bool + { + return $this->transfer_success; + } + + /** + * @return String + */ + public function getTopicId(): String + { + return $this->topic_id; + } + + /** + * @return String + */ + public function getTransactionId(): string + { + return $this->transaction_id; + } + + /** + * @return String + */ + public function getExplorerUrl(): string + { + return $this->explorer_url; + } + + /** + * @return object + */ + public function getInscription(): object + { + return $this->inscription; + } + + /** + * @return array + */ + public function getErrors(): array + { + return $this->errors; + } +} diff --git a/src/Models/Inscriptions/MintInscription.php b/src/Models/Inscriptions/MintInscription.php new file mode 100644 index 0000000..beae45d --- /dev/null +++ b/src/Models/Inscriptions/MintInscription.php @@ -0,0 +1,130 @@ +ticker = $ticker; + $this->to = $to; + $this->amount = $amount; + } + + public function forRequest(): array + { + $payload = [ + 'to' => $this->getTo(), + '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) + { + $this->ticker = $ticker; + } + + /** + * @return String + */ + public function getMemo() + { + return $this->memo; + } + + /** + * @param String $memo + */ + public function setMemo($memo) + { + $this->memo = $memo; + } + + /** + * @return String + */ + public function getPrivateTopicId() + { + return $this->topic_id; + } + + /** + * @param String $topic_id + */ + public function setPrivateTopic($topic_id) + { + $this->topic_id = $topic_id; + } + + /** + * @return string|string + */ + public function getTo() + { + return $this->to; + } + + /** + * @param string|string $to + */ + public function setTo($to) + { + $this->to = $to; + } + + /** + * @return int|int + */ + public function getAmount() + { + return $this->amount; + } + + /** + * @param int|int $amount + */ + public function setAmount($amount) + { + $this->amount = $amount; + } +} + diff --git a/src/Models/Inscriptions/TransferInscription.php b/src/Models/Inscriptions/TransferInscription.php new file mode 100644 index 0000000..3a9c594 --- /dev/null +++ b/src/Models/Inscriptions/TransferInscription.php @@ -0,0 +1,151 @@ +ticker = $ticker; + $this->from = $from; + $this->to = $to; + $this->amount = $amount; + } + + public function forRequest(): array + { + $payload = [ + 'from' => $this->getFrom(), + 'to' => $this->getTo(), + '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) + { + $this->ticker = $ticker; + } + + /** + * @return String + */ + public function getMemo() + { + return $this->memo; + } + + /** + * @param String $memo + */ + public function setMemo($memo) + { + $this->memo = $memo; + } + + /** + * @return String + */ + public function getPrivateTopicId() + { + return $this->topic_id; + } + + /** + * @param String $topic_id + */ + public function setPrivateTopic($topic_id) + { + $this->topic_id = $topic_id; + } + + /** + * @return string|string + */ + public function getFrom() + { + return $this->from; + } + + /** + * @param string|string $from + */ + public function setFrom($from) + { + $this->from = $from; + } + + /** + * @return string|string + */ + public function getTo() + { + return $this->to; + } + + /** + * @param string|string $to + */ + public function setTo($to) + { + $this->to = $to; + } + + /** + * @return int|int + */ + public function getAmount() + { + return $this->amount; + } + + /** + * @param int|int $amount + */ + public function setAmount($amount) + { + $this->amount = $amount; + } +} + diff --git a/tests/HashgraphInscriptionTest.php b/tests/HashgraphInscriptionTest.php new file mode 100644 index 0000000..8be7bcd --- /dev/null +++ b/tests/HashgraphInscriptionTest.php @@ -0,0 +1,117 @@ +setPrivateTopic(self::$TOPIC_ID); + + $minted = LaravelHashgraph::deployInscription($deploy); + + $inscription = $minted->getInscription(); + + $this->assertTrue($minted->hasSucceeded()); + + $this->assertEquals(300, $inscription->max); + $this->assertEquals('TICK1', $inscription->tick); + $this->assertEquals('Ticker', $inscription->name); + $this->assertEquals('deploy', $inscription->op); + $this->assertEquals('hcs-20', $inscription->p); + } + + /** + * Attempt to mint an inscription + * + * @test + */ + public function mint_an_inscription() + { + $mint = new MintInscription('TICK1', '0.0.1', 1); + + $mint->setPrivateTopic(self::$TOPIC_ID); + + $minted = LaravelHashgraph::mintInscription($mint); + + $inscription = $minted->getInscription(); + + $this->assertTrue($minted->hasSucceeded()); + + $this->assertEquals('TICK1', $inscription->tick); + $this->assertEquals('0.0.1', $inscription->to); + $this->assertEquals(1, $inscription->amt); + $this->assertEquals('mint', $inscription->op); + $this->assertEquals('hcs-20', $inscription->p); + } + + + /** + * Attempt to burn an inscription + * + * @test + */ + public function burn_an_inscription() + { + $burn = new BurnInscription('TICK1', '0.0.1', 1); + + $burn->setPrivateTopic(self::$TOPIC_ID); + + $minted = LaravelHashgraph::burnInscription($burn); + + $inscription = $minted->getInscription(); + + $this->assertTrue($minted->hasSucceeded()); + + $this->assertEquals('TICK1', $inscription->tick); + $this->assertEquals('0.0.1', $inscription->from); + $this->assertEquals(1, $inscription->amt); + $this->assertEquals('burn', $inscription->op); + $this->assertEquals('hcs-20', $inscription->p); + } + + /** + * Attempt to transfer an inscription + * + * @test + */ + public function transfer_a_inscription() + { + $transfer = new TransferInscription('TICK1', '0.0.1', '0.0.2', 1); + + $transfer->setPrivateTopic(self::$TOPIC_ID); + + $minted = LaravelHashgraph::transferInscription($transfer); + + $inscription = $minted->getInscription(); + + $this->assertTrue($minted->hasSucceeded()); + + $this->assertEquals('TICK1', $inscription->tick); + $this->assertEquals('0.0.1', $inscription->from); + $this->assertEquals('0.0.2', $inscription->to); + $this->assertEquals(1, $inscription->amt); + $this->assertEquals('transfer', $inscription->op); + $this->assertEquals('hcs-20', $inscription->p); + } +}