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