Skip to content

Commit

Permalink
Merge pull request #6 from wharfkit/bigdecimal
Browse files Browse the repository at this point in the history
Big Decimal Fixes
  • Loading branch information
aaroncox authored Apr 8, 2024
2 parents 5ad4d70 + 7b18998 commit 798a69f
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 37 deletions.
3 changes: 2 additions & 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.1",
"version": "1.2.2-rc3",
"homepage": "https://github.com/wharfkit/resources",
"license": "BSD-3-Clause",
"main": "lib/wharfkit-resources.js",
Expand All @@ -22,6 +22,7 @@
"dependencies": {
"@wharfkit/antelope": "^1.0.0",
"bn.js": "^4.11.9",
"js-big-decimal": "^2.0.7",
"tslib": "^2.1.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function divCeil(num: BN, den: BN): UInt128 {
let v: BN = num.div(den)
const zero = new BN(0)
const one = new BN(1)
if (num.mod(den).gt(zero)) {
if (num.mod(den).gt(zero) && v.gt(one)) {
v = v.sub(one)
}
return UInt128.from(v)
Expand Down
22 changes: 13 additions & 9 deletions src/powerup/abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {

import BN from 'bn.js'
import {PowerUpStateOptions} from './options'
import bigDecimal from 'js-big-decimal'

export abstract class PowerUpStateResource extends Struct {
@Struct.field('uint8') version!: UInt8
Expand Down Expand Up @@ -42,7 +43,7 @@ export abstract class PowerUpStateResource extends Struct {

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

// Get the symbol definition for the token
Expand All @@ -58,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: Number(this.weight),
weight: new BN(String(this.weight)),
weight_ratio: Number(this.weight_ratio),
}
}
Expand All @@ -82,7 +83,8 @@ export abstract class PowerUpStateResource extends Struct {
if (new_exponent <= 0.0) {
return max_price
} else {
price += (max_price - min_price) * Math.pow(utilization / weight, new_exponent)
const util_weight = new BN(utilization) / weight
price += (max_price - min_price) * Math.pow(util_weight, new_exponent)
}
return price
}
Expand All @@ -93,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 = start_utilization / weight
const end_u = end_utilization / weight
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 @@ -112,10 +114,12 @@ export abstract class PowerUpStateResource extends Struct {

let fee = 0
if (start_utilization < adjusted_utilization) {
fee +=
(this.price_function(adjusted_utilization) *
Math.min(utilization_increase, adjusted_utilization - start_utilization)) /
weight
const min = Math.min(utilization_increase, adjusted_utilization - start_utilization)
fee += Number(
new bigDecimal(this.price_function(adjusted_utilization) * min)
.divide(new bigDecimal(weight.toString()))
.getValue()
)
start_utilization = adjusted_utilization
}
if (start_utilization < end_utilization) {
Expand Down
3 changes: 2 additions & 1 deletion src/powerup/cpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Struct, UInt128} from '@wharfkit/antelope'
import {BNPrecision, SampleUsage} from '..'
import {PowerUpStateResource} from './abstract'
import {PowerUpStateOptions} from './options'
import BN from 'bn.js'

@Struct.type('powerupstateresourcecpu')
export class PowerUpStateResourceCPU extends PowerUpStateResource {
Expand Down Expand Up @@ -42,7 +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 = this.us_to_weight(usage.cpu, us) / weight
const frac = new BN(this.us_to_weight(usage.cpu, us)) / weight
return Math.floor(frac * Math.pow(10, 15))
}

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

@Struct.type('powerupstateresourcenet')
export class PowerUpStateResourceNET extends PowerUpStateResource {
Expand Down Expand Up @@ -43,7 +44,7 @@ export class PowerUpStateResourceNET extends PowerUpStateResource {
// Frac generation by bytes
frac_by_bytes(usage: SampleUsage, bytes: number) {
const {weight} = this.cast()
const frac = this.bytes_to_weight(usage.net, bytes) / weight
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)
})
})
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,11 @@ istanbul-reports@^3.0.2:
html-escaper "^2.0.0"
istanbul-lib-report "^3.0.0"

js-big-decimal@^2.0.7:
version "2.0.7"
resolved "https://registry.yarnpkg.com/js-big-decimal/-/js-big-decimal-2.0.7.tgz#fb9b44b4c1eae08903cb191c0cf37b82f3a8d7c4"
integrity sha512-XGc79t2Iv3b7LFlYaTT8WoQBuWL4K81aST+dq2YGHV6giedbnoG0s33ju24Uw/BGqLYfPPgn4HGRrPS2mfKk3Q==

js-tokens@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
Expand Down

0 comments on commit 798a69f

Please sign in to comment.