1: <?php declare( strict_types = 1 );
2:
3: namespace Waves\Transactions;
4:
5: use Exception;
6: use Waves\Account\PrivateKey;
7: use Waves\Common\Base58String;
8: use Waves\Account\PublicKey;
9: use Waves\Common\ExceptionCode;
10: use Waves\Common\Json;
11: use Waves\Model\ChainId;
12:
13: use Waves\Transactions\ReissueTransaction as CurrentTransaction;
14:
15: class ReissueTransaction extends Transaction
16: {
17: const TYPE = 5;
18: const LATEST_VERSION = 3;
19: const MIN_FEE = 100_000;
20:
21: private Amount $amount;
22: private bool $isReissuable;
23:
24: static function build( PublicKey $sender, Amount $amount, bool $isReissuable ): CurrentTransaction
25: {
26: $tx = new CurrentTransaction;
27: $tx->setBase( $sender, CurrentTransaction::TYPE, CurrentTransaction::LATEST_VERSION, CurrentTransaction::MIN_FEE );
28:
29: // REISSUE TRANSACTION
30: {
31: $tx->setAmount( $amount );
32: $tx->setIsReissuable( $isReissuable );
33: }
34:
35: return $tx;
36: }
37:
38: function getUnsigned(): CurrentTransaction
39: {
40: // VERSION
41: if( $this->version() !== CurrentTransaction::LATEST_VERSION )
42: throw new Exception( __FUNCTION__ . ' unexpected version = ' . $this->version(), ExceptionCode::UNEXPECTED );
43:
44: // BASE
45: $pb_Transaction = $this->getProtobufTransactionBase();
46:
47: // REISSUE TRANSACTION
48: {
49: $pb_TransactionData = new \Waves\Protobuf\ReissueTransactionData;
50: // AMOUNT
51: {
52: $pb_TransactionData->setAssetAmount( $this->amount()->toProtobuf() );
53: }
54: // REISSUABLE
55: {
56: $pb_TransactionData->setReissuable( $this->isReissuable() );
57: }
58: }
59:
60: // REISSUE TRANSACTION
61: $this->setBodyBytes( $pb_Transaction->setReissue( $pb_TransactionData )->serializeToString() );
62: return $this;
63: }
64:
65: function amount(): Amount
66: {
67: if( !isset( $this->amount ) )
68: $this->amount = Amount::fromJson( $this->json, 'quantity' );
69: return $this->amount;
70: }
71:
72: function setAmount( Amount $amount ): CurrentTransaction
73: {
74: $this->amount = $amount;
75: $this->json->put( 'quantity', $amount->value() );
76: $this->json->put( 'assetId', $amount->assetId()->toJsonValue() );
77: return $this;
78: }
79:
80: function isReissuable(): bool
81: {
82: if( !isset( $this->isReissuable ) )
83: $this->isReissuable = $this->json->get( 'reissuable' )->asBoolean();
84: return $this->isReissuable;
85: }
86:
87: function setIsReissuable( bool $isReissuable ): CurrentTransaction
88: {
89: $this->isReissuable = $isReissuable;
90: $this->json->put( 'reissuable', $isReissuable );
91: return $this;
92: }
93:
94: // COMMON
95:
96: function __construct( Json $json = null )
97: {
98: parent::__construct( $json );
99: }
100:
101: function addProof( PrivateKey $privateKey, int $index = null ): CurrentTransaction
102: {
103: $proof = (new \deemru\WavesKit)->sign( $this->bodyBytes(), $privateKey->bytes() );
104: if( $proof === false )
105: throw new Exception( __FUNCTION__ . ' unexpected sign() error', ExceptionCode::UNEXPECTED );
106: $proof = Base58String::fromBytes( $proof )->encoded();
107:
108: $proofs = $this->proofs();
109: if( !isset( $index ) )
110: $proofs[] = $proof;
111: else
112: $proofs[$index] = $proof;
113: return $this->setProofs( $proofs );
114: }
115:
116: /**
117: * @return CurrentTransaction
118: */
119: function setType( int $type )
120: {
121: parent::setType( $type );
122: return $this;
123: }
124:
125: /**
126: * @return CurrentTransaction
127: */
128: function setSender( PublicKey $sender )
129: {
130: parent::setSender( $sender );
131: return $this;
132: }
133:
134: /**
135: * @return CurrentTransaction
136: */
137: function setVersion( int $version )
138: {
139: parent::setVersion( $version );
140: return $this;
141: }
142:
143: /**
144: * @return CurrentTransaction
145: */
146: function setFee( Amount $fee )
147: {
148: parent::setFee( $fee );
149: return $this;
150: }
151:
152: /**
153: * @return CurrentTransaction
154: */
155: function setChainId( ChainId $chainId = null )
156: {
157: parent::setChainId( $chainId );
158: return $this;
159: }
160:
161: /**
162: * @return CurrentTransaction
163: */
164: function setTimestamp( int $timestamp = null )
165: {
166: parent::setTimestamp( $timestamp );
167: return $this;
168: }
169:
170: /**
171: * @param array<int, string> $proofs
172: * @return CurrentTransaction
173: */
174: function setProofs( array $proofs = null )
175: {
176: parent::setProofs( $proofs );
177: return $this;
178: }
179:
180: function bodyBytes(): string
181: {
182: if( !isset( $this->bodyBytes ) )
183: $this->getUnsigned();
184: return parent::bodyBytes();
185: }
186: }
187: