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\LeaseTransaction as CurrentTransaction;
14:
15: class LeaseTransaction extends Transaction
16: {
17: const TYPE = 8;
18: const LATEST_VERSION = 3;
19: const MIN_FEE = 100_000;
20:
21: private Recipient $recipient;
22: private int $amount;
23:
24: static function build( PublicKey $sender, Recipient $recipient, int $amount ): CurrentTransaction
25: {
26: $tx = new CurrentTransaction;
27: $tx->setBase( $sender, CurrentTransaction::TYPE, CurrentTransaction::LATEST_VERSION, CurrentTransaction::MIN_FEE );
28:
29: // LEASE TRANSACTION
30: {
31: $tx->setRecipient( $recipient );
32: $tx->setAmount( $amount );
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: // LEASE TRANSACTION
48: {
49: $pb_TransactionData = new \Waves\Protobuf\LeaseTransactionData;
50: // RECIPIENT
51: {
52: $pb_TransactionData->setRecipient( $this->recipient()->toProtobuf() );
53: }
54: // AMOUNT
55: {
56: $pb_TransactionData->setAmount( $this->amount() );
57: }
58: }
59:
60: // LEASE TRANSACTION
61: $this->setBodyBytes( $pb_Transaction->setLease( $pb_TransactionData )->serializeToString() );
62: return $this;
63: }
64:
65: function recipient(): Recipient
66: {
67: if( !isset( $this->recipient ) )
68: $this->recipient = $this->json->get( 'recipient' )->asRecipient();
69: return $this->recipient;
70: }
71:
72: function setRecipient( Recipient $recipient ): CurrentTransaction
73: {
74: $this->recipient = $recipient;
75: $this->json->put( 'recipient', $recipient->toString() );
76: return $this;
77: }
78:
79: function amount(): int
80: {
81: if( !isset( $this->amount ) )
82: $this->amount = $this->json->get( 'amount' )->asInt();
83: return $this->amount;
84: }
85:
86: function setAmount( int $amount ): CurrentTransaction
87: {
88: $this->amount = $amount;
89: $this->json->put( 'amount', $amount );
90: return $this;
91: }
92:
93: // COMMON
94:
95: function __construct( Json $json = null )
96: {
97: parent::__construct( $json );
98: }
99:
100: function addProof( PrivateKey $privateKey, int $index = null ): CurrentTransaction
101: {
102: $proof = (new \deemru\WavesKit)->sign( $this->bodyBytes(), $privateKey->bytes() );
103: if( $proof === false )
104: throw new Exception( __FUNCTION__ . ' unexpected sign() error', ExceptionCode::UNEXPECTED );
105: $proof = Base58String::fromBytes( $proof )->encoded();
106:
107: $proofs = $this->proofs();
108: if( !isset( $index ) )
109: $proofs[] = $proof;
110: else
111: $proofs[$index] = $proof;
112: return $this->setProofs( $proofs );
113: }
114:
115: /**
116: * @return CurrentTransaction
117: */
118: function setType( int $type )
119: {
120: parent::setType( $type );
121: return $this;
122: }
123:
124: /**
125: * @return CurrentTransaction
126: */
127: function setSender( PublicKey $sender )
128: {
129: parent::setSender( $sender );
130: return $this;
131: }
132:
133: /**
134: * @return CurrentTransaction
135: */
136: function setVersion( int $version )
137: {
138: parent::setVersion( $version );
139: return $this;
140: }
141:
142: /**
143: * @return CurrentTransaction
144: */
145: function setFee( Amount $fee )
146: {
147: parent::setFee( $fee );
148: return $this;
149: }
150:
151: /**
152: * @return CurrentTransaction
153: */
154: function setChainId( ChainId $chainId = null )
155: {
156: parent::setChainId( $chainId );
157: return $this;
158: }
159:
160: /**
161: * @return CurrentTransaction
162: */
163: function setTimestamp( int $timestamp = null )
164: {
165: parent::setTimestamp( $timestamp );
166: return $this;
167: }
168:
169: /**
170: * @param array<int, string> $proofs
171: * @return CurrentTransaction
172: */
173: function setProofs( array $proofs = null )
174: {
175: parent::setProofs( $proofs );
176: return $this;
177: }
178:
179: function bodyBytes(): string
180: {
181: if( !isset( $this->bodyBytes ) )
182: $this->getUnsigned();
183: return parent::bodyBytes();
184: }
185: }
186: