@wharfkit/resources 1.4.0-rc1 → 1.5.0-rc2

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.
@@ -13,6 +13,7 @@ import {
13
13
  import BN from 'bn.js'
14
14
  import {PowerUpStateOptions} from './options'
15
15
  import bigDecimal from 'js-big-decimal'
16
+ import {intToBigDecimal} from '..'
16
17
 
17
18
  export abstract class PowerUpStateResource extends Struct {
18
19
  @Struct.field('uint8') version!: UInt8
@@ -37,35 +38,30 @@ export abstract class PowerUpStateResource extends Struct {
37
38
  abstract per_day(options?: PowerUpStateOptions): number
38
39
 
39
40
  // Get the current number of allocated units (shift from REX -> PowerUp)
40
- public get allocated() {
41
+ public get allocated_legacy() {
41
42
  return 1 - Number(this.weight_ratio) / Number(this.target_weight_ratio) / 100
42
43
  }
43
44
 
45
+ public get allocated(): number {
46
+ return 1 - Number(this.weight_ratio.dividing(this.target_weight_ratio)) / 100
47
+ }
48
+
44
49
  // Get the current percentage of reserved units
45
- public get reserved() {
50
+ public get reserved_legacy() {
46
51
  return new BN(String(this.utilization)) / new BN(String(this.weight))
47
52
  }
48
53
 
54
+ public get reserved(): Int64 {
55
+ return this.utilization.dividing(this.weight)
56
+ }
57
+
49
58
  // Get the symbol definition for the token
50
59
  public get symbol() {
51
60
  return this.min_price.symbol
52
61
  }
53
62
 
54
- // Common casting for typed values to numbers
55
- cast() {
56
- return {
57
- adjusted_utilization: String(this.adjusted_utilization),
58
- decay_secs: Number(this.decay_secs.value),
59
- exponent: Number(this.exponent),
60
- utilization: String(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
63
  // Mimic: https://github.com/EOSIO/eosio.contracts/blob/d7bc0a5cc8c0c2edd4dc61b0126517d0cb46fd94/contracts/eosio.system/src/powerup.cpp#L358
68
- utilization_increase(sample: UInt128, frac) {
64
+ utilization_increase_legacy(frac) {
69
65
  const {weight} = this
70
66
  const frac128 = UInt128.from(frac)
71
67
  const utilization_increase =
@@ -73,42 +69,80 @@ export abstract class PowerUpStateResource extends Struct {
73
69
  return Math.ceil(utilization_increase)
74
70
  }
75
71
 
72
+ utilization_increase(frac: UInt128) {
73
+ return frac.multiplying(this.weight).dividing(UInt128.from(Math.pow(10, 15)), 'ceil')
74
+ }
75
+
76
76
  // 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()
77
+ price_function_legacy(utilization: Int64): number {
78
+ const {exponent, weight} = this
79
79
  const max_price: number = this.max_price.value
80
80
  const min_price: number = this.min_price.value
81
81
  let price = min_price
82
- const new_exponent = exponent - 1.0
82
+ const new_exponent = Number(exponent) - 1.0
83
83
  if (new_exponent <= 0.0) {
84
84
  return max_price
85
85
  } else {
86
- const util_weight = new BN(utilization) / weight
86
+ const util_weight = new BN(Number(utilization)) / Number(weight)
87
+ console.log('old', String(util_weight))
88
+ console.log('diff', max_price - min_price)
87
89
  price += (max_price - min_price) * Math.pow(util_weight, new_exponent)
88
90
  }
89
91
  return price
90
92
  }
91
93
 
94
+ price_function(utilization: Int64): number {
95
+ const {weight} = this
96
+ let price = this.min_price.value
97
+ const new_exponent = Number(this.exponent) - 1.0
98
+ if (new_exponent <= 0.0) {
99
+ return this.max_price.value
100
+ } else {
101
+ // const util_weight = utilization.dividing(weight)
102
+ const util_weight = intToBigDecimal(utilization).divide(intToBigDecimal(weight), 18)
103
+ console.log('new', String(util_weight.getValue()))
104
+ const difference = this.max_price.units.subtracting(this.min_price.units)
105
+ console.log('diff', String(difference))
106
+ price += difference.value * Math.pow(Number(util_weight.getValue()), new_exponent)
107
+ }
108
+ return price
109
+ }
110
+
92
111
  // Mimic: https://github.com/EOSIO/eosio.contracts/blob/d7bc0a5cc8c0c2edd4dc61b0126517d0cb46fd94/contracts/eosio.system/src/powerup.cpp#L274-L280
93
- price_integral_delta(start_utilization: Int64, end_utilization: Int64): number {
94
- const {exponent, weight} = this.cast()
112
+ price_integral_delta_legacy(start_utilization: Int64, end_utilization: Int64): number {
113
+ const {exponent, weight} = this
95
114
  const max_price: number = this.max_price.value
96
115
  const min_price: number = this.min_price.value
97
- const coefficient = (max_price - min_price) / exponent
116
+ const coefficient = (max_price - min_price) / exponent.value
98
117
  const start_u = Number(start_utilization.dividing(weight))
99
118
  const end_u = Number(end_utilization.dividing(weight))
100
119
  const delta =
101
120
  min_price * end_u -
102
121
  min_price * start_u +
103
- coefficient * Math.pow(end_u, exponent) -
104
- coefficient * Math.pow(start_u, exponent)
122
+ coefficient * Math.pow(end_u, exponent.value) -
123
+ coefficient * Math.pow(start_u, exponent.value)
124
+ return delta
125
+ }
126
+
127
+ price_integral_delta(start_utilization: Int64, end_utilization: Int64): number {
128
+ const difference = Asset.fromUnits(
129
+ this.max_price.units.subtracting(this.min_price.units),
130
+ this.symbol
131
+ )
132
+ const coefficient = difference.value / this.exponent.value
133
+ const start_u = Number(start_utilization.dividing(this.weight))
134
+ const end_u = Number(end_utilization.dividing(this.weight))
135
+ const delta =
136
+ this.min_price.value * end_u -
137
+ this.min_price.value * start_u +
138
+ coefficient * Math.pow(end_u, this.exponent.value) -
139
+ coefficient * Math.pow(start_u, this.exponent.value)
105
140
  return delta
106
141
  }
107
142
 
108
143
  // 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 {weight} = this.cast()
111
- const {utilization} = this
144
+ fee_legacy(utilization_increase, adjusted_utilization) {
145
+ const {utilization, weight} = this
112
146
 
113
147
  let start_utilization = Int64.from(utilization)
114
148
  const end_utilization = start_utilization.adding(utilization_increase)
@@ -120,7 +154,38 @@ export abstract class PowerUpStateResource extends Struct {
120
154
  adjusted_utilization.subtracting(start_utilization)
121
155
  )
122
156
  fee += Number(
123
- new bigDecimal(this.price_function(adjusted_utilization) * min)
157
+ new bigDecimal(this.price_function_legacy(adjusted_utilization) * min)
158
+ .divide(new bigDecimal(weight.toString()))
159
+ .getValue()
160
+ )
161
+ start_utilization = adjusted_utilization
162
+ }
163
+ if (start_utilization.gt(end_utilization)) {
164
+ fee += this.price_integral_delta(start_utilization, end_utilization)
165
+ }
166
+ return fee
167
+ }
168
+
169
+ fee(utilization_increase: UInt128, adjusted_utilization: Int64) {
170
+ const {utilization, weight} = this
171
+
172
+ let start_utilization = Int64.from(utilization)
173
+ const end_utilization = start_utilization.adding(utilization_increase)
174
+
175
+ let fee = 0
176
+ if (start_utilization.lt(adjusted_utilization)) {
177
+ const min = Math.min(
178
+ Number(utilization_increase),
179
+ Number(adjusted_utilization.subtracting(start_utilization))
180
+ )
181
+ fee += Number(
182
+ new bigDecimal(
183
+ String(
184
+ (this.price_function(adjusted_utilization) /
185
+ Math.pow(10, this.symbol.precision)) *
186
+ min
187
+ )
188
+ )
124
189
  .divide(new bigDecimal(weight.toString()))
125
190
  .getValue()
126
191
  )
@@ -135,8 +200,7 @@ export abstract class PowerUpStateResource extends Struct {
135
200
  // Mimic: https://github.com/EOSIO/eosio.contracts/blob/d7bc0a5cc8c0c2edd4dc61b0126517d0cb46fd94/contracts/eosio.system/src/powerup.cpp#L105-L117
136
201
  determine_adjusted_utilization(options?: PowerUpStateOptions) {
137
202
  // Casting EOSIO types to usable formats for JS calculations
138
- const {decay_secs, utilization_timestamp} = this.cast()
139
- const {utilization} = this
203
+ const {decay_secs, utilization, utilization_timestamp} = this
140
204
  let {adjusted_utilization} = this
141
205
  // If utilization is less than adjusted, calculate real time value
142
206
  if (utilization.lt(adjusted_utilization)) {
@@ -144,7 +208,9 @@ export abstract class PowerUpStateResource extends Struct {
144
208
  const ts = options && options.timestamp ? options.timestamp : new Date()
145
209
  const now = TimePointSec.from(ts).toMilliseconds() / 1000
146
210
  const diff = adjusted_utilization.subtracting(utilization).toNumber()
147
- let delta: number = diff * Math.exp(-(now - utilization_timestamp) / decay_secs)
211
+ let delta: number =
212
+ diff *
213
+ Math.exp(-(now - utilization_timestamp.toMilliseconds()) / Number(decay_secs))
148
214
  delta = Math.min(Math.max(delta, 0), diff) // Clamp the delta
149
215
  adjusted_utilization = utilization.adding(delta)
150
216
  }
@@ -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)