datai-sdk 1.0.4 → 1.0.6
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/package.json +1 -1
- package/utils/mathUtils.ts +18 -7
package/package.json
CHANGED
package/utils/mathUtils.ts
CHANGED
|
@@ -2,21 +2,32 @@ import { BigDecimal, BigInt, Bytes, Address } from '@graphprotocol/graph-ts'
|
|
|
2
2
|
import { BD_0, BD_1 } from './constants'
|
|
3
3
|
|
|
4
4
|
const MAX_DECIMALS = 77 // max decimals in decimal128 IEEE 754 standard (limitation of thegraph host)
|
|
5
|
-
const MIN_DECIMALS = -
|
|
5
|
+
const MIN_DECIMALS = -50
|
|
6
6
|
|
|
7
7
|
export function pow(value: BigDecimal, power: i32): BigDecimal {
|
|
8
8
|
if (power == 0) {
|
|
9
|
-
return BD_1
|
|
9
|
+
return BD_1 // Any number to the power of 0 is 1
|
|
10
10
|
}
|
|
11
|
+
|
|
11
12
|
const negativePower = power < 0
|
|
12
|
-
let result =
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
let result = BD_1 // Start with 1
|
|
14
|
+
let base = value
|
|
15
|
+
let powerAbs = power < 0 ? -power : power // Use absolute value of power
|
|
16
|
+
|
|
17
|
+
while (powerAbs > 0) {
|
|
18
|
+
if (powerAbs & 1) {
|
|
19
|
+
// Bitwise check for oddness
|
|
20
|
+
result = result.times(base)
|
|
21
|
+
}
|
|
22
|
+
base = base.times(base)
|
|
23
|
+
powerAbs >>= 1 // Bitwise right shift is equivalent to divide by 2
|
|
16
24
|
}
|
|
17
25
|
|
|
18
26
|
if (negativePower) {
|
|
19
|
-
|
|
27
|
+
if (result.equals(BD_0)) {
|
|
28
|
+
throw new Error('Division by zero in power calculation')
|
|
29
|
+
}
|
|
30
|
+
result = safeDiv(BD_1, result) // Handle negative powers
|
|
20
31
|
}
|
|
21
32
|
|
|
22
33
|
return result
|