-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
49d1d05
commit eaef4e6
Showing
5 changed files
with
135 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,50 @@ | ||
import BN from 'bn.js' | ||
|
||
import { BNUSDC } from 'models/Wallet' | ||
import { BN_USDC_CENT_WEI, ceilingBNUSDCToNearestCent } from 'utils/wallet' | ||
|
||
import { PurchaseContentStage } from './types' | ||
|
||
export const zeroBalance = () => new BN(0) as BNUSDC | ||
|
||
export const isContentPurchaseInProgress = (stage: PurchaseContentStage) => { | ||
return [ | ||
PurchaseContentStage.BUY_USDC, | ||
PurchaseContentStage.PURCHASING, | ||
PurchaseContentStage.CONFIRMING_PURCHASE | ||
].includes(stage) | ||
} | ||
|
||
type PurchaseSummaryValues = { | ||
amountDue: number | ||
existingBalance: number | undefined | ||
basePrice: number | ||
artistCut: number | ||
} | ||
|
||
export const getPurchaseSummaryValues = ( | ||
price: number, | ||
currentBalance: BNUSDC = zeroBalance() | ||
): PurchaseSummaryValues => { | ||
let amountDue = price | ||
let existingBalance | ||
const priceBN = new BN(price).mul(BN_USDC_CENT_WEI) | ||
|
||
if (currentBalance.gte(priceBN)) { | ||
amountDue = 0 | ||
existingBalance = price | ||
} | ||
// Only count the balance if it's greater than 1 cent | ||
else if (currentBalance.gt(BN_USDC_CENT_WEI)) { | ||
// Note: Rounding amount due *up* to nearest cent for cases where the balance | ||
// is between cents so that we aren't advertising *lower* than what the user | ||
// will have to pay. | ||
const diff = priceBN.sub(currentBalance) | ||
amountDue = ceilingBNUSDCToNearestCent(diff as BNUSDC) | ||
.div(BN_USDC_CENT_WEI) | ||
.toNumber() | ||
existingBalance = price - amountDue | ||
} | ||
|
||
return { amountDue, existingBalance, basePrice: price, artistCut: price } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters