Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wighawag committed Feb 8, 2024
1 parent 9438e4b commit 2d38453
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 25 deletions.
7 changes: 7 additions & 0 deletions examples/basic/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# basic

## 0.0.3

### Patch Changes

- Updated dependencies
- ethereum-indexer-browser@0.7.0

## 0.0.2

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions examples/basic/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "basic",
"private": true,
"version": "0.0.2",
"version": "0.0.3",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"ethereum-indexer-browser": "workspace:^0.6.14",
"ethereum-indexer-browser": "workspace:^0.7.0",
"ethereum-indexer-js-processor": "workspace:^0.6.16",
"react": "^18.2.0",
"react-dom": "^18.2.0"
Expand Down
7 changes: 7 additions & 0 deletions examples/mud/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# web-demo

## 0.1.62

### Patch Changes

- Updated dependencies
- ethereum-indexer-browser@0.7.0

## 0.1.61

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion examples/mud/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mud-demo",
"private": true,
"version": "0.1.61",
"version": "0.1.62",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
7 changes: 7 additions & 0 deletions examples/web-demo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# web-demo

## 0.1.62

### Patch Changes

- Updated dependencies
- ethereum-indexer-browser@0.7.0

## 0.1.61

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion examples/web-demo/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "web-demo",
"private": true,
"version": "0.1.61",
"version": "0.1.62",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
6 changes: 6 additions & 0 deletions packages/ethereum-indexer-browser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# ethereum-indexer-browser

## 0.7.0

### Minor Changes

- support url

## 0.6.30

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/ethereum-indexer-browser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ethereum-indexer-browser",
"version": "0.6.30",
"version": "0.7.0",
"publishConfig": {
"access": "public"
},
Expand Down
62 changes: 42 additions & 20 deletions packages/ethereum-indexer-browser/src/storage/state/OnIndexedDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,53 +40,75 @@ export function keepStateOnIndexedDB<ABI extends Abi, ProcessResultType, Process
let remoteState: StateData<ABI, ProcessResultType, unknown> | undefined;
if (remote) {
if (Array.isArray(remote)) {

let latest: {index: number; lastSync?: LastSync<Abi>} | undefined;
for (let i = 0; i < remote.length; i++) {
if (typeof remote[i] === 'string' || 'url' in remote[i]) {
// console.log(`raw url, do not fetch lastSync`);
continue;
}
const urlOfLastSync = getURL(remote[i], context, true);
try {
const response = await fetch(urlOfLastSync);
const json: LastSync<Abi> = await response.json();
if (!latest || !latest.lastSync || json.lastToBlock > latest.lastSync.lastToBlock) {
latest = {
index: i,
lastSync: json
const urlOfRemote = getURL(remote[i], context);
try {
const response = await fetch(urlOfRemote);
const json: {
state: ProcessResultType;
lastSync: LastSync<ABI>;
} = await response.json();
if (!latest || !latest.lastSync || (json.lastSync && json.lastSync.lastToBlock > latest.lastSync.lastToBlock)) {
latest = {
index: i,
lastSync: json.lastSync,
};
}
} catch (err) {
console.error(`failed to fetch remote lastSync`, err);
}
} else {
const urlOfLastSync = getURL(remote[i], context, true);
try {
const response = await fetch(urlOfLastSync);
const json: LastSync<Abi> = await response.json();
if (!latest || !latest.lastSync || json.lastToBlock > latest.lastSync.lastToBlock) {
latest = {
index: i,
lastSync: json,
};
}
} catch (err) {
console.error(`failed to fetch remote lastSync`, err);
}
} catch (err) {
console.error(`failed to fetch remote lastSync`, err);
}
}

if (existingState && latest && latest.lastSync && latest.lastSync.lastToBlock < existingState.lastSync.lastToBlock) {
if (
existingState &&
latest &&
latest.lastSync &&
latest.lastSync.lastToBlock < existingState.lastSync.lastToBlock
) {
// console.log(`Existing State`)
return existingState;
}

if (!latest) {
console.error(`could not fetch any valid lastSync, still continue with first`);
latest = {
index: 0
}
}
index: 0,
};
}
// else {
// console.log(`Using ${latest.index}`)
// }
const url = getURL(remote[latest.index], context);
try {
const response = await fetch(url);
const json = await response.json();
remoteState = json;
} catch (err) {
console.error(`failed to fetch remote-state, try second`, err);

const url = getURL(remote[(latest.index + 1) % remote.length], context);
try {
const response = await fetch(url);
const json = await response.json();
remoteState = json;
} catch(err) {
} catch (err) {
console.error(`failed to fetch second remote-state`, err);
// TODO more than 2
}
Expand Down

0 comments on commit 2d38453

Please sign in to comment.