1: <?php declare( strict_types = 1 );
2:
3: namespace Waves\Transactions;
4:
5: use Waves\Common\Json;
6: use Waves\Model\AssetId;
7:
8: class Amount
9: {
10: private int $amount;
11: private AssetId $assetId;
12:
13: function __construct( int $amount, AssetId $assetId = null )
14: {
15: $this->amount = $amount;
16: $this->assetId = $assetId ?? AssetId::WAVES();
17: }
18:
19: static function of( int $amount, AssetId $assetId = null ): Amount
20: {
21: return new Amount( $amount, $assetId );
22: }
23:
24: static function fromJson( Json $json, string $amountKey = 'amount', string $assetIdKey = ' assetId' ): Amount
25: {
26: return Amount::of( $json->get( $amountKey )->asInt(), $json->getOr( $assetIdKey, AssetId::WAVES_STRING )->asAssetId() );
27: }
28:
29: function value(): int
30: {
31: return $this->amount;
32: }
33:
34: function assetId(): AssetId
35: {
36: return $this->assetId;
37: }
38:
39: function toString(): string
40: {
41: return serialize( $this );
42: }
43:
44: function toProtobuf(): \Waves\Protobuf\Amount
45: {
46: $pb_Amount = new \Waves\Protobuf\Amount;
47: $pb_Amount->setAmount( $this->value() );
48: if( !$this->assetId()->isWaves() )
49: $pb_Amount->setAssetId( $this->assetId()->bytes() );
50: return $pb_Amount;
51: }
52: }
53: