Skip to content

Commit

Permalink
Merge pull request #16 from trustenterprises/feature/laravel-batch-in…
Browse files Browse the repository at this point in the history
…tegration

Laravel batch NFT integration
  • Loading branch information
mattsmithies authored Nov 17, 2022
2 parents 3477f93 + c1f9188 commit 05062ba
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 5 deletions.
17 changes: 17 additions & 0 deletions src/Http/Client/HashgraphClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Trustenterprises\LaravelHashgraph\Models\AccountTokenBalanceResponse;
use Trustenterprises\LaravelHashgraph\Models\BequestToken;
use Trustenterprises\LaravelHashgraph\Models\BequestTokenResponse;
use Trustenterprises\LaravelHashgraph\Models\NFT\BatchTransferNft;
use Trustenterprises\LaravelHashgraph\Models\NFT\BatchTransferNftResponse;
use Trustenterprises\LaravelHashgraph\Models\NFT\ClaimNft;
use Trustenterprises\LaravelHashgraph\Models\NFT\ClaimNftResponse;
use Trustenterprises\LaravelHashgraph\Models\NFT\MintToken;
Expand Down Expand Up @@ -310,6 +312,21 @@ public function transferNft(TransferNft $transferNft): TransferNftResponse {
return new TransferNftResponse($data);
}

/**
* Transfer a batch of NFTs from a treasury to a user.
*
* @param BatchTransferNft $transferNft
* @return BatchTransferNftResponse
* @throws GuzzleException
*/
public function batchTransferNft(BatchTransferNft $transferNft): BatchTransferNftResponse {
$response = $this->guzzle->post('api/nft/batch', $transferNft->forRequest());

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

return new BatchTransferNftResponse($data);
}

/**
* @param ClaimNft $claimNft
* @return ClaimNftResponse
Expand Down
12 changes: 12 additions & 0 deletions src/LaravelHashgraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Trustenterprises\LaravelHashgraph\Models\AccountTokenBalanceResponse;
use Trustenterprises\LaravelHashgraph\Models\BequestToken;
use Trustenterprises\LaravelHashgraph\Models\BequestTokenResponse;
use Trustenterprises\LaravelHashgraph\Models\NFT\BatchTransferNft;
use Trustenterprises\LaravelHashgraph\Models\NFT\BatchTransferNftResponse;
use Trustenterprises\LaravelHashgraph\Models\NFT\ClaimNft;
use Trustenterprises\LaravelHashgraph\Models\NFT\ClaimNftResponse;
use Trustenterprises\LaravelHashgraph\Models\NFT\MintToken;
Expand Down Expand Up @@ -129,6 +131,16 @@ public static function transferNonFungibleToken(TransferNft $transferNft) : Tran
return static::withAuthenticatedClient()->getClient()->transferNft($transferNft);
}

/**
* Batch transfer an NFT to a user
*
* @throws GuzzleException
*/
public static function batchTransferNonFungibleToken(BatchTransferNft $batchTransferNft) : BatchTransferNftResponse
{
return static::withAuthenticatedClient()->getClient()->batchTransferNft($batchTransferNft);
}

/**
* A user can claim an NFT if they hold the correct pass
*
Expand Down
63 changes: 63 additions & 0 deletions src/Models/NFT/BatchTransferNft.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Trustenterprises\LaravelHashgraph\Models\NFT;

use GuzzleHttp\RequestOptions;

class BatchTransferNft
{
private String $token_id;

private String $receiver_id;

private int $amount;

/**
* SendToken constructor.
*
* @param String $token_id
* @param String $receiver_id
* @param int $amount
*/
public function __construct(string $token_id, string $receiver_id, int $amount)
{
$this->token_id = $token_id;
$this->receiver_id = $receiver_id;
$this->amount = $amount;
}

public function forRequest(): array
{
return [
RequestOptions::JSON => [
'token_id' => $this->getTokenId(),
'receiver_id' => $this->getReceiverId(),
'amount' => $this->getAmount(),
]
];
}

/**
* @return string
*/
public function getTokenId(): string
{
return $this->token_id;
}

/**
* @return string
*/
public function getReceiverId(): string
{
return $this->receiver_id;
}

/**
* @return int
*/
public function getAmount(): int
{
return $this->amount;
}
}
83 changes: 83 additions & 0 deletions src/Models/NFT/BatchTransferNftResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Trustenterprises\LaravelHashgraph\Models\NFT;

class BatchTransferNftResponse
{
private array $results;

private int $expected;

private int $actual_sent;

private array $errors = [];

private bool $transfer_success = false;

/**
* @param object $response
*/
public function __construct(object $response)
{
if (property_exists($response->data, 'errors')) {
$this->errors = $response->data->errors;
} else {
$this->transfer_success = true;
$this->results = $response->data->results;
$this->expected = $response->data->expected;
$this->actual_sent = $response->data->actual_sent;
}
}

/**
* @return bool
*/
public function hasTransferSucceeded(): bool
{
return $this->transfer_success;
}

/**
* @return array
*/
public function getErrors(): array
{
return $this->errors;
}

/**
* @return array
*/
public function getResults(): array
{
return $this->results;
}

/**
* @return int
*/
public function getExpected(): int
{
return $this->expected;
}

/**
* @return int
*/
public function getActualSent(): int
{
return $this->actual_sent;
}

/**
* @return bool
*/
public function isTransferSuccess(): bool
{
return $this->transfer_success;
}

public function isSuccessful(): bool {
return count($this->errors) == 0;
}
}
8 changes: 4 additions & 4 deletions src/Models/NFT/NonFungibleToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,17 @@ public function setName(string $name): void
}

/**
* @return String
* @return Int
*/
public function getSupply(): string
public function getSupply(): Int
{
return $this->supply;
}

/**
* @param String $supply
* @param Int $supply
*/
public function setSupply(string $supply): void
public function setSupply(int $supply): void
{
$this->supply = $supply;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Utilities/Hmac.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Hmac
* Generate a HMAC from request content to validate that a payload matches with a x-signature
*
* @link https://github.com/symfony/http-foundation/blob/master/Request.php#L1535 Documentation of Foo.
* @var string|resource|false|null
* @param $content
* @return string
*/
public static function generate($content): string
Expand Down
58 changes: 58 additions & 0 deletions tests/HashgraphBatchNftTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Trustenterprises\LaravelHashgraph\Tests;

use Trustenterprises\LaravelHashgraph\LaravelHashgraph;
use Trustenterprises\LaravelHashgraph\Models\NFT\BatchTransferNft;
use Trustenterprises\LaravelHashgraph\Models\NFT\ClaimNft;
use Trustenterprises\LaravelHashgraph\Models\NFT\MintToken;
use Trustenterprises\LaravelHashgraph\Models\NFT\NftMetadata;
use Trustenterprises\LaravelHashgraph\Models\NFT\NonFungibleToken;
use Trustenterprises\LaravelHashgraph\Models\NFT\TransferNft;

class HashgraphBatchNftTest extends TestCase
{
/**
* Attempt to send a pre-minted NFT
*
* @test-ignore
*/
public function attempt_batch_transfer()
{

$account = LaravelHashgraph::createAccount();

// Mirrornode ain't real time...
$nft_id = '0.0.48905313';
$batch_amount = 19;

$batch = new BatchTransferNft($nft_id, $account->getAccountId(), $batch_amount);

$result = LaravelHashgraph::batchTransferNonFungibleToken($batch);

$this->assertEquals($batch_amount, $result->getActualSent());
$this->assertEquals($batch_amount, $result->getExpected());
}

/**
* Attempt to send to many NFTs
*
* @test
*/
public function attempt_failed_too_many_batch_transfer()
{
$account = LaravelHashgraph::createAccount();

// Mirrornode ain't real time...
$nft_id = '0.0.48905313';
$batch_amount = 1009;

$batch = new BatchTransferNft($nft_id, $account->getAccountId(), $batch_amount);

$result = LaravelHashgraph::batchTransferNonFungibleToken($batch);

$this->assertEquals('The treasury does not hold the amount of NFTs of id ' . $nft_id . ' to do the required batch transfer', $result->getErrors()[0]);
}


}

0 comments on commit 05062ba

Please sign in to comment.