Skip to content

Commit

Permalink
Merge pull request #2 from kadena-php/feature/make-meta-object-easier…
Browse files Browse the repository at this point in the history
…-to-create

Added static create method to Meta object, taking an optional array of options.
  • Loading branch information
HergenD authored Jan 7, 2023
2 parents be18bcc + c877122 commit ac3544a
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 11 deletions.
63 changes: 63 additions & 0 deletions .github/workflows/code-quality.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: Code Quality

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

defaults:
run:
working-directory: .

jobs:

unit-test:
runs-on: ubuntu-latest

steps:
- uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
- uses: actions/checkout@v3
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Get Composer Cache Directory
id: composer-cache
run: |
echo "composer_cache_dir=$(composer config cache-files-dir)" >> $GITHUB_ENV
- uses: actions/cache@v3
with:
path: ${{ env.composer_cache_dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Unit tests
run: vendor/bin/phpunit

code-style-check:
runs-on: ubuntu-latest

steps:
- uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
- uses: actions/checkout@v3
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Get Composer Cache Directory
id: composer-cache
run: |
echo "composer_cache_dir=$(composer config cache-files-dir)" >> $GITHUB_ENV
- uses: actions/cache@v3
with:
path: ${{ env.composer_cache_dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Code Style checks
run: vendor/bin/ecs check --no-progress-bar
24 changes: 14 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,22 @@ The `networkId` is `null` by default, and when no nonce is set, it will default

Signers must be present in a signed command, but using the `getSignedCommand` will take care of that for you.

First, let's create a payload and metadata to construct our command:
First, let's create metadata to construct our command:

```php
$metadata = new Meta(
creationTime: Carbon::now(),
ttl: 0,
gasLimit: 0,
chainId: '',
gasPrice: 0,
sender: ''
);

$metadata = Meta::create();
```
The `create()` method takes an optional array of options, options with their default values are:
```php
creationTime: Carbon::now(),
ttl: 7200,
gasLimit: 10000,
chainId: '0',
gasPrice: 1e-8,
sender: ''
```
Then create a payload:
```php
$executePayload = new Payload(
payloadType: PayloadType::EXECUTE,
executePayload: new ExecutePayload(
Expand Down
16 changes: 15 additions & 1 deletion src/Pact/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

final class Meta
{
private const MIN_GAS_PRICE = 0.00000001;

public function __construct(
public readonly Carbon $creationTime,
public readonly int $ttl,
Expand All @@ -19,12 +21,24 @@ public function __construct(
public function toArray(): array
{
return [
'creationTime' => (int) $this->creationTime->getTimestamp(),
'creationTime' => $this->creationTime->getTimestamp(),
'ttl' => $this->ttl,
'gasLimit' => $this->gasLimit,
'chainId' => $this->chainId,
'gasPrice' => $this->gasPrice,
'sender' => $this->sender,
];
}

public static function create(?array $options = []): self
{
return new self(
creationTime: (isset($options['creationTime'])) ? Carbon::createFromTimestamp((int) $options['creationTime']) : Carbon::now(),
ttl: (int) ($options['ttl'] ?? 7200),
gasLimit: (int) ($options['gasLimit'] ?? 10000),
chainId: (string) ($options['chainId'] ?? '0'),
gasPrice: (float) ($options['gasPrice'] ?? self::MIN_GAS_PRICE),
sender: (string) ($options['sender'] ?? '')
);
}
}
1 change: 1 addition & 0 deletions src/Pact/SignedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function toArray(): array
foreach ($this->signatures->toArray() as $signature) {
$signatures[] = ['sig' => $signature->signature];
}

return [
'hash' => $this->hash,
'sigs' => $signatures,
Expand Down
50 changes: 50 additions & 0 deletions tests/Unit/Pact/MetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

final class MetaTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

Carbon::setTestNow('2021-11-26 12:30:00');
}

/** @test */
public function it_should_return_the_correct_array_representation_of_the_meta_object(): void
{
Expand All @@ -30,4 +37,47 @@ public function it_should_return_the_correct_array_representation_of_the_meta_ob

$this->assertSame($expectedArray, $meta->toArray());
}

/** @test */
public function it_should_be_able_to_be_constructed_from_an_options_array(): void
{
$creationTime = Carbon::now();
$ttl = 300;
$gasLimit = 50;
$chainId = 'test-chain-id';
$gasPrice = 1.25;
$sender = 'test-sender';

$expected = new Meta($creationTime, $ttl, $gasLimit, $chainId, $gasPrice, $sender);

$options = [
'creationTime' => $creationTime->getTimestamp(),
'ttl' => $ttl,
'gasLimit' => $gasLimit,
'chainId' => $chainId,
'gasPrice' => $gasPrice,
'sender' => $sender
];

$actual = Meta::create($options);

$this->assertEquals($expected, $actual);
}

/** @test */
public function it_should_be_able_to_be_constructed_using_the_create_method_with_default_options(): void
{
$creationTime = Carbon::now();
$ttl = 7200;
$gasLimit = 10000;
$chainId = '0';
$gasPrice = 1e-8;
$sender = '';

$expected = new Meta($creationTime, $ttl, $gasLimit, $chainId, $gasPrice, $sender);

$actual = Meta::create();

$this->assertEquals($expected, $actual);
}
}

0 comments on commit ac3544a

Please sign in to comment.