Skip to content

Commit 1b63f0e

Browse files
committed
fix eos symbol to core token symbol
1 parent 5766cae commit 1b63f0e

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

src/pages/resources/resources.ts

+28-21
Original file line numberDiff line numberDiff line change
@@ -99,29 +99,29 @@ export const msToRent: Readable<number> = derived(activeBlockchain, ($activeBloc
9999

100100
//price per ms
101101
export const cpuPowerupPrice = derived(
102-
[msToRent, sampleUsage, statePowerUp, info],
103-
([$msToRent, $sampleUsage, $statePowerUp, $info]) => {
102+
[activeBlockchain, msToRent, sampleUsage, statePowerUp, info],
103+
([$activeBlockchain, $msToRent, $sampleUsage, $statePowerUp, $info]) => {
104104
if ($msToRent && $sampleUsage && $statePowerUp) {
105105
return Asset.from(
106106
$statePowerUp.cpu.price_per_ms($sampleUsage, $msToRent, $info),
107-
'4,EOS'
107+
$activeBlockchain.coreTokenSymbol
108108
)
109109
}
110-
return Asset.from(0, '4,EOS')
110+
return Asset.from(0, $activeBlockchain.coreTokenSymbol)
111111
}
112112
)
113113

114114
// price per kb
115115
export const netPowerupPrice = derived(
116-
[sampleUsage, statePowerUp, info],
117-
([$sampleUsage, $statePowerUp, $info]) => {
116+
[activeBlockchain,sampleUsage, statePowerUp, info],
117+
([$activeBlockchain, $sampleUsage, $statePowerUp, $info]) => {
118118
if ($sampleUsage && $statePowerUp) {
119119
return Asset.from(
120120
$statePowerUp.net.price_per_kb($sampleUsage, 1, $info),
121-
'4,EOS'
121+
$activeBlockchain.coreTokenSymbol
122122
)
123123
}
124-
return Asset.from(0, '4,EOS')
124+
return Asset.from(0, $activeBlockchain.coreTokenSymbol)
125125
}
126126
)
127127

@@ -182,31 +182,38 @@ export const stateREX: Readable<REXState | undefined> = derived(
182182

183183
// The price of CPU in the REX system
184184
export const cpuRexPrice = derived(
185-
[msToRent, sampleUsage, stateREX],
186-
([$msToRent, $sampleUsage, $stateREX]) => {
185+
[activeBlockchain, msToRent, sampleUsage, stateREX],
186+
([$activeBlockchain, $msToRent, $sampleUsage, $stateREX]) => {
187187
if ($msToRent && $sampleUsage && $stateREX) {
188-
return Asset.from($stateREX.price_per($sampleUsage, $msToRent * 30000), '4,EOS')
188+
const price = $stateREX.price_per($sampleUsage, $msToRent * 30000);
189+
const coreTokenSymbol = $activeBlockchain.coreTokenSymbol
190+
return compatPriceWithPrecision(price, coreTokenSymbol)
189191
}
190-
return Asset.from(0, '4,EOS')
192+
return Asset.from(0, $activeBlockchain.coreTokenSymbol)
191193
}
192194
)
193195

194196
// The price of Net in the REX system
195197
export const netRexPrice = derived(
196-
[sampleUsage, stateREX],
197-
([$sampleUsage, $stateREX]) => {
198+
[activeBlockchain, sampleUsage, stateREX],
199+
([$activeBlockchain, $sampleUsage, $stateREX]) => {
198200
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`)
201+
const price = calculateNetRexPrice($stateREX, $sampleUsage, 30000)
202+
const coreTokenSymbol = $activeBlockchain.coreTokenSymbol
203+
return compatPriceWithPrecision(price, coreTokenSymbol)
205204
}
206-
return Asset.from(0, '4,EOS')
205+
return Asset.from(0, $activeBlockchain.coreTokenSymbol)
207206
}
208207
)
209208

209+
function compatPriceWithPrecision(price : number, coreTokenSymbol : Asset.Symbol){
210+
let precision = coreTokenSymbol.precision;
211+
if (price > 0 && price < 1/Math.pow(10, precision)) {
212+
precision = Number(price.toExponential().split('-')[1])
213+
}
214+
return Asset.from(price, `${precision},${coreTokenSymbol.name}`)
215+
}
216+
210217
function calculateNetRexPrice(stateRex: REXState, sample: SampleUsage, unit = 1000): number {
211218
// Sample token units
212219
const tokens = Asset.fromUnits(10000, stateRex.symbol)

0 commit comments

Comments
 (0)