Skip to content

Commit

Permalink
add query action
Browse files Browse the repository at this point in the history
  • Loading branch information
michavie committed Jan 12, 2025
1 parent fcca8d7 commit e39ff98
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Actions/ActionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
enum ActionType: string
{
case Contract = 'contract';
case Query = 'query';
case Link = 'link';
}
26 changes: 26 additions & 0 deletions src/Actions/QueryAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Vleap\Actions;

use MultiversX\Address;
use Illuminate\Support\Collection;

final class QueryAction implements IWarpAction
{
public function __construct(
public readonly string $label,
public readonly ?string $description,
public readonly Address $address,
public readonly string $func,
public readonly array $args,
public readonly ?string $abi,
/** @var Collection<WarpActionInput> */
public readonly Collection $inputs = new Collection,
) {
}

public function getType(): ActionType
{
return ActionType::Query;
}
}
1 change: 1 addition & 0 deletions src/Transformers/Actions/ActionTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static function fromArray(array $data): IWarpAction
{
return match ($data['type']) {
ActionType::Contract->value => ContractActionTransformer::fromArray($data),
ActionType::Query->value => QueryActionTransformer::fromArray($data),
ActionType::Link->value => LinkActionTransformer::fromArray($data),
default => throw new Exception("unsupported action type: {$data['type']}"),
};
Expand Down
35 changes: 35 additions & 0 deletions src/Transformers/Actions/QueryActionTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Vleap\Transformers\Actions;

use Brick\Math\BigInteger;
use MultiversX\Address;
use Vleap\Actions\QueryAction;

final class QueryActionTransformer
{
public static function toArray(QueryAction $action): array
{
return [
'type' => $action->getType()->value,
'label' => $action->label,
'description' => $action->description,
'address' => $action->address->bech32(),
'func' => $action->func,
'args' => $action->args,
'abi' => $action->abi,
];
}

public static function fromArray(array $data): QueryAction
{
return new QueryAction(
label: $data['label'],
description: $data['description'] ?? null,
address: Address::newFromBech32($data['address']),
func: $data['func'],
args: $data['args'],
abi: $data['abi'],
);
}
}
10 changes: 10 additions & 0 deletions src/WarpAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use MultiversX\Address;
use Vleap\Actions\ContractAction;
use Vleap\Actions\LinkAction;
use Vleap\Actions\QueryAction;

class WarpAction
{
Expand All @@ -29,6 +30,15 @@ public function contract(string|Address $address, ?string $endpoint, array $args
return new ContractAction($this->name, $this->description, $address, $endpoint, $args, BigInteger::of($value), BigInteger::of($gasLimit));
}

public function query(string|Address $address, string $func, array $args, ?string $abi = null): QueryAction
{
$address = $address instanceof Address
? $address
: Address::newFromBech32($address);

return new QueryAction($this->name, $this->description, $address, $func, $args, $abi);
}

public function link(string $url): LinkAction
{
return new LinkAction($this->name, $this->description, $url);
Expand Down
24 changes: 24 additions & 0 deletions tests/Transformers/Actions/ContractQueryTransformerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use Brick\Math\BigInteger;
use Vleap\WarpAction;
use MultiversX\Address;
use Vleap\Actions\ActionType;
use Vleap\Transformers\Actions\QueryActionTransformer;

it('transforms a contract query', function () {
$address = Address::zero();
$action = WarpAction::create('test action')->query($address, 'test endpoint', ['test arg'], 'https://vleap.io/abi.json');

$actual = QueryActionTransformer::toArray($action);

expect($actual)->toBe([
'type' => ActionType::Query->value,
'label' => 'test action',
'description' => null,
'address' => 'erd1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq6gq4hu',
'func' => 'test endpoint',
'args' => ['test arg'],
'abi' => 'https://vleap.io/abi.json',
]);
});

0 comments on commit e39ff98

Please sign in to comment.