Skip to content

Commit 8a6e890

Browse files
committed
fix transport
1 parent b387d6c commit 8a6e890

File tree

8 files changed

+60
-45
lines changed

8 files changed

+60
-45
lines changed

digit-recognition/frontend/src/features/cat-identifier/api/readRpcState.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { TypeRegistry } from "@polkadot/types";
2+
import { RegistryTypes } from "@polkadot/types/types";
23
import { useQuery } from "@tanstack/react-query";
34
import { useReadContract, useWatchContractEvent } from "wagmi";
45

@@ -7,11 +8,12 @@ import { catDogIdentifierAbi } from "./catDogIdentifierAbi";
78
import { CAT_IDENTIFIER_CONTRACT_ADDRESS, GEAR_API_NODE } from "@/consts";
89
import { CalcResult } from "../types";
910
import { mirrorAbi } from "./mirrorAbi";
11+
import { retryWhileDataChanged } from "@/lib/utils";
1012

1113
export const readRpcState = async (mirrorId?: HexString) => {
1214
if (!mirrorId) return;
1315

14-
const types: Record<string, any> = {
16+
const types: RegistryTypes = {
1517
FixedPoint: { num: "i128", scale: "u32" },
1618
CalcResult: {
1719
probability: "FixedPoint",
@@ -57,7 +59,7 @@ export const readRpcState = async (mirrorId?: HexString) => {
5759
json.result.payload
5860
);
5961

60-
let data = result[2].toJSON() as unknown as CalcResult;
62+
const data = result[2].toJSON() as unknown as CalcResult;
6163

6264
return data;
6365
};
@@ -85,13 +87,9 @@ export const useReadRpcState = ({ isSubmiting, onSuccess }: Params) => {
8587
eventName: "StateChanged",
8688
address: mirrorId as HexString,
8789
onLogs() {
88-
if (isSubmiting) {
89-
console.log("success reply");
90-
onSuccess();
91-
refetch();
92-
}
90+
retryWhileDataChanged({ data, refetch }).then(() => onSuccess());
9391
},
94-
enabled: !!mirrorId,
92+
enabled: !!mirrorId && isSubmiting,
9593
});
9694

9795
return {

digit-recognition/frontend/src/features/digit-recognizer/api/readRpcState.ts

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { digitRecognitionAbi } from "./DigitRecognitionAbi";
88
import { DIGIT_RECOGNITION_CONTRACT_ADDRESS, GEAR_API_NODE } from "@/consts";
99
import { Result } from "../types";
1010
import { mirrorAbi } from "./mirrorAbi";
11+
import { retryWhileDataChanged } from "@/lib/utils";
1112

1213
export const readRpcState = async (mirrorId?: HexString) => {
1314
if (!mirrorId) return;
@@ -77,38 +78,14 @@ export const useReadRpcState = ({ isSubmiting, onSuccess }: Params) => {
7778
enabled: !!mirrorId,
7879
});
7980

80-
const retryWhileDataChanged = () =>
81-
new Promise<void>((resolve) => {
82-
const prevData = JSON.stringify(data);
83-
84-
const retry = async (atempt = 0) => {
85-
const response = await refetch();
86-
const isSameData = JSON.stringify(response.data) === prevData;
87-
88-
if (isSameData) {
89-
setTimeout(() => {
90-
retry(atempt + 1);
91-
}, 1000);
92-
} else {
93-
console.log("resolved on atempt", atempt);
94-
resolve();
95-
}
96-
};
97-
98-
retry();
99-
});
100-
10181
useWatchContractEvent({
10282
abi: mirrorAbi,
10383
eventName: "StateChanged",
10484
address: mirrorId as HexString,
10585
onLogs() {
106-
if (isSubmiting) {
107-
console.log("success reply");
108-
retryWhileDataChanged().then(() => onSuccess());
109-
}
86+
retryWhileDataChanged({ data, refetch }).then(() => onSuccess());
11087
},
111-
enabled: !!mirrorId,
88+
enabled: !!mirrorId && isSubmiting,
11289
});
11390

11491
return {

digit-recognition/frontend/src/lib/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { FixedPoint } from "../types";
22
export { isMobileDevice, isMobile } from "./device-detection";
3+
export { retryWhileDataChanged } from "./retry-while-data-changed";
34

45
export const copyToClipboard = async ({
56
value,
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { QueryObserverResult } from "@tanstack/react-query";
2+
3+
type Params = {
4+
data: unknown;
5+
refetch: () => Promise<QueryObserverResult<unknown | undefined, Error>>;
6+
maxRetries?: number;
7+
};
8+
9+
const retryWhileDataChanged = ({ data, refetch, maxRetries = 15 }: Params) =>
10+
new Promise<void>((resolve) => {
11+
const prevData = JSON.stringify(data);
12+
13+
const retry = async (attempt = 0) => {
14+
if (attempt >= maxRetries) {
15+
console.log("maxRetries exceeded", maxRetries);
16+
resolve();
17+
return;
18+
}
19+
20+
const response = await refetch();
21+
const isSameData = JSON.stringify(response.data) === prevData;
22+
23+
if (isSameData) {
24+
setTimeout(() => retry(attempt + 1), 1000);
25+
} else {
26+
console.log("resolved on attempt", attempt);
27+
resolve();
28+
}
29+
};
30+
31+
retry();
32+
});
33+
34+
export { retryWhileDataChanged };

digit-recognition/frontend/src/providers.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { WagmiAdapter } from "@reown/appkit-adapter-wagmi";
22
import { PropsWithChildren } from "react";
3-
import { WagmiProvider, http } from "wagmi";
3+
import { WagmiProvider, webSocket } from "wagmi";
44
import { createAppKit } from "@reown/appkit/react";
55
import { AppKitNetwork } from "@reown/appkit/networks";
66
import * as allNetworks from "@reown/appkit/networks";
@@ -32,7 +32,7 @@ const networks = [getNetwork(ETH_CHAIN_ID)] as [
3232
const wagmiAdapter = new WagmiAdapter({
3333
networks,
3434
projectId: PROJECT_ID,
35-
transports: { [ETH_CHAIN_ID]: http(ETH_NODE_ADDRESS) },
35+
transports: { [ETH_CHAIN_ID]: webSocket(ETH_NODE_ADDRESS) },
3636
});
3737

3838
createAppKit({

mandelbrot-set/frontend/src/api/lib.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { GearApi, decodeAddress } from "@gear-js/api";
22
import { TypeRegistry } from "@polkadot/types";
3+
import { RegistryTypes } from "@polkadot/types/types";
34
import { TransactionBuilder, ActorId, ZERO_ADDRESS } from "sails-js";
45

56
export interface FixedPoint {
@@ -18,8 +19,11 @@ export class Program {
1819
public readonly registry: TypeRegistry;
1920
public readonly manager: Manager;
2021

21-
constructor(public api: GearApi, public programId?: `0x${string}`) {
22-
const types: Record<string, any> = {
22+
constructor(
23+
public api: GearApi,
24+
public programId?: `0x${string}`
25+
) {
26+
const types: RegistryTypes = {
2327
FixedPoint: { num: "i64", scale: "u32" },
2428
PointResult: { c_re: "i128", c_im: "i128", iter: "u32", checked: "bool" },
2529
};

mandelbrot-set/frontend/src/api/readRpcState.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { HexString } from "@gear-js/api";
22
import { TypeRegistry } from "@polkadot/types";
3+
import { RegistryTypes } from "@polkadot/types/types";
34
import { useQuery } from "@tanstack/react-query";
45
import { useReadContract } from "wagmi";
56
import { PointResult } from "./lib";
@@ -16,7 +17,7 @@ export const readRpcState = async (
1617
if (!mirrorId) return [];
1718
console.log("read state from startIndex:", startIndex);
1819

19-
const types: Record<string, any> = {
20+
const types: RegistryTypes = {
2021
PointResult: {
2122
c_re: "i128",
2223
c_im: "i128",
@@ -71,7 +72,7 @@ export const readRpcState = async (
7172
let data = result[2].toJSON() as unknown as Array<PointResult>;
7273

7374
if (data?.length === RESPONSE_SIZE) {
74-
let newData = await readRpcState(
75+
const newData = await readRpcState(
7576
mirrorId,
7677
startIndex + RESPONSE_SIZE,
7778
endIndex + RESPONSE_SIZE

mandelbrot-set/frontend/src/providers.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { WagmiAdapter } from "@reown/appkit-adapter-wagmi";
22
import { PropsWithChildren } from "react";
3-
import { WagmiProvider, http } from "wagmi";
3+
import { WagmiProvider, webSocket } from "wagmi";
44
import { createAppKit } from "@reown/appkit/react";
55
import { AppKitNetwork } from "@reown/appkit/networks";
66
import * as allNetworks from "@reown/appkit/networks";
@@ -32,13 +32,13 @@ const getNetwork = (id: number) => {
3232

3333
const networks = [getNetwork(ETH_CHAIN_ID)] as [
3434
AppKitNetwork,
35-
...AppKitNetwork[]
35+
...AppKitNetwork[],
3636
];
3737

3838
const wagmiAdapter = new WagmiAdapter({
3939
networks,
4040
projectId: PROJECT_ID,
41-
transports: { [ETH_CHAIN_ID]: http(ETH_NODE_ADDRESS) },
41+
transports: { [ETH_CHAIN_ID]: webSocket(ETH_NODE_ADDRESS) },
4242
});
4343

4444
createAppKit({
@@ -57,15 +57,15 @@ createAppKit({
5757
},
5858
enableWalletConnect: false,
5959
enableWalletGuide: false,
60-
60+
6161
allWallets: "HIDE",
6262
themeMode: "dark",
6363
themeVariables: {
6464
"--w3m-font-family": '"Roboto Mono", monospace',
6565
"--w3m-border-radius-master": "0px",
6666
"--w3m-font-size-master": "12px",
6767
"--w3m-accent": "#ffffff",
68-
"--w3m-color-mix": '#a8f593'
68+
"--w3m-color-mix": "#a8f593",
6969
},
7070
});
7171

0 commit comments

Comments
 (0)