@wharfkit/resources 1.5.0-rc3 → 1.5.0-rc5
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.
- package/lib/wharfkit-resources.d.ts +3 -24
- package/lib/wharfkit-resources.js +22 -163
- package/lib/wharfkit-resources.js.map +1 -1
- package/lib/wharfkit-resources.m.js +22 -163
- package/lib/wharfkit-resources.m.js.map +1 -1
- package/package.json +1 -1
- package/src/powerup/abstract.ts +3 -108
- package/src/powerup/cpu.ts +21 -59
- package/src/powerup/net.ts +0 -39
- package/src/powerup/options.ts +3 -1
- package/src/rex.ts +10 -9
package/src/powerup/cpu.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import {Int64, Int64Type, Struct, UInt128, UInt128Type} from '@wharfkit/antelope'
|
|
1
|
+
import {Asset, Int64, Int64Type, Struct, UInt128, UInt128Type} from '@wharfkit/antelope'
|
|
2
2
|
|
|
3
3
|
import {BNPrecision, intToBigDecimal, SampleUsage} from '..'
|
|
4
4
|
import {PowerUpStateResource} from './abstract'
|
|
5
5
|
import {PowerUpStateOptions} from './options'
|
|
6
|
-
import BN from 'bn.js'
|
|
7
|
-
import bigDecimal from 'js-big-decimal'
|
|
8
6
|
|
|
9
7
|
@Struct.type('powerupstateresourcecpu')
|
|
10
8
|
export class PowerUpStateResourceCPU extends PowerUpStateResource {
|
|
@@ -26,9 +24,6 @@ export class PowerUpStateResourceCPU extends PowerUpStateResource {
|
|
|
26
24
|
}
|
|
27
25
|
|
|
28
26
|
// Convert weight to μs (microseconds)
|
|
29
|
-
weight_to_us_legacy(sample: UInt128, weight: number): number {
|
|
30
|
-
return Math.ceil((weight * Number(sample)) / BNPrecision)
|
|
31
|
-
}
|
|
32
27
|
weight_to_us(sample: UInt128Type, weight: Int64Type): UInt128 {
|
|
33
28
|
return UInt128.from(
|
|
34
29
|
UInt128.from(weight).multiplying(Int64.from(sample)).dividing(BNPrecision, 'ceil')
|
|
@@ -36,29 +31,17 @@ export class PowerUpStateResourceCPU extends PowerUpStateResource {
|
|
|
36
31
|
}
|
|
37
32
|
|
|
38
33
|
// Convert μs (microseconds) to weight
|
|
39
|
-
us_to_weight_legacy(sample: UInt128, us: number): number {
|
|
40
|
-
const sampled = intToBigDecimal(sample)
|
|
41
|
-
return Math.floor((us / Number(sampled.getValue())) * BNPrecision)
|
|
42
|
-
}
|
|
43
34
|
us_to_weight(sample: UInt128, us: Int64Type): Int64 {
|
|
44
|
-
return Int64.from(us).multiplying(BNPrecision).dividing(
|
|
35
|
+
return Int64.from(us).multiplying(BNPrecision).dividing(sample, 'floor')
|
|
45
36
|
}
|
|
46
37
|
|
|
47
38
|
// Default frac generation by smallest unit type
|
|
48
|
-
frac_legacy = (usage: SampleUsage, us: number) => this.frac_by_us_legacy(usage, us)
|
|
49
39
|
frac = (usage: SampleUsage, us: number) => this.frac_by_us(usage, us)
|
|
50
40
|
|
|
51
41
|
// Frac generation by ms (milliseconds)
|
|
52
|
-
frac_by_ms_legacy = (usage: SampleUsage, ms: number) => this.frac_by_us_legacy(usage, ms * 1000)
|
|
53
42
|
frac_by_ms = (usage: SampleUsage, ms: number) => this.frac_by_us(usage, ms * 1000)
|
|
54
43
|
|
|
55
44
|
// Frac generation by μs (microseconds)
|
|
56
|
-
frac_by_us_legacy(usage: SampleUsage, us: number) {
|
|
57
|
-
const weight = intToBigDecimal(this.us_to_weight_legacy(usage.cpu, us))
|
|
58
|
-
const current = intToBigDecimal(this.weight)
|
|
59
|
-
const frac = weight.divide(current)
|
|
60
|
-
return Math.floor(Number(frac.getValue()) * Math.pow(10, 15))
|
|
61
|
-
}
|
|
62
45
|
frac_by_us(usage: SampleUsage, us: Int64Type): Int64 {
|
|
63
46
|
const precision = 15
|
|
64
47
|
const converted = intToBigDecimal(this.us_to_weight(usage.cpu, us))
|
|
@@ -69,64 +52,43 @@ export class PowerUpStateResourceCPU extends PowerUpStateResource {
|
|
|
69
52
|
}
|
|
70
53
|
|
|
71
54
|
// Price generation by smallest units, μs (microseconds)
|
|
72
|
-
price_per_legacy = (usage: SampleUsage, us = 1000, options?: PowerUpStateOptions): number =>
|
|
73
|
-
this.price_per_us_legacy(usage, us, options)
|
|
74
55
|
price_per = (usage: SampleUsage, us = 1000, options?: PowerUpStateOptions): number =>
|
|
75
56
|
this.price_per_us(usage, us, options)
|
|
76
57
|
|
|
77
58
|
// Price generation by ms (milliseconds)
|
|
78
|
-
price_per_ms_legacy = (usage: SampleUsage, ms = 1, options?: PowerUpStateOptions): number =>
|
|
79
|
-
this.price_per_us_legacy(usage, ms * 1000, options)
|
|
80
59
|
price_per_ms = (usage: SampleUsage, ms = 1, options?: PowerUpStateOptions): number =>
|
|
81
60
|
this.price_per_us(usage, ms * 1000, options)
|
|
82
61
|
|
|
83
62
|
// Price generation by μs (microseconds)
|
|
84
|
-
price_per_us_legacy(usage: SampleUsage, us = 1000, options?: PowerUpStateOptions): number {
|
|
85
|
-
// Determine the utilization increase by this action
|
|
86
|
-
const frac = UInt128.from(this.frac_legacy(usage, us))
|
|
87
|
-
console.log('frac', String(frac))
|
|
88
|
-
|
|
89
|
-
const utilization_increase = this.utilization_increase_legacy(frac)
|
|
90
|
-
console.log('utilization_increase', String(utilization_increase))
|
|
91
|
-
|
|
92
|
-
// Determine the adjusted utilization if needed
|
|
93
|
-
const adjusted_utilization = this.determine_adjusted_utilization(options)
|
|
94
|
-
console.log('adjusted_utilization', String(adjusted_utilization))
|
|
95
|
-
|
|
96
|
-
// Derive the fee from the increase and utilization
|
|
97
|
-
const fee = this.fee_legacy(utilization_increase, adjusted_utilization)
|
|
98
|
-
console.log('fee', fee)
|
|
99
|
-
|
|
100
|
-
// Force the fee up to the next highest value of precision
|
|
101
|
-
const precision = Math.pow(10, this.max_price.symbol.precision)
|
|
102
|
-
const value = Math.ceil(fee * precision) / precision
|
|
103
|
-
console.log('ceil', value)
|
|
104
|
-
|
|
105
|
-
// Return the modified fee
|
|
106
|
-
return value
|
|
107
|
-
}
|
|
108
63
|
price_per_us(usage: SampleUsage, us = 1000, options?: PowerUpStateOptions): number {
|
|
109
64
|
// Determine the utilization increase by this action
|
|
110
65
|
const frac = UInt128.from(this.frac(usage, us))
|
|
111
|
-
console.log('frac', String(frac))
|
|
112
|
-
|
|
113
66
|
const utilization_increase = this.utilization_increase(frac)
|
|
114
|
-
console.log('utilization_increase', String(utilization_increase))
|
|
115
|
-
|
|
116
67
|
// Determine the adjusted utilization if needed
|
|
117
68
|
const adjusted_utilization = this.determine_adjusted_utilization(options)
|
|
118
|
-
console.log('adjusted_utilization', String(adjusted_utilization))
|
|
119
|
-
|
|
120
69
|
// Derive the fee from the increase and utilization
|
|
121
70
|
const fee = this.fee(utilization_increase, adjusted_utilization)
|
|
122
|
-
console.log('fee', fee)
|
|
123
|
-
|
|
124
71
|
// Force the fee up to the next highest value of precision
|
|
125
72
|
const precision = Math.pow(10, this.max_price.symbol.precision)
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
73
|
+
const price = fee * precision
|
|
74
|
+
const value = Math.ceil(price) / precision
|
|
75
|
+
const asset = Asset.fromFloat(fee, this.symbol)
|
|
76
|
+
if (price < 1) {
|
|
77
|
+
throw new Error(
|
|
78
|
+
`Price (${String(
|
|
79
|
+
asset
|
|
80
|
+
)}) for requested CPU amount (${us}us) below required precision, increase requested amount.`
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
if (options && options.min_payment && options.min_payment.units.gt(asset.units)) {
|
|
84
|
+
throw new Error(
|
|
85
|
+
`Price (${String(
|
|
86
|
+
asset
|
|
87
|
+
)}) for requested CPU amount (${us}us) below minimum required payment (${String(
|
|
88
|
+
options.min_payment
|
|
89
|
+
)}), increase requested CPU amount.`
|
|
90
|
+
)
|
|
91
|
+
}
|
|
130
92
|
// Return the modified fee
|
|
131
93
|
return value
|
|
132
94
|
}
|
package/src/powerup/net.ts
CHANGED
|
@@ -3,7 +3,6 @@ import {Int64, Int64Type, Struct, UInt128} from '@wharfkit/antelope'
|
|
|
3
3
|
import {BNPrecision, intToBigDecimal, SampleUsage} from '..'
|
|
4
4
|
import {PowerUpStateResource} from './abstract'
|
|
5
5
|
import {PowerUpStateOptions} from './options'
|
|
6
|
-
import BN from 'bn.js'
|
|
7
6
|
|
|
8
7
|
@Struct.type('powerupstateresourcenet')
|
|
9
8
|
export class PowerUpStateResourceNET extends PowerUpStateResource {
|
|
@@ -25,9 +24,6 @@ export class PowerUpStateResourceNET extends PowerUpStateResource {
|
|
|
25
24
|
}
|
|
26
25
|
|
|
27
26
|
// Convert weight to bytes
|
|
28
|
-
weight_to_bytes_legacy(sample: UInt128, weight: number): number {
|
|
29
|
-
return Math.ceil((weight * Number(sample)) / BNPrecision)
|
|
30
|
-
}
|
|
31
27
|
weight_to_bytes(sample: UInt128, weight: number): UInt128 {
|
|
32
28
|
return UInt128.from(
|
|
33
29
|
UInt128.from(weight).multiplying(Int64.from(sample)).dividing(BNPrecision, 'ceil')
|
|
@@ -35,29 +31,18 @@ export class PowerUpStateResourceNET extends PowerUpStateResource {
|
|
|
35
31
|
}
|
|
36
32
|
|
|
37
33
|
// Convert bytes to weight
|
|
38
|
-
bytes_to_weight_legacy(sample: UInt128, bytes: number): number {
|
|
39
|
-
return Math.floor((bytes / Number(sample)) * BNPrecision)
|
|
40
|
-
}
|
|
41
34
|
bytes_to_weight(sample: UInt128, bytes: Int64Type): Int64 {
|
|
42
35
|
return Int64.from(bytes).multiplying(BNPrecision).dividing(Int64.from(sample), 'floor')
|
|
43
36
|
}
|
|
44
37
|
|
|
45
38
|
// Default frac generation by smallest unit type
|
|
46
|
-
frac_legacy = (usage: SampleUsage, bytes: number) => this.frac_by_bytes_legacy(usage, bytes)
|
|
47
39
|
frac = (usage: SampleUsage, bytes: number) => this.frac_by_bytes(usage, bytes)
|
|
48
40
|
|
|
49
41
|
// Frac generation by kb
|
|
50
|
-
frac_by_kb_legacy = (usage: SampleUsage, kilobytes: number) =>
|
|
51
|
-
this.frac_by_bytes_legacy(usage, kilobytes * 1000)
|
|
52
42
|
frac_by_kb = (usage: SampleUsage, kilobytes: number) =>
|
|
53
43
|
this.frac_by_bytes(usage, kilobytes * 1000)
|
|
54
44
|
|
|
55
45
|
// Frac generation by bytes
|
|
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)
|
|
59
|
-
return Math.floor(frac * Math.pow(10, 15))
|
|
60
|
-
}
|
|
61
46
|
frac_by_bytes(usage: SampleUsage, bytes: Int64Type): Int64 {
|
|
62
47
|
const precision = 15
|
|
63
48
|
const converted = intToBigDecimal(this.bytes_to_weight(usage.net, bytes))
|
|
@@ -68,38 +53,14 @@ export class PowerUpStateResourceNET extends PowerUpStateResource {
|
|
|
68
53
|
}
|
|
69
54
|
|
|
70
55
|
// 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)
|
|
73
56
|
price_per = (usage: SampleUsage, bytes = 1000, options?: PowerUpStateOptions) =>
|
|
74
57
|
this.price_per_byte(usage, bytes, options)
|
|
75
58
|
|
|
76
59
|
// 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)
|
|
82
60
|
price_per_kb = (usage: SampleUsage, kilobytes = 1, options?: PowerUpStateOptions): number =>
|
|
83
61
|
this.price_per_byte(usage, kilobytes * 1000, options)
|
|
84
62
|
|
|
85
63
|
// 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
|
-
}
|
|
103
64
|
price_per_byte(usage: SampleUsage, bytes = 1000, options?: PowerUpStateOptions): number {
|
|
104
65
|
// Determine the utilization increase by this action
|
|
105
66
|
const frac = UInt128.from(this.frac(usage, bytes))
|
package/src/powerup/options.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {TimePointType, UInt64} from '@wharfkit/antelope'
|
|
1
|
+
import {Asset, TimePointType, UInt64} from '@wharfkit/antelope'
|
|
2
2
|
|
|
3
3
|
export interface PowerUpStateOptions {
|
|
4
4
|
// timestamp to base adjusted_utilization off
|
|
@@ -6,4 +6,6 @@ export interface PowerUpStateOptions {
|
|
|
6
6
|
// blockchain resource limits for calculating usage
|
|
7
7
|
virtual_block_cpu_limit?: UInt64
|
|
8
8
|
virtual_block_net_limit?: UInt64
|
|
9
|
+
// minimum payment amount
|
|
10
|
+
min_payment?: Asset
|
|
9
11
|
}
|
package/src/rex.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {BNPrecision, Resources, SampleUsage} from './'
|
|
1
|
+
import {BNPrecision, intToBigDecimal, Resources, SampleUsage} from './'
|
|
2
2
|
|
|
3
3
|
import {Asset, Struct, UInt128, UInt64, UInt8} from '@wharfkit/antelope'
|
|
4
4
|
|
|
@@ -26,17 +26,18 @@ export class REXState extends Struct {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
public get value() {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
)
|
|
29
|
+
const lent = intToBigDecimal(this.total_lent.units)
|
|
30
|
+
const unlent = intToBigDecimal(this.total_unlent.units)
|
|
31
|
+
const rex = intToBigDecimal(this.total_rex.units)
|
|
32
|
+
return Number(lent.add(unlent).divide(rex, 18).getValue())
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
exchange(amount: Asset): Asset {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
)
|
|
36
|
+
const value = intToBigDecimal(amount.units)
|
|
37
|
+
const lendable = intToBigDecimal(this.total_lendable.units)
|
|
38
|
+
const rex = intToBigDecimal(this.total_rex.units)
|
|
39
|
+
const tokens = value.multiply(lendable).divide(rex, this.precision)
|
|
40
|
+
return Asset.fromUnits(Number(tokens.getValue()), this.symbol)
|
|
40
41
|
}
|
|
41
42
|
|
|
42
43
|
cpu_price_per_ms(sample: SampleUsage, ms = 1): number {
|