@@ -2,6 +2,7 @@ import { derived, Readable } from 'svelte/store'
2
2
import { Int64 , API , Asset } from '@greymass/eosio'
3
3
import { Resources , SampleUsage , PowerUpState , RAMState , REXState } from '@greymass/eosio-resources'
4
4
import { activeBlockchain } from '~/store'
5
+ import { BNPrecision } from '@greymass/eosio-resources'
5
6
6
7
import { getClient } from '../../api-client'
7
8
import { ChainConfig , ChainFeatures , resourceFeatures } from '~/config'
@@ -112,12 +113,11 @@ export const cpuPowerupPrice = derived(
112
113
113
114
// price per kb
114
115
export const netPowerupPrice = derived (
115
- [ msToRent , sampleUsage , statePowerUp , info ] ,
116
- ( [ $msToRent , $sampleUsage , $statePowerUp , $info ] ) => {
117
- if ( $msToRent && $sampleUsage && $statePowerUp ) {
118
- const price = $statePowerUp . net . price_per_kb ( $sampleUsage , $msToRent , $info )
116
+ [ sampleUsage , statePowerUp , info ] ,
117
+ ( [ $sampleUsage , $statePowerUp , $info ] ) => {
118
+ if ( $sampleUsage && $statePowerUp ) {
119
119
return Asset . from (
120
- $statePowerUp . net . price_per_kb ( $sampleUsage , $msToRent , $info ) ,
120
+ $statePowerUp . net . price_per_kb ( $sampleUsage , 1 , $info ) ,
121
121
'4,EOS'
122
122
)
123
123
}
@@ -133,21 +133,18 @@ export const cpuStakingPrice = derived(
133
133
const { account } = $sampleUsage
134
134
const cpu_weight = Number ( account . total_resources . cpu_weight . units )
135
135
const cpu_limit = Number ( account . cpu_limit . max . value )
136
- let price = cpu_weight / cpu_limit
137
- if ( $activeBlockchain . resourceSampleMilliseconds ) {
138
- price *= $activeBlockchain . resourceSampleMilliseconds
139
- }
136
+ let price = ( cpu_weight / cpu_limit ) * $msToRent
140
137
return Asset . fromUnits ( price * 1000 , $activeBlockchain . coreTokenSymbol )
141
138
}
142
139
return Asset . from ( 0 , $activeBlockchain . coreTokenSymbol )
143
140
}
144
141
)
145
142
146
- // price per kb
143
+ // price per kb for staking
147
144
export const netStakingPrice = derived (
148
- [ activeBlockchain , msToRent , sampleUsage ] ,
149
- ( [ $activeBlockchain , $msToRent , $ sampleUsage] ) => {
150
- if ( $msToRent && $ sampleUsage) {
145
+ [ activeBlockchain , sampleUsage ] ,
146
+ ( [ $activeBlockchain , $sampleUsage ] ) => {
147
+ if ( $sampleUsage ) {
151
148
const { account } = $sampleUsage
152
149
const net_weight = Number ( account . total_resources . net_weight . units )
153
150
const net_limit = Number ( account . net_limit . max . value )
@@ -184,7 +181,7 @@ export const stateREX: Readable<REXState | undefined> = derived(
184
181
)
185
182
186
183
// The price of CPU in the REX system
187
- export const rexPrice = derived (
184
+ export const cpuRexPrice = derived (
188
185
[ msToRent , sampleUsage , stateREX ] ,
189
186
( [ $msToRent , $sampleUsage , $stateREX ] ) => {
190
187
if ( $msToRent && $sampleUsage && $stateREX ) {
@@ -194,6 +191,38 @@ export const rexPrice = derived(
194
191
}
195
192
)
196
193
194
+ // The price of Net in the REX system
195
+ export const netRexPrice = derived (
196
+ [ sampleUsage , stateREX ] ,
197
+ ( [ $sampleUsage , $stateREX ] ) => {
198
+ if ( $sampleUsage && $stateREX ) {
199
+ const price = calculateNetRexPrice ( $stateREX , $sampleUsage , 30000 ) ;
200
+ let precision = 4 ;
201
+ if ( price > 0 && price < 0.0001 ) {
202
+ precision = Number ( price . toExponential ( ) . split ( '-' ) [ 1 ] )
203
+ }
204
+ return Asset . from ( price , `${ precision } ,EOS` )
205
+ }
206
+ return Asset . from ( 0 , '4,EOS' )
207
+ }
208
+ )
209
+
210
+ function calculateNetRexPrice ( stateRex : REXState , sample : SampleUsage , unit = 1000 ) : number {
211
+ // Sample token units
212
+ const tokens = Asset . fromUnits ( 10000 , stateRex . symbol )
213
+
214
+ // Spending 1 EOS (10000 units) on REX gives this many tokens
215
+ const bancor = Number ( tokens . units ) / ( stateRex . total_rent . value / stateRex . total_unlent . value )
216
+ // The ratio of the number of tokens received vs the sampled values
217
+ const unitPrice = bancor * ( Number ( sample . net ) / BNPrecision )
218
+ // The token units spent per unit
219
+ const perunit = Number ( tokens . units ) / unitPrice
220
+ // Multiply the per unit cost by the units requested
221
+ const cost = perunit * unit
222
+ // Converting to an Asset
223
+ return cost / Math . pow ( 10 , stateRex . precision )
224
+ }
225
+
197
226
// The state of the REX system
198
227
export const stateRAM : Readable < RAMState | undefined > = derived (
199
228
[ activeBlockchain ] ,
0 commit comments