Skip to content

Commit 6decb90

Browse files
committed
Add auto-submit functionality for address submission after chain switch
1 parent c5a544f commit 6decb90

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

src/App.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ function App() {
4848
const [connected, setConnected] = useState("not connected");
4949
const [selectedRpcs, setSelectedRpcs] = useState({}); // chainId -> rpc URL mapping
5050
const [currentRpc, setCurrentRpc] = useState(null); // Currently active RPC URL
51+
const [shouldAutoSubmit, setShouldAutoSubmit] = useState(false); // Auto-submit after chain switch
5152

5253
// On Mount
5354
useEffect(() => {
@@ -202,6 +203,26 @@ function App() {
202203
}));
203204
};
204205

206+
const handleExampleContractClick = (contract) => {
207+
const currentChainId = chainArray[chainIndex]?.chainId;
208+
const targetChainId = contract.chainId;
209+
210+
// Set the address first
211+
setAddress(contract.address);
212+
213+
// If we're switching chains, set flag to auto-submit after connection
214+
if (currentChainId !== targetChainId) {
215+
setShouldAutoSubmit(true);
216+
const index = chainIdToIndex(contract.chainId);
217+
setChainIndex(index);
218+
} else if (connected === "connected") {
219+
// Same chain and connected, submit immediately
220+
handleSubmitAddress();
221+
} else {
222+
// Same chain but not connected, wait for connection
223+
setShouldAutoSubmit(true);
224+
}
225+
};
205226

206227
const handleAddressChange = (e) => {
207228
e.preventDefault();
@@ -272,6 +293,17 @@ function App() {
272293
[address, provider]
273294
);
274295

296+
// Auto-submit when connected after example contract click
297+
useEffect(() => {
298+
if (shouldAutoSubmit && connected === "connected" && provider) {
299+
// Make sure the provider is for the correct chain
300+
const expectedChainId = chainArray[chainIndex]?.chainId;
301+
if (provider.network?.chainId === expectedChainId) {
302+
setShouldAutoSubmit(false);
303+
handleSubmitAddress();
304+
}
305+
}
306+
}, [shouldAutoSubmit, connected, provider, chainArray, chainIndex, handleSubmitAddress]);
275307

276308
const handleDecodeCustomByteCode = useCallback(
277309
(e) => {
@@ -450,11 +482,7 @@ function App() {
450482
{/* <ul> */}
451483
{nonrandomContracts.map((contract, i) => (
452484
<button
453-
onClick={() => {
454-
setAddress(contract.address);
455-
const index = chainIdToIndex(contract.chainId);
456-
setChainIndex(index);
457-
}}
485+
onClick={() => handleExampleContractClick(contract)}
458486
key={`exampleContract-${i}`}
459487
className="mx-1 py-2 px-4 my-1 bg-ceruleanBlue-10 hover:bg-ceruleanBlue-100 hover:text-white text-ceruleanBlue-100 transition ease-in duration-100 text-center text-sm focus:outline-none focus:ring-2 focus:ring-offset-2 rounded-lg"
460488
>

0 commit comments

Comments
 (0)