-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathWalletsProvider.tsx
159 lines (142 loc) · 3.87 KB
/
WalletsProvider.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"use client";
import type { ReactNode } from "react";
import React, {
createContext,
useContext,
useEffect,
useMemo,
useReducer,
useState,
} from "react";
import { ERC20 as L1_ERC20_ABI } from "@/abi/L1/ERC20";
import L2_C1ERC20 from "@/abi/L2/C1ERC20.json";
import L2_ERC20 from "@/abi/L2/ERC20.json";
import {
NETWORK_NAME,
SUPPORTED_L1_CHAIN_ID,
SUPPORTED_L2_CHAIN_ID,
} from "@/constants/env";
import { ChainType, tokens as tokensConst } from "@/constants/tokens";
import {
useContractRead,
useAccount as useL2Account,
} from "@starknet-react/core";
import { hash, uint256 } from "starknet";
import { useBalance, useAccount as useL1Account } from "wagmi";
import { ETH, LORDS } from "@realms-world/constants";
import { initialState, reducer } from "./wallets-reducer";
interface WalletsProviderContextValue {
accountHash: string;
balances: {
l1: {
lords?: bigint;
eth?: bigint;
};
l2: {
lords?: bigint;
eth?: bigint;
};
};
l2loading: boolean;
refetch: () => void;
}
const WalletsProviderContext = createContext<
WalletsProviderContextValue | undefined
>(undefined);
export const useWalletsProviderContext = (): WalletsProviderContextValue => {
const context = useContext(WalletsProviderContext);
if (!context) {
throw new Error(
"useWalletsProviderContext must be used within a WalletsProvider",
);
}
return context;
};
interface WalletsContextProviderProps {
children: ReactNode;
}
export const WalletsProvider: React.FC<WalletsContextProviderProps> = ({
children,
}) => {
const [accountHash, setAccountHash] = useState("");
const calcAccountHash = (account1: string, account2: string) => {
return hash.computeHashOnElements([
BigInt(account1).toString(),
BigInt(account2).toString(),
]);
};
const [{ tokens }] = useReducer(reducer, initialState);
const { address: l1Account } = useL1Account();
const { address: l2Account } = useL2Account();
const {
data: l2LordsBalance,
isFetching: l2LordsIsLoading,
refetch: l2LordsRefetch,
} = useContractRead({
address:
LORDS[SUPPORTED_L2_CHAIN_ID]
?.address /*tokensConst.L2.LORDS.tokenAddress[ChainType.L2[NETWORK_NAME]]!,*/,
abi: L2_C1ERC20,
functionName: "balance_of",
enabled: !!l2Account,
args: l2Account ? [l2Account] : undefined,
watch: true,
});
const { data: l2EthBalance, isFetching: l2EthIsLoading } = useContractRead({
address: ETH[SUPPORTED_L2_CHAIN_ID]?.address,
abi: L2_ERC20,
functionName: "balanceOf",
enabled: !!l2Account,
args: l2Account ? [l2Account] : undefined,
watch: true,
});
const l1ERC20Contract = {
address: LORDS[SUPPORTED_L1_CHAIN_ID]?.address,
abi: L1_ERC20_ABI,
};
const { data: l1LordsBalance, refetch: l1LordsRefetch } = useBalance({
...l1ERC20Contract,
address: l1Account!,
token: LORDS[SUPPORTED_L1_CHAIN_ID]?.address as `0x${string}`,
});
const { data: l1EthBalance } = useBalance({
address: l1Account!,
});
useEffect(() => {
if (l1Account && l2Account) {
setAccountHash(calcAccountHash(l1Account, l2Account));
} else if (accountHash) {
setAccountHash("");
}
}, [l1Account, l2Account]);
const refetch = async () => {
l2LordsRefetch;
await l1LordsRefetch();
};
const value = {
accountHash,
tokens,
// updateTokenBalance,
l2loading: l2LordsIsLoading || l2EthIsLoading,
refetch: refetch,
balances: {
l1: {
eth: l1EthBalance?.value,
lords: l1LordsBalance?.value,
},
l2: {
//@ts-ignore
eth: l2EthBalance?.balance
? //@ts-ignore
uint256.uint256ToBN(l2EthBalance?.balance)
: 0n,
lords: l2LordsBalance as bigint,
},
},
};
return (
<WalletsProviderContext.Provider value={value}>
{children}
</WalletsProviderContext.Provider>
);
};