Skip to content

Commit

Permalink
fix: small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gagdiez committed Oct 10, 2024
1 parent 39a08d5 commit 1eaf5e7
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 456 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"lodash": "^4.17.21",
"moment": "^2.30.1",
"near-api-js": "^4.0.3",
"near-fastauth-wallet": "2.0.1",
"near-fastauth-wallet": "2.0.4",
"near-social-vm": "github:calebjacob/VM#dev",
"next": "^13.5.6",
"next-pwa": "^5.6.0",
Expand Down
50 changes: 35 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 6 additions & 16 deletions src/components/wallet-selector/WalletSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { setupFastAuthWallet } from 'near-fastauth-wallet';
import type { Context } from 'react';
import { createContext } from 'react';

import { networkId as defaultNetwork, signInContractId } from '@/config';
import { networkId as defaultNetwork } from '@/config';
import { KEYPOM_OPTIONS } from '@/utils/keypom-options';
import type { NetworkId } from '@/utils/types';

Expand Down Expand Up @@ -52,6 +52,8 @@ export class Wallet {
setupMeteorWallet(),
setupBitteWallet(),
setupHereWallet(),
setupLedger(),
setupNearMobileWallet(),
setupMyNearWallet(),
setupSender(),
setupMintbaseWallet(),
Expand Down Expand Up @@ -86,10 +88,8 @@ export class Wallet {
: 'https://dev.near.org/#instant-url/ACCOUNT_ID/SECRET_KEY/MODULE_ID',
},
networkId: this.networkId,
signInContractId,
signInContractId: this.createAccessKeyFor || '',
}) as any, // TODO: Refactor setupKeypom() to TS
setupLedger(),
setupNearMobileWallet(),
],
});

Expand Down Expand Up @@ -190,23 +190,13 @@ export class Wallet {
signAndSendTransactions = async ({ transactions }: { transactions: any[] }) => {
const selectedWallet = await (await this.selector).wallet();
const result = await selectedWallet.signAndSendTransactions({ transactions });

if (!result) {
throw new Error('Transaction failed');
} else {
return Promise.all(result.map(async (o) => providers.getTransactionLastResult(o)));
}
if (result) return Promise.all(result.map(async (o) => providers.getTransactionLastResult(o)));
};

signAndSendTransaction = async (transaction: any) => {
const selectedWallet = await (await this.selector).wallet();
const result = await selectedWallet.signAndSendTransaction(transaction);

if (!result) {
throw new Error(`Transaction ${JSON.stringify(transaction)} failed`);
} else {
return providers.getTransactionLastResult(result);
}
if (result) return providers.getTransactionLastResult(result);
};

getAccessKeys = async (accountId: string) => {
Expand Down
42 changes: 35 additions & 7 deletions src/components/wallet-utilities/ExportFastAuthAccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,20 @@ export const ExportFastAuthAccount = () => {
const [secretKey, setSecretKey] = useState('');
const [fastAuthAccountSignedIn, setFastAuthAccountSignedIn] = useState(false);
const [privateKeyVisible, setPrivateKeyVisible] = useState(false);
const [enoughBalance, setEnoughBalance] = useState(false);
const { wallet, signedAccountId } = useContext(NearContext);

useEffect(() => {
const balanceCheck = async () => {
if (!wallet || !signedAccountId) return;

const balance = await wallet.getBalance(signedAccountId);
setEnoughBalance(balance >= 0.001);
};

balanceCheck();
}, [wallet, signedAccountId]);

useEffect(() => {
const checkFastAuthAccount = async () => {
const selector = await wallet?.selector;
Expand All @@ -40,9 +52,7 @@ export const ExportFastAuthAccount = () => {

const generateKey = async () => {
try {
if (!signedAccountId || !wallet) {
throw new Error('Wallet has not initialized');
}
if (!wallet) throw new Error('Wallet has not initialized');

setGeneratingKey(true);

Expand All @@ -63,7 +73,25 @@ export const ExportFastAuthAccount = () => {
],
};

await wallet.signAndSendTransaction(addKeyTransaction);
await wallet.signAndSendTransactions({ transactions: [addKeyTransaction] });

openToast({
type: 'info',
title: 'Checking for key',
description: 'We are checking if the key was added to your account, this can take up to 10 seconds',
duration: 5000,
});

let hasKey = false;

for (let i = 0; i < 5; i++) {
await new Promise((resolve) => setTimeout(resolve, 2000));
const accessKeys = await wallet.getAccessKeys(signedAccountId);
hasKey = accessKeys.some((key: any) => key.public_key === publicKey);
if (hasKey) break;
}

if (!hasKey) throw new Error('Failed to add access key');

setSecretKey(keyPair.toString());

Expand All @@ -80,7 +108,7 @@ export const ExportFastAuthAccount = () => {
};

if (!fastAuthAccountSignedIn) {
return <Text>The export feature is only for FastAuth accounts. You are currently signed in with a wallet.</Text>;
return <Text>This tool allows to export accounts created with an email (also known as FastAuth accounts)</Text>;
}

return (
Expand Down Expand Up @@ -148,10 +176,10 @@ export const ExportFastAuthAccount = () => {
</Card>
) : (
<Button
disabled={!signedAccountId}
disabled={!signedAccountId || !enoughBalance}
loading={generatingKey}
onClick={generateKey}
label="Create Full Access Key"
label={enoughBalance ? 'Create Full Access Key' : 'Not enough balance (0.001 Ⓝ required)'}
variant="affirmative"
/>
)}
Expand Down
2 changes: 2 additions & 0 deletions src/components/wallet-utilities/ReceiveNear.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { NearContext } from '../wallet-selector/WalletSelector';
export const ReceiveNear = () => {
const { signedAccountId } = useContext(NearContext);

if (!signedAccountId) return <Text> Sign in to see your NEAR account name </Text>;

return (
<Flex stack>
<Text size="text-s">Copy and share your account ID to receive NEAR:</Text>
Expand Down
4 changes: 3 additions & 1 deletion src/components/wallet-utilities/SendNear.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Button, Flex, Form, handleClientError, Input, openToast } from '@near-pagoda/ui';
import { Button, Flex, Form, handleClientError, Input, openToast, Text } from '@near-pagoda/ui';
import { utils } from 'near-api-js';
import { useEffect, useState } from 'react';
import { useContext } from 'react';
Expand Down Expand Up @@ -95,6 +95,8 @@ export const SendNear = () => {
}
};

if (!signedAccountId) return <Text> Sign in to send NEAR </Text>;

return (
<Form onSubmit={form.handleSubmit(validSubmitHandler)}>
<Flex stack gap="l">
Expand Down
Loading

0 comments on commit 1eaf5e7

Please sign in to comment.