@wharfkit/resources 1.3.1 → 1.5.0-rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -37,35 +37,30 @@ export abstract class PowerUpStateResource extends Struct {
37
37
  abstract per_day(options?: PowerUpStateOptions): number
38
38
 
39
39
  // Get the current number of allocated units (shift from REX -> PowerUp)
40
- public get allocated() {
40
+ public get allocated_legacy() {
41
41
  return 1 - Number(this.weight_ratio) / Number(this.target_weight_ratio) / 100
42
42
  }
43
43
 
44
+ public get allocated(): number {
45
+ return 1 - Number(this.weight_ratio.dividing(this.target_weight_ratio)) / 100
46
+ }
47
+
44
48
  // Get the current percentage of reserved units
45
- public get reserved() {
49
+ public get reserved_legacy() {
46
50
  return new BN(String(this.utilization)) / new BN(String(this.weight))
47
51
  }
48
52
 
53
+ public get reserved(): Int64 {
54
+ return this.utilization.dividing(this.weight)
55
+ }
56
+
49
57
  // Get the symbol definition for the token
50
58
  public get symbol() {
51
59
  return this.min_price.symbol
52
60
  }
53
61
 
54
- // Common casting for typed values to numbers
55
- cast() {
56
- return {
57
- adjusted_utilization: Number(this.adjusted_utilization),
58
- decay_secs: Number(this.decay_secs.value),
59
- exponent: Number(this.exponent),
60
- utilization: Number(this.utilization),
61
- utilization_timestamp: Number(this.utilization_timestamp.value),
62
- weight: new BN(String(this.weight)),
63
- weight_ratio: Number(this.weight_ratio),
64
- }
65
- }
66
-
67
62
  // Mimic: https://github.com/EOSIO/eosio.contracts/blob/d7bc0a5cc8c0c2edd4dc61b0126517d0cb46fd94/contracts/eosio.system/src/powerup.cpp#L358
68
- utilization_increase(sample: UInt128, frac) {
63
+ utilization_increase_legacy(frac) {
69
64
  const {weight} = this
70
65
  const frac128 = UInt128.from(frac)
71
66
  const utilization_increase =
@@ -73,56 +68,127 @@ export abstract class PowerUpStateResource extends Struct {
73
68
  return Math.ceil(utilization_increase)
74
69
  }
75
70
 
71
+ utilization_increase(frac: UInt128) {
72
+ return frac.multiplying(this.weight).dividing(UInt128.from(Math.pow(10, 15)), 'ceil')
73
+ }
74
+
76
75
  // Mimic: https://github.com/EOSIO/eosio.contracts/blob/d7bc0a5cc8c0c2edd4dc61b0126517d0cb46fd94/contracts/eosio.system/src/powerup.cpp#L284-L298
77
- price_function(utilization: number): number {
78
- const {exponent, weight} = this.cast()
76
+ price_function_legacy(utilization: Int64): number {
77
+ const {exponent, weight} = this
79
78
  const max_price: number = this.max_price.value
80
79
  const min_price: number = this.min_price.value
81
80
  let price = min_price
82
- const new_exponent = exponent - 1.0
81
+ const new_exponent = Number(exponent) - 1.0
83
82
  if (new_exponent <= 0.0) {
84
83
  return max_price
85
84
  } else {
86
- const util_weight = new BN(utilization) / weight
87
- price += (max_price - min_price) * Math.pow(util_weight, new_exponent)
85
+ const util_weight = utilization.dividing(weight)
86
+ price += (max_price - min_price) * Math.pow(Number(util_weight), new_exponent)
87
+ }
88
+ return price
89
+ }
90
+
91
+ price_function(utilization: Int64): Int64 {
92
+ const {weight} = this
93
+ const price = this.min_price.units
94
+ const new_exponent = Number(this.exponent) - 1.0
95
+ if (new_exponent <= 0.0) {
96
+ return this.max_price.units
97
+ } else {
98
+ const util_weight = utilization.dividing(weight)
99
+ const difference = Asset.fromUnits(
100
+ this.max_price.units.subtracting(this.min_price.units),
101
+ this.symbol
102
+ )
103
+ price.add(difference.value * Math.pow(Number(util_weight), new_exponent))
88
104
  }
89
105
  return price
90
106
  }
91
107
 
92
108
  // Mimic: https://github.com/EOSIO/eosio.contracts/blob/d7bc0a5cc8c0c2edd4dc61b0126517d0cb46fd94/contracts/eosio.system/src/powerup.cpp#L274-L280
93
- price_integral_delta(start_utilization: number, end_utilization: number): number {
94
- const {exponent, weight} = this.cast()
109
+ price_integral_delta_legacy(start_utilization: Int64, end_utilization: Int64): number {
110
+ const {exponent, weight} = this
95
111
  const max_price: number = this.max_price.value
96
112
  const min_price: number = this.min_price.value
97
- const coefficient = (max_price - min_price) / exponent
98
- const start_u = new BN(start_utilization) / weight
99
- const end_u = new BN(end_utilization) / weight
113
+ const coefficient = (max_price - min_price) / exponent.value
114
+ const start_u = Number(start_utilization.dividing(weight))
115
+ const end_u = Number(end_utilization.dividing(weight))
100
116
  const delta =
101
117
  min_price * end_u -
102
118
  min_price * start_u +
103
- coefficient * Math.pow(end_u, exponent) -
104
- coefficient * Math.pow(start_u, exponent)
119
+ coefficient * Math.pow(end_u, exponent.value) -
120
+ coefficient * Math.pow(start_u, exponent.value)
121
+ return delta
122
+ }
123
+
124
+ price_integral_delta(start_utilization: Int64, end_utilization: Int64): number {
125
+ const difference = Asset.fromUnits(
126
+ this.max_price.units.subtracting(this.min_price.units),
127
+ this.symbol
128
+ )
129
+ const coefficient = difference.value / this.exponent.value
130
+ const start_u = Number(start_utilization.dividing(this.weight))
131
+ const end_u = Number(end_utilization.dividing(this.weight))
132
+ const delta =
133
+ this.min_price.value * end_u -
134
+ this.min_price.value * start_u +
135
+ coefficient * Math.pow(end_u, this.exponent.value) -
136
+ coefficient * Math.pow(start_u, this.exponent.value)
105
137
  return delta
106
138
  }
107
139
 
108
140
  // Mimic: https://github.com/EOSIO/eosio.contracts/blob/d7bc0a5cc8c0c2edd4dc61b0126517d0cb46fd94/contracts/eosio.system/src/powerup.cpp#L262-L315
109
- fee(utilization_increase, adjusted_utilization) {
110
- const {utilization, weight} = this.cast()
141
+ fee_legacy(utilization_increase, adjusted_utilization) {
142
+ const {utilization, weight} = this
111
143
 
112
- let start_utilization: number = utilization
113
- const end_utilization: number = start_utilization + utilization_increase
144
+ let start_utilization = Int64.from(utilization)
145
+ const end_utilization = start_utilization.adding(utilization_increase)
114
146
 
115
147
  let fee = 0
116
148
  if (start_utilization < adjusted_utilization) {
117
- const min = Math.min(utilization_increase, adjusted_utilization - start_utilization)
149
+ const min = Math.min(
150
+ utilization_increase,
151
+ adjusted_utilization.subtracting(start_utilization)
152
+ )
153
+ fee += Number(
154
+ new bigDecimal(this.price_function_legacy(adjusted_utilization) * min)
155
+ .divide(new bigDecimal(weight.toString()))
156
+ .getValue()
157
+ )
158
+ start_utilization = adjusted_utilization
159
+ }
160
+ if (start_utilization.gt(end_utilization)) {
161
+ fee += this.price_integral_delta(start_utilization, end_utilization)
162
+ }
163
+ return fee
164
+ }
165
+
166
+ fee(utilization_increase, adjusted_utilization) {
167
+ const {utilization, weight} = this
168
+
169
+ let start_utilization = Int64.from(utilization)
170
+ const end_utilization = start_utilization.adding(utilization_increase)
171
+
172
+ let fee = 0
173
+ if (start_utilization.lt(adjusted_utilization)) {
174
+ const min = Math.min(
175
+ utilization_increase,
176
+ adjusted_utilization.subtracting(start_utilization)
177
+ )
118
178
  fee += Number(
119
- new bigDecimal(this.price_function(adjusted_utilization) * min)
179
+ new bigDecimal(
180
+ String(
181
+ this.price_function(adjusted_utilization)
182
+ .dividing(Math.pow(10, this.symbol.precision))
183
+ .multiplying(min)
184
+ )
185
+ )
120
186
  .divide(new bigDecimal(weight.toString()))
121
187
  .getValue()
122
188
  )
123
189
  start_utilization = adjusted_utilization
124
190
  }
125
- if (start_utilization < end_utilization) {
191
+ if (start_utilization.gt(end_utilization)) {
126
192
  fee += this.price_integral_delta(start_utilization, end_utilization)
127
193
  }
128
194
  return fee
@@ -131,17 +197,19 @@ export abstract class PowerUpStateResource extends Struct {
131
197
  // Mimic: https://github.com/EOSIO/eosio.contracts/blob/d7bc0a5cc8c0c2edd4dc61b0126517d0cb46fd94/contracts/eosio.system/src/powerup.cpp#L105-L117
132
198
  determine_adjusted_utilization(options?: PowerUpStateOptions) {
133
199
  // Casting EOSIO types to usable formats for JS calculations
134
- const {decay_secs, utilization, utilization_timestamp} = this.cast()
135
- let {adjusted_utilization} = this.cast()
200
+ const {decay_secs, utilization, utilization_timestamp} = this
201
+ let {adjusted_utilization} = this
136
202
  // If utilization is less than adjusted, calculate real time value
137
- if (utilization < adjusted_utilization) {
203
+ if (utilization.lt(adjusted_utilization)) {
138
204
  // Create now & adjust JS timestamp to match EOSIO timestamp values
139
205
  const ts = options && options.timestamp ? options.timestamp : new Date()
140
206
  const now = TimePointSec.from(ts).toMilliseconds() / 1000
141
- const diff: number = adjusted_utilization - utilization
142
- let delta: number = diff * Math.exp(-(now - utilization_timestamp) / decay_secs)
207
+ const diff = adjusted_utilization.subtracting(utilization).toNumber()
208
+ let delta: number =
209
+ diff *
210
+ Math.exp(-(now - utilization_timestamp.toMilliseconds()) / Number(decay_secs))
143
211
  delta = Math.min(Math.max(delta, 0), diff) // Clamp the delta
144
- adjusted_utilization = utilization + delta
212
+ adjusted_utilization = utilization.adding(delta)
145
213
  }
146
214
  return adjusted_utilization
147
215
  }
@@ -1,9 +1,10 @@
1
- import {Struct, UInt128} from '@wharfkit/antelope'
1
+ import {Int64, Int64Type, Struct, UInt128, UInt128Type} from '@wharfkit/antelope'
2
2
 
3
- import {BNPrecision, SampleUsage} from '..'
3
+ import {BNPrecision, intToBigDecimal, SampleUsage} from '..'
4
4
  import {PowerUpStateResource} from './abstract'
5
5
  import {PowerUpStateOptions} from './options'
6
6
  import BN from 'bn.js'
7
+ import bigDecimal from 'js-big-decimal'
7
8
 
8
9
  @Struct.type('powerupstateresourcecpu')
9
10
  export class PowerUpStateResourceCPU extends PowerUpStateResource {
@@ -25,41 +26,80 @@ export class PowerUpStateResourceCPU extends PowerUpStateResource {
25
26
  }
26
27
 
27
28
  // Convert weight to μs (microseconds)
28
- weight_to_us(sample: UInt128, weight: number): number {
29
+ weight_to_us_legacy(sample: UInt128, weight: number): number {
29
30
  return Math.ceil((weight * Number(sample)) / BNPrecision)
30
31
  }
32
+ weight_to_us(sample: UInt128Type, weight: Int64Type): UInt128 {
33
+ return UInt128.from(
34
+ UInt128.from(weight).multiplying(Int64.from(sample)).dividing(BNPrecision, 'ceil')
35
+ )
36
+ }
31
37
 
32
38
  // Convert μs (microseconds) to weight
33
- us_to_weight(sample: UInt128, us: number): number {
39
+ us_to_weight_legacy(sample: UInt128, us: number): number {
34
40
  return Math.floor((us / Number(sample)) * BNPrecision)
35
41
  }
42
+ us_to_weight(sample: UInt128, us: Int64Type): Int64 {
43
+ return Int64.from(us).multiplying(BNPrecision).dividing(Int64.from(sample), 'floor')
44
+ }
36
45
 
37
46
  // Default frac generation by smallest unit type
47
+ frac_legacy = (usage: SampleUsage, us: number) => this.frac_by_us_legacy(usage, us)
38
48
  frac = (usage: SampleUsage, us: number) => this.frac_by_us(usage, us)
39
49
 
40
50
  // Frac generation by ms (milliseconds)
51
+ frac_by_ms_legacy = (usage: SampleUsage, ms: number) => this.frac_by_us_legacy(usage, ms * 1000)
41
52
  frac_by_ms = (usage: SampleUsage, ms: number) => this.frac_by_us(usage, ms * 1000)
42
53
 
43
54
  // Frac generation by μs (microseconds)
44
- frac_by_us(usage: SampleUsage, us: number) {
45
- const {weight} = this.cast()
46
- const frac = new BN(this.us_to_weight(usage.cpu, us)) / weight
55
+ frac_by_us_legacy(usage: SampleUsage, us: number) {
56
+ const frac = new BN(this.us_to_weight_legacy(usage.cpu, us)) / Number(this.weight)
47
57
  return Math.floor(frac * Math.pow(10, 15))
48
58
  }
59
+ frac_by_us(usage: SampleUsage, us: Int64Type): Int64 {
60
+ const precision = 15
61
+ const converted = intToBigDecimal(this.us_to_weight(usage.cpu, us))
62
+ const current = intToBigDecimal(this.weight)
63
+ const multiplier = intToBigDecimal(Math.pow(10, precision))
64
+ const frac = converted.divide(current, precision).multiply(multiplier)
65
+ return Int64.from(frac.getValue())
66
+ }
49
67
 
50
68
  // Price generation by smallest units, μs (microseconds)
69
+ price_per_legacy = (usage: SampleUsage, us = 1000, options?: PowerUpStateOptions): number =>
70
+ this.price_per_us_legacy(usage, us, options)
51
71
  price_per = (usage: SampleUsage, us = 1000, options?: PowerUpStateOptions): number =>
52
72
  this.price_per_us(usage, us, options)
53
73
 
54
74
  // Price generation by ms (milliseconds)
75
+ price_per_ms_legacy = (usage: SampleUsage, ms = 1, options?: PowerUpStateOptions): number =>
76
+ this.price_per_us_legacy(usage, ms * 1000, options)
55
77
  price_per_ms = (usage: SampleUsage, ms = 1, options?: PowerUpStateOptions): number =>
56
78
  this.price_per_us(usage, ms * 1000, options)
57
79
 
58
80
  // Price generation by μs (microseconds)
81
+ price_per_us_legacy(usage: SampleUsage, us = 1000, options?: PowerUpStateOptions): number {
82
+ // Determine the utilization increase by this action
83
+ const frac = UInt128.from(this.frac_legacy(usage, us))
84
+ const utilization_increase = this.utilization_increase_legacy(frac)
85
+
86
+ // Determine the adjusted utilization if needed
87
+ const adjusted_utilization = this.determine_adjusted_utilization(options)
88
+
89
+ // Derive the fee from the increase and utilization
90
+ const fee = this.fee_legacy(utilization_increase, adjusted_utilization)
91
+
92
+ // Force the fee up to the next highest value of precision
93
+ const precision = Math.pow(10, this.max_price.symbol.precision)
94
+ const value = Math.ceil(fee * precision) / precision
95
+
96
+ // Return the modified fee
97
+ return value
98
+ }
59
99
  price_per_us(usage: SampleUsage, us = 1000, options?: PowerUpStateOptions): number {
60
100
  // Determine the utilization increase by this action
61
101
  const frac = UInt128.from(this.frac(usage, us))
62
- const utilization_increase = this.utilization_increase(usage.cpu, frac)
102
+ const utilization_increase = this.utilization_increase(frac)
63
103
 
64
104
  // Determine the adjusted utilization if needed
65
105
  const adjusted_utilization = this.determine_adjusted_utilization(options)
@@ -1,6 +1,6 @@
1
- import {Struct, UInt128} from '@wharfkit/antelope'
1
+ import {Int64, Int64Type, Struct, UInt128} from '@wharfkit/antelope'
2
2
 
3
- import {BNPrecision, SampleUsage} from '..'
3
+ import {BNPrecision, intToBigDecimal, SampleUsage} from '..'
4
4
  import {PowerUpStateResource} from './abstract'
5
5
  import {PowerUpStateOptions} from './options'
6
6
  import BN from 'bn.js'
@@ -25,42 +25,85 @@ export class PowerUpStateResourceNET extends PowerUpStateResource {
25
25
  }
26
26
 
27
27
  // Convert weight to bytes
28
- weight_to_bytes(sample: UInt128, weight: number): number {
28
+ weight_to_bytes_legacy(sample: UInt128, weight: number): number {
29
29
  return Math.ceil((weight * Number(sample)) / BNPrecision)
30
30
  }
31
+ weight_to_bytes(sample: UInt128, weight: number): UInt128 {
32
+ return UInt128.from(
33
+ UInt128.from(weight).multiplying(Int64.from(sample)).dividing(BNPrecision, 'ceil')
34
+ )
35
+ }
31
36
 
32
37
  // Convert bytes to weight
33
- bytes_to_weight(sample: UInt128, bytes: number): number {
38
+ bytes_to_weight_legacy(sample: UInt128, bytes: number): number {
34
39
  return Math.floor((bytes / Number(sample)) * BNPrecision)
35
40
  }
41
+ bytes_to_weight(sample: UInt128, bytes: Int64Type): Int64 {
42
+ return Int64.from(bytes).multiplying(BNPrecision).dividing(Int64.from(sample), 'floor')
43
+ }
36
44
 
37
45
  // Default frac generation by smallest unit type
46
+ frac_legacy = (usage: SampleUsage, bytes: number) => this.frac_by_bytes_legacy(usage, bytes)
38
47
  frac = (usage: SampleUsage, bytes: number) => this.frac_by_bytes(usage, bytes)
39
48
 
40
49
  // Frac generation by kb
50
+ frac_by_kb_legacy = (usage: SampleUsage, kilobytes: number) =>
51
+ this.frac_by_bytes_legacy(usage, kilobytes * 1000)
41
52
  frac_by_kb = (usage: SampleUsage, kilobytes: number) =>
42
53
  this.frac_by_bytes(usage, kilobytes * 1000)
43
54
 
44
55
  // Frac generation by bytes
45
- frac_by_bytes(usage: SampleUsage, bytes: number) {
46
- const {weight} = this.cast()
47
- const frac = new BN(this.bytes_to_weight(usage.net, bytes)) / weight
56
+ frac_by_bytes_legacy(usage: SampleUsage, bytes: number) {
57
+ const {weight} = this
58
+ const frac = new BN(this.bytes_to_weight_legacy(usage.net, bytes)) / Number(weight)
48
59
  return Math.floor(frac * Math.pow(10, 15))
49
60
  }
61
+ frac_by_bytes(usage: SampleUsage, bytes: Int64Type): Int64 {
62
+ const precision = 15
63
+ const converted = intToBigDecimal(this.bytes_to_weight(usage.net, bytes))
64
+ const current = intToBigDecimal(this.weight)
65
+ const multiplier = intToBigDecimal(Math.pow(10, precision))
66
+ const frac = converted.divide(current, precision).multiply(multiplier)
67
+ return Int64.from(frac.getValue())
68
+ }
50
69
 
51
70
  // Price generation by smallest units, bytes
71
+ price_per_legacy = (usage: SampleUsage, bytes = 1000, options?: PowerUpStateOptions) =>
72
+ this.price_per_byte_legacy(usage, bytes, options)
52
73
  price_per = (usage: SampleUsage, bytes = 1000, options?: PowerUpStateOptions) =>
53
74
  this.price_per_byte(usage, bytes, options)
54
75
 
55
76
  // Price generation by kb
77
+ price_per_kb_legacy = (
78
+ usage: SampleUsage,
79
+ kilobytes = 1,
80
+ options?: PowerUpStateOptions
81
+ ): number => this.price_per_byte_legacy(usage, kilobytes * 1000, options)
56
82
  price_per_kb = (usage: SampleUsage, kilobytes = 1, options?: PowerUpStateOptions): number =>
57
83
  this.price_per_byte(usage, kilobytes * 1000, options)
58
84
 
59
85
  // Price generation by bytes
86
+ price_per_byte_legacy(usage: SampleUsage, bytes = 1000, options?: PowerUpStateOptions): number {
87
+ // Determine the utilization increase by this action
88
+ const frac = UInt128.from(this.frac_legacy(usage, bytes))
89
+ const utilization_increase = this.utilization_increase_legacy(frac)
90
+
91
+ // Determine the adjusted utilization if needed
92
+ const adjusted_utilization = this.determine_adjusted_utilization(options)
93
+
94
+ // Derive the fee from the increase and utilization
95
+ const fee = this.fee_legacy(utilization_increase, adjusted_utilization)
96
+
97
+ const precision = Math.pow(10, this.max_price.symbol.precision)
98
+ const value = Math.ceil(fee * precision) / precision
99
+
100
+ // Return the modified fee
101
+ return value
102
+ }
60
103
  price_per_byte(usage: SampleUsage, bytes = 1000, options?: PowerUpStateOptions): number {
61
104
  // Determine the utilization increase by this action
62
105
  const frac = UInt128.from(this.frac(usage, bytes))
63
- const utilization_increase = this.utilization_increase(usage.net, frac)
106
+ const utilization_increase = this.utilization_increase(frac)
64
107
 
65
108
  // Determine the adjusted utilization if needed
66
109
  const adjusted_utilization = this.determine_adjusted_utilization(options)