From b4da5cff8b5ce96ce09034d19e3000de86d617d1 Mon Sep 17 00:00:00 2001 From: Rahul Yadav Date: Wed, 16 Oct 2024 08:54:26 +0530 Subject: [PATCH] feat: add support for external messages --- src/components/workspace/ABIUi/ABIUi.tsx | 2 +- src/components/workspace/ABIUi/TactABIUi.tsx | 16 +++++++----- .../WorkspaceSidebar/WorkspaceSidebar.tsx | 12 +++++++++ src/hooks/contract.hooks.ts | 25 ++++++++++++------- src/hooks/project.hooks.ts | 8 +++++- src/hooks/setting.hooks.ts | 7 ++++++ src/interfaces/setting.interface.ts | 1 + src/interfaces/workspace.interface.ts | 1 + src/state/IDE.context.tsx | 1 + src/utility/abi.ts | 2 ++ 10 files changed, 58 insertions(+), 17 deletions(-) diff --git a/src/components/workspace/ABIUi/ABIUi.tsx b/src/components/workspace/ABIUi/ABIUi.tsx index efa3a8a..17191b3 100644 --- a/src/components/workspace/ABIUi/ABIUi.tsx +++ b/src/components/workspace/ABIUi/ABIUi.tsx @@ -70,7 +70,7 @@ const ABIUi: FC = ({ abi.name, contract as SandboxContract, 'func', - abi.kind, + 'none', stack as TupleItem[], network, ); diff --git a/src/components/workspace/ABIUi/TactABIUi.tsx b/src/components/workspace/ABIUi/TactABIUi.tsx index 8c33838..b726369 100644 --- a/src/components/workspace/ABIUi/TactABIUi.tsx +++ b/src/components/workspace/ABIUi/TactABIUi.tsx @@ -372,7 +372,7 @@ const TactABIUi: FC = ({ fieldName, contract as SandboxContract, 'tact', - '', + abiType.receiverType, parsedInputsValues as TupleItem[], network, ); @@ -380,11 +380,15 @@ const TactABIUi: FC = ({ if (Array.isArray(response)) { createLog(JSON.stringify(response, null, 2)); } else if (response?.logs) { - for (const log of response.logs) { - createLog( - log, - response.status ? (response.status as LogType) : 'info', - ); + if (response.logs.length === 0 && response.message) { + createLog(response.message); + } else { + for (const log of response.logs) { + createLog( + log, + response.status ? (response.status as LogType) : 'info', + ); + } } } else { createLog(JSON.stringify(response, null, 2)); diff --git a/src/components/workspace/WorkspaceSidebar/WorkspaceSidebar.tsx b/src/components/workspace/WorkspaceSidebar/WorkspaceSidebar.tsx index af01b9d..a66d0a3 100644 --- a/src/components/workspace/WorkspaceSidebar/WorkspaceSidebar.tsx +++ b/src/components/workspace/WorkspaceSidebar/WorkspaceSidebar.tsx @@ -37,9 +37,11 @@ const WorkspaceSidebar: FC = ({ toggleAutoBuildAndDeploy, getSettingStateByKey, updateEditorMode, + toggleExternalMessage, } = useSettingAction(); const editorMode = getSettingStateByKey('editorMode'); + const isExternalMessage = getSettingStateByKey('isExternalMessage'); const menuItems: MenuItem[] = [ { @@ -81,6 +83,16 @@ const WorkspaceSidebar: FC = ({

+
+ + { + toggleExternalMessage(toggleState); + }} + /> + +
| null = null, language: ContractLanguage, - kind?: string, + receiverType?: 'none' | 'external' | 'internal', stack?: TupleItem[], network?: Network | Partial, ): Promise< @@ -276,14 +276,21 @@ export function useContractAction() { sender = new TonConnectSender(tonConnector.connector); } - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const response = await (contract as any).send( - sender, - { - value: tonAmountForInteraction, - }, - stack ? stack[0] : '', - ); + let response = null; + + if (receiverType === 'internal') { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + response = await (contract as any).send( + sender, + { + value: tonAmountForInteraction, + }, + stack ? stack[0] : '', + ); + } else { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + response = await (contract as any).sendExternal(stack ? stack[0] : ''); + } return { message: 'Message sent successfully', diff --git a/src/hooks/project.hooks.ts b/src/hooks/project.hooks.ts index 565a8c2..3dc20be 100644 --- a/src/hooks/project.hooks.ts +++ b/src/hooks/project.hooks.ts @@ -6,6 +6,7 @@ import { build as buildTact, createVirtualFileSystem, } from '@tact-lang/compiler'; +import { featureEnable } from '@tact-lang/compiler/dist/config/features'; import stdLibFiles from '@tact-lang/compiler/dist/imports/stdlib'; import { precompile } from '@tact-lang/compiler/dist/pipeline/precompile'; @@ -23,9 +24,10 @@ import { useProject } from './projectV2.hooks'; import { useSettingAction } from './setting.hooks'; export function useProjectActions() { - const { isContractDebugEnabled } = useSettingAction(); + const { isContractDebugEnabled, getSettingStateByKey } = useSettingAction(); const { writeFiles, projectFiles } = useProject(); const { getFile } = useFile(); + const isExternalMessage = getSettingStateByKey('isExternalMessage'); return { compileFuncProgram, @@ -137,6 +139,9 @@ export function useProjectActions() { let ctx = new CompilerContext(); const stdlib = createVirtualFileSystem('@stdlib', stdLibFiles); const entryFile = file.path; + if (isExternalMessage) { + ctx = featureEnable(ctx, 'external'); + } ctx = precompile(ctx, fs, stdlib, entryFile); const response = await buildTact({ @@ -146,6 +151,7 @@ export function useProjectActions() { name: 'tact', options: { debug: isContractDebugEnabled(), + external: !!isExternalMessage, }, }, project: fs, diff --git a/src/hooks/setting.hooks.ts b/src/hooks/setting.hooks.ts index 2c0efe6..18725b2 100644 --- a/src/hooks/setting.hooks.ts +++ b/src/hooks/setting.hooks.ts @@ -22,6 +22,7 @@ export function useSettingAction() { isAutoBuildAndDeployEnabled, toggleAutoBuildAndDeploy, updateEditorMode, + toggleExternalMessage, }; async function init() { @@ -103,4 +104,10 @@ export function useSettingAction() { editorMode: mode, }); } + + function toggleExternalMessage(active: boolean = !setting.isExternalMessage) { + updateStateByKey({ + isExternalMessage: active, + }); + } } diff --git a/src/interfaces/setting.interface.ts b/src/interfaces/setting.interface.ts index adec0d0..ed65c04 100644 --- a/src/interfaces/setting.interface.ts +++ b/src/interfaces/setting.interface.ts @@ -4,4 +4,5 @@ export interface SettingInterface { autoBuildAndDeploy?: boolean; tonAmountForInteraction?: string; editorMode: 'default' | 'vim'; + isExternalMessage?: boolean; } diff --git a/src/interfaces/workspace.interface.ts b/src/interfaces/workspace.interface.ts index d50ef48..cf2d244 100644 --- a/src/interfaces/workspace.interface.ts +++ b/src/interfaces/workspace.interface.ts @@ -128,6 +128,7 @@ export interface TactABIField { export interface TactType { name: string; + receiverType: 'internal' | 'external'; type?: ABITypeRef; params: TactABIField[]; returnType: Maybe; diff --git a/src/state/IDE.context.tsx b/src/state/IDE.context.tsx index c83f762..859accd 100644 --- a/src/state/IDE.context.tsx +++ b/src/state/IDE.context.tsx @@ -32,6 +32,7 @@ const defaultSetting = { tonAmountForInteraction: '0.05', autoBuildAndDeploy: true, editorMode: 'default' as const, + isExternalMessage: false, }; const defaultState = { diff --git a/src/utility/abi.ts b/src/utility/abi.ts index 6d43a1c..b0cbcca 100644 --- a/src/utility/abi.ts +++ b/src/utility/abi.ts @@ -119,6 +119,7 @@ export class ABIParser { if (receiver.message.kind !== 'typed') { return { name: argumentName, + receiverType: receiver.receiver, type: { kind: 'simple', type: receiver.message.kind, @@ -138,6 +139,7 @@ export class ABIParser { return { name: argumentName, + receiverType: receiver.receiver, params: [ { name: argumentName,