Skip to content

Commit

Permalink
Move sandbox creation to workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulyadav-57 committed Aug 19, 2023
1 parent 76817d0 commit 5a1fe39
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@
}
.info {
margin-top: 1rem;
font-size: 0.9rem;
}
}
63 changes: 42 additions & 21 deletions src/components/workspace/BuildProject/BuildProject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,22 @@ import { FC, useEffect, useRef, useState } from 'react';
import { Cell } from 'ton-core';
import ContractInteraction from '../ContractInteraction';
import ExecuteFile from '../ExecuteFile/ExecuteFile';
import OpenFile from '../OpenFile/OpenFile';
import s from './BuildProject.module.scss';
interface Props {
projectId: string;
onCodeCompile: (codeBOC: string) => void;
sandboxBlockchain: Blockchain;
contract: any;
updateContract: (contractInstance: any) => void;
}
const BuildProject: FC<Props> = ({ projectId, onCodeCompile }) => {
const BuildProject: FC<Props> = ({
projectId,
onCodeCompile,
sandboxBlockchain,
contract,
updateContract,
}) => {
const [isLoading, setIsLoading] = useState('');
const { createLog } = useLogActivity();
const [environment, setEnvironment] = useState<NetworkEnvironment>('SANDBOX');
Expand All @@ -34,10 +44,7 @@ const BuildProject: FC<Props> = ({ projectId, onCodeCompile }) => {
const cellBuilderRef = useRef<HTMLIFrameElement>(null);
const [tonConnector] = useTonConnectUI();
const chain = tonConnector.wallet?.account.chain;
const [sandboxBlockchain, setSandboxBlockchain] = useState<Blockchain | null>(
null
);
const [contract, setContract] = useState<any>('');

const [sandboxWallet, setSandboxWallet] =
useState<SandboxContract<TreasuryContract>>();

Expand Down Expand Up @@ -102,7 +109,7 @@ const BuildProject: FC<Props> = ({ projectId, onCodeCompile }) => {
return;
}
if (contract) {
setContract(contract);
updateContract(contract);
}

updateProjectById(
Expand Down Expand Up @@ -160,24 +167,29 @@ const BuildProject: FC<Props> = ({ projectId, onCodeCompile }) => {
}
};

const createSandbox = async () => {
if (sandboxBlockchain) {
return;
const isContractInteraction = () => {
let isValid =
activeProject?.id && tonConnector && activeProject?.contractAddress
? true
: false;

if (environment === 'SANDBOX') {
isValid = isValid && sandboxBlockchain ? true : false;
}
const blockchain = await Blockchain.create();
const wallet = await blockchain.treasury('user');
createLog(
`Sanbox account created. Address: <i>${wallet.address.toString()}</i>`,
'info',
false
);
setSandboxWallet(wallet);
setSandboxBlockchain(blockchain);
return isValid;
};

useEffect(() => {
if (environment === 'SANDBOX') {
createSandbox();
(async () => {
const wallet = await sandboxBlockchain.treasury('user');
createLog(
`Sandbox account created. Address: <i>${wallet.address.toString()}</i>`,
'info',
false
);
setSandboxWallet(wallet);
})();
}
}, []);

Expand Down Expand Up @@ -243,12 +255,21 @@ const BuildProject: FC<Props> = ({ projectId, onCodeCompile }) => {

{environment !== 'SANDBOX' && <TonAuth />}

<p className={s.info}>
1. Update initial contract state in{' '}
<OpenFile
projectId={projectId}
name="stateInit.cell.ts"
path="stateInit.cell.ts"
/>{' '}
</p>

<div className={s.actionWrapper}>
<ExecuteFile
file={currentActiveFile}
projectId={projectId as string}
label={environment === 'SANDBOX' ? 'Build and Deploy' : 'Build'}
description="Select a contract file to build and deploy"
description="2. Select a contract file to build and deploy"
allowedFile={['fc']}
onCompile={() => {
if (environment == 'SANDBOX') {
Expand Down Expand Up @@ -284,7 +305,7 @@ const BuildProject: FC<Props> = ({ projectId, onCodeCompile }) => {
</div>
)}

{activeProject?.id && tonConnector && activeProject?.contractAddress && (
{isContractInteraction() && (
<div className={s.contractInteraction}>
<ContractInteraction
contractAddress={activeProject?.contractAddress!!}
Expand Down
1 change: 1 addition & 0 deletions src/components/workspace/Tabs/Tabs.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
flex: 1;
flex-shrink: 1;
overflow: hidden;
overflow-x: auto;
flex-shrink: 0;
}

Expand Down
27 changes: 26 additions & 1 deletion src/components/workspace/WorkSpace/WorkSpace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useLogActivity } from '@/hooks/logActivity.hooks';
import { useWorkspaceActions } from '@/hooks/workspace.hooks';
import { Project } from '@/interfaces/workspace.interface';
import EventEmitter from '@/utility/eventEmitter';
import { Blockchain } from '@ton-community/sandbox';
import { Spin } from 'antd';
import { useRouter } from 'next/router';
import { FC, useEffect, useState } from 'react';
Expand All @@ -25,6 +26,10 @@ const WorkSpace: FC = () => {
const [activeMenu, setActiveMenu] = useState<WorkSpaceMenu>('code');
const [isLoaded, setIsLoaded] = useState(false);
const [isLoading, setIsloading] = useState(false);
const [sandboxBlockchain, setSandboxBlockchain] = useState<Blockchain | null>(
null
);
const [contract, setContract] = useState<any>('');

const { id: projectId, tab } = router.query;

Expand All @@ -35,10 +40,19 @@ const WorkSpace: FC = () => {
workspaceAction.createNewItem('', name, type, projectId as string);
};

const createSandbox = async () => {
if (sandboxBlockchain) {
return;
}
const blockchain = await Blockchain.create();
setSandboxBlockchain(blockchain);
};

useEffect(() => {
if (activeProject) {
createLog(`Project '${activeProject?.name}' is opened`);
}
createSandbox();
document.addEventListener('keydown', (e) => {
if ((e.ctrlKey || e.metaKey) && e.key === 's') {
e.preventDefault();
Expand All @@ -60,6 +74,12 @@ const WorkSpace: FC = () => {
console.log = originalConsoleLog;
document.removeEventListener('keydown', () => {});
// workspaceAction.closeAllFile();
workspaceAction.updateProjectById(
{
contractAddress: '',
},
projectId as string
);
clearLog();
};
}, []);
Expand Down Expand Up @@ -110,10 +130,15 @@ const WorkSpace: FC = () => {
<FileTree projectId={projectId as string} />
</div>
)}
{activeMenu === 'build' && (
{activeMenu === 'build' && sandboxBlockchain && (
<BuildProject
projectId={projectId as string}
onCodeCompile={(_codeBOC) => {}}
sandboxBlockchain={sandboxBlockchain}
contract={contract}
updateContract={(contractInstance) => {
setContract(contractInstance);
}}
/>
)}
{activeMenu === 'test-cases' && (
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/logActivity.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function useLogActivity() {
return;
}
const logEntry: LogEntry = {
text: text.replace(/\n/g, '<br />').trim(),
text: text.trim(),
type,
timestamp: new Date().toISOString(),
};
Expand Down

0 comments on commit 5a1fe39

Please sign in to comment.