1: <?php declare( strict_types = 1 );
2:
3: namespace Waves\Transactions;
4:
5: use Waves\Common\JsonBase;
6: use Waves\Account\PublicKey;
7: use Waves\Model\ChainId;
8: use Waves\Model\Id;
9: use Waves\Model\WavesConfig;
10: use Waves\Util\Functions;
11:
12: class TransactionOrOrder extends JsonBase
13: {
14: private Id $id;
15: private int $version;
16: private ChainId $chainId;
17: private PublicKey $sender;
18: private int $timestamp;
19: private Amount $fee;
20: /**
21: * @var array<int, string>
22: */
23: private array $proofs;
24: private string $bodyBytes;
25:
26: function id(): Id
27: {
28: if( !isset( $this->id ) )
29: {
30: if( !$this->json()->exists( 'id' ) && isset( $this->bodyBytes ) )
31: $this->setId( Functions::calculateTransactionId( $this->bodyBytes ) );
32: else
33: $this->id = $this->json->get( 'id' )->asId();
34: }
35: return $this->id;
36: }
37:
38: function setId( Id $id ): void
39: {
40: $this->id = $id;
41: $this->json->put( 'id', $id->toString() );
42: }
43:
44: function version(): int
45: {
46: if( !isset( $this->version ) )
47: $this->version = $this->json->get( 'version' )->asInt();
48: return $this->version;
49: }
50:
51: /**
52: * @return mixed
53: */
54: function setVersion( int $version )
55: {
56: $this->version = $version;
57: $this->json->put( 'version', $version );
58: return $this;
59: }
60:
61: function chainId(): ChainId
62: {
63: if( !isset( $this->chainId ) )
64: {
65: if( $this->json->exists( 'chainId' ) )
66: $this->chainId = $this->json->get( 'chainId' )->asChainId();
67: else if( $this->json->exists( 'sender' ) )
68: $this->chainId = $this->json->get( 'sender' )->asAddress()->chainId();
69: else
70: $this->chainId = WavesConfig::chainId();
71: }
72: return $this->chainId;
73: }
74:
75: /**
76: * @return mixed
77: */
78: function setChainId( ChainId $chainId = null )
79: {
80: if( !isset( $chainId ) )
81: $chainId = WavesConfig::chainId();
82: $this->chainId = $chainId;
83: $this->json->put( 'chainId', $chainId->asInt() );
84: return $this;
85: }
86:
87: function sender(): PublicKey
88: {
89: if( !isset( $this->sender ) )
90: {
91: $this->sender = $this->json->get( 'senderPublicKey' )->asPublicKey();
92: if( $this->json->exists( 'sender' ) )
93: $this->sender->attachAddress( $this->json->get( 'sender' )->asAddress() );
94: }
95: return $this->sender;
96: }
97:
98: /**
99: * @return mixed
100: */
101: function setSender( PublicKey $sender )
102: {
103: $this->sender = $sender;
104: $this->json->put( 'senderPublicKey', $sender->toString() );
105: $this->json->put( 'sender', $sender->address()->toString() );
106: return $this;
107: }
108:
109: function timestamp(): int
110: {
111: if( !isset( $this->timestamp ) )
112: $this->timestamp = $this->json->get( 'timestamp' )->asInt();
113: return $this->timestamp;
114: }
115:
116: /**
117: * @return mixed
118: */
119: function setTimestamp( int $timestamp = null )
120: {
121: if( !isset( $timestamp ) )
122: $timestamp = intval( microtime( true ) * 1000 );
123: $this->timestamp = $timestamp;
124: $this->json->put( 'timestamp', $timestamp );
125: return $this;
126: }
127:
128: function fee(): Amount
129: {
130: if( !isset( $this->fee ) )
131: $this->fee = Amount::fromJson( $this->json, 'fee', 'feeAssetId' );
132: return $this->fee;
133: }
134:
135: /**
136: * @return mixed
137: */
138: function setFee( Amount $fee )
139: {
140: $this->fee = $fee;
141: $this->json->put( 'fee', $fee->value() );
142: $this->json->put( 'feeAssetId', $fee->assetId()->toJsonValue() );
143: return $this;
144: }
145:
146: /**
147: * @return array<int, string>
148: */
149: function proofs(): array
150: {
151: if( !isset( $this->proofs ) )
152: $this->proofs = $this->json->getOr( 'proofs', [] )->asArrayString();
153: return $this->proofs;
154: }
155:
156: /**
157: * @param array<int, string> $proofs
158: * @return mixed
159: */
160: function setProofs( array $proofs = null )
161: {
162: if( !isset( $proofs ) )
163: $proofs = [];
164: $this->proofs = $proofs;
165: $this->json->put( 'proofs', $proofs );
166: return $this;
167: }
168:
169: function bodyBytes(): string
170: {
171: return $this->bodyBytes;
172: }
173:
174: protected function setBodyBytes( string $bodyBytes ): void
175: {
176: $this->bodyBytes = $bodyBytes;
177: }
178: }
179: