@wharfkit/resources 1.4.0-rc1 → 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.
@@ -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)