Skip to content

Commit

Permalink
fix: store separate block numbers for each chain
Browse files Browse the repository at this point in the history
  • Loading branch information
jackmellis committed Nov 26, 2023
1 parent a0623dd commit 07a4d33
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
12 changes: 8 additions & 4 deletions packages/api/src/utils/nsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import config from '@nftx/config';
import queryApi from './queryApi';

const isApiBehind = () => {
const { apiBlockNumber, requiredBlockNumber } = config.internal;
const { network, internal } = config;
const apiBlockNumber = internal.apiBlockNumber[network];
const requiredBlockNumber = internal.requiredBlockNumber[network];

return apiBlockNumber < requiredBlockNumber;
};
Expand Down Expand Up @@ -47,21 +49,23 @@ const updateApiBlock = ({ source }: { source: 'live' | 'api' }) => {
// Wait a few seconds before polling the api again
setTimeout(async () => {
// Get the last indexed block on the api
config.internal.apiBlockNumber = await fetchLastIndexedBlock();
config.internal.apiBlockNumber[config.network] =
await fetchLastIndexedBlock();
// Run this fn again until we're up to date...
checkApiBlock();
}, 5000);
};

const resetRequiredBlock = () => {
// Reset the required block number
config.internal.requiredBlockNumber = 0;
config.internal.requiredBlockNumber[config.network] = 0;
// Switch back to using the api as the SoT
config.internal.source = 'api';
};

const checkApiBlock = (): void => {
const { requiredBlockNumber } = config.internal;
const requiredBlockNumber =
config.internal.requiredBlockNumber[config.network];

// We don't need to worry about syncing if there's no required block number
if (!requiredBlockNumber) {
Expand Down
43 changes: 32 additions & 11 deletions packages/config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,36 @@ export interface Config {
/** Internal config settings managed by nftx.js */
internal: {
source: 'api' | 'live';
requiredBlockNumber: number;
apiBlockNumber: number;
requiredBlockNumber: Record<string, number>;
apiBlockNumber: Record<string, number>;
};
}

const storeSetting = <T, K extends keyof T>(obj: T, name: K) => {
const storeSetting = (
obj: Record<string, any>,
propertyName: string,
storageKey: string,
value: any
) => {
if (
typeof window === 'undefined' ||
typeof window?.localStorage?.getItem !== 'function'
) {
return;
}
let defaultValue = JSON.stringify(obj[name]);
const key = `nftxjs_itl_${String(name)}`;
Object.defineProperty(obj, name, {

if (value && typeof value === 'object') {
Object.entries(value).forEach(([k, v]) => {
const key = `${storageKey}_${k}`;
storeSetting(value, k, key, v);
});
return;
}

let defaultValue = JSON.stringify(value);

const key = `nftxjs_itl_${storageKey}`;
Object.defineProperty(obj, propertyName, {
configurable: true,
enumerable: true,
get() {
Expand All @@ -83,9 +98,9 @@ const storeSetting = <T, K extends keyof T>(obj: T, name: K) => {
},
});
};
const storeSettings = <T extends Record<string, any>>(obj: T) => {
Object.keys(obj).forEach((key: keyof T) => {
storeSetting(obj, key);
const storeSettings = (obj: Record<string, any>) => {
Object.entries(obj).forEach(([key, value]) => {
storeSetting(obj, key, key, value);
});
};

Expand Down Expand Up @@ -118,8 +133,14 @@ const defaultConfig: Config = {

internal: {
source: 'api',
requiredBlockNumber: 0,
apiBlockNumber: 0,
requiredBlockNumber: {
[Network.Goerli]: 0,
[Network.Sepolia]: 0,
},
apiBlockNumber: {
[Network.Goerli]: 0,
[Network.Sepolia]: 0,
},
},
};

Expand Down
4 changes: 3 additions & 1 deletion packages/react/src/useTransaction/useWrapTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ export default function useWrapTransaction<F extends Fn>(
description,
});

config.internal.requiredBlockNumber = Number(receipt.blockNumber);
config.internal.requiredBlockNumber[network] = Number(
receipt.blockNumber
);

return receipt;
},
Expand Down

0 comments on commit 07a4d33

Please sign in to comment.