Skip to content

Commit

Permalink
feat: swap native to cw20
Browse files Browse the repository at this point in the history
  • Loading branch information
tung-lee committed Jan 24, 2024
1 parent 0488b8c commit bb91df4
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 73 deletions.
24 changes: 12 additions & 12 deletions packages/extension/src/pages/chatbot/dropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import React from 'react';
import { OptionEnum } from './enum';
import style from './style.module.scss';

export const Dropdown = ({ chosenOption, dispatch }) => {
return (
<div>
<select
defaultValue={OptionEnum.ORAIDEX}
value={chosenOption}
onChange={(evt) =>
dispatch({ type: 'choose_option', payload: evt.target.value })
}
>
<option value={OptionEnum.ORAIDEX}>Oraidex</option>
<option value={OptionEnum.SWAP}>Swap</option>
</select>
</div>
<select
className={style.inputBox}
defaultValue={OptionEnum.ORAIDEX}
value={chosenOption}
onChange={(evt) =>
dispatch({ type: 'choose_option', payload: evt.target.value })
}
>
<option value={OptionEnum.ORAIDEX}>Oraidex</option>
<option value={OptionEnum.SWAP}>Swap</option>
</select>
);
};
138 changes: 77 additions & 61 deletions packages/extension/src/pages/chatbot/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const initialState = {
prompt: '',
status: StatusEnum.READY,
chosenOption: OptionEnum.ORAIDEX,
pairContractAddr:
'orai1agqfdtyd9lr0ntmfjtzl4f6gyswpeq4z4mdnq4npdxdc99tcw35qesmr9v',
};

function reducer(state, action) {
Expand Down Expand Up @@ -50,6 +52,16 @@ function reducer(state, action) {
prompt: '',
status: StatusEnum.READY,
};
case 'on_change_pair_contract_addr':
return {
...state,
pairContractAddr: payload,
};
case 'reload_messages':
return {
...state,
messages: JSON.parse(localStorage.getItem('messages')),
};
}
throw Error('Unknown action: ' + action.type);
}
Expand All @@ -61,32 +73,20 @@ export const ChatbotPage: FunctionComponent = observer(() => {
const accountOrai = accountStore.getAccount(ChainIdEnum.Oraichain);
const client = useClientTestnet(accountOrai);

// const [prompt, setPrompt] = useState('');
// const [messages, setMessages] = useState([]);

const messagesEndRef = useRef(null);

const [{ messages, prompt, status, choose_option }, dispatch] = useReducer(
reducer,
initialState
);
const [
{ messages, prompt, status, chosenOption, pairContractAddr },
dispatch,
] = useReducer(reducer, initialState);

console.log(userAddr);

// const testChatUI = useCallback(() => {
// setMessages([
// ...messages,
// {
// isUser: true,
// msg: prompt,
// },
// {
// isUser: false,
// msg: 'Answer',
// },
// ]);
// setPrompt('');
// }, null);
useEffect(() => {
dispatch({
type: 'reload_messages',
});
}, []);

const testChatUI = () => {
dispatch({
Expand All @@ -104,19 +104,6 @@ export const ChatbotPage: FunctionComponent = observer(() => {
msg: 'Answer',
},
});

// setMessages([
// ...messages,
// {
// isUser: true,
// msg: prompt,
// },
// {
// isUser: false,
// msg: 'Answer',
// },
// ]);
// setPrompt('');
};

const scrollToBottom = () => {
Expand All @@ -127,19 +114,23 @@ export const ChatbotPage: FunctionComponent = observer(() => {
scrollToBottom();
}, [messages]);

const callBot = async (userAddr, dispatch, prompt) => {
// setMessages([
// ...messages,
// {
// isUser: true,
// msg: prompt,
// },
// ]);
// setPrompt('');

const callBot = async (
userAddr,
dispatch,
prompt,
pairContractAddr,
chosenOption
) => {
let endPoint = '';
if (choose_option === OptionEnum.SWAP) {
if (chosenOption === OptionEnum.SWAP) {
endPoint = `${BACKEND_URL}/swapNative`;
dispatch({
type: 'chat',
payload: {
isUser: true,
msg: prompt,
},
});
try {
const resp = await fetch(endPoint, {
method: 'POST',
Expand All @@ -148,18 +139,20 @@ export const ChatbotPage: FunctionComponent = observer(() => {
},
body: JSON.stringify({
user_address: userAddr,
user_input: 'Swap',
user_input: prompt,
pair_contract: pairContractAddr,
}),
});
const data = await resp.json();
console.log(data);
const { answer, msg } = data;
const msgObject = JSON.parse(msg);
const {
Action: action,
Comment: botCmt,
Pair_contract: contractAddr,
inputamout: inputAmount,
Parameters: params,
} = data;
} = msgObject;
const executeMsg = params.msg;
const amount = inputAmount
? [{ amount: inputAmount, denom: 'orai' }]
Expand All @@ -171,11 +164,12 @@ export const ChatbotPage: FunctionComponent = observer(() => {
executeMsg,
amount
);
const { transactionHash } = result;
dispatch({
type: 'chat',
payload: {
isUser: false,
msg: botCmt,
msg: `${answer}. Go to here https://testnet.scan.orai.io/txs/${transactionHash} to see transaction`,
},
});
console.log(result);
Expand Down Expand Up @@ -354,26 +348,48 @@ export const ChatbotPage: FunctionComponent = observer(() => {
type="text"
placeholder="Ask anything..."
/>

<i
onClick={() => {
dispatch({
type: 'reset',
});
}}
className={`fas fa-trash-alt ${style.iconTrash}`}
/>
<img
onClick={() => callBot(userAddr, dispatch, prompt)}
onClick={() =>
callBot(
userAddr,
dispatch,
prompt,
pairContractAddr,
chosenOption
)
}
// onClick={() => testChatUI()}
style={{ cursor: 'pointer' }}
className="arrow-up-square"
alt="Arrow up square"
src={require('../../public/assets/img/arrow-up-square.svg')}
/>
</div>
<Dropdown chosenOption={choose_option} dispatch={dispatch} />
<button
onClick={() => {
dispatch({
type: 'reset',
});
}}
>
Delete
</button>
<div className={style.inputWrapperContent}>
<Dropdown chosenOption={chosenOption} dispatch={dispatch} />
</div>
<div className={style.inputWrapperContent}>
<input
className={style.inputBox}
placeholder="Pair contract address"
value={pairContractAddr}
onChange={(e) =>
dispatch({
payload: e.target.value,
type: 'on_change_pair_contract_addr',
})
}
type="text"
/>
</div>
</div>
</div>
</div>
Expand Down
9 changes: 9 additions & 0 deletions packages/extension/src/pages/chatbot/style.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
padding: 16px;

p {
word-break: break-all;
font-size: 12px;
color: var(--white);
font-weight: bold;
Expand All @@ -129,10 +130,18 @@
border-radius: 24px 24px 24px 0px;
padding: 16px;
p {
word-break: break-all;
font-size: 12px;
margin: 0;
color: #64578c;
font-weight: bold;
line-height: var(--text-line-height);
}
}

.iconTrash {
color: red;
font-size: 30px;
padding: 0 10px;
cursor: pointer;
}

0 comments on commit bb91df4

Please sign in to comment.