Skip to content

Commit

Permalink
Merge pull request #10 from wharfkit/bigdecimal-fix
Browse files Browse the repository at this point in the history
Bigdecimal fix
  • Loading branch information
aaroncox authored Apr 8, 2024
2 parents 9e354ee + a26554e commit 7b18998
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@wharfkit/resources",
"description": "Library to assist in Antelope-blockchain resource calculations.",
"version": "1.2.2-rc2",
"version": "1.2.2-rc3",
"homepage": "https://github.com/wharfkit/resources",
"license": "BSD-3-Clause",
"main": "lib/wharfkit-resources.js",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface SampleUsage {
net: UInt128
}

export const BNPrecision = new BN(1000 * 1000)
export const BNPrecision = new BN(100 * 1000 * 1000)

export class Resources {
static __className = 'Resources'
Expand Down
14 changes: 6 additions & 8 deletions src/powerup/abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ export abstract class PowerUpStateResource extends Struct {

// Get the current percentage of reserved units
public get reserved() {
return new bigDecimal(String(this.utilization))
.divide(new bigDecimal(String(this.weight)))
.getValue()
return new BN(String(this.utilization)) / new BN(String(this.weight))
}

// Get the symbol definition for the token
Expand All @@ -61,7 +59,7 @@ export abstract class PowerUpStateResource extends Struct {
exponent: Number(this.exponent),
utilization: Number(this.utilization),
utilization_timestamp: Number(this.utilization_timestamp.value),
weight: new bigDecimal(String(this.weight)),
weight: new BN(String(this.weight)),
weight_ratio: Number(this.weight_ratio),
}
}
Expand All @@ -85,7 +83,7 @@ export abstract class PowerUpStateResource extends Struct {
if (new_exponent <= 0.0) {
return max_price
} else {
const util_weight = Number(new bigDecimal(utilization).divide(weight).getValue())
const util_weight = new BN(utilization) / weight
price += (max_price - min_price) * Math.pow(util_weight, new_exponent)
}
return price
Expand All @@ -97,8 +95,8 @@ export abstract class PowerUpStateResource extends Struct {
const max_price: number = this.max_price.value
const min_price: number = this.min_price.value
const coefficient = (max_price - min_price) / exponent
const start_u = Number(new bigDecimal(start_utilization).divide(weight).getValue())
const end_u = Number(new bigDecimal(end_utilization).divide(weight).getValue())
const start_u = new BN(start_utilization) / weight
const end_u = new BN(end_utilization) / weight
const delta =
min_price * end_u -
min_price * start_u +
Expand All @@ -119,7 +117,7 @@ export abstract class PowerUpStateResource extends Struct {
const min = Math.min(utilization_increase, adjusted_utilization - start_utilization)
fee += Number(
new bigDecimal(this.price_function(adjusted_utilization) * min)
.divide(weight)
.divide(new bigDecimal(weight.toString()))
.getValue()
)
start_utilization = adjusted_utilization
Expand Down
6 changes: 2 additions & 4 deletions src/powerup/cpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Struct, UInt128} from '@wharfkit/antelope'
import {BNPrecision, SampleUsage} from '..'
import {PowerUpStateResource} from './abstract'
import {PowerUpStateOptions} from './options'
import bigDecimal from 'js-big-decimal'
import BN from 'bn.js'

@Struct.type('powerupstateresourcecpu')
export class PowerUpStateResourceCPU extends PowerUpStateResource {
Expand Down Expand Up @@ -43,9 +43,7 @@ export class PowerUpStateResourceCPU extends PowerUpStateResource {
// Frac generation by μs (microseconds)
frac_by_us(usage: SampleUsage, us: number) {
const {weight} = this.cast()
const frac = Number(
new bigDecimal(this.us_to_weight(usage.cpu, us)).divide(weight).getValue()
)
const frac = new BN(this.us_to_weight(usage.cpu, us)) / weight
return Math.floor(frac * Math.pow(10, 15))
}

Expand Down
6 changes: 2 additions & 4 deletions src/powerup/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Struct, UInt128} from '@wharfkit/antelope'
import {BNPrecision, SampleUsage} from '..'
import {PowerUpStateResource} from './abstract'
import {PowerUpStateOptions} from './options'
import bigDecimal from 'js-big-decimal'
import BN from 'bn.js'

@Struct.type('powerupstateresourcenet')
export class PowerUpStateResourceNET extends PowerUpStateResource {
Expand Down Expand Up @@ -44,9 +44,7 @@ export class PowerUpStateResourceNET extends PowerUpStateResource {
// Frac generation by bytes
frac_by_bytes(usage: SampleUsage, bytes: number) {
const {weight} = this.cast()
const frac = Number(
new bigDecimal(this.bytes_to_weight(usage.net, bytes)).divide(weight).getValue()
)
const frac = new BN(this.bytes_to_weight(usage.net, bytes)) / weight
return Math.floor(frac * Math.pow(10, 15))
}

Expand Down
6 changes: 3 additions & 3 deletions test/data/714c990e6ea2e283c39d1f3a8d1b7680fedaed53.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
"status": 200,
"json": {
"rows": [
"000092a87508d51305009f1c75a7c183030042f8c30ce420d5010080c6a47e8d030000404c948b320300411caf6500f6b265000000000000004080510100006e10bacb000000085741580000000000e4eccddf170000085741580000000048628c5a000000008c93765a00000000cd85af6500cf8c9e7e4c9201009f1c75a7c18303001619a2ac0e3291000080c6a47e8d030000404c948b320300411caf6500f6b265000000000000004080510100006e10bacb000000085741580000000000e4eccddf17000008574158000000004bb34c9000000000a356344100000000cd85af650100000001000000000000000857415800000000"
"0000ea70c0561920340000404c948b32030042f8c30ce420d5010080c6a47e8d030000404c948b320300411caf6500f6b265000000000000004080510100006e10bacb000000085741580000000000e4eccddf1700000857415800000000c9dc94f2d9000000d881d7e7650100008c30ea65001e5867a10122100000404c948b3203001619a2ac0e3291000080c6a47e8d030000404c948b320300411caf6500f6b265000000000000004080510100006e10bacb000000085741580000000000e4eccddf1700000857415800000000e5eba314f0d9010086d44d5355fd01008c30ea650100000001000000000000000857415800000000"
],
"more": false,
"next_key": ""
},
"text": "{\"rows\":[\"000092a87508d51305009f1c75a7c183030042f8c30ce420d5010080c6a47e8d030000404c948b320300411caf6500f6b265000000000000004080510100006e10bacb000000085741580000000000e4eccddf170000085741580000000048628c5a000000008c93765a00000000cd85af6500cf8c9e7e4c9201009f1c75a7c18303001619a2ac0e3291000080c6a47e8d030000404c948b320300411caf6500f6b265000000000000004080510100006e10bacb000000085741580000000000e4eccddf17000008574158000000004bb34c9000000000a356344100000000cd85af650100000001000000000000000857415800000000\"],\"more\":false,\"next_key\":\"\"}"
}
"text": "{\"rows\":[\"0000ea70c0561920340000404c948b32030042f8c30ce420d5010080c6a47e8d030000404c948b320300411caf6500f6b265000000000000004080510100006e10bacb000000085741580000000000e4eccddf1700000857415800000000c9dc94f2d9000000d881d7e7650100008c30ea65001e5867a10122100000404c948b3203001619a2ac0e3291000080c6a47e8d030000404c948b320300411caf6500f6b265000000000000004080510100006e10bacb000000085741580000000000e4eccddf1700000857415800000000e5eba314f0d9010086d44d5355fd01008c30ea650100000001000000000000000857415800000000\"],\"more\":false,\"next_key\":\"\"}"
}
40 changes: 19 additions & 21 deletions test/powerup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const resources_jungle = new Resources({

const resources_wax = new Resources({
api: makeClient('https://wax.greymass.com'),
sampleAccount: 'boost.wax'
sampleAccount: 'boost.wax',
})

// Fixture for tests to provide reproducable values
Expand Down Expand Up @@ -232,13 +232,12 @@ suite('[wax] powerup - cpu calculations', function () {
test('powerup.cpu.allocated', async function () {
const powerup = await resources_wax.v1.powerup.get_state()

assert.equal(powerup.cpu.allocated, 0.9890078589830127)
// 99% represented as float
assert.equal(powerup.cpu.allocated, 0.99)
})
test('powerup.cpu.reserved', async function () {
const powerup = await resources_wax.v1.powerup.get_state()

assert.equal(powerup.cpu.reserved, 0.000005473139080385654)
assert.equal(powerup.cpu.reserved, 0.1147547419042913)
})
test('powerup.cpu.price_per_us(1000000)', async function () {
const powerup = await resources_wax.v1.powerup.get_state()
Expand All @@ -247,31 +246,31 @@ suite('[wax] powerup - cpu calculations', function () {
const price_us = powerup.cpu.price_per_us(sample, 1000000, this.testFixture)
const price_ms = powerup.cpu.price_per_ms(sample, 1000, this.testFixture)
assert.equal(price_us, price_ms)
assert.equal(price_ms, 17.53764085)
assert.equal(price_ms, 7.6019681)
})
test('powerup.cpu.price_per_ms(1000)', async function () {
const powerup = await resources_wax.v1.powerup.get_state()
const sample = await resources_wax.getSampledUsage()

const price = powerup.cpu.price_per_ms(sample, 1000, this.testFixture)
assert.equal(price, 17.53764085)
assert.equal(price, 7.6019681)

const asset = Asset.from(price, '8,WAX')
assert.equal(String(asset), '17.53764085 WAX')
assert.equal(asset.value, 17.53764085)
assert.equal(Number(asset.units), 1753764085)
assert.equal(String(asset), '7.60196810 WAX')
assert.equal(asset.value, 7.6019681)
assert.equal(Number(asset.units), 760196810)
})
test('powerup.cpu.price_per_ms(1000000)', async function () {
const powerup = await resources_wax.v1.powerup.get_state()
const sample = await resources_wax.getSampledUsage()

const price = powerup.cpu.price_per_ms(sample, 1000000, this.testFixture)
assert.equal(price, 498962.40065742)
assert.equal(price, 11770.99029685)

const asset = Asset.from(price, '8,WAX')
assert.equal(String(asset), '498962.40065742 WAX')
assert.equal(asset.value, 498962.40065742)
assert.equal(Number(asset.units), 49896240065742)
assert.equal(String(asset), '11770.99029685 WAX')
assert.equal(asset.value, 11770.99029685)
assert.equal(Number(asset.units), 1177099029685)
})
})

Expand All @@ -287,31 +286,30 @@ suite('[wax] powerup - net calculations', function () {
test('powerup.net.allocated', async function () {
const powerup = await resources_wax.v1.powerup.get_state()

assert.equal(powerup.net.allocated, 0.9890078589830127)
// 99% represented as float
assert.equal(powerup.net.allocated, 0.99)
})
test('powerup.net.reserved', async function () {
const powerup = await resources_wax.v1.powerup.get_state()

assert.equal(powerup.net.reserved, 0.0000010629515032355908)
assert.equal(powerup.net.reserved, 0.00006380031736302496)
})
test('powerup.net.price_per_kb(1000000000000)', async function () {
const powerup = await resources_wax.v1.powerup.get_state()
const sample = await resources_wax.getSampledUsage()

const price = powerup.net.price_per_kb(sample, 1000, this.testFixture)
assert.equal(price, 0.00451099)
assert.equal(price, 0.00044073)

const asset = Asset.from(price, '8,WAX')
assert.equal(String(asset), '0.00451099 WAX')
assert.equal(asset.value, 0.00451099)
assert.equal(Number(asset.units), 451099)
assert.equal(String(asset), '0.00044073 WAX')
assert.equal(asset.value, 0.00044073)
assert.equal(Number(asset.units), 44073)
})
test('powerup.net.frac()', async function () {
const powerup = await resources_wax.v1.powerup.get_state()
const sample = await resources_wax.getSampledUsage()

const frac1000 = powerup.net.frac(sample, 1000000)
assert.equal(frac1000, 515521337)
assert.equal(frac1000, 50216295)
})
})

0 comments on commit 7b18998

Please sign in to comment.