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

feat: add support for external messages #125

Merged
merged 1 commit into from
Oct 16, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/components/workspace/ABIUi/ABIUi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const ABIUi: FC<ABIUiProps> = ({
abi.name,
contract as SandboxContract<UserContract>,
'func',
abi.kind,
'none',
stack as TupleItem[],
network,
);
Expand Down
16 changes: 10 additions & 6 deletions src/components/workspace/ABIUi/TactABIUi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -372,19 +372,23 @@ const TactABIUi: FC<TactABI> = ({
fieldName,
contract as SandboxContract<UserContract>,
'tact',
'',
abiType.receiverType,
parsedInputsValues as TupleItem[],
network,
);

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));
Expand Down
12 changes: 12 additions & 0 deletions src/components/workspace/WorkspaceSidebar/WorkspaceSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ const WorkspaceSidebar: FC<Props> = ({
toggleAutoBuildAndDeploy,
getSettingStateByKey,
updateEditorMode,
toggleExternalMessage,
} = useSettingAction();

const editorMode = getSettingStateByKey('editorMode');
const isExternalMessage = getSettingStateByKey('isExternalMessage');

const menuItems: MenuItem[] = [
{
Expand Down Expand Up @@ -81,6 +83,16 @@ const WorkspaceSidebar: FC<Props> = ({
</small>
</p>
</div>
<div className={s.settingItem}>
<Form.Item label="External Message" valuePropName="checked">
<Switch
checked={!!isExternalMessage}
onChange={(toggleState) => {
toggleExternalMessage(toggleState);
}}
/>
</Form.Item>
</div>
<div className={s.settingItem}>
<Form.Item label="Format code on save" valuePropName="checked">
<Switch
Expand Down
25 changes: 16 additions & 9 deletions src/hooks/contract.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ export function useContractAction() {
methodName: string,
contract: SandboxContract<UserContract> | null = null,
language: ContractLanguage,
kind?: string,
receiverType?: 'none' | 'external' | 'internal',
stack?: TupleItem[],
network?: Network | Partial<NetworkEnvironment>,
): Promise<
Expand All @@ -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',
Expand Down
8 changes: 7 additions & 1 deletion src/hooks/project.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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,
Expand Down Expand Up @@ -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({
Expand All @@ -146,6 +151,7 @@ export function useProjectActions() {
name: 'tact',
options: {
debug: isContractDebugEnabled(),
external: !!isExternalMessage,
},
},
project: fs,
Expand Down
7 changes: 7 additions & 0 deletions src/hooks/setting.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function useSettingAction() {
isAutoBuildAndDeployEnabled,
toggleAutoBuildAndDeploy,
updateEditorMode,
toggleExternalMessage,
};

async function init() {
Expand Down Expand Up @@ -103,4 +104,10 @@ export function useSettingAction() {
editorMode: mode,
});
}

function toggleExternalMessage(active: boolean = !setting.isExternalMessage) {
updateStateByKey({
isExternalMessage: active,
});
}
}
1 change: 1 addition & 0 deletions src/interfaces/setting.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export interface SettingInterface {
autoBuildAndDeploy?: boolean;
tonAmountForInteraction?: string;
editorMode: 'default' | 'vim';
isExternalMessage?: boolean;
}
1 change: 1 addition & 0 deletions src/interfaces/workspace.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export interface TactABIField {

export interface TactType {
name: string;
receiverType: 'internal' | 'external';
type?: ABITypeRef;
params: TactABIField[];
returnType: Maybe<ABITypeRef>;
Expand Down
1 change: 1 addition & 0 deletions src/state/IDE.context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const defaultSetting = {
tonAmountForInteraction: '0.05',
autoBuildAndDeploy: true,
editorMode: 'default' as const,
isExternalMessage: false,
};

const defaultState = {
Expand Down
2 changes: 2 additions & 0 deletions src/utility/abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -138,6 +139,7 @@ export class ABIParser {

return {
name: argumentName,
receiverType: receiver.receiver,
params: [
{
name: argumentName,
Expand Down
Loading