This repository was archived by the owner on Feb 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathTransaction.php
More file actions
102 lines (91 loc) · 2.69 KB
/
Transaction.php
File metadata and controls
102 lines (91 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
declare(strict_types=1);
namespace Web3\ValueObjects;
use Web3\Formatters\BigIntegerToHex;
final class Transaction
{
/**
* Creates a new Transaction instance.
*
* @param array<string, string> $options
*/
private function __construct(
private string $from, private string $to, private array $options,
) {
// ..
}
/**
* Creates a new Transaction instance between the given accounts.
*
* @param array<string, string> $options
*/
public static function between(string $from, string $to, array $options = []): self
{
foreach (['value', 'gas', 'gasPrice', 'nonce'] as $option) {
if (array_key_exists($option, $options)) {
$options[$option] = BigIntegerToHex::format($options[$option]);
}
}
return new self($from, $to, $options);
}
/**
* Creates a new Transaction instance with the given value in wei.
*/
public function withValue(Wei $wei): self
{
return self::between($this->from, $this->to, array_merge($this->options, [
'value' => $wei->value(),
]));
}
/**
* Creates a new Transaction instance with the given gas in wei.
*
* Gas is the gas provided for transaction execution.
*/
public function withGas(string $quantity): self
{
return self::between($this->from, $this->to, array_merge($this->options, [
'gas' => $quantity,
]));
}
/**
* Creates a new Transaction instance with the given gas price in wei.
*
* Gas Price is the price in wei of each gas used.
*/
public function withGasPrice(Wei $wei): self
{
return self::between($this->from, $this->to, array_merge($this->options, [
'gasPrice' => $wei->value(),
]));
}
/**
* Creates a new Transaction instance with the given nonce.
*
* Nonce is the unique number identifying this transaction.
*/
public function withNonce(string $number): self
{
return self::between($this->from, $this->to, array_merge($this->options, [
'nonce' => $number,
]));
}
/**
* Returns the array representation of the Transaction.
*
* @return array<string, string>
*
* @internal
*/
public function toArray(): array
{
return array_filter([
'from' => $this->from,
'to' => $this->to,
'value' => $this->options['value'] ?? null,
'gas' => $this->options['gas'] ?? null,
'gasPrice' => $this->options['gasPrice'] ?? null,
'nonce' => $this->options['nonce'] ?? null,
]);
}
}