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

Add safeguard for transaction type #29

Merged
merged 1 commit into from
Feb 26, 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/backend/services/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Keep your response short, using 2 or 3 sentences maximum.

Respond in a valid JSON without comments to be consumed by an application following this pattern:
{"response", "your response goes here", "transaction", "user transaction object goes here"}.
Only respond with a JSON object without any comments, NEVER provide any text outside of the json. Your respond only in a valid JSON.
Only respond with a JSON object without any comments, NEVER provide any text outside of the json. You respond only in a valid JSON.
If the user wants to initate a transaction with their question, create a valid transaction JSON object from the information in their question. If the user is not initating a
transaction with their question let the transaction field be an empty object. Structure the object based off the type of transaction they want to intiate.

Expand Down
65 changes: 37 additions & 28 deletions src/frontend/views/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,35 +83,44 @@ const ChatView = (): JSX.Element => {
return;
}

if (transaction.type.toLowerCase() === 'balance') {
let message: string;
try {
message = await handleBalanceRequest(provider, account);
} catch (error) {
message = `Error: Failed to retrieve a valid balance from Metamask, try reconnecting.`;
}
updateDialogueEntries(question, message);
} else {
try {
const builtTx = await handleTransactionRequest(provider, transaction, account, question);
console.log('from: ' + builtTx.params[0].from);
//if gas is more than 5% of balance - check with user
const balance = await handleBalanceRequest(provider, account);
const isGasCostMoreThan5Percent = checkGasCost(balance, builtTx.params[0]);
if (isGasCostMoreThan5Percent) {
updateDialogueEntries(
question,
`Important: The gas cost is expensive relative to your balance please proceed with caution\n\n${response}`,
);
} else {
updateDialogueEntries(question, response);
switch (transaction.type.toLowerCase()) {
case 'balance':
let message: string;
try {
message = await handleBalanceRequest(provider, account);
} catch (error) {
message = `Error: Failed to retrieve a valid balance from Metamask, try reconnecting.`;
}
await provider?.request(builtTx);
} catch (error) {
const badTransactionMessage =
'Error: There was an error sending your transaction, if the transaction type is balance or transfer please reconnect to metamask';
updateDialogueEntries(question, badTransactionMessage);
}
updateDialogueEntries(question, message);
break;

case 'transfer':
try {
const builtTx = await handleTransactionRequest(provider, transaction, account, question);
console.log('from: ' + builtTx.params[0].from);
//if gas is more than 5% of balance - check with user
const balance = await handleBalanceRequest(provider, account);
const isGasCostMoreThan5Percent = checkGasCost(balance, builtTx.params[0]);
if (isGasCostMoreThan5Percent) {
updateDialogueEntries(
question,
`Important: The gas cost is expensive relative to your balance please proceed with caution\n\n${response}`,
);
} else {
updateDialogueEntries(question, response);
}
await provider?.request(builtTx);
} catch (error) {
const badTransactionMessage =
'Error: There was an error sending your transaction, if the transaction type is balance or transfer please reconnect to metamask';
updateDialogueEntries(question, badTransactionMessage);
}
break;

default:
// If the transaction type is not recognized, we will not proceed with the transaction.
const errorMessage = `Error: Invalid transaction type: ${transaction.type}`;
updateDialogueEntries(question, errorMessage);
}
};

Expand Down
Loading