diff --git a/state/actions/addFaucetAccount.ts b/state/actions/addFaucetAccount.ts index 7ca2f6a..31fd3e0 100644 --- a/state/actions/addFaucetAccount.ts +++ b/state/actions/addFaucetAccount.ts @@ -20,7 +20,14 @@ export const names = [ "Walter", ]; +/* This function adds faucet account to application global state. + * It calls the /api/faucet endpoint which in send a HTTP POST to + * https://hooks-testnet.xrpl-labs.com/newcreds and it returns + * new account with 10 000 XRP. Hooks Testnet /newcreds endpoint + * is protected with CORS so that's why we did our own endpoint + */ export const addFaucetAccount = async (showToast: boolean = false) => { + // Lets limit the number of faucet accounts to 5 for now if (state.accounts.length > 4) { return toast.error("You can only have maximum 5 accounts"); } diff --git a/state/actions/compileCode.ts b/state/actions/compileCode.ts index d844f74..938f3fd 100644 --- a/state/actions/compileCode.ts +++ b/state/actions/compileCode.ts @@ -6,15 +6,24 @@ import { saveFile } from "./saveFile"; import { decodeBinary } from "../../utils/decodeBinary"; import { ref } from "valtio"; +/* compileCode sends the code of the active file to compile endpoint + * If all goes well you will get base64 encoded wasm file back with + * some extra logging information if we can provide it. This function + * also decodes the returned wasm and creates human readable WAT file + * out of it and store both in global state. + */ export const compileCode = async (activeId: number) => { + // Save the file to global state saveFile(false); if (!process.env.NEXT_PUBLIC_COMPILE_API_ENDPOINT) { throw Error("Missing env!"); } + // Bail out if we're already compiling if (state.compiling) { // if compiling is ongoing return return; } + // Set loading state to true state.compiling = true; try { const res = await fetch(process.env.NEXT_PUBLIC_COMPILE_API_ENDPOINT, { @@ -54,9 +63,11 @@ export const compileCode = async (activeId: number) => { link: Router.asPath.replace("develop", "deploy"), linkText: "Go to deploy", }); + // Decode base64 encoded wasm that is coming back from the endpoint const bufferData = await decodeBinary(json.output); state.files[state.active].compiledContent = ref(bufferData); - + // Import wabt from and create human readable version of wasm file and + // put it into state import("wabt").then((wabt) => { const ww = wabt.default(); const myModule = ww.readWasm(new Uint8Array(bufferData), { diff --git a/state/actions/createNewFile.ts b/state/actions/createNewFile.ts index 1884cf7..cf0b6a6 100644 --- a/state/actions/createNewFile.ts +++ b/state/actions/createNewFile.ts @@ -1,5 +1,6 @@ import state, { IFile } from '../index'; +/* Initializes empty file to global state */ export const createNewFile = (name: string) => { const emptyFile: IFile = { name, language: "c", content: "" }; state.files.push(emptyFile); diff --git a/state/actions/deployHook.ts b/state/actions/deployHook.ts index 308f461..0d2f9ca 100644 --- a/state/actions/deployHook.ts +++ b/state/actions/deployHook.ts @@ -25,6 +25,11 @@ function arrayBufferToHex(arrayBuffer?: ArrayBuffer | null) { return result; } + +/* deployHook function turns the wasm binary into + * hex string, signs the transaction and deploys it to + * Hooks testnet. + */ export const deployHook = async (account: IAccount & { name?: string }) => { if ( !state.files || diff --git a/state/actions/fetchFiles.ts b/state/actions/fetchFiles.ts index e5196ad..a97b5c4 100644 --- a/state/actions/fetchFiles.ts +++ b/state/actions/fetchFiles.ts @@ -4,7 +4,9 @@ import state from '../index'; const octokit = new Octokit(); -// Fetch content from Githug Gists +/* Fetches Gist files from Githug Gists based on + * gistId and stores the content in global state + */ export const fetchFiles = (gistId: string) => { state.loading = true; if (gistId && !state.files.length) { diff --git a/state/actions/importAccount.ts b/state/actions/importAccount.ts index 3833b0f..c4453ba 100644 --- a/state/actions/importAccount.ts +++ b/state/actions/importAccount.ts @@ -4,6 +4,7 @@ import { derive } from "xrpl-accountlib"; import state from '../index'; import { names } from './addFaucetAccount'; +// Adds test account to global state with secret key export const importAccount = (secret: string) => { if (!secret) { return toast.error("You need to add secret!"); diff --git a/state/actions/saveFile.ts b/state/actions/saveFile.ts index 1ee1099..6acecd7 100644 --- a/state/actions/saveFile.ts +++ b/state/actions/saveFile.ts @@ -1,6 +1,7 @@ import toast from "react-hot-toast"; import state from '../index'; +// Saves the current editor content to global state export const saveFile = (showToast: boolean = true) => { const editorModels = state.editorCtx?.getModels(); const currentModel = editorModels?.find((editorModel) => { diff --git a/state/actions/syncToGist.ts b/state/actions/syncToGist.ts index 0556a95..d70362e 100644 --- a/state/actions/syncToGist.ts +++ b/state/actions/syncToGist.ts @@ -7,6 +7,7 @@ import state from '../index'; const octokit = new Octokit(); +// Syncs the current files from the state to GitHub Gists. export const syncToGist = async ( session?: Session | null, createNewGist?: boolean @@ -30,6 +31,10 @@ export const syncToGist = async ( session?.user.username === state.gistOwner && !createNewGist ) { + // You can only remove files from Gist by updating file with empty contents + // So we need to fetch existing files and compare those to local state + // and then send empty content if we don't have matching files anymore + // on local state const currentFilesRes = await octokit.request("GET /gists/{gist_id}", { gist_id: state.gistId, }); diff --git a/state/actions/updateEditorSettings.ts b/state/actions/updateEditorSettings.ts index 9f1d1e8..4cb854a 100644 --- a/state/actions/updateEditorSettings.ts +++ b/state/actions/updateEditorSettings.ts @@ -1,5 +1,7 @@ import state, { IState } from '../index'; +// Updates editor settings and stores them +// in global state export const updateEditorSettings = ( editorSettings: IState["editorSettings"] ) => {