mirror of
https://github.com/oceanprotocol/market.git
synced 2024-12-02 05:57:29 +01:00
use decimal.js for price comparison
This commit is contained in:
parent
e7b17a9d65
commit
5ce00c9ddc
@ -34,6 +34,7 @@
|
|||||||
"axios": "^0.19.2",
|
"axios": "^0.19.2",
|
||||||
"classnames": "^2.2.6",
|
"classnames": "^2.2.6",
|
||||||
"date-fns": "^2.15.0",
|
"date-fns": "^2.15.0",
|
||||||
|
"decimal.js": "^10.2.0",
|
||||||
"dotenv": "^8.2.0",
|
"dotenv": "^8.2.0",
|
||||||
"ethereum-blockies": "github:MyEtherWallet/blockies",
|
"ethereum-blockies": "github:MyEtherWallet/blockies",
|
||||||
"filesize": "^6.1.0",
|
"filesize": "^6.1.0",
|
||||||
|
@ -6,7 +6,7 @@ import { MetadataMarket } from '../../../@types/Metadata'
|
|||||||
import { DDO } from '@oceanprotocol/lib'
|
import { DDO } from '@oceanprotocol/lib'
|
||||||
import Tabs from '../../atoms/Tabs'
|
import Tabs from '../../atoms/Tabs'
|
||||||
import { useOcean } from '@oceanprotocol/react'
|
import { useOcean } from '@oceanprotocol/react'
|
||||||
import compareAsBN, { Comparison } from '../../../utils/compareAsBN'
|
import compareAsBN from '../../../utils/compareAsBN'
|
||||||
|
|
||||||
export default function AssetActions({
|
export default function AssetActions({
|
||||||
metadata,
|
metadata,
|
||||||
@ -26,9 +26,7 @@ export default function AssetActions({
|
|||||||
if (!price || !balance || !balance.ocean) return
|
if (!price || !balance || !balance.ocean) return
|
||||||
|
|
||||||
const isFree = price === '0'
|
const isFree = price === '0'
|
||||||
setIsBalanceSufficient(
|
setIsBalanceSufficient(isFree ? true : compareAsBN(balance.ocean, price))
|
||||||
isFree ? true : compareAsBN(balance.ocean, price, Comparison.gte)
|
|
||||||
)
|
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
setIsBalanceSufficient(false)
|
setIsBalanceSufficient(false)
|
||||||
|
@ -1,21 +1,16 @@
|
|||||||
import BN from 'bn.js'
|
import { Decimal } from 'decimal.js'
|
||||||
|
|
||||||
export enum Comparison {
|
// Run decimal.js comparison
|
||||||
'lt' = 'lt',
|
// http://mikemcl.github.io/decimal.js/#cmp
|
||||||
'lte' = 'lte',
|
export default function compareAsBN(balance: string, price: string): boolean {
|
||||||
'gt' = 'gt',
|
const aBN = new Decimal(balance)
|
||||||
'gte' = 'gte',
|
const bBN = new Decimal(price)
|
||||||
'eq' = 'eq'
|
const compare = aBN.comparedTo(bBN)
|
||||||
}
|
|
||||||
|
|
||||||
// Run the corresponding bn.js comparison:
|
switch (compare) {
|
||||||
// https://github.com/indutny/bn.js/#utilities
|
case 1 || 0: // balance is greater or equal to price
|
||||||
export default function compareAsBN(
|
return true
|
||||||
a: string,
|
default:
|
||||||
b: string,
|
return false
|
||||||
comparison: Comparison
|
}
|
||||||
): boolean {
|
|
||||||
const aBN = new BN(a)
|
|
||||||
const bBN = new BN(b)
|
|
||||||
return aBN[comparison](bBN)
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user