From 600b1138d51ecec9be224b3e6f34552c6f234c6b Mon Sep 17 00:00:00 2001 From: Matt Smithies Date: Thu, 29 Sep 2022 15:34:08 +0100 Subject: [PATCH 1/4] Added support for external decimals of tokens --- src/Http/Client/HashgraphClient.php | 12 ++++++++++-- src/LaravelHashgraph.php | 4 ++-- src/Models/BequestToken.php | 26 +++++++++++++++++++++++++- src/Models/FungibleToken.php | 26 ++++++++++++++++++++++++-- src/Models/SendToken.php | 26 +++++++++++++++++++++++++- 5 files changed, 86 insertions(+), 8 deletions(-) diff --git a/src/Http/Client/HashgraphClient.php b/src/Http/Client/HashgraphClient.php index c4e8aef..ee50836 100644 --- a/src/Http/Client/HashgraphClient.php +++ b/src/Http/Client/HashgraphClient.php @@ -186,12 +186,20 @@ public function bequestToken(BequestToken $bequestToken): BequestTokenResponse /** * @param string $account_id * @param string $token_id + * @param int|null $decimals * @return AccountTokenBalanceResponse * @throws GuzzleException */ - public function getTokenBalance(string $account_id, string $token_id): AccountTokenBalanceResponse + public function getTokenBalance(string $account_id, string $token_id, ?int $decimals = null): AccountTokenBalanceResponse { - $response = $this->guzzle->get('api/account/' . $account_id . '/' . $token_id); + $uri = 'api/account/' . $account_id . '/' . $token_id; + + // Explicitly append decimal param. + if ($decimals) { + $uri .= '?decimals=' . $decimals; + } + + $response = $this->guzzle->get($uri); $data = json_decode($response->getBody()->getContents())->data; diff --git a/src/LaravelHashgraph.php b/src/LaravelHashgraph.php index 8b3021e..22497f2 100644 --- a/src/LaravelHashgraph.php +++ b/src/LaravelHashgraph.php @@ -160,9 +160,9 @@ public static function createAccount() : AccountCreateResponse return static::withAuthenticatedClient()->getClient()->createAccount(); } - public static function getTokenBalance(string $account_id, string $token_id) : AccountTokenBalanceResponse + public static function getTokenBalance(string $account_id, string $token_id, ?int $decimals = null) : AccountTokenBalanceResponse { - return static::withAuthenticatedClient()->getClient()->getTokenBalance($account_id, $token_id); + return static::withAuthenticatedClient()->getClient()->getTokenBalance($account_id, $token_id, $decimals); } public static function checkTokenHoldings(string $account_id, string $token_id) : AccountHoldingsResponse diff --git a/src/Models/BequestToken.php b/src/Models/BequestToken.php index ec71ac6..038dab7 100644 --- a/src/Models/BequestToken.php +++ b/src/Models/BequestToken.php @@ -12,6 +12,8 @@ class BequestToken private String $amount; + private ?int $decimals = null; + /** * BequestToken constructor. * @param String $encrypted_receiver_key @@ -29,12 +31,18 @@ public function __construct(string $encrypted_receiver_key, string $token_id, st public function forRequest(): array { - return [ + $payload = [ 'encrypted_receiver_key' => $this->getEncryptedReceiverKey(), 'token_id' => $this->getTokenId(), 'receiver_id' => $this->getReceiverId(), 'amount' => $this->getAmount(), ]; + + if ($this->getDecimals()) { + $payload['decimals'] = $this->getDecimals(); + } + + return $payload; } /** @@ -68,4 +76,20 @@ public function getAmount(): string { return $this->amount; } + + /** + * @return int|null + */ + public function getDecimals(): ?int + { + return $this->decimals; + } + + /** + * @param int|null $decimals + */ + public function setDecimals(?int $decimals): void + { + $this->decimals = $decimals; + } } diff --git a/src/Models/FungibleToken.php b/src/Models/FungibleToken.php index 508ba6c..98412db 100644 --- a/src/Models/FungibleToken.php +++ b/src/Models/FungibleToken.php @@ -13,6 +13,8 @@ class FungibleToken private String $memo; + private ?int $decimals = null; + /** * FungibleToken constructor. * @param String $symbol @@ -30,14 +32,18 @@ public function __construct(string $symbol, string $name, string $supply, string public function forTokenRequest(): array { - $token_payload = [ + $payload = [ 'memo' => $this->getMemo(), 'symbol' => $this->getSymbol(), 'name' => $this->getName(), 'supply' => $this->getSupply(), ]; - return $token_payload; + if ($this->getDecimals()) { + $payload['decimals'] = $this->getDecimals(); + } + + return $payload; } /** @@ -103,4 +109,20 @@ public function setMemo(string $memo): void { $this->memo = $memo; } + + /** + * @return int|null + */ + public function getDecimals(): ?int + { + return $this->decimals; + } + + /** + * @param int|null $decimals + */ + public function setDecimals(?int $decimals): void + { + $this->decimals = $decimals; + } } diff --git a/src/Models/SendToken.php b/src/Models/SendToken.php index 87226c9..0e60baa 100644 --- a/src/Models/SendToken.php +++ b/src/Models/SendToken.php @@ -10,6 +10,8 @@ class SendToken private String $amount; + private ?int $decimals = null; + /** * SendToken constructor. * @@ -26,11 +28,17 @@ public function __construct(string $token_id, string $receiver_id, string $amoun public function forRequest(): array { - return [ + $payload = [ 'token_id' => $this->getTokenId(), 'receiver_id' => $this->getReceiverId(), 'amount' => $this->getAmount(), ]; + + if ($this->getDecimals()) { + $payload['decimals'] = $this->getDecimals(); + } + + return $payload; } /** @@ -56,4 +64,20 @@ public function getAmount(): string { return $this->amount; } + + /** + * @return int|null + */ + public function getDecimals(): ?int + { + return $this->decimals; + } + + /** + * @param int $decimals + */ + public function setDecimals(int $decimals): void + { + $this->decimals = $decimals; + } } From 2f5b3fb51e70377b7a7874b3f525327a542c44b4 Mon Sep 17 00:00:00 2001 From: Matt Smithies Date: Thu, 29 Sep 2022 15:34:17 +0100 Subject: [PATCH 2/4] tests for decimals --- tests/LaravelHashgraphTest.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/LaravelHashgraphTest.php b/tests/LaravelHashgraphTest.php index 8289aa7..0c4f3e7 100644 --- a/tests/LaravelHashgraphTest.php +++ b/tests/LaravelHashgraphTest.php @@ -148,6 +148,38 @@ public function e2e_create_account_and_bequest() $this->assertNotNull($bequest_response->getTransactionId()); } + /** + * Mint a fungible token with decimals and check correct balance + * + * @test + */ + public function e2e_create_decimal_token_send_to_account() + { + // Use decimals for token and transfer + $decimals = 2; + + // Create an account + $account = LaravelHashgraph::createAccount(); + + // Create a token + $token = new FungibleToken("e2e", "e2e decimal token test", "10", "e2e bequest token test"); + + // Ensure decimals + $token->setDecimals($decimals); + + $hashgraph_token = LaravelHashgraph::mintFungibleToken($token); + + // Send the bequest to a user + $bequest_token = new BequestToken($account->getEncryptedKey(), $hashgraph_token->getTokenId(), $account->getAccountId(), 1); + + // Ensure decimal bequest + $bequest_token->setDecimals($decimals); + + $bequest_response = LaravelHashgraph::bequestToken($bequest_token); + + $this->assertEquals(1, (int) $bequest_response->getAmount()); + } + /** * @test */ @@ -242,4 +274,5 @@ public function failed_send_tokens_to_a_full_venly_wallet() $this->assertFalse($token_sent->hasTransferSucceeded()); } + } From e282804be1f0ccc698ccb178a24d1ccb2a1637d0 Mon Sep 17 00:00:00 2001 From: Matt Smithies Date: Thu, 29 Sep 2022 15:39:14 +0100 Subject: [PATCH 3/4] Update LaravelHashgraphTest.php --- tests/LaravelHashgraphTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/LaravelHashgraphTest.php b/tests/LaravelHashgraphTest.php index 0c4f3e7..6cf4387 100644 --- a/tests/LaravelHashgraphTest.php +++ b/tests/LaravelHashgraphTest.php @@ -168,7 +168,7 @@ public function e2e_create_decimal_token_send_to_account() $token->setDecimals($decimals); $hashgraph_token = LaravelHashgraph::mintFungibleToken($token); - + // Send the bequest to a user $bequest_token = new BequestToken($account->getEncryptedKey(), $hashgraph_token->getTokenId(), $account->getAccountId(), 1); From 29c17398692d534cd7952a66b3ed3959e06bf9fe Mon Sep 17 00:00:00 2001 From: Matt Smithies Date: Thu, 29 Sep 2022 15:40:40 +0100 Subject: [PATCH 4/4] Update psalm.yml --- .github/workflows/psalm.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/psalm.yml b/.github/workflows/psalm.yml index ec1935b..b7a426e 100644 --- a/.github/workflows/psalm.yml +++ b/.github/workflows/psalm.yml @@ -16,7 +16,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '7.4' + php-version: '8.1' extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick coverage: none