-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathPricingMode.ts
157 lines (140 loc) · 4.6 KB
/
PricingMode.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { jsonObject, jsonMember } from 'typedjson';
import { Hash } from './key';
import { CLValue } from './clvalue';
import { CalltableSerialization } from './CalltableSerialization';
/**
* Represents the payment limited ( classic before ) pricing mode, including parameters for gas price tolerance,
* payment amount, and standard payment.
*/
@jsonObject
export class PaymentLimitedMode {
/**
* The tolerance for gas price fluctuations in classic pricing mode.
*/
@jsonMember({ name: 'gas_price_tolerance', constructor: Number })
gasPriceTolerance: number;
/**
* The payment amount associated with classic pricing mode.
*/
@jsonMember({ name: 'payment_amount', constructor: Number })
paymentAmount: number;
/**
* Whether the payment is a standard payment.
*/
@jsonMember({ name: 'standard_payment', constructor: Boolean })
standardPayment: boolean;
public toBytes(): Uint8Array {
const calltableSerializer = new CalltableSerialization();
calltableSerializer.addField(0, CLValue.newCLUint8(0).bytes());
calltableSerializer.addField(
1,
CLValue.newCLUint64(this.paymentAmount).bytes()
);
calltableSerializer.addField(
2,
CLValue.newCLUint8(this.gasPriceTolerance).bytes()
);
calltableSerializer.addField(
3,
CLValue.newCLValueBool(this.standardPayment).bytes()
);
return calltableSerializer.toBytes();
}
}
/**
* Represents the fixed pricing mode, including a parameter for gas price tolerance.
*/
@jsonObject
export class FixedMode {
/**
* The tolerance for gas price fluctuations in fixed pricing mode.
*/
@jsonMember({ name: 'gas_price_tolerance', constructor: Number })
gasPriceTolerance: number;
/**
* User-specified additional computation factor (minimum 0).
*
* - If `0` is provided, no additional logic is applied to the computation limit.
* - Each value above `0` tells the node that it needs to treat the transaction
* as if it uses more gas than its serialized size indicates.
* - Each increment of `1` increases the "wasm lane" size bucket for this transaction by `1`.
*
* For example:
* - If the transaction's size indicates bucket `0` and `additionalComputationFactor = 2`,
* the transaction will be treated as if it belongs to bucket `2`.
*/
@jsonMember({ name: 'additional_computation_factor', constructor: Number })
additionalComputationFactor!: number;
public toBytes(): Uint8Array {
const calltableSerializer = new CalltableSerialization();
calltableSerializer.addField(0, CLValue.newCLUint8(1).bytes());
calltableSerializer.addField(
1,
CLValue.newCLUint8(this.gasPriceTolerance).bytes()
);
calltableSerializer.addField(
2,
CLValue.newCLUint8(this.additionalComputationFactor).bytes()
);
return calltableSerializer.toBytes();
}
}
/**
* Represents the prepair ( reserved before ) pricing mode, which includes a receipt hash.
*/
@jsonObject
export class PrepaidMode {
/**
* The receipt associated with the reserved pricing mode.
*/
@jsonMember({
name: 'receipt',
constructor: Hash,
deserializer: json => Hash.fromJSON(json),
serializer: value => value.toJSON()
})
receipt: Hash;
public toBytes(): Uint8Array {
const calltableSerializer = new CalltableSerialization();
calltableSerializer.addField(0, CLValue.newCLUint8(2).bytes());
calltableSerializer.addField(1, this.receipt.toBytes());
return calltableSerializer.toBytes();
}
}
/**
* Represents the pricing mode, which can be one of the following: PaymentLimited, Fixed, or Prepaid.
*/
@jsonObject
export class PricingMode {
/**
* The PaymentLimited pricing mode, if applicable.
*/
@jsonMember({ name: 'PaymentLimited', constructor: PaymentLimitedMode })
paymentLimited?: PaymentLimitedMode;
/**
* The fixed pricing mode, if applicable.
*/
@jsonMember({ name: 'Fixed', constructor: FixedMode })
fixed?: FixedMode;
/**
* The Prepaid pricing mode, if applicable.
*/
@jsonMember({ name: 'Prepaid', constructor: PrepaidMode })
prepaid?: PrepaidMode;
/**
* Converts the pricing mode instance into a byte array representation.
* This method serializes the current pricing mode into bytes that can be used for transactions.
*
* @returns A `Uint8Array` representing the serialized pricing mode.
*/
toBytes(): Uint8Array {
if (this.paymentLimited) {
return this.paymentLimited.toBytes();
} else if (this.fixed) {
return this.fixed.toBytes();
} else if (this.prepaid) {
return this.prepaid.toBytes();
}
throw new Error('Unable to serialize PricingMode');
}
}