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