Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle deployment selection inconsistencies #161

Open
wants to merge 2 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 78 additions & 47 deletions src/components/workspace/BuildProject/BuildProject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import { Analytics } from '@/utility/analytics';
import { buildTs } from '@/utility/typescriptHelper';
import {
delay,
getFileExtension,
htmlToAnsi,
isIncludesTypeCellOrSlice,
stripPrefix,
tonHttpEndpoint,
} from '@/utility/utils';
import { Network } from '@orbs-network/ton-access';
Expand All @@ -35,6 +35,9 @@ import { useFile } from '@/hooks';
import { useProject } from '@/hooks/projectV2.hooks';
import { useSettingAction } from '@/hooks/setting.hooks';
import { ABIParser, parseInputs } from '@/utility/abi';
import { extractContractName } from '@/utility/contract';
import { filterABIFiles } from '@/utility/file';
import { replaceFileExtension } from '@/utility/filePath';
import { Maybe } from '@ton/core/dist/utils/maybe';
import { TonClient } from '@ton/ton';
import { useForm } from 'antd/lib/form/Form';
Expand Down Expand Up @@ -91,34 +94,24 @@ const BuildProject: FC<Props> = ({ projectId, contract, updateContract }) => {
const connectedWalletAddress = useTonAddress();

const { sandboxBlockchain } = globalWorkspace;
const tactVersion = packageJson.dependencies['@tact-lang/compiler'].replace(
const tactVersion = stripPrefix(
packageJson.dependencies['@tact-lang/compiler'],
'^',
'',
);

const [deployForm] = useForm();

const { deployContract } = useContractAction();

const contractsToDeploy = () => {
return projectFiles
.filter((f) => {
const _fileExtension = getFileExtension(f.name || '');
return (
f.path.startsWith(`${activeProject?.path}/dist`) &&
['abi'].includes(_fileExtension as string)
);
})
.map((f) => {
return {
id: f.id,
name: f.name
.replace('.abi', '')
.replace('tact_', '')
.replace('func_', ''),
path: f.path,
};
});
if (!activeProject?.path || !activeProject.language) {
return [];
}
return filterABIFiles(
projectFiles,
activeProject.path,
activeProject.language,
);
};

const cellBuilder = (info: string) => {
Expand Down Expand Up @@ -276,8 +269,17 @@ const BuildProject: FC<Props> = ({ projectId, contract, updateContract }) => {

const deploy = async () => {
createLog(`Deploying contract ...`, 'info');
const contractBOCPath = selectedContract?.replace('.abi', '.code.boc');
const contractBOC = (await getFile(contractBOCPath!)) as string;
if (!selectedContract) {
createLog('Select a contract', 'error');
return;
}

const contractBOCPath = replaceFileExtension(
selectedContract,
'.abi',
'.code.boc',
);
const contractBOC = (await getFile(contractBOCPath)) as string;
if (!contractBOC) {
throw new Error('Contract BOC is missing. Rebuild the contract.');
}
Expand Down Expand Up @@ -346,25 +348,29 @@ const BuildProject: FC<Props> = ({ projectId, contract, updateContract }) => {
};

const createStateInitCell = async (initParams = '') => {
if (!selectedContract) {
if (!selectedContract || !activeProject?.path) {
throw new Error('Please select contract');
}
const contractScriptPath = selectedContract.replace('.abi', '.ts');
const contractScriptPath = replaceFileExtension(
selectedContract,
'.abi',
'.ts',
);
if (!cellBuilderRef.current?.contentWindow) return;
let contractScript = '';
try {
contractScript = (await getFile(contractScriptPath)) as string;
} catch (error) {
/* empty */
}
if (activeProject?.language === 'tact' && !contractScript) {
if (activeProject.language === 'tact' && !contractScript) {
throw new Error('Contract script is missing. Rebuild the contract.');
}

try {
let jsOutout = [];

if (activeProject?.language == 'tact') {
if (activeProject.language == 'tact') {
jsOutout = await buildTs(
{
'tact.ts': contractScript,
Expand All @@ -376,7 +382,7 @@ const BuildProject: FC<Props> = ({ projectId, contract, updateContract }) => {
let cellCode = '';
try {
stateInitContent = (await getFile(
`${activeProject?.path}/stateInit.cell.ts`,
`${activeProject.path}/stateInit.cell.ts`,
)) as string;
} catch (error) {
console.log('stateInit.cell.ts is missing');
Expand Down Expand Up @@ -406,9 +412,12 @@ const BuildProject: FC<Props> = ({ projectId, contract, updateContract }) => {

const finalJsoutput = fromJSModule((jsOutout as OutputChunk[])[0].code);

const contractName = extractContractName(selectedContract);
const contractName = extractContractName(
selectedContract,
activeProject.path,
);

if (activeProject?.language == 'tact') {
if (activeProject.language == 'tact') {
const _code = `async function main(initParams) {
${finalJsoutput}
const contractInit = await ${contractName}.fromInit(...Object.values(initParams));
Expand All @@ -429,8 +438,8 @@ const BuildProject: FC<Props> = ({ projectId, contract, updateContract }) => {
name: 'ton-web-ide',
type: 'state-init-data',
code: finalJsoutput,
language: activeProject?.language,
contractName: activeProject?.contractName,
language: activeProject.language,
contractName: activeProject.contractName,
initParams,
},
'*',
Expand Down Expand Up @@ -519,22 +528,13 @@ const BuildProject: FC<Props> = ({ projectId, contract, updateContract }) => {
setEnvironment(network);
};

const updateSelectedContract = (contract: string) => {
const updateSelectedContract = (contract: string | undefined) => {
setSelectedContract(contract);
updateProjectSetting({
selectedContract: contract,
} as ProjectSetting);
};

const extractContractName = (currentContractName: string) => {
return currentContractName
.replace(activeProject?.path + '/', '')
.replace('dist/', '')
.replace('.abi', '')
.replace('tact_', '')
.replace('func_', '');
};

const fromJSModule = (jsModuleCode: string) => {
return jsModuleCode
.replace(/^import\s+{/, 'const {')
Expand All @@ -547,7 +547,11 @@ const BuildProject: FC<Props> = ({ projectId, contract, updateContract }) => {
language: 'tact' | 'func',
supressErrors = false,
) => {
const contractScriptPath = currentContractName.replace('.abi', '.ts');
const contractScriptPath = replaceFileExtension(
currentContractName,
'.abi',
'.ts',
);
const contractScript = (await getFile(contractScriptPath)) as string;
if (language === 'tact' && !contractScript) {
if (supressErrors) {
Expand Down Expand Up @@ -589,7 +593,10 @@ const BuildProject: FC<Props> = ({ projectId, contract, updateContract }) => {
);
if (!output) return;

const contractName = extractContractName(selectedContract);
const contractName = extractContractName(
selectedContract,
activeProject.path!,
);

const _code = `async function main() {
${output.finalJSoutput}
Expand Down Expand Up @@ -638,9 +645,33 @@ const BuildProject: FC<Props> = ({ projectId, contract, updateContract }) => {
if (activeProject?.network) {
setEnvironment(activeProject.network);
}
if (activeProject?.selectedContract) {
setSelectedContract(activeProject.selectedContract);
deployForm.setFieldsValue({ contract: activeProject.selectedContract });
const selectedABIPath = activeProject?.selectedContract;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Abbreviations in camel case are handled as regular words.

Here's a review of the reasoning behind it.

The most important part there is that automated tools can only work if rules do not depend on semantics. For example, if there would be a file that exports selectedABIPath, its name by Node.js file naming standards would be selected-a-b-i-path, and linter would enforce that.


if (selectedABIPath) {
const correspondingScriptPath = replaceFileExtension(
selectedABIPath,
'.abi',
'.ts',
);

let _selectedContract: string | undefined = selectedABIPath;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is used. _ convention is for variables that are unused, or for private class fields.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable doesn't have to be mutable. By abstracting the logic into getSelectedContract() it could be just a return.


if (activeProject.language === 'tact') {
const scriptFile = projectFiles.find(
(file) => file.path === correspondingScriptPath,
);
const hasValidScriptFile =
scriptFile &&
projectFiles.find((file) => file.path === selectedABIPath);

_selectedContract = hasValidScriptFile ? selectedABIPath : undefined;
}

deployForm.setFieldsValue({
contract: _selectedContract,
});

updateSelectedContract(_selectedContract);
}
const handler = (
event: MessageEvent<{
Expand Down
24 changes: 24 additions & 0 deletions src/utility/contract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { relativePath } from './filePath';
import { stripPrefix, stripSuffix } from './utils';

/**
* Extracts the contract name from a file path like:
* /projects/projectName/dist/func_contractName.abi
*/
export function extractContractName(
contractFilePath: string,
projectPath: string,
): string {
let filePath = relativePath(contractFilePath, projectPath);

filePath = stripPrefix(filePath, 'dist/');

// Remove either 'tact_' or 'func_' from start, if present
filePath = stripPrefix(filePath, 'tact_');
filePath = stripPrefix(filePath, 'func_');

// Remove extension
filePath = stripSuffix(filePath, '.abi');

return filePath;
}
42 changes: 42 additions & 0 deletions src/utility/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ContractLanguage, Tree } from '@/interfaces/workspace.interface';
import { replaceFileExtension } from './filePath';
import { getFileExtension, stripPrefix, stripSuffix } from './utils';

export function filterABIFiles(
files: Tree[],
basePath: string,
lang: ContractLanguage,
) {
return files
.filter((file) => {
const fileExtension = getFileExtension(file.name);
const isAbiFile =
file.path.startsWith(`${basePath}/dist`) && fileExtension === 'abi';

if (lang === 'func') {
return isAbiFile;
}

// For tact we have to check if both ABI and It's wrapper TS file is present.
const hasTsFile = files.some(
(f) => f.path === replaceFileExtension(file.path, '.abi', '.ts'),
);
return isAbiFile && hasTsFile;
})
.map((file) => ({
id: file.id,
name: cleanAbiFileName(file.name),
path: file.path,
}));
}

/**
* A convenience function to remove .abi if at the end,
* and also remove 'tact_' or 'func_' prefixes if at the start.
*/
export function cleanAbiFileName(rawName: string): string {
let name = stripSuffix(rawName, '.abi');
name = stripPrefix(name, 'tact_');
name = stripPrefix(name, 'func_');
return name;
}
24 changes: 24 additions & 0 deletions src/utility/filePath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { stripPrefix } from './utils';

export function relativePath(fullPath: string, basePath: string): string {
let path = stripPrefix(fullPath, basePath);

// If there's a leading slash (after removing basePath), remove it:
if (path.startsWith('/')) {
path = path.slice(1);
}

return path;
}

export function replaceFileExtension(
filePath: string,
oldExt: string,
newExt: string,
): string {
if (filePath.endsWith(oldExt)) {
return filePath.slice(0, -oldExt.length) + newExt;
}
// If the file doesn’t end with `oldExt`, return unchanged, or handle otherwise
return filePath;
}
16 changes: 16 additions & 0 deletions src/utility/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,19 @@ export function isIncludesTypeCellOrSlice(obj: Record<string, any>): boolean {
}
return false;
}

/**
* Removes a specified suffix from the input string if it's present.
* Otherwise, returns the input string unchanged.
*/
export function stripSuffix(input: string, suffix: string): string {
return input.endsWith(suffix) ? input.slice(0, -suffix.length) : input;
}

/**
* Removes a specified prefix from the input string if it's present.
* Otherwise, returns the input string unchanged.
*/
export function stripPrefix(input: string, prefix: string): string {
return input.startsWith(prefix) ? input.slice(prefix.length) : input;
}