Skip to content

Commit

Permalink
Luke/big num to precision improvement (#1364)
Browse files Browse the repository at this point in the history
* Updated BigNum's toPrecision method to more accurately handle small decimal numbers

* Improved types for fillSpotOrder method

* Bump

* Prettier fix

* Prettier fix

* Bump

* Fix isomorphic build

* Prettify

* Bump

* Prettify
  • Loading branch information
lukecaan authored Dec 20, 2024
1 parent b827636 commit 816dff0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
4 changes: 2 additions & 2 deletions sdk/src/driftClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4189,7 +4189,7 @@ export class DriftClient {
public async fillSpotOrder(
userAccountPublicKey: PublicKey,
user: UserAccount,
order?: Order,
order?: Pick<Order, 'marketIndex' | 'orderId'>,
fulfillmentConfig?:
| SerumV3FulfillmentConfigAccount
| PhoenixV1FulfillmentConfigAccount
Expand Down Expand Up @@ -4219,7 +4219,7 @@ export class DriftClient {
public async getFillSpotOrderIx(
userAccountPublicKey: PublicKey,
userAccount: UserAccount,
order?: Order,
order?: Pick<Order, 'marketIndex' | 'orderId'>,
fulfillmentConfig?:
| SerumV3FulfillmentConfigAccount
| PhoenixV1FulfillmentConfigAccount
Expand Down
34 changes: 33 additions & 1 deletion sdk/src/factory/bigNum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,42 @@ export class BigNum {
}

const isNeg = this.isNeg();

const printString = this.abs().print();
const thisString = this.abs().toString();

// Handle small numbers (those with leading zeros after decimal)
if (printString.includes(BigNum.delim)) {
const [leftSide, rightSide] = printString.split(BigNum.delim);
if (leftSide === '0' && rightSide) {
// Count leading zeros
let leadingZeros = 0;
for (let i = 0; i < rightSide.length; i++) {
if (rightSide[i] === '0') {
leadingZeros++;
} else {
break;
}
}
// Get significant digits starting after leading zeros
const significantPart = rightSide.slice(leadingZeros);
let significantDigits = significantPart.slice(0, fixedPrecision);

// Remove trailing zeros if not requested
if (!trailingZeroes) {
significantDigits = significantDigits.replace(/0+$/, '');
}

// Only return result if we have significant digits
if (significantDigits.length > 0) {
const result = `${isNeg ? '-' : ''}0${BigNum.delim}${rightSide.slice(
0,
leadingZeros
)}${significantDigits}`;
return result;
}
}
}

let precisionPrintString = printString.slice(0, fixedPrecision + 1);

if (
Expand Down
13 changes: 13 additions & 0 deletions sdk/tests/bn/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import {
// if you used the '@types/mocha' method to install mocha type definitions, uncomment the following line
// import 'mocha';

const bn = (value: number, precision: number) =>
new BigNum(Math.round(value * 10 ** precision), precision);
const _bnPrice = (value: number) => bn(value, 6); // Price precision (6 decimals)
const _bnNotional = (value: number) => bn(value, 6); // USDC precision (6 decimals)
const _bnPercentage = (value: number) => bn(value, 4); // Percentage precision (4 decimals)
const bnBaseAmount = (value: number) => bn(value, 8); // BTC-like precision (8 decimals)

describe('BigNum Tests', () => {
it('basic string representations are correct', () => {
const bn = BigNum.from(TEN_THOUSAND);
Expand Down Expand Up @@ -108,6 +115,12 @@ describe('BigNum Tests', () => {
expect(val4.toNum().toFixed(3)).to.equal('0.025');
expect(val4.toPrecision(4)).to.equal('0.025');

expect(bnBaseAmount(0.001234).toPrecision(4)).to.equal('0.001234');

expect(bnBaseAmount(0.001004).toPrecision(4)).to.equal('0.001004');

expect(bnBaseAmount(0.001).toPrecision(4)).to.equal('0.001');

// Case 5
expect(BigNum.fromPrint('1').toMillified()).to.equal('1.00');
expect(BigNum.fromPrint('12').toMillified()).to.equal('12.0');
Expand Down

0 comments on commit 816dff0

Please sign in to comment.