Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sdk: getTokenAmount uses divCeil #371

Merged
merged 5 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixes

- ts-sdk: fix resolvePerpBankrupcty to work with all perp market indexes
- ts-sdk: getTokenAmount uses divCeil ([#371](https://github.com/drift-labs/protocol-v2/pull/371))
- program: allow limit orders to have explicit zero auction duration passed in params ([#373](https://github.com/drift-labs/protocol-v2/pull/373))

### Breaking
Expand Down
16 changes: 11 additions & 5 deletions sdk/src/math/spotBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
} from './margin';
import { OraclePriceData } from '../oracles/types';
import { PERCENTAGE_PRECISION } from '../constants/numericConstants';
import { divCeil } from './utils';

export function getBalance(
tokenAmount: BN,
Expand Down Expand Up @@ -49,11 +50,16 @@ export function getTokenAmount(
): BN {
const precisionDecrease = TEN.pow(new BN(19 - spotMarket.decimals));

const cumulativeInterest = isVariant(balanceType, 'deposit')
? spotMarket.cumulativeDepositInterest
: spotMarket.cumulativeBorrowInterest;

return balanceAmount.mul(cumulativeInterest).div(precisionDecrease);
if (isVariant(balanceType, 'deposit')) {
return balanceAmount
.mul(spotMarket.cumulativeDepositInterest)
.div(precisionDecrease);
} else {
return divCeil(
balanceAmount.mul(spotMarket.cumulativeBorrowInterest),
precisionDecrease
);
}
}

export function getSignedTokenAmount(
Expand Down
14 changes: 13 additions & 1 deletion sdk/src/math/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BN } from '../';
import { BN, ONE, ZERO } from '../';

export function clampBN(x: BN, min: BN, max: BN): BN {
return BN.max(min, BN.min(x, max));
Expand All @@ -21,3 +21,15 @@ export const squareRootBN = (n: BN): BN => {
return largeCand;
}
};

export const divCeil = (a: BN, b: BN): BN => {
const quotient = a.div(b);

const remainder = a.mod(b);

if (remainder.gt(ZERO)) {
return quotient.add(ONE);
} else {
return quotient;
}
};
4 changes: 2 additions & 2 deletions tests/liquidateSpotSocialLoss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
createWSolTokenAccountForUser,
initializeSolSpotMarket,
} from './testHelpers';
import { BulkAccountLoader, isVariant, ONE } from '../sdk';
import { BulkAccountLoader, isVariant } from '../sdk';

describe('liquidate spot w/ social loss', () => {
const provider = anchor.AnchorProvider.local(undefined, {
Expand Down Expand Up @@ -288,7 +288,7 @@ describe('liquidate spot w/ social loss', () => {

const interestOfUpdate = currentDepositAmount.sub(depositAmountBefore);
console.log('interestOfUpdate:', interestOfUpdate.toString());
assert(interestOfUpdate.eq(ONE));
assert(interestOfUpdate.eq(ZERO));
});

it('resolve bankruptcy', async () => {
Expand Down
10 changes: 3 additions & 7 deletions tests/spotDepositWithdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,9 +669,7 @@ describe('spot deposit and withdraw', () => {
userBalanceAfter.scaledBalance.toString()
);

assert(
expectedUserBalance.eq(userBalanceAfter.scaledBalance.add(new BN(1000)))
);
assert(expectedUserBalance.eq(userBalanceAfter.scaledBalance));
assert(isVariant(userBalanceAfter.balanceType, 'deposit'));

const expectedSpotMarketDepositBalance =
Expand All @@ -683,9 +681,7 @@ describe('spot deposit and withdraw', () => {
);

assert(
spotMarketAccount.depositBalance.eq(
expectedSpotMarketDepositBalance.sub(new BN(1000))
)
spotMarketAccount.depositBalance.eq(expectedSpotMarketDepositBalance)
);
assert(spotMarketAccount.borrowBalance.eq(ZERO));
});
Expand Down Expand Up @@ -780,7 +776,7 @@ describe('spot deposit and withdraw', () => {
expectedUserUSDCAmount.toString(),
userUSDCAmountAfter.toString()
);
assert(expectedUserUSDCAmount.eq(userUSDCAmountAfter.add(ONE)));
assert(expectedUserUSDCAmount.eq(userUSDCAmountAfter));

const userBalanceAfter = secondUserDriftClient.getSpotPosition(marketIndex);
assert(userBalanceAfter.scaledBalance.eq(ZERO));
Expand Down