From 7c099312deef3eb2a43165e6eb02d5217cd11721 Mon Sep 17 00:00:00 2001 From: Max Daunarovich Date: Mon, 25 Apr 2022 10:01:05 +0300 Subject: [PATCH 01/16] Fix typo --- pages/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pages/index.js b/pages/index.js index 1bf02c5..c590dc7 100644 --- a/pages/index.js +++ b/pages/index.js @@ -78,7 +78,7 @@ export default function Home() { const templateInfo = getTemplateInfo(code); const { type, signers, args } = templateInfo; - // METHDOS + // METHODS const updateImports = async () => { const env = await getEnvironment(network); const newCode = replaceImportAddresses(code, env); From 10f1927239ce752f193013156f28e1d99d7754d6 Mon Sep 17 00:00:00 2001 From: Max Daunarovich Date: Mon, 25 Apr 2022 10:02:35 +0300 Subject: [PATCH 02/16] Reorder comments --- pages/index.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/pages/index.js b/pages/index.js index c590dc7..dfc04fe 100644 --- a/pages/index.js +++ b/pages/index.js @@ -65,6 +65,7 @@ const prepareFinalImports = async (list, setFinal) => { }; export default function Home() { + // HOOKS const [monacoReady, setMonacoReady] = useState(false); const [code, updateScriptCode] = useState(flovatarTotalSupply); const [result, setResult] = useState(); @@ -72,12 +73,8 @@ export default function Home() { const [registry, setRegistry] = useState(null); const [importList, setImportList] = useState({}); const [finalImports, setFinalImports] = useState([]); - const network = useNetworkContext() || "testnet"; - const templateInfo = getTemplateInfo(code); - const { type, signers, args } = templateInfo; - // METHODS const updateImports = async () => { const env = await getEnvironment(network); @@ -133,6 +130,13 @@ export default function Home() { setRegistry(registry); }; + // CONSTANTS + const templateInfo = getTemplateInfo(code); + const { type, signers, args } = templateInfo; + const fclAble = signers && signers === 1 && type === "transaction"; + const disabled = + type === "unknown" || type === "contract" || !monacoReady || signers > 1; + // EFFECTS useEffect(() => { fcl.unauthenticate(); @@ -152,10 +156,7 @@ export default function Home() { [importList, network] ); - const fclAble = signers && signers === 1 && type === "transaction"; - const disabled = - type === "unknown" || type === "contract" || !monacoReady || signers > 1; - + // RENDER return (
From a72aff390e4995f608067e678225d3a644cbc758 Mon Sep 17 00:00:00 2001 From: Max Daunarovich Date: Mon, 25 Apr 2022 14:05:34 +0300 Subject: [PATCH 03/16] Use cdc method from fcl --- templates/code.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/templates/code.js b/templates/code.js index e28a873..089208a 100644 --- a/templates/code.js +++ b/templates/code.js @@ -1,19 +1,22 @@ -export const baseScript = ` +import { cdc } from "@onflow/fcl"; + +export const baseScript = cdc` // This is the most basic script you can execute on Flow Network pub fun main():Int { return 42 } -`.slice(1); // remove new line at the bof +`(); -export const flovatarTotalSupply = ` +export const flovatarTotalSupply = cdc` +/// pragma title Flovatar Total Supply import Flovatar from 0x01 pub fun main():UInt64{ return Flovatar.totalSupply } -`.slice(1); // remove new line at the bof +`(); -export const baseTransaction = ` +export const baseTransaction = cdc` // This is the most basic transaction you can execute on Flow Network transaction() { prepare(signer: AuthAccount) { @@ -23,4 +26,4 @@ transaction() { } } -`.slice(1); // remove new line at the bof +`(); From 20fed7c432f674cecef5f9c3115d0b1f3cd5642e Mon Sep 17 00:00:00 2001 From: Max Daunarovich Date: Mon, 25 Apr 2022 14:05:47 +0300 Subject: [PATCH 04/16] Reorder imports --- pages/index.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pages/index.js b/pages/index.js index dfc04fe..8ce3762 100644 --- a/pages/index.js +++ b/pages/index.js @@ -1,6 +1,7 @@ import React, { useEffect, useState } from "react"; import Head from "next/head"; import dynamic from "next/dynamic"; +import * as fcl from "@onflow/fcl"; import { executeScript, sendTransaction, @@ -19,11 +20,9 @@ import { useNetworkContext } from "../contexts/NetworkContext"; import { buttonLabels } from "../templates/labels"; import { baseTransaction, flovatarTotalSupply } from "../templates/code"; -import * as fcl from "@onflow/fcl"; - import "../flow/config.js"; import { configureForNetwork } from "../flow/config"; -import { debounce, fetchRegistry, prepareEnvironments } from "../utils"; +import { debounce, fetchRegistry, prepareEnvironments,cdc } from "../utils"; const CadenceChecker = dynamic( () => import("../components/LSP/CadenceChecker"), From 9275aed2a8df8fd6710bf3e27543ce72699ab5bf Mon Sep 17 00:00:00 2001 From: Max Daunarovich Date: Mon, 25 Apr 2022 14:06:01 +0300 Subject: [PATCH 05/16] Update flow-cadut to latest release --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a2c8755..7bfb79c 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "@onflow/fcl": "^0.0.77", "@picocss/pico": "^1.4.1", "copy-webpack-plugin": "^10.2.4", - "flow-cadut": "^0.1.15-alpha.26", + "flow-cadut": "^0.1.15", "monaco-editor": "^0.31.1", "monaco-editor-webpack-plugin": "^7.0.1", "monaco-languageclient": "^0.18.1", From c5f882af58f26ef581c3e4fadee6ef57175e9078 Mon Sep 17 00:00:00 2001 From: Max Daunarovich Date: Mon, 25 Apr 2022 16:46:20 +0300 Subject: [PATCH 06/16] Create Registry context --- components/Registry/hooks.js | 87 ++++++++++++++++++++++++++++++++++++ components/Registry/index.js | 15 +++++++ 2 files changed, 102 insertions(+) create mode 100644 components/Registry/hooks.js create mode 100644 components/Registry/index.js diff --git a/components/Registry/hooks.js b/components/Registry/hooks.js new file mode 100644 index 0000000..52e9392 --- /dev/null +++ b/components/Registry/hooks.js @@ -0,0 +1,87 @@ +import { useEffect, useReducer, useState } from "react"; +import { extendEnvironment, extractImports } from "flow-cadut"; +import * as fcl from "@onflow/fcl"; + +import { fetchRegistry, prepareEnvironments } from "../../utils"; +import { useNetworkContext } from "../../contexts/NetworkContext"; + +const contractReducer = (state, action) => { + const { contracts, network } = action; + + return { + ...state, + [network]: { + ...state[network], + ...contracts, + }, + }; +}; + +export const useRegistry = () => { + const network = useNetworkContext() || "testnet"; + const [registry, setRegistry] = useState({}); + const [contracts, dispatch] = useReducer(contractReducer, { + testnet: {}, + mainnet: {}, + }); + + const getRegistry = async () => { + const data = await fetchRegistry(); + const registry = prepareEnvironments(data); + extendEnvironment(registry); + setRegistry(registry); + }; + + useEffect(() => { + getRegistry().then(); + }, []); + + const fetchDependencies = (list) => { + const keys = Object.keys(list); + for (let i = 0; i < keys.length; i++) { + // TODO: check if it's cached + const name = keys[i]; + const address = list[name]; + const contract = contracts[network][name]; + if (!contract) { + console.log("FETCH DEPENDENCY", network, name, address); + fetchContract(name, address); + } + } + }; + + const fetchContract = async (name, exactAddress) => { + const address = exactAddress || registry[network][name]; + console.log(name, address); + try { + const { contracts } = await fcl + .send([fcl.getAccount(address)]) + .then(fcl.decode); + const code = contracts[name] || ""; + + console.log("get dependencies for", name); + const dependencies = extractImports(code); + console.log({ dependencies }); + fetchDependencies(dependencies); + + console.log("DISPATCH", name, network); + // Update state + dispatch({ + contracts, + network, + }); + } catch (e) { + console.warn(e); + } + }; + + useEffect(() => { + console.log({ contracts }); + }, [contracts]); + + const getContractCode = (name) => { + return contracts[network][name] || ""; + }; + + return { registry, contracts, fetchContract, getContractCode }; +}; diff --git a/components/Registry/index.js b/components/Registry/index.js new file mode 100644 index 0000000..3c8312c --- /dev/null +++ b/components/Registry/index.js @@ -0,0 +1,15 @@ +import { createContext, useContext } from "react"; +import { useRegistry } from "./hooks"; + +export const RegistryContext = createContext({}); +export const useRegistryContext = () => useContext(RegistryContext); + +export default function RegistryProvider(props) { + const registry = useRegistry(); + const { children } = props; + return ( + + {children} + + ); +} From c261adef54a22379bb63ede7571a21ef473ee3b2 Mon Sep 17 00:00:00 2001 From: Max Daunarovich Date: Mon, 25 Apr 2022 16:46:36 +0300 Subject: [PATCH 07/16] Add RegistryContext to top level --- pages/_app.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pages/_app.js b/pages/_app.js index 9cf4947..2522742 100644 --- a/pages/_app.js +++ b/pages/_app.js @@ -6,6 +6,7 @@ import * as fcl from "@onflow/fcl"; import { NetworkProvider } from "../contexts/NetworkContext"; import { configureForNetwork } from "../flow/config"; import React, { useState } from "react"; +import RegistryProvider from "../components/Registry"; function MyApp({ Component, pageProps }) { const [network, setNetwork] = useState(); @@ -45,7 +46,9 @@ function MyApp({ Component, pageProps }) {
- + + +
From 573302a16fabc843e5d0c95f40bc7e7a916efb73 Mon Sep 17 00:00:00 2001 From: Max Daunarovich Date: Mon, 25 Apr 2022 16:46:56 +0300 Subject: [PATCH 08/16] Update template --- templates/code.js | 1 + 1 file changed, 1 insertion(+) diff --git a/templates/code.js b/templates/code.js index 089208a..dd1899c 100644 --- a/templates/code.js +++ b/templates/code.js @@ -10,6 +10,7 @@ pub fun main():Int { export const flovatarTotalSupply = cdc` /// pragma title Flovatar Total Supply import Flovatar from 0x01 +import FIND from 0x02 pub fun main():UInt64{ return Flovatar.totalSupply From 719ec85072a812bc5752fe5c24ba9b65f1db8865 Mon Sep 17 00:00:00 2001 From: Max Daunarovich Date: Mon, 25 Apr 2022 16:47:14 +0300 Subject: [PATCH 09/16] Update language server to use contracts registry --- components/LSP/useLanguageServer.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/components/LSP/useLanguageServer.js b/components/LSP/useLanguageServer.js index ff3e2d2..25ceb25 100644 --- a/components/LSP/useLanguageServer.js +++ b/components/LSP/useLanguageServer.js @@ -4,6 +4,9 @@ import { MonacoServices } from "monaco-languageclient/lib/monaco-services"; import { CadenceLanguageServer } from "./language-server"; import { createCadenceLanguageClient } from "./language-client"; +import { useNetworkContext } from "../../contexts/NetworkContext"; +import { setEnvironment } from "flow-cadut"; +import { useRegistryContext } from "../Registry"; let monacoServicesInstalled = false; @@ -39,6 +42,9 @@ const launchLanguageClient = async ( }; export default function useLanguageServer() { + const network = useNetworkContext() || "testnet"; + const { getContractCode } = useRegistryContext(); + let initialCallbacks = { // The actual callback will be set as soon as the language server is initialized toServer: null, @@ -63,13 +69,10 @@ export default function useLanguageServer() { const [languageClient, setLanguageClient] = useState(null); const [callbacks, setCallbacks] = useState(initialCallbacks); - const getCode = (address) => { - // TODO: Fetch code from address. - /* - This is probably can't be implemented right now as server expects only address, - but not the name of the contract. - */ - return ""; + const getCode = (importStatement) => { + const contractName = importStatement.split(".")[1] + const code = getContractCode(contractName) + return code }; const restartServer = () => { @@ -85,6 +88,7 @@ export default function useLanguageServer() { // The Monaco Language Client services have to be installed globally, once. // An editor must be passed, which is only used for commands. // As the Cadence language server is not providing any commands this is OK + setEnvironment(network); console.log("Installing monaco services"); if (!monacoServicesInstalled) { From 5001a27c3cf32dade220111a66879724eaff1c12 Mon Sep 17 00:00:00 2001 From: Max Daunarovich Date: Mon, 25 Apr 2022 16:47:43 +0300 Subject: [PATCH 10/16] Utilize registry context to properly resolve imports --- pages/index.js | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/pages/index.js b/pages/index.js index 8ce3762..52cc9ac 100644 --- a/pages/index.js +++ b/pages/index.js @@ -15,6 +15,7 @@ import { import Transaction from "../components/Transaction"; import CadenceEditor from "../components/CadenceEditor"; +import Registry from "../components/Registry"; import { useNetworkContext } from "../contexts/NetworkContext"; import { buttonLabels } from "../templates/labels"; @@ -22,7 +23,8 @@ import { baseTransaction, flovatarTotalSupply } from "../templates/code"; import "../flow/config.js"; import { configureForNetwork } from "../flow/config"; -import { debounce, fetchRegistry, prepareEnvironments,cdc } from "../utils"; +import { debounce } from "../utils"; +import { useRegistryContext } from "../components/Registry"; const CadenceChecker = dynamic( () => import("../components/LSP/CadenceChecker"), @@ -69,11 +71,15 @@ export default function Home() { const [code, updateScriptCode] = useState(flovatarTotalSupply); const [result, setResult] = useState(); const [user, setUser] = useState(); - const [registry, setRegistry] = useState(null); const [importList, setImportList] = useState({}); const [finalImports, setFinalImports] = useState([]); const network = useNetworkContext() || "testnet"; + // CONTEXTS + const fullRegistry = useRegistryContext(); + console.log({ fullRegistry }); + const { registry, fetchContract } = fullRegistry; + // METHODS const updateImports = async () => { const env = await getEnvironment(network); @@ -83,7 +89,6 @@ export default function Home() { const send = async () => { await setEnvironment(network); extendEnvironment(registry); - switch (true) { // Script Handling case type === "script": { @@ -122,11 +127,15 @@ export default function Home() { break; } }; - const getRegistry = async () => { - const data = await fetchRegistry(); - const registry = prepareEnvironments(data); - extendEnvironment(registry); - setRegistry(registry); + const fetchContracts = () => { + if (fetchContract) { + const contracts = Object.keys(importList); + for (let i = 0; i < contracts.length; i++) { + const name = contracts[i]; + console.log(i, name); + fetchContract(name); + } + } }; // CONSTANTS @@ -140,20 +149,20 @@ export default function Home() { useEffect(() => { fcl.unauthenticate(); fcl.currentUser().subscribe(setUser); - - getRegistry().then(); }, []); useEffect(() => { setEnvironment(network); - if (registry !== null) { + if (registry) { extendEnvironment(registry); } }, [network]); useEffect(() => getImports(code, setImportList), [code]); - useEffect( - () => prepareFinalImports(importList, setFinalImports), - [importList, network] - ); + useEffect(() => { + prepareFinalImports(importList, setFinalImports); + }, [importList, network]); + useEffect(() => { + fetchContracts(); + }, [importList]); // RENDER return ( From 66e26741d0089de59a8f21a30b75c93d970c1f58 Mon Sep 17 00:00:00 2001 From: Max Daunarovich Date: Mon, 25 Apr 2022 17:08:26 +0300 Subject: [PATCH 11/16] Debugging --- components/LSP/useLanguageServer.js | 5 ++++- components/Registry/hooks.js | 13 ++++++++++++- package.json | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/components/LSP/useLanguageServer.js b/components/LSP/useLanguageServer.js index 25ceb25..1ce5fbf 100644 --- a/components/LSP/useLanguageServer.js +++ b/components/LSP/useLanguageServer.js @@ -71,7 +71,10 @@ export default function useLanguageServer() { const getCode = (importStatement) => { const contractName = importStatement.split(".")[1] - const code = getContractCode(contractName) + const code = getContractCode(contractName) || "" + if(!code){ + console.log(`Could not find code for ${contractName}`) + } return code }; diff --git a/components/Registry/hooks.js b/components/Registry/hooks.js index 52e9392..4d7fbbc 100644 --- a/components/Registry/hooks.js +++ b/components/Registry/hooks.js @@ -51,7 +51,12 @@ export const useRegistry = () => { }; const fetchContract = async (name, exactAddress) => { - const address = exactAddress || registry[network][name]; + let address = exactAddress; + if (!exactAddress && registry[network]) { + address = exactAddress || registry[network][name]; + } else { + address = ""; + } console.log(name, address); try { const { contracts } = await fcl @@ -80,6 +85,12 @@ export const useRegistry = () => { }, [contracts]); const getContractCode = (name) => { + console.log("REQUESTED ", name); + const contract = contracts[network][name]; + if (!contract) { + console.log({contracts}); + console.log(`%cNOT FOUND ${name}`, "color: red"); + } return contracts[network][name] || ""; }; diff --git a/package.json b/package.json index 7bfb79c..c3e7a6f 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "@onflow/fcl": "^0.0.77", "@picocss/pico": "^1.4.1", "copy-webpack-plugin": "^10.2.4", - "flow-cadut": "^0.1.15", + "flow-cadut": "^0.1.16-alpha.1", "monaco-editor": "^0.31.1", "monaco-editor-webpack-plugin": "^7.0.1", "monaco-languageclient": "^0.18.1", From f885d742e9b178be0cc976a7d5b63d07451bd1cc Mon Sep 17 00:00:00 2001 From: Max Daunarovich Date: Tue, 26 Apr 2022 15:19:49 +0300 Subject: [PATCH 12/16] Update flow-cadut --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c3e7a6f..8b87034 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "@onflow/fcl": "^0.0.77", "@picocss/pico": "^1.4.1", "copy-webpack-plugin": "^10.2.4", - "flow-cadut": "^0.1.16-alpha.1", + "flow-cadut": "^0.1.16-alpha.3", "monaco-editor": "^0.31.1", "monaco-editor-webpack-plugin": "^7.0.1", "monaco-languageclient": "^0.18.1", From f02247cb9da167ecbfd39c8eaa155687be795d20 Mon Sep 17 00:00:00 2001 From: Max Daunarovich Date: Tue, 26 Apr 2022 15:20:09 +0300 Subject: [PATCH 13/16] Update template --- pages/index.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/pages/index.js b/pages/index.js index 52cc9ac..f1d7913 100644 --- a/pages/index.js +++ b/pages/index.js @@ -25,6 +25,7 @@ import "../flow/config.js"; import { configureForNetwork } from "../flow/config"; import { debounce } from "../utils"; import { useRegistryContext } from "../components/Registry"; +import { cdc } from "@onflow/fcl"; const CadenceChecker = dynamic( () => import("../components/LSP/CadenceChecker"), @@ -66,6 +67,15 @@ const prepareFinalImports = async (list, setFinal) => { }; export default function Home() { + const initCode = cdc` + // + import FlovatarComponent from 0x0cf264811b95d465 + + pub fun main(): UInt64 { + return FlovatarComponent.totalSupply + } + `(); + // HOOKS const [monacoReady, setMonacoReady] = useState(false); const [code, updateScriptCode] = useState(flovatarTotalSupply); @@ -77,8 +87,9 @@ export default function Home() { // CONTEXTS const fullRegistry = useRegistryContext(); - console.log({ fullRegistry }); - const { registry, fetchContract } = fullRegistry; + const { registry, contracts, fetchContract } = fullRegistry; + + console.log({fullRegistry}) // METHODS const updateImports = async () => { @@ -127,12 +138,12 @@ export default function Home() { break; } }; + const fetchContracts = () => { if (fetchContract) { const contracts = Object.keys(importList); for (let i = 0; i < contracts.length; i++) { const name = contracts[i]; - console.log(i, name); fetchContract(name); } } @@ -164,6 +175,8 @@ export default function Home() { fetchContracts(); }, [importList]); + // TODO: Refresh editor to enable recheck + // RENDER return (
From 45fe0c4159111a0380b1a92b846d4786f1c1876d Mon Sep 17 00:00:00 2001 From: Max Daunarovich Date: Tue, 26 Apr 2022 15:20:21 +0300 Subject: [PATCH 14/16] Remove FIND for clear check --- templates/code.js | 1 - 1 file changed, 1 deletion(-) diff --git a/templates/code.js b/templates/code.js index dd1899c..089208a 100644 --- a/templates/code.js +++ b/templates/code.js @@ -10,7 +10,6 @@ pub fun main():Int { export const flovatarTotalSupply = cdc` /// pragma title Flovatar Total Supply import Flovatar from 0x01 -import FIND from 0x02 pub fun main():UInt64{ return Flovatar.totalSupply From af3cb4c70412f0dcfbcb94918cd6418a0c98f4e4 Mon Sep 17 00:00:00 2001 From: Max Daunarovich Date: Tue, 26 Apr 2022 15:20:58 +0300 Subject: [PATCH 15/16] Update registry hook --- components/Registry/hooks.js | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/components/Registry/hooks.js b/components/Registry/hooks.js index 4d7fbbc..29d1028 100644 --- a/components/Registry/hooks.js +++ b/components/Registry/hooks.js @@ -44,7 +44,6 @@ export const useRegistry = () => { const address = list[name]; const contract = contracts[network][name]; if (!contract) { - console.log("FETCH DEPENDENCY", network, name, address); fetchContract(name, address); } } @@ -53,45 +52,36 @@ export const useRegistry = () => { const fetchContract = async (name, exactAddress) => { let address = exactAddress; if (!exactAddress && registry[network]) { - address = exactAddress || registry[network][name]; - } else { - address = ""; + address = registry[network][name] + } + if(!address){ + return false } - console.log(name, address); try { + console.log("--------> ADDRESS:", {name, exactAddress, address}) const { contracts } = await fcl .send([fcl.getAccount(address)]) .then(fcl.decode); const code = contracts[name] || ""; - - console.log("get dependencies for", name); const dependencies = extractImports(code); - console.log({ dependencies }); fetchDependencies(dependencies); - console.log("DISPATCH", name, network); // Update state dispatch({ contracts, network, }); } catch (e) { - console.warn(e); + console.error(e); } }; - useEffect(() => { - console.log({ contracts }); - }, [contracts]); - - const getContractCode = (name) => { - console.log("REQUESTED ", name); + const getContractCode = (name, address) => { const contract = contracts[network][name]; - if (!contract) { - console.log({contracts}); - console.log(`%cNOT FOUND ${name}`, "color: red"); + if(!contract){ + fetchContract(name, address).then(); } - return contracts[network][name] || ""; + return contract || ""; }; return { registry, contracts, fetchContract, getContractCode }; From f473a51e4b9327b27380e1d7b220a9f9f95f3ea5 Mon Sep 17 00:00:00 2001 From: Max Daunarovich Date: Tue, 26 Apr 2022 15:21:09 +0300 Subject: [PATCH 16/16] Update language server --- components/LSP/useLanguageServer.js | 38 +++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/components/LSP/useLanguageServer.js b/components/LSP/useLanguageServer.js index 1ce5fbf..494f7e7 100644 --- a/components/LSP/useLanguageServer.js +++ b/components/LSP/useLanguageServer.js @@ -1,4 +1,4 @@ -import { useState, useEffect } from "react"; +import { useState, useEffect, useMemo, useRef } from "react"; import { monaco } from "react-monaco-editor"; import { MonacoServices } from "monaco-languageclient/lib/monaco-services"; @@ -7,6 +7,7 @@ import { createCadenceLanguageClient } from "./language-client"; import { useNetworkContext } from "../../contexts/NetworkContext"; import { setEnvironment } from "flow-cadut"; import { useRegistryContext } from "../Registry"; +import { debounce } from "../../utils"; let monacoServicesInstalled = false; @@ -43,7 +44,7 @@ const launchLanguageClient = async ( export default function useLanguageServer() { const network = useNetworkContext() || "testnet"; - const { getContractCode } = useRegistryContext(); + const { registry, contracts, getContractCode } = useRegistryContext(); let initialCallbacks = { // The actual callback will be set as soon as the language server is initialized @@ -69,23 +70,38 @@ export default function useLanguageServer() { const [languageClient, setLanguageClient] = useState(null); const [callbacks, setCallbacks] = useState(initialCallbacks); + const timer = useRef(null) + const getCode = (importStatement) => { - const contractName = importStatement.split(".")[1] - const code = getContractCode(contractName) || "" - if(!code){ - console.log(`Could not find code for ${contractName}`) + const [address,contractName] = importStatement.split(".") + const code = getContractCode(contractName, address) || "" + if(code === ""){ + console.log(`%c+++++++++++++ NOT FOUND!!!!!!!!!! ${contractName}`,"color: red") } return code }; - const restartServer = () => { + const restartServer = ()=>{ console.log("Restarting server..."); startLanguageServer(callbacks, getCode, { setLanguageServer, setCallbacks, }).then(); - }; + } + + const debouncedRestart = () => { + if(timer.current){ + clearTimeout(timer.current); + } + timer.current = setTimeout(()=>{ + console.log("Restart language server") + if(languageServer){ + languageServer.updateCodeGetter(getCode) + // restartServer() + } + }, 2000); + } useEffect(() => { // The Monaco Language Client services have to be installed globally, once. @@ -108,6 +124,12 @@ export default function useLanguageServer() { } }, [languageServer]); + useEffect(()=>{ + if(languageServer){ + languageServer.updateCodeGetter(getCode) + } + },[registry, contracts]) + return { languageClient, languageServer,