Skip to content

Commit

Permalink
feat(recipients): add claimed field to airdrops recipients
Browse files Browse the repository at this point in the history
  • Loading branch information
intranettus committed Jul 12, 2023
1 parent 60d30ce commit 90314a5
Show file tree
Hide file tree
Showing 17 changed files with 110 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .taq/state.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// WARNING: This file is autogenerated and should NOT be modified
{
"build": "1fdcff4",
"configHash": "4803522e2f08c81a7f56d977e3f057055bc1796e33e3115bcc59e5411c94fbdd",
"configHash": "66f6bf5e15e29a0ab7fb962a892e83401fb5b14c3fc4b2e061f9e1d408a4eca0",
"tasks": {
"ligo": {
"type": "npm",
Expand Down
3 changes: 2 additions & 1 deletion app/assets/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ nav button {
/* homepage
------------------------------------------------------------*/

.recent-airdrops { @apply p-4; }
.recent-airdrops h2 {
@apply font-bold border-sky-400;
@apply text-xl font-extrabold ;
}

.recent-airdrops p {
Expand Down
42 changes: 42 additions & 0 deletions app/config/Migrations/20230709213403_AirdropsUsers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;

class AirdropsUsers extends AbstractMigration
{
/**
* Up Method.
*
* More information on this method is available here:
* https://book.cakephp.org/phinx/0/en/migrations.html#the-up-method
* @return void
*/
public function up(): void
{

$this->table('airdrops_recipients')
->addColumn('claimed', 'timestamp', [
'after' => 'amount',
'default' => null,
'length' => null,
'null' => true,
])
->update();
}

/**
* Down Method.
*
* More information on this method is available here:
* https://book.cakephp.org/phinx/0/en/migrations.html#the-down-method
* @return void
*/
public function down(): void
{

$this->table('airdrops_recipients')
->removeColumn('claimed')
->update();
}
}
Binary file modified app/config/Migrations/schema-dump-default.lock
Binary file not shown.
2 changes: 1 addition & 1 deletion app/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
],
'short' => [
'className' => 'File',
'duration' => '+1 hours',
'duration' => '+5 minutes',
'path' => CACHE,
'prefix' => 'cake_short_'
],
Expand Down
14 changes: 14 additions & 0 deletions app/src/Controller/HomepageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace App\Controller;

use Cake\Datasource\ResultSetInterface;
use Cake\Log\Log;

class HomepageController extends AppController
{
public function beforeFilter(\Cake\Event\EventInterface $event)
Expand All @@ -13,8 +16,19 @@ public function beforeFilter(\Cake\Event\EventInterface $event)

public function index()
{
/** @var ResultSetInterface */
$recentAirdrops = $this->fetchTable('Airdrops')->recentAirdrops();
$totalAmounts = $recentAirdrops->reduce(
function ($acc, $airdrop) {
/** @var ResultSetInterface */
$recipients = $this->fetchTable('AirdropsRecipients')->byAirdrop($airdrop->id);
$totalAmount = $recipients->sumOf('amount');
return [...$acc, $airdrop->id => $totalAmount];
},
[]
);

$this->set('recentAirdrops', $recentAirdrops);
$this->set('totalAmounts', $totalAmounts);
}
}
2 changes: 2 additions & 0 deletions app/src/Model/Entity/AirdropsRecipient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* @property int $airdrop_id
* @property int $recipient_id
* @property int $amount
* @property \Cake\I18n\FrozenTime|null $claimed
*
* @property \App\Model\Entity\Airdrop $airdrop
* @property \App\Model\Entity\Recipient $recipient
Expand All @@ -31,6 +32,7 @@ class AirdropsRecipient extends Entity
'airdrop_id' => true,
'recipient_id' => true,
'amount' => true,
'claimed' => true,
'airdrop' => true,
'recipient' => true,
];
Expand Down
12 changes: 12 additions & 0 deletions app/src/Model/Table/AirdropsRecipientsTable.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace App\Model\Table;
Expand Down Expand Up @@ -75,6 +76,10 @@ public function validationDefault(Validator $validator): Validator
->requirePresence('amount', 'create')
->notEmptyString('amount');

$validator
->dateTime('claimed')
->allowEmptyDateTime('claimed');

return $validator;
}

Expand All @@ -92,4 +97,11 @@ public function buildRules(RulesChecker $rules): RulesChecker

return $rules;
}

public function byAirdrop(int $airdropId)
{
return $this->find('all', [
'conditions' => ['airdrop_id' => $airdropId]
])->all();
}
}
9 changes: 9 additions & 0 deletions app/src/Tezos/Airdrop/ClaimStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace App\Tezos\Airdrop;

enum ClaimStatus {
case CLAIMED;
case UNCLAIMED;
case INELIGIBLE;
}
10 changes: 9 additions & 1 deletion app/src/Tezos/Network.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ enum Network: string
case GHOSTNET = 'ghostnet';
case LOCAL = 'local';

public function base_url(): string
public function rpc_url(): string
{
return match ($this) {
Network::MAINNET => 'https://rpc.tzbeta.net',
Network::GHOSTNET => 'https://ghostnet.tezos.marigold.dev',
Network::LOCAL => 'http://localhost:20000',
};
}

public function tzkt_url():string {
return match ($this) {
Network::MAINNET => 'https://api.tzkt.io',
Network::GHOSTNET => 'https://api.ghostnet.tzkt.io',
Network::LOCAL => 'http://localhost:5000',
};
}
}
7 changes: 5 additions & 2 deletions app/src/View/Cell/BalanceCell.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
use App\Tezos\Mutez;
use Bzzhh\Tzkt\Api\AccountsApi;
use Cake\View\Cell;
use App\Tezos\Network;
use Bzzhh\Tzkt\Configuration;

class BalanceCell extends Cell
{
public function display()
public function display(string $network = 'local')
{
$identity = $this->request->getAttribute('identity')->getOriginalData();
$mutez = (new AccountsApi())->accountsGetBalance($identity->get('address'));
$host = Network::from($network)->tzkt_url();
$mutez = (new AccountsApi(null, (new Configuration)->setHost($host)))->accountsGetBalance($identity->get('address'));
$balance = (new Mutez($mutez))->tez();

$this->set('balance', $balance);
Expand Down
1 change: 1 addition & 0 deletions app/templates/AirdropsRecipients/add.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
echo $this->Form->control('airdrop_id', ['options' => $airdrops]);
echo $this->Form->control('recipient_id', ['options' => $recipients]);
echo $this->Form->control('amount');
echo $this->Form->control('claimed');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
Expand Down
1 change: 1 addition & 0 deletions app/templates/AirdropsRecipients/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
echo $this->Form->control('airdrop_id', ['options' => $airdrops]);
echo $this->Form->control('recipient_id', ['options' => $recipients]);
echo $this->Form->control('amount');
echo $this->Form->control('claimed');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
Expand Down
4 changes: 3 additions & 1 deletion app/templates/AirdropsRecipients/index.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @var \App\View\AppView $this
* @var \App\Model\Entity\AirdropsRecipient[]|\Cake\Collection\CollectionInterface $airdropsRecipients
* @var iterable<\App\Model\Entity\AirdropsRecipient> $airdropsRecipients
*/
?>
<div class="airdropsRecipients index content">
Expand All @@ -15,6 +15,7 @@
<th><?= $this->Paginator->sort('airdrop_id') ?></th>
<th><?= $this->Paginator->sort('recipient_id') ?></th>
<th><?= $this->Paginator->sort('amount') ?></th>
<th><?= $this->Paginator->sort('claimed') ?></th>
<th class="actions"><?= __('Actions') ?></th>
</tr>
</thead>
Expand All @@ -25,6 +26,7 @@
<td><?= $airdropsRecipient->has('airdrop') ? $this->Html->link($airdropsRecipient->airdrop->name, ['controller' => 'Airdrops', 'action' => 'view', $airdropsRecipient->airdrop->id]) : '' ?></td>
<td><?= $airdropsRecipient->has('recipient') ? $this->Html->link($airdropsRecipient->recipient->id, ['controller' => 'Recipients', 'action' => 'view', $airdropsRecipient->recipient->id]) : '' ?></td>
<td><?= $this->Number->format($airdropsRecipient->amount) ?></td>
<td><?= h($airdropsRecipient->claimed) ?></td>
<td class="actions">
<?= $this->Html->link(__('View'), ['action' => 'view', $airdropsRecipient->id]) ?>
<?= $this->Html->link(__('Edit'), ['action' => 'edit', $airdropsRecipient->id]) ?>
Expand Down
4 changes: 4 additions & 0 deletions app/templates/AirdropsRecipients/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
<th><?= __('Amount') ?></th>
<td><?= $this->Number->format($airdropsRecipient->amount) ?></td>
</tr>
<tr>
<th><?= __('Claimed') ?></th>
<td><?= h($airdropsRecipient->claimed) ?></td>
</tr>
</table>
</div>
</div>
Expand Down
3 changes: 3 additions & 0 deletions app/templates/Homepage/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
<?php foreach ($recentAirdrops as $airdrop) : ?>
<div class="airdrop">
<h3><?= $airdrop->name ?><h3>
<!-- todo: fetch metadata -->
<h4><?= $airdrop->token->address ?></h4>
<!-- todo: decimal conversion from metadata -->
<p><?= $totalAmounts[$airdrop->id] ?> tokens</p>
<p><?= $airdrop->description ?></p>
</div>
<?php if ($this->Identity->isLoggedIn()) : ?>
Expand Down
1 change: 1 addition & 0 deletions app/tests/Fixture/AirdropsRecipientsFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function init(): void
'airdrop_id' => 1,
'recipient_id' => 1,
'amount' => 1,
'claimed' => 1688938384,
],
];
parent::init();
Expand Down

0 comments on commit 90314a5

Please sign in to comment.