Skip to content

Commit

Permalink
Merge pull request #94 from terra-money/fix/hackathon
Browse files Browse the repository at this point in the history
template fixes
  • Loading branch information
simke9445 authored Oct 3, 2023
2 parents 927fff1 + 23d2c6e commit af7c158
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 16 deletions.
1 change: 1 addition & 0 deletions apps/shared/hooks/useLocalWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ export const useLocalWallet = (): LocalWallet => {
walletAddress: connectedWallet ? connectedWallet.addresses[selectedChainId] : '',
lcd,
};
// }, [connectedWallet, lcdClientConfig, selectedChainId]);
}, [connectedWallet, wallet, lcdClientConfig, lcd, selectedChain, selectedChainId]);
};
2 changes: 1 addition & 1 deletion apps/warp-protocol/src/pages/job-new/JobNew.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export const parseMsgs = (value: string): warp_resolver.CosmosMsgFor_Empty[] =>
return msgs;
};

const encodeMsg = (input: warp_resolver.CosmosMsgFor_Empty): warp_resolver.CosmosMsgFor_Empty => {
export const encodeMsg = (input: warp_resolver.CosmosMsgFor_Empty): warp_resolver.CosmosMsgFor_Empty => {
if (!('wasm' in input)) {
return input;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,32 @@ import { TemplatesInput } from '../templates-input/TemplatesInput';
import { TemplateVarInput } from '../template-var-input/TemplateVarInput';
import { findVariablePath, templateVariables } from 'utils/variable';
import { EditorInput } from 'forms/QueryExprForm/EditorInput';
import { decodeMsgs, encodeMsg, parseMsgs } from 'pages/job-new/JobNew';

const composeMsgFromTemplate = (template: warp_templates.Template): string => {
const vars = templateVariables(template);
let json = JSON.parse(template.msg);

vars.forEach((v) => {
try {
const path = findVariablePath(json, v.name);
let msgs = decodeMsgs(parseMsgs(template.msg));

if (path) {
jsonpath.value(json, path, v.value);
msgs = msgs.map((msg) => {
let json = JSON.stringify(msg);

vars.forEach((v) => {
try {
const path = findVariablePath(json, v.name);

if (path) {
jsonpath.value(json, path, v.value);
}
} catch (err) {
// consume the error
}
} catch (err) {
// consume the error
}
});

return encodeMsg(parseMsgs(json)[0]);
});

return JSON.stringify(json, null, 2);
return JSON.stringify(msgs, null, 2);
};

type TemplateFormProps = {
Expand Down
2 changes: 1 addition & 1 deletion apps/warp-protocol/src/pages/template-new/TemplateNew.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const TemplateNew = (props: TemplateNewProps) => {
const msgs = parseMsgs(msg);
const res = await createTemplateTx({
formatted_str: formattedStr,
msg,
msgs,
condition,
vars: extractUsedVariables(formattedStr, msgs, vars, condition),
name,
Expand Down
14 changes: 10 additions & 4 deletions apps/warp-protocol/src/tx/useCreateTemplateTx.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
import { useTx } from '@terra-money/apps/libs/transactions';
import { useWarpSdk } from '@terra-money/apps/hooks';
import { TX_KEY } from './txKey';
import { warp_templates } from '@terra-money/warp-sdk';
import { warp_resolver } from '@terra-money/warp-sdk';

export type CreateTemplateTxProps = warp_templates.SubmitTemplateMsg;
export type CreateTemplateTxProps = {
condition?: warp_resolver.Condition | null;
formatted_str: string;
msgs: warp_resolver.CosmosMsgFor_Empty[];
name: string;
vars: warp_resolver.Variable[];
};

export const useCreateTemplateTx = () => {
const sdk = useWarpSdk();

return useTx<CreateTemplateTxProps>(
async (options) => {
const { wallet, formatted_str, vars, name, msg, condition } = options;
const { wallet, formatted_str, vars, name, msgs, condition } = options;

return sdk.tx.submitTemplate(wallet.walletAddress, {
formatted_str,
condition,
vars,
name,
msg,
msg: JSON.stringify(msgs),
});
},
{
Expand Down

0 comments on commit af7c158

Please sign in to comment.