Skip to content

Commit

Permalink
Merge pull request #15 from trustenterprises/feature-support-explicit…
Browse files Browse the repository at this point in the history
…-decimal

Support explicit token decimals
  • Loading branch information
mattsmithies authored Sep 29, 2022
2 parents a747bf6 + 29c1739 commit 3477f93
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/psalm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 10 additions & 2 deletions src/Http/Client/HashgraphClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/LaravelHashgraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 25 additions & 1 deletion src/Models/BequestToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class BequestToken

private String $amount;

private ?int $decimals = null;

/**
* BequestToken constructor.
* @param String $encrypted_receiver_key
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}
}
26 changes: 24 additions & 2 deletions src/Models/FungibleToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class FungibleToken

private String $memo;

private ?int $decimals = null;

/**
* FungibleToken constructor.
* @param String $symbol
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}
}
26 changes: 25 additions & 1 deletion src/Models/SendToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class SendToken

private String $amount;

private ?int $decimals = null;

/**
* SendToken constructor.
*
Expand All @@ -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;
}

/**
Expand All @@ -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;
}
}
33 changes: 33 additions & 0 deletions tests/LaravelHashgraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -242,4 +274,5 @@ public function failed_send_tokens_to_a_full_venly_wallet()

$this->assertFalse($token_sent->hasTransferSucceeded());
}

}

0 comments on commit 3477f93

Please sign in to comment.