Skip to content

Commit

Permalink
createAccount faucet button working (sendcoin button error)
Browse files Browse the repository at this point in the history
  • Loading branch information
raunak-dev-edu committed Dec 9, 2023
1 parent d597a22 commit ec4694c
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 87 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@
"dependencies": {
"@emotion/react": "^11.11.1",
"@emotion/styled": "^11.11.0",
"@mui/icons-material": "^5.14.19",
"@mui/material": "^5.14.20",
"aes256": "^1.1.0",
"clipboard-copy": "^4.0.1",
"react-modal": "^3.16.1"
}
}
77 changes: 17 additions & 60 deletions packages/site/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@ import DialogContent from '@mui/material/DialogContent';
import DialogActions from '@mui/material/DialogActions';
import Divider from '@mui/material/Divider';

// import Modal from '@mui/material/Modal';
// import Modal from 'react-modal';

import {
ConnectButton,
InstallFlaskButton,
ReconnectButton,
SendHelloButton,
Card,
} from '../components';
import { defaultSnapOrigin } from '../config';
import { MetamaskActions, MetaMaskContext } from '../hooks';
import {
Expand Down Expand Up @@ -150,7 +140,7 @@ border: 1px solid rgba(25, 118, 210, 0.5);
margin-bottom: 20px;
color: black;
font-size: 1.2rem;
width: 200px;
width: auto;
height: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
background-color: rgba(25, 118, 210, 0.1);
Expand All @@ -165,10 +155,7 @@ const AccountModalContent = styled(DialogContent)`
`;
const Index = () => {
const [state, dispatch] = useContext(MetaMaskContext);
const [transferAmount, setTransferAmount] = useState(0);
const [reciever, setReciever] = useState('');
const [password, setPassword] = useState<string>('');
const [isPasswordSet, setIsPasswordSet] = useState<boolean>(false);
const [password, setPassword] = useState('');
const [isModalOpen, setIsModalOpen] = useState(false);

const [isAccountCreated, setIsAccountCreated] = useState(false);
Expand All @@ -179,7 +166,7 @@ const Index = () => {
? state.isFlask
: state.snapsDetected;
const [recipientAddress, setRecipientAddress] = useState('');
const [sendAmount, setSendAmount] = useState('');
const [sendAmount, setSendAmount] = useState(0);
const [isSendModalOpen, setIsSendModalOpen] = useState(false);
const [isNextButtonDisabled, setIsNextButtonDisabled] = useState(true);

Expand Down Expand Up @@ -208,7 +195,7 @@ const Index = () => {

const handleAmountChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const enteredAmount = event.target.value;
setSendAmount(enteredAmount);
setSendAmount(parseFloat(enteredAmount));
setIsNextButtonDisabled(Number(enteredAmount) > balance);
};

Expand Down Expand Up @@ -254,28 +241,22 @@ const Index = () => {
openCreateAccountModal();
};
const handleCreateAccount = () => {
handleSendGetAccount()
setIsCreatingAccount(false);
setIsAccountCreated(true);
setInputPassword('');
};

const handleSendHelloClick = async () => {
try {
await sendHello();
} catch (error) {
console.error(error);
dispatch({ type: MetamaskActions.SetError, payload: error });
}
};
const handleSend = () => {
closeSendModal();
closeConfirmDialog();
setIsSendModalOpen(true);
};
const handleSendGetAccount = async () => {
try {
const accountinfo = await sendGetAccount();
const { address, bal } = accountinfo;
const accountinfo: any = await sendGetAccount();
const { accountInfo } = accountinfo;
const {address, bal} = accountInfo;
setAddress(address);
setBalance(bal);
setIsAccountCreated(true);
Expand All @@ -287,7 +268,7 @@ const Index = () => {

const handleCoinTransfer = async () => {
try {
await sendCoin(reciever, transferAmount);
await sendCoin(recipientAddress, sendAmount);
} catch (error) {
console.error(error);
dispatch({ type: MetamaskActions.SetError, payload: error });
Expand All @@ -297,30 +278,14 @@ const Index = () => {
const handleFundMeWithFaucet = async () => {
try {
await sendFundMe();
const updatedBalance = await sendGetAccount();
setBalance(updatedBalance.bal);
const updatedBalance = await sendGetBalance();
setBalance(updatedBalance.balance);
} catch (error) {
console.error(error);
dispatch({ type: MetamaskActions.SetError, payload: error });
}
};

const handleCreateAccountClick = () => {
if (isPasswordSet) {
handleSendGetAccount();
const accountAddress = console.log('password is set');
} else {
const newPassword = prompt('Please enter your password:');
console.log('password is not set');
if (newPassword) {
setIsPasswordSet(true);
setPassword(newPassword);
handleSendGetAccount();
console.log('password is set');
}
}
};

const handleGetAllTransactions = async () => {
try {
await sendTxnHistory();
Expand All @@ -330,15 +295,6 @@ const Index = () => {
}
};

const getBalance = async () => {
try {
const bal = await sendGetBalance();
} catch (error) {
console.error(error);
dispatch({ type: MetamaskActions.SetError, payload: error });
}
};

return (
<Container>
<Dialog open={isSendModalOpen} onClose={closeSendModal}>
Expand All @@ -362,7 +318,7 @@ const Index = () => {
</DialogContent>
<DialogActions>
<Button onClick={closeSendModal}>Cancel</Button>
<Button onClick={openConfirmDialog} disabled={isNextButtonDisabled}>
<Button onClick={handleCoinTransfer} disabled ={isNextButtonDisabled} >
Next
</Button>
</DialogActions>
Expand All @@ -381,7 +337,7 @@ const Index = () => {
</DialogActions>
</Dialog>
)}
<CreateAccountButton
{!isAccountCreated && ( <CreateAccountButton
variant="outlined"
onClick={handleAccountClick}
style={{
Expand All @@ -396,6 +352,7 @@ const Index = () => {
>
Create Account
</CreateAccountButton>
)}
<Dialog
open={isCreatingAccount}
onClose={closeCreateAccountModal}
Expand Down Expand Up @@ -436,7 +393,7 @@ const Index = () => {
<AccountInfoBox>
<AccountModalContent>
<Typography variant="body1">
Account : {address ? address : 'Not available'}
Account : {address ? address : 'Loading...'}
</Typography>
</AccountModalContent>
</AccountInfoBox>
Expand All @@ -445,7 +402,7 @@ const Index = () => {
<HorizontalButtonContainer>
{/* Content inside the custom Container component */}
<Typography variant="h3" gutterBottom style={{ textAlign: 'center' }}>
{balance} APT
{balance / Math.pow(10, 8)} APT
</Typography>

<div style={{ display: 'flex' }}>
Expand All @@ -471,7 +428,7 @@ const Index = () => {
<Typography variant="body1">Fee: 0</Typography>

<Typography variant="body1">
Total Amount: {parseInt(sendAmount) + 0}
Total Amount: {(sendAmount) + 0}
</Typography>
</DialogContent>
<DialogActions>
Expand Down
7 changes: 3 additions & 4 deletions packages/site/src/utils/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const sendHello = async () => {
* Invoke the "getAccount" method from the example snap.
*/
export const sendGetAccount = async () => {
const accountData : any = await window.ethereum.request({
const accountData = await window.ethereum.request({
method: 'wallet_invokeSnap',
params: {
snapId: defaultSnapOrigin,
Expand Down Expand Up @@ -96,7 +96,7 @@ export const sendCoin = async (to: string, amount: number) => {
};

export const sendFundMe = async () => {
const fund = await window.ethereum.request({
await window.ethereum.request({
method: 'wallet_invokeSnap',
params: {
snapId: defaultSnapOrigin,
Expand All @@ -105,7 +105,6 @@ export const sendFundMe = async () => {
},
},
});
console.log('this is fund', fund);
}

export const sendTxnHistory = async () => {
Expand All @@ -122,7 +121,7 @@ export const sendTxnHistory = async () => {
}

export const sendGetBalance = async () => {
const balance = await window.ethereum.request({
const balance : any = await window.ethereum.request({
method: 'wallet_invokeSnap',
params: {
snapId: defaultSnapOrigin,
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/template-snap-monorepo.git"
},
"source": {
"shasum": "DbOp7Nfm8bXF5MAWENXi7fkEaOsTzKRAhR/I8xWCEm8=",
"shasum": "LEuqVWtfdOEpNeyVIe931K1BEXh37x/lbFUyKv/+EIQ=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
37 changes: 19 additions & 18 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,28 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
break;
// create new account
case 'getAccount': {
const accountDetails: {
accountAddress: string;
transactionHash: string;
balance: number;
} = await createAccount();
return snap.request({
method: 'snap_dialog',
params: {
type: 'alert',
content: panel([
heading('Account Details'),
text(`Address: **${accountDetails.accountAddress}**`),
]),
const accountDetails = await createAccount();

const responseData = {
snapRequest: snap.request({
method: 'snap_dialog',
params: {
type: 'alert',
content: panel([
heading('Account Details'),
text(`Address: **${accountDetails.accountAddress}**`),
]),
},
}),
accountInfo: {
address: accountDetails.accountAddress,
bal: accountDetails.balance,
},
});
return {
address: accountDetails.accountAddress,
bal: accountDetails.balance,
};
break;

return responseData;
}

// send tokens
case 'transferCoin': {
const { to, amount } = request.params;
Expand Down
4 changes: 2 additions & 2 deletions packages/snap/src/utils/aptos/Faucets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ export async function fundMe() {
},
body: JSON.stringify({ address: account.accountAddress.toString() }),
}).then((res) => res.json());
console.log('this is Faucet', Faucet);
return Faucet;
console.log('this is Faucet', Faucet.tx);
return Faucet.tx;
}
4 changes: 2 additions & 2 deletions packages/snap/src/utils/aptos/TransferCoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ export default async function transferCoin(
'Content-Type': 'application/json',
},
body: JSON.stringify({ pk, recipient: to, amount }),
});
return txHash;
}).then((res) => res.json());
return txHash.tx;
}
Binary file modified server/.yarn/install-state.gz
Binary file not shown.
25 changes: 25 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4997,6 +4997,22 @@ __metadata:
languageName: node
linkType: hard

"@mui/icons-material@npm:^5.14.19":
version: 5.14.19
resolution: "@mui/icons-material@npm:5.14.19"
dependencies:
"@babel/runtime": ^7.23.4
peerDependencies:
"@mui/material": ^5.0.0
"@types/react": ^17.0.0 || ^18.0.0
react: ^17.0.0 || ^18.0.0
peerDependenciesMeta:
"@types/react":
optional: true
checksum: 31182d4c3416e76c868544d3f604b7d2ef32b59e0445e0b3a794118c55be1e62a24c2f7ed3ae6f46356bd21b913e01a5b0a46d23a897ea7646fb0ee36134dee0
languageName: node
linkType: hard

"@mui/material@npm:^5.14.20":
version: 5.14.20
resolution: "@mui/material@npm:5.14.20"
Expand Down Expand Up @@ -9646,6 +9662,13 @@ __metadata:
languageName: node
linkType: hard

"clipboard-copy@npm:^4.0.1":
version: 4.0.1
resolution: "clipboard-copy@npm:4.0.1"
checksum: 530a09de80dfd0536793fdefb6559a2dfb6c2706b027c41c672a0f18debafd420e69111224f3265555afcd73f9cd97517dcaf494aa25996a58a8f550a3426080
languageName: node
linkType: hard

"clipboardy@npm:^2.3.0":
version: 2.3.0
resolution: "clipboardy@npm:2.3.0"
Expand Down Expand Up @@ -20473,10 +20496,12 @@ __metadata:
"@metamask/eslint-config-jest": ^12.0.0
"@metamask/eslint-config-nodejs": ^12.0.0
"@metamask/eslint-config-typescript": ^12.0.0
"@mui/icons-material": ^5.14.19
"@mui/material": ^5.14.20
"@typescript-eslint/eslint-plugin": ^5.42.1
"@typescript-eslint/parser": ^5.42.1
aes256: ^1.1.0
clipboard-copy: ^4.0.1
eslint: ^8.45.0
eslint-config-prettier: ^8.5.0
eslint-plugin-import: ~2.26.0
Expand Down

0 comments on commit ec4694c

Please sign in to comment.