Skip to content

Commit

Permalink
fix: pass in getStreamPrices to remove knowledge of a+ API and localS…
Browse files Browse the repository at this point in the history
…torage
  • Loading branch information
alexandermendes committed Nov 7, 2024
1 parent f557672 commit edf3c0e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 69 deletions.
20 changes: 2 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,5 @@ yarn add @aurora-is-near/staking

## Usage

Wrap your application in a `StakingProvider`, for example:

```tsx
<StackingProvider
isConnected
network="mainnet"
>
<p>Hello, World!</p>
</StackingProvider>
```

Then access the staking functionality via the `useStaking` hook:

```tsx
const staking = useStaking();

console.log(staking.balance) // => 1337
```
Wrap your application in a `StakingProvider`, then access the staking
functionality via the `useStaking` hook.
6 changes: 5 additions & 1 deletion src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
getIsPaused,
getPendingWithdrawals,
getStreamedAmounts,
getStreamPrices,
getStreamsProgress,
getStreamsSchedule,
getTotalShares,
Expand All @@ -37,12 +36,17 @@ type StakingProviderProps = {
isConnected: boolean;
network: AuroraNetwork;
children: ReactNode;
getStreamPrices: (streamNames: string[]) => Promise<{
prices: number[];
marketCaps: number[];
}>;
};

export const StakingProvider = ({
isConnected,
network,
children,
getStreamPrices,
}: StakingProviderProps) => {
const networkConfig = config[network];
const { tokenStreams } = networkConfig;
Expand Down
51 changes: 1 addition & 50 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { differenceInMinutes } from 'date-fns';
import { BigNumber, ethers, providers } from 'ethers';
import { stakingAbi } from './abis/staking';
import { erc20abi } from './abis/erc20';
Expand Down Expand Up @@ -262,54 +261,6 @@ export const calculateStakedPctOfSupply = (
return pct;
};

export const getStreamPrices = async (
names: string[],
): Promise<{ prices: number[]; marketCaps: number[] }> => {
// Token ids in coingecko: https://docs.google.com/spreadsheets/d/1wTTuxXt8n9q7C4NDXqQpI3wpKu1_5bGVmP9Xz0XGSyU/

let refetch = false;
let data: any = null;

// Get cached data from localStorage
const localStorageData = localStorage.getItem('stream-prices');

data = localStorageData && JSON.parse(localStorageData);

// If there's no data or it's over a minute old, refetch
if (
!data ||
(data?.timestamp &&
differenceInMinutes(new Date(), new Date(data.timestamp)) > 0)
) {
refetch = true;
}

if (refetch) {
// Get fresh data and save to localStorage
const url = `/api/market/prices`;
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});

data = await response.json();

localStorage.setItem(
'stream-prices',
JSON.stringify({ ...data, timestamp: Date.now() }),
);
}

return {
prices: names.map((name) => (name === 'vote' ? 0 : data[name].usd)),
marketCaps: names.map((name) =>
name === 'vote' ? 0 : data[name].usd_market_cap,
),
};
};

export const getIsPaused = async (
provider: providers.JsonRpcProvider,
networkConfig: AuroraNetworkConfig,
Expand All @@ -322,7 +273,7 @@ export const getIsPaused = async (

const pausedFlag = await staking.paused();

return !!(pausedFlag.toNumber() & 1);
return pausedFlag.toNumber() === 1;
};

export const approveStaking = async (
Expand Down

0 comments on commit edf3c0e

Please sign in to comment.