Skip to content

Commit

Permalink
Merge pull request #64 from Premian-Labs/issue/parse-number
Browse files Browse the repository at this point in the history
Issue/parse number
  • Loading branch information
froggiedev committed Nov 16, 2023
2 parents 0c5cb51 + 24275ad commit d3223d5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@premia/v3-sdk",
"version": "1.0.7",
"version": "1.0.8",
"description": "The official SDK for building applications on Premia V3.",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
23 changes: 18 additions & 5 deletions src/api/optionAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,33 @@ export class OptionAPI extends BaseAPI {
}

/**
* Calculates the increment to use for strike prices based on the spot price.
* Calculates the increment to use for strike prices based on the spot (or reference) price.
*
* @param {BigNumberish} spotPrice - The spot price.
* @param {BigNumberish} spotPrice - The spot (or reference) price.
* @param {number} decimals - The number of decimal places for the price (defaults to WAD_DECIMALS).
* @returns {bigint} - The increment for the strike prices.
*/
getStrikeIncrement(
spotPrice: BigNumberish,
decimals: number = Number(WAD_DECIMALS)
decimals: number | bigint = WAD_DECIMALS
): bigint {
const price = parseNumber(spotPrice, decimals)
const _decimals = Number(decimals)
const price = parseNumber(spotPrice, _decimals)
const exponent = Math.floor(Math.log10(price))
const multiplier = price >= 5 * 10 ** exponent ? 5 : 1
return parseBigInt(multiplier * 10 ** (exponent - 1), decimals)

if (exponent - 1 < 0) {
return (
(toBigInt(multiplier) * toBigInt(10) ** toBigInt(decimals)) /
toBigInt(10) ** toBigInt(Math.abs(exponent - 1))
)
}

return (
toBigInt(multiplier) *
toBigInt(10) ** toBigInt(decimals) *
toBigInt(10) ** toBigInt(exponent - 1)
)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function parseNumber(
decimals: number = Number(WAD_DECIMALS)
): number {
const isNegative = bn.toString().startsWith('-')
const str = bn.toString().replace('-', '')
const str = String(toFixed(bn)).replace('-', '')
const left =
str.length >= decimals ? str.substring(0, str.length - decimals) : 0

Expand Down

0 comments on commit d3223d5

Please sign in to comment.