Skip to content

Commit

Permalink
Implement send message
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulyadav-57 committed Oct 24, 2023
1 parent dd31875 commit 8a56763
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 43 deletions.
2 changes: 1 addition & 1 deletion public/html/tonweb.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
let contractName = event.data?.contractName;
_code = `async function main() {
${event.data.code}
const contractInit = await TactCounter.fromInit(${event.data.initParams});
const contractInit = await ${contractName}.fromInit(${event.data.initParams});
window.contractInit = contractInit;
return contractInit;
} main()`;
Expand Down
3 changes: 1 addition & 2 deletions src/components/shared/Layout/Layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useUserOnboardingAction } from '@/hooks/userOnboarding.hooks';
import { FC, useEffect, useState } from 'react';
import UserOnboardingWizard from '../UserOnboardingWizard';
import s from './Layout.module.scss';

interface Props {
Expand All @@ -19,7 +18,7 @@ export const Layout: FC<Props> = ({ className = '', children }) => {
}
return (
<>
<UserOnboardingWizard />
{/* <UserOnboardingWizard /> */}
<main
className={`${s.root} ${
onboarding().tourActive ? 'onboarding-active' : ''
Expand Down
18 changes: 14 additions & 4 deletions src/components/workspace/ABIUi/ABIUi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ interface Props {
network: NetworkEnvironment;
contract: SandboxContract<UserContract> | null;
language?: ContractLanguage;
type: 'Getter' | 'Setter';
}
const ABIUi: FC<Props> = ({
abi,
contractAddress,
network,
contract = null,
language = 'func',
type,
}) => {
const possiblesTypes = abi.parameters.map((item) => {
if (['cell', 'slice'].includes(item.type)) {
Expand All @@ -36,17 +38,25 @@ const ABIUi: FC<Props> = ({
const [isLoading, setIsLoading] = useState(false);
const { createLog } = useLogActivity();

const { callGetter } = useContractAction();
const { callGetter, callSetter } = useContractAction();

const onSubmit = async (formValues: any) => {
let stack = Object.values(formValues).map((param: any) => {
const { type, value } = Object.values(param)[0] as any;
return { type: type, value: value };
const formField: any = Object.entries(param);
const { type, value } = formField[0][1];
if (language === 'tact') {
return { type, value, name: formField[0][0] };
} else {
const { type, value } = Object.values(param)[0] as any;
return { type: type, value: value };
}
});
try {
setIsLoading(true);

const getterReponse = await callGetter(
const callableFunction = type === 'Getter' ? callGetter : callSetter;

const getterReponse = await callableFunction(
contractAddress,
abi.name,
contract as any,
Expand Down
36 changes: 28 additions & 8 deletions src/components/workspace/BuildProject/BuildProject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ const BuildProject: FC<Props> = ({
await createStateInitCell(initParams);
} catch (error: any) {
setIsLoading('');
console.log(error, 'error');
if (typeof error === 'string') {
createLog(error, 'error');
return;
Expand Down Expand Up @@ -162,6 +163,10 @@ const BuildProject: FC<Props> = ({
let jsOutout = [{ code: '' }];

if (activeProject?.language == 'tact') {
const contractScript = activeProject?.contractScript?.toString();
if (!contractScript || typeof contractScript !== 'string') {
throw 'Build project built first';
}
jsOutout = await buildTs(
{
'tact.ts': activeProject?.contractScript?.toString(),
Expand Down Expand Up @@ -191,6 +196,20 @@ const BuildProject: FC<Props> = ({
.replace(/}\s+from\s.+/, '} = window.TonCore;')
.replace(/^\s*export\s+\{[^}]*\};\s*/m, '');

let contractName = activeProject?.contractName;

if (activeProject?.language == 'tact') {
const _code = `async function main() {
${finalJsoutput}
const contractInit = await ${contractName}.fromInit(${initParams});
return contractInit;
} main()`;
const contractInit = await eval(_code);
(window as any).contractInit = contractInit;
deploy();
return;
}

cellBuilderRef.current.contentWindow.postMessage(
{
name: 'nujan-ton-ide',
Expand All @@ -203,14 +222,14 @@ const BuildProject: FC<Props> = ({
'*'
);
} catch (error: any) {
// setIsLoading('');
if (error.message.includes("'default' is not exported by ")) {
setIsLoading('');
if (error?.message?.includes("'default' is not exported by ")) {
throw "'default' is not exported by stateInit.cell.ts";
}
createLog(
'Something went wrong. Check browser console for details.',
'error'
);
if (error?.message) {
createLog(error?.message, 'error');
return;
}
throw error;
}
};
Expand Down Expand Up @@ -348,8 +367,9 @@ const BuildProject: FC<Props> = ({
<Button
type="primary"
htmlType="submit"
loading={isLoading == 'deploy'}
disabled={!currentActiveFile || !activeProject?.contractBOC}
// loading={isLoading == 'deploy'}
disabled={!activeProject?.contractBOC}
className="w-100"
>
Deploy
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,14 @@ const ContractInteraction: FC<Props> = ({
network={network}
contract={contract}
language={language}
type="Getter"
/>
))}
</>
)}
<br />

<h3 className={s.label}>Send internal message:</h3>
<h3 className={s.label}>Send internal message: ({abi?.setters?.length || '-'})</h3>
{language !== 'tact' && (
<>
<p>
Expand All @@ -206,6 +207,21 @@ const ContractInteraction: FC<Props> = ({
</Form>
</>
)}
{abi && abi.setters.length > 0 && (
<>
{abi.setters.map((item, i) => (
<ABIUi
abi={item}
key={i}
contractAddress={contractAddress}
network={network}
contract={contract}
language={language}
type="Setter"
/>
))}
</>
)}
</div>
);
};
Expand Down
67 changes: 44 additions & 23 deletions src/hooks/contract.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function useContractAction() {
return {
deployContract,
sendMessage,
callSetter,
callGetter,
};
async function deployContract(
Expand All @@ -44,12 +45,10 @@ export function useContractAction() {
let codeCell = Cell.fromBoc(Buffer.from(codeBOC, 'base64'))[0];

// Amount to send to contract. Gas fee
const value = toNano('0.002');
const value = toNano('0.02');
let stateInit: StateInit = {};
const cellBuilderRef = document.querySelector('.cell-builder-ref');
if (project.language === 'tact') {
const _contractInit = (cellBuilderRef as any)?.contentWindow
?.contractInit;
const _contractInit = (window as any).contractInit;
stateInit = {
code: _contractInit.init.code,
data: _contractInit.init.data,
Expand All @@ -63,12 +62,8 @@ export function useContractAction() {

if (network.toUpperCase() === 'SANDBOX' && sandboxBlockchain) {
if (project.language === 'tact') {
const _contractInit = (cellBuilderRef as any)?.contentWindow
?.contractInit;

const _contractInit = (window as any).contractInit;
const _userContract = sandboxBlockchain.openContract(_contractInit);
(window as any).userContract = _userContract;

// TODO: Handle last parameter i.e. message
const sender = sandboxWallet!!.getSender();
const queryId = BigInt(0);
Expand All @@ -88,24 +83,15 @@ export function useContractAction() {
};
}
}
const response = await _userContract.send(
sender,
{ value: toNano(1) },
messageParams
);

const response1 = await _userContract.send(
const response = await _userContract.send(
sender,
{ value: toNano(1) },
{
$$type: 'Add',
queryId: BigInt(0),
amount: BigInt(5),
}
value,
},
messageParams
);

const data = await _userContract.getCounter();

return {
address: _userContract.address.toString(),
contract: _userContract,
Expand Down Expand Up @@ -204,6 +190,42 @@ export function useContractAction() {
}
}

async function callSetter(
contractAddress: string,
methodName: string,
contract: SandboxContract<UserContract> | null = null,
language: ContractLanguage,
stack?: TupleItem[],
network?: Network | Partial<NetworkEnvironment>
) {
if (network === 'SANDBOX' && contract) {
const { sandboxWallet } = globalWorkspace;

const sender = sandboxWallet!!.getSender();

let messageParams = {
$$type: methodName,
};
stack?.forEach((item: any) => {
messageParams = {
...messageParams,
[item.name]: BigInt(item.value),
};
});

if (language === 'tact') {
const response = await (contract as any).send(
sender,
{ value: toNano('0.02') },
messageParams
);
return { message: 'Message sent successfully' };
} else {
}
return;
}
}

async function callGetter(
contractAddress: string,
methodName: string,
Expand Down Expand Up @@ -235,7 +257,6 @@ export function useContractAction() {
};
}
});

if (network === 'SANDBOX' && contract) {
let responseValues = [];
if (language === 'tact') {
Expand Down
24 changes: 20 additions & 4 deletions src/hooks/project.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,35 @@ export function useProjectActions() {
return {
name: parameter.name,
type: parameter.type,
format: parameter.format,
optional: parameter.optional,
};
}),
};
});

const setters = (output.abi as any)?.receivers?.map((item: any) => {
let fields = [];
let setters: any = [];
(output.abi as any)?.receivers?.forEach((item: any) => {
if (item.message.type === 'Deploy') {
return;
}
if (item.message.type) {
fields = (output.abi as any).types.find(
const singleItem = (output.abi as any).types.find(
(type: any) => type.name === item.message.type
);
const singleField = {
name: singleItem.name,
parameters: singleItem.fields.map((parameter: any) => {
return {
name: parameter.name,
type: parameter.type.type,
format: parameter.type.format,
optional: parameter.type.optional,
};
}),
};
setters.push(singleField);
}
return fields;
});

let ctx = new CompilerContext({ shared: {} });
Expand Down

0 comments on commit 8a56763

Please sign in to comment.