Skip to content

refactor: replace builders with evm #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 32 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
c49cdc7
evm call builder is now the main transaction
alfonsobries Oct 23, 2024
2faf459
formatting
alfonsobries Oct 23, 2024
b24292a
cleanup evm builder
alfonsobries Oct 24, 2024
4058542
add gasPrice alias for fee
alfonsobries Oct 24, 2024
6366771
remove vendor field serialization
alfonsobries Oct 24, 2024
1b26027
cleanup to be in match with js version
alfonsobries Oct 24, 2024
3352037
wip
alfonsobries Oct 24, 2024
3feb2c5
add test for deserializer
alfonsobries Oct 24, 2024
210a291
replace transfer test with serializer deserializer
alfonsobries Oct 24, 2024
5ba1431
Delete TransferTest.php
alfonsobries Oct 24, 2024
96f0609
formatting
alfonsobries Oct 24, 2024
b9dbd46
transaction is now the evm call
alfonsobries Oct 24, 2024
7fe53ce
remove enums
alfonsobries Oct 24, 2024
4ef035f
style: resolve style guide violations
alfonsobries Oct 24, 2024
384453a
rollback fee
alfonsobries Oct 24, 2024
a555a4b
Merge branch 'refactor/replace-builders-with-evm' of github.com:ArkEc…
alfonsobries Oct 24, 2024
8860a68
remove deprecated transactions types & builders
alfonsobries Oct 24, 2024
420bf91
abi encoder
alfonsobries Oct 29, 2024
db5291f
add abi decoder
alfonsobries Oct 29, 2024
b3623fb
Create AbiDecoderTest.php
alfonsobries Oct 29, 2024
8744c1e
wip
alfonsobries Oct 30, 2024
a5cf18a
style: resolve style guide violations
alfonsobries Oct 30, 2024
5c924f2
wip
alfonsobries Oct 30, 2024
73c0166
Merge branch 'refactor/replace-builders-with-evm' of github.com:ArkEc…
alfonsobries Oct 30, 2024
4a53f5f
wip
alfonsobries Oct 30, 2024
8bde0d5
style: resolve style guide violations
alfonsobries Oct 30, 2024
19a5dd9
wip
alfonsobries Oct 30, 2024
c0c0d3c
Merge branch 'refactor/replace-builders-with-evm' of github.com:ArkEc…
alfonsobries Oct 30, 2024
e02a5d8
wip
alfonsobries Oct 30, 2024
df1822b
add validator registration
alfonsobries Oct 30, 2024
b772050
add validator resignation
alfonsobries Oct 30, 2024
a7fadb8
style: resolve style guide violations
alfonsobries Oct 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Enums/AbiFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace ArkEcosystem\Crypto\Enums;

use ArkEcosystem\Crypto\Transactions\Types\Unvote;
use ArkEcosystem\Crypto\Transactions\Types\ValidatorRegistration;
use ArkEcosystem\Crypto\Transactions\Types\ValidatorResignation;
use ArkEcosystem\Crypto\Transactions\Types\Vote;

enum AbiFunction: string
{
case VOTE = 'vote';
case UNVOTE = 'unvote';
case VALIDATOR_REGISTRATION = 'registerValidator';
case VALIDATOR_RESIGNATION = 'resignValidator';

public function transactionClass(): string
{
return match ($this) {
self::VOTE => Vote::class,
self::UNVOTE => Unvote::class,
self::VALIDATOR_REGISTRATION => ValidatorRegistration::class,
self::VALIDATOR_RESIGNATION => ValidatorResignation::class,
};
}
}
1 change: 0 additions & 1 deletion src/Enums/Fees.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@ class Fees

public const USERNAME_RESIGNATION = '2500000000';

// @TODO: review this fee
public const EVM = '0';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EVM fees will be based on network usage, similar to ethereum. In that case we will need a method to define a given fee, and the client (most likely) should get a method to retrieve network based fees (slow/avg/fast) that can then be passed to the crypto library after the user selects one of them

}
32 changes: 4 additions & 28 deletions src/Enums/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@

namespace ArkEcosystem\Crypto\Enums;

use ArkEcosystem\Crypto\Transactions\Types\EvmCall;
use ArkEcosystem\Crypto\Transactions\Types\MultiPayment;
use ArkEcosystem\Crypto\Transactions\Types\MultiSignatureRegistration;
use ArkEcosystem\Crypto\Transactions\Types\Transfer;
use ArkEcosystem\Crypto\Transactions\Types\UsernameRegistration;
use ArkEcosystem\Crypto\Transactions\Types\UsernameResignation;
use ArkEcosystem\Crypto\Transactions\Types\ValidatorRegistration;
use ArkEcosystem\Crypto\Transactions\Types\ValidatorResignation;
use ArkEcosystem\Crypto\Transactions\Types\Vote;
use ReflectionEnum;

/**
Expand All @@ -30,32 +21,17 @@ enum Types: int
case USERNAME_RESIGNATION = 9;
case EVM_CALL = 10;

public function transactionClass(): string
{
return match ($this) {
Types::TRANSFER => Transfer::class,
Types::VALIDATOR_REGISTRATION => ValidatorRegistration::class,
Types::VOTE => Vote::class,
Types::MULTI_SIGNATURE_REGISTRATION => MultiSignatureRegistration::class,
Types::MULTI_PAYMENT => MultiPayment::class,
Types::VALIDATOR_RESIGNATION => ValidatorResignation::class,
Types::USERNAME_REGISTRATION => UsernameRegistration::class,
Types::USERNAME_RESIGNATION => UsernameResignation::class,
Types::EVM_CALL => EvmCall::class,
};
}

public function defaultFee(): string
{
return match ($this) {
Types::TRANSFER => Fees::TRANSFER,
Types::VALIDATOR_REGISTRATION => Fees::VALIDATOR_REGISTRATION,
Types::VOTE => Fees::VOTE,
Types::MULTI_SIGNATURE_REGISTRATION => Fees::MULTI_SIGNATURE_REGISTRATION,
Types::MULTI_PAYMENT => Fees::MULTI_PAYMENT,
// Types::MULTI_SIGNATURE_REGISTRATION => Fees::MULTI_SIGNATURE_REGISTRATION,
// Types::MULTI_PAYMENT => Fees::MULTI_PAYMENT,
Types::VALIDATOR_RESIGNATION => Fees::VALIDATOR_RESIGNATION,
Types::USERNAME_REGISTRATION => Fees::USERNAME_REGISTRATION,
Types::USERNAME_RESIGNATION => Fees::USERNAME_RESIGNATION,
// Types::USERNAME_REGISTRATION => Fees::USERNAME_REGISTRATION,
// Types::USERNAME_RESIGNATION => Fees::USERNAME_RESIGNATION,
Types::EVM_CALL => Fees::EVM,
};
}
Expand Down
183 changes: 57 additions & 126 deletions src/Transactions/Builder/AbstractTransactionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,98 +6,91 @@

use ArkEcosystem\Crypto\Configuration\Fee;
use ArkEcosystem\Crypto\Configuration\Network;
use ArkEcosystem\Crypto\Enums\TypeGroup;
use ArkEcosystem\Crypto\Enums\Types;
use ArkEcosystem\Crypto\Identities\PrivateKey;
use ArkEcosystem\Crypto\Transactions\Types\Transaction;
use ArkEcosystem\Crypto\Transactions\Types\AbstractTransaction;

abstract class AbstractTransactionBuilder
{
public $transaction;
public AbstractTransaction $transaction;

/**
* Create a new transaction instance.
*/
public function __construct()
public function __construct(?array $data = null)
{
$this->transaction = $this->getTransactionInstance();
$this->transaction->data['type'] = $this->getType();
$this->transaction->data['typeGroup'] = $this->getTypeGroup();
$this->transaction->data['nonce'] = '0';
$this->transaction->data['amount'] = '0';
$this->transaction->data['fee'] = $this->getFee();
$this->transaction->data['version'] = 1;
$this->transaction->data['network'] = Network::get()->pubKeyHash();
$this->transaction = $this->getTransactionInstance();

$this->transaction->data = $data ?? [
'type' => Types::EVM_CALL->value,
'typeGroup' => TypeGroup::CORE,
'amount' => '0',
'senderPublicKey' => '',
'fee' => Fee::get(Types::EVM_CALL->value),
'nonce' => '1',
'version' => 1,
'network' => Network::get()->pubKeyHash(),
'asset' => [
'evmCall' => [
'gasLimit' => 1000000, // Default gas limit
'payload' => '', // EVM code in hex format
],
],
];
}

/**
* Convert the message to its string representation.
*
* @return string
*/
public function __toString()
public function __toString(): string
{
return $this->toJson();
}

/**
* Create a new transaction instance.
*
* @return AbstractTransactionBuilder
*/
public static function new(): self
public static function new(?array $data = null): static
{
return new static();
return new static($data);
}

/**
* Set the transaction fee.
*
* @param string $fee
*
* @return AbstractTransactionBuilder
*/
public function withFee(string $fee): self
public function gasLimit(int $gasLimit): static
{
$this->transaction->data['asset']['evmCall']['gasLimit'] = $gasLimit;

return $this;
}

public function recipient(string $recipientId): static
{
$this->transaction->data['recipientId'] = $recipientId;

return $this;
}

public function fee(string $fee): static
{
$this->transaction->data['fee'] = $fee;

return $this;
}

/**
* Set the transaction nonce.
*
* @param string $nonce
*
* @return AbstractTransactionBuilder
* Alias for fee.
*/
public function withNonce(string $nonce): self
public function gasPrice(string $gasPrice): static
{
return $this->fee($gasPrice);
}

public function nonce(string $nonce): static
{
$this->transaction->data['nonce'] = $nonce;

return $this;
}

/**
* Set the transaction network.
*
* @param int $network
*
* @return AbstractTransactionBuilder
*/
public function withNetwork(int $network): self
public function network(int $network): static
{
$this->transaction->data['network'] = $network;

return $this;
}

/**
* Sign the transaction using the given passphrase.
*
* @param string $passphrase
*
* @return AbstractTransactionBuilder
*/
public function sign(string $passphrase): self
public function sign(string $passphrase): static
{
$keys = PrivateKey::fromPassphrase($passphrase);
$this->transaction->data['senderPublicKey'] = $keys->getPublicKey()->getHex();
Expand All @@ -108,105 +101,43 @@ public function sign(string $passphrase): self
return $this;
}

/**
* Sign the transaction using the given passphrase.
*
* @param string $passphrase
*
* @return AbstractTransactionBuilder
*/
public function multiSign(string $passphrase, int $index = -1): self
public function multiSign(string $passphrase, int $index = -1): static
{
$keys = PrivateKey::fromPassphrase($passphrase);

$keys = PrivateKey::fromPassphrase($passphrase);
$this->transaction = $this->transaction->multiSign($keys, $index);

return $this;
}

/**
* Sign the transaction using the given second passphrase.
*
* @param string $secondPassphrase
*
* @return AbstractTransactionBuilder
*/
public function secondSign(string $secondPassphrase): self
public function secondSign(string $secondPassphrase): static
{
$this->transaction = $this->transaction->secondSign(PrivateKey::fromPassphrase($secondPassphrase));
$this->transaction = $this->transaction->secondSign(
PrivateKey::fromPassphrase($secondPassphrase)
);
$this->transaction->data['id'] = $this->transaction->getId();

return $this;
}

/**
* Verify the transaction validity.
*
* @return bool
*/
public function verify(): bool
{
return $this->transaction->verify();
}

/**
* Verify the transaction validity with a second signature.
*
* @return bool
*/
public function secondVerify(string $secondPublicKey): bool
{
return $this->transaction->secondVerify($secondPublicKey);
}

/**
* Convert the transaction to its array representation.
*
* @return array
*/
public function toArray(): array
{
return $this->transaction->toArray();
}

/**
* Convert the transaction to its JSON representation.
*
* @return string
*/
public function toJson(): string
{
return $this->transaction->toJson();
}

/**
* Get the transaction type.
*
* @return int
*/
abstract protected function getType(): int;

/**
* Get the transaction typeGroup.
*
* @return int
*/
abstract protected function getTypeGroup(): int;

/**
* Get the transaction instance.
*
* @return object
*/
abstract protected function getTransactionInstance(): object;

/**
* Get the transaction fee.
*
* @return string
*/
protected function getFee(): string
{
return Fee::get($this->transaction->data['type']);
}
abstract protected function getTransactionInstance(?array $data = []): AbstractTransaction;
}
Loading
Loading