Skip to content

Commit

Permalink
add DecimalNumber component
Browse files Browse the repository at this point in the history
  • Loading branch information
bvalosek committed Dec 8, 2021
1 parent 0c3ce77 commit ca2a9b0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-react": "^7.26.1",
"eslint-plugin-react-hooks": "^4.2.0",
"eslint-plugin-react-hooks": "^4.3.0",
"husky": "^7.0.4",
"lint-staged": "^11.2.6",
"prettier": "^2.4.1",
Expand Down
51 changes: 51 additions & 0 deletions src/components/DecimalNumber.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { commify, formatUnits } from '@ethersproject/units';
import { BigNumber } from 'ethers';
import { useEffect, useState } from 'react';

interface Props {
value: BigNumber | null | undefined;
decimals: number;
precision?: number;
interpolation?: {
sampledAt: number;
dailyRate: BigNumber;
max: BigNumber;
};
}

/**
* Render a BigNumber, optionally lerped in real time based on a sample time and
* daily rate
*/
export default ({ value, decimals, interpolation }: Props) => {
const [alpha, setAlpha] = useState(BigNumber.from(0));

useEffect(() => {
const h = setInterval(update, 100);
return () => clearInterval(h);
}, [interpolation]);

const update = () => {
if (interpolation == null) return;

// compute time since sample and amount to add (alpha)
const delta = Math.max(0, Date.now() - interpolation.sampledAt * 1000);
const nextAlpha = interpolation.dailyRate
.mul(delta)
.div(1000 * 60 * 60 * 24);

setAlpha(nextAlpha);
};

if (value == null) {
return '-';
}

// add alpha to value and clamp to max
let num = value.add(alpha);
if (interpolation != null) {
num = num.gt(interpolation.max) ? interpolation.max : num;
}

return commify(formatUnits(num, decimals));
};
6 changes: 4 additions & 2 deletions src/hypervibes/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ export interface GetTokenViewOutput {

/**
* get infusion data about a batch of (realm,nft,tokenId) tuples directly from
* the blockchain. Will not throw if invalid token or non-infused token, view
* will just be null. Will throw on an invalid realm id
* the blockchain.
*
* Will not throw if invalid token or non-infused token, view will just be null.
* Will throw on an invalid realm id
*/
export const batchGetTokenView = async (
batch: GetTokenViewInput[],
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4287,10 +4287,10 @@ eslint-plugin-prettier@^4.0.0:
dependencies:
prettier-linter-helpers "^1.0.0"

eslint-plugin-react-hooks@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-react-hooks/-/eslint-plugin-react-hooks-4.2.0.tgz#8c229c268d468956334c943bb45fc860280f5556"
integrity sha512-623WEiZJqxR7VdxFCKLI6d6LLpwJkGPYKODnkH3D7WpOG5KM8yWueBd8TLsNAetEJNF5iJmolaAKO3F8yzyVBQ==
eslint-plugin-react-hooks@^4.3.0:
version "4.3.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-react-hooks/-/eslint-plugin-react-hooks-4.3.0.tgz#318dbf312e06fab1c835a4abef00121751ac1172"
integrity sha512-XslZy0LnMn+84NEG9jSGR6eGqaZB3133L8xewQo3fQagbQuGt7a63gf+P1NGKZavEYEC3UXaWEAA/AqDkuN6xA==

eslint-plugin-react@^7.26.1:
version "7.26.1"
Expand Down

0 comments on commit ca2a9b0

Please sign in to comment.