Skip to content

Commit

Permalink
Merge pull request #615 from ar-io/fix-dynamic-ticker
Browse files Browse the repository at this point in the history
fix(PE-7259): make ticker dynamic
  • Loading branch information
atticusofsparta authored Dec 9, 2024
2 parents b312bf4 + 040afc3 commit 8e86c90
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/state/contexts/GlobalState.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { AOProcess, AoClient, AoIORead, AoIOWrite, IO } from '@ar.io/sdk/web';
import { connect } from '@permaweb/aoconnect';
import eventEmitter from '@src/utils/events';
import React, {
Dispatch,
createContext,
useContext,
useEffect,
useReducer,
useState,
} from 'react';

import { ArweaveCompositeDataProvider } from '../../services/arweave/ArweaveCompositeDataProvider';
Expand Down Expand Up @@ -48,7 +48,7 @@ export type GlobalState = {

const initialState: GlobalState = {
ioProcessId: IO_PROCESS_ID,
ioTicker: '',
ioTicker: 'ARIO',
gateway: ARWEAVE_HOST,
aoNetwork: NETWORK_DEFAULTS.AO,
aoClient: connect(NETWORK_DEFAULTS.AO),
Expand Down Expand Up @@ -86,25 +86,24 @@ export function GlobalStateProvider({
? { ...initialState, arweaveDataProvider }
: initialState,
);
const [updatingTicker, setUpdatingTicker] = useState(false);

useEffect(() => {
if (state.ioTicker === initialState.ioTicker && !updatingTicker) {
updateTicker();
async function updateTicker() {
try {
const ticker = (await state.arioContract.getInfo()).Ticker;
dispatchGlobalState({ type: 'setIoTicker', payload: ticker });
} catch (error: any) {
eventEmitter.emit(
'error',
new Error(
'Unable to fetch ticker from network process: ' + error.message,
),
);
}
}
}, []);

async function updateTicker() {
try {
setUpdatingTicker(true);
const ticker = 'tIO'; // TODO, use contract to get ticker
dispatchGlobalState({ type: 'setIoTicker', payload: ticker });
} catch (error) {
console.error(error);
} finally {
setUpdatingTicker(false);
}
}
updateTicker();
}, [state.arioContract]);

return (
<GlobalStateContext.Provider value={[state, dispatchGlobalState]}>
Expand Down

0 comments on commit 8e86c90

Please sign in to comment.