diff --git a/sdk/src/driftClient.ts b/sdk/src/driftClient.ts index 5d9212179..617ab9f09 100644 --- a/sdk/src/driftClient.ts +++ b/sdk/src/driftClient.ts @@ -4189,7 +4189,7 @@ export class DriftClient { public async fillSpotOrder( userAccountPublicKey: PublicKey, user: UserAccount, - order?: Order, + order?: Pick, fulfillmentConfig?: | SerumV3FulfillmentConfigAccount | PhoenixV1FulfillmentConfigAccount @@ -4219,7 +4219,7 @@ export class DriftClient { public async getFillSpotOrderIx( userAccountPublicKey: PublicKey, userAccount: UserAccount, - order?: Order, + order?: Pick, fulfillmentConfig?: | SerumV3FulfillmentConfigAccount | PhoenixV1FulfillmentConfigAccount diff --git a/sdk/src/factory/bigNum.ts b/sdk/src/factory/bigNum.ts index dc08b3ec6..f74de0692 100644 --- a/sdk/src/factory/bigNum.ts +++ b/sdk/src/factory/bigNum.ts @@ -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 ( diff --git a/sdk/tests/bn/test.ts b/sdk/tests/bn/test.ts index 444c44315..e4733dee6 100644 --- a/sdk/tests/bn/test.ts +++ b/sdk/tests/bn/test.ts @@ -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); @@ -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');