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

feat: Add safe wallet support #77

Merged
merged 24 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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: 2 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ NEXT_PUBLIC_ARBITRUM_GOERLI_RPC_URL=
NEXT_PUBLIC_ARBITRUM_NOVA_RPC_URL=
NEXT_PUBLIC_ARBITRUM_SEPOLIA_RPC_URL=

NEXT_PUBLIC_WALLETCONNECT_PROJECT_ID=

NEXT_PUBLIC_LOCAL_ETHEREUM_RPC_URL=http://localhost:8545
NEXT_PUBLIC_LOCAL_ARBITRUM_RPC_URL=http://localhost:8547
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16.18.1
18
13 changes: 12 additions & 1 deletion next.config.js
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
module.exports = {};
module.exports = {
webpack: (config, context) => {
if (config.plugins) {
config.plugins.push(
new context.webpack.IgnorePlugin({
resourceRegExp: /^(lokijs|pino-pretty|encoding)$/,
}),
);
}
return config;
Comment on lines +5 to +12
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix error in building for nextjs: WalletConnect/walletconnect-monorepo#1908 (comment)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

best if we add a link to this in the file

},
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"dependencies": {
"@arbitrum/sdk": "^3.1.12",
"@ethersproject/bignumber": "^5.1.1",
"@rainbow-me/rainbowkit": "^0.12.16",
fionnachan marked this conversation as resolved.
Show resolved Hide resolved
"@types/node": "^16.7.13",
"@types/react": "^18.0.26",
"@types/react-dom": "^18.0.10",
Expand All @@ -16,7 +17,7 @@
"react-feather": "^2.0.10",
"react-tooltip": "^5.5.1",
"typescript": "^4.4.2",
"wagmi": "^0.8.10"
"wagmi": "^0.12.18"
fionnachan marked this conversation as resolved.
Show resolved Hide resolved
},
"engines": {
"node": ">=16"
Expand Down
2 changes: 2 additions & 0 deletions src/app/global.css
fionnachan marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ main {
max-width: 740px;
gap: 10px;
margin-bottom: var(--space-xl);
margin-left: auto;
margin-right: auto;
}

.form-inner {
Expand Down
6 changes: 3 additions & 3 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NextPage } from 'next';
import { WagmiProvider } from '@/components/WagmiProvider';
import { Providers } from '@/components/Providers';
import { Logo } from '@/components/Logo';
import { Form } from '@/components/Form';

Expand All @@ -12,9 +12,9 @@ const PageIndex: NextPage = () => {
</header>

<main>
<WagmiProvider>
<Providers>
<Form />
</WagmiProvider>
</Providers>
</main>
</>
);
Expand Down
51 changes: 4 additions & 47 deletions src/app/recover-funds/[address]/RecoverFunds.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use client';
import { Address } from '@arbitrum/sdk';
import { BigNumber, utils, constants } from 'ethers';
import { useNetwork } from 'wagmi';
import { getProviderFromChainId, getTargetChainId } from '@/utils';
import { BigNumber, utils } from 'ethers';
import '../style.css';

export interface OperationInfo {
balanceToRecover: BigNumber;
aliasedAddress: string;
chainId: string;
}

export const hasBalanceOverThreshold = (balanceToRecover: BigNumber) => {
Expand All @@ -16,59 +13,19 @@ export const hasBalanceOverThreshold = (balanceToRecover: BigNumber) => {
return balanceToRecover.gte(BigNumber.from(5_000_000_000_000_000));
};

export async function getData(
chainID: number,
address: string,
): Promise<OperationInfo | null> {
// First, obtain the aliased address of the signer
const destinationAddress = new Address(address);
const { value: aliasedAddress } = destinationAddress.applyAlias();

// And get its balance to find out the amount we are transferring
try {
const l2Provider = getProviderFromChainId(chainID);
const aliasedSignerBalance = await l2Provider.getBalance(aliasedAddress);

return {
balanceToRecover: hasBalanceOverThreshold(aliasedSignerBalance)
? aliasedSignerBalance
: constants.Zero,
aliasedAddress,
};
} catch (e) {
return {
balanceToRecover: constants.Zero,
aliasedAddress,
};
}
}

type Props = {
operationInfo: OperationInfo;
address: string;
};
const RecoverFunds = ({ operationInfo, address }: Props) => {
const { chain } = useNetwork();
const targetChainID = getTargetChainId(chain?.id);

// No funds to recover
if (!hasBalanceOverThreshold(operationInfo.balanceToRecover)) {
return (
<div className="funds-message">
There are no funds stuck on {operationInfo.aliasedAddress}
<br />
(Alias of {address}) on this network
{targetChainID ? ` (${targetChainID})` : ''}.
</div>
);
}
const l2ChainId = operationInfo.chainId;

return (
<div className="funds-message">
There are {utils.formatEther(operationInfo.balanceToRecover)} ETH on{' '}
{operationInfo.aliasedAddress}
<br />
(Alias of {address}).
(Alias of {address}) on this network {l2ChainId ?? ''}.
</div>
);
};
Expand Down
12 changes: 8 additions & 4 deletions src/app/recover-funds/[address]/RecoverFundsButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@arbitrum/sdk';
import { Inbox__factory } from '@arbitrum/sdk/dist/lib/abi/factories/Inbox__factory';
import { getBaseFee } from '@arbitrum/sdk/dist/lib/utils/lib';
import { useNetwork, useSigner } from 'wagmi';
import { goerli, mainnet, sepolia, useNetwork, useSigner } from 'wagmi';
import { getProviderFromChainId, getTargetChainId } from '@/utils';
import { BigNumber } from 'ethers';

Expand Down Expand Up @@ -143,16 +143,20 @@ function RecoverFundsButton({

if (!signer) return null;

if (chain?.id !== 1 && chain?.id !== 5) {
if (
chain?.id !== mainnet.id &&
chain?.id !== goerli.id &&
chain?.id !== sepolia.id
) {
return (
<div>Unknown L1 chain id. This chain is not supported by this tool</div>
);
}

if (chain?.id !== chainID) {
if (getTargetChainId(chain?.id) !== chainID) {
return (
<div>
To recover funds, connect to chain ${chain?.id} (${chain?.name})
To recover funds, connect to chain {chain?.id} ({chain?.name})
</div>
);
}
Expand Down
156 changes: 120 additions & 36 deletions src/app/recover-funds/[address]/page.tsx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i was extremely confused by the UX here

image

clicking the submit button on this page should not bring the user to
http://localhost:3001/retryables-tracker/{address}, which it currently does

As a user, I am expecting to stay on this page after clicking "Submit" because the address it was showing was initially my MM address because I connected to the site using it first

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Enter the destination address" is not clear enough for a normie user
I would put Enter which address you want to send the funds to on Arbitrum One
and also add a red warning
"CAUTION: Only put an address you are 100% sure you control on Arbitrum One, or your funds will be lost forever!"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should avoid showing the chain id directly to users, and always use network name

Original file line number Diff line number Diff line change
@@ -1,53 +1,78 @@
'use client';
import { getTargetChainId } from '@/utils';
import { utils } from 'ethers';
import { supportedL2Networks } from '@/utils/network';
import { Address } from '@arbitrum/sdk';
import { constants, utils } from 'ethers';
import dynamic from 'next/dynamic';
import React, { useEffect, useState } from 'react';
import { useNetwork } from 'wagmi';
import {
getData,
hasBalanceOverThreshold,
OperationInfo,
} from './RecoverFunds';
import { hasBalanceOverThreshold, OperationInfo } from './RecoverFunds';
import { RecoverFundsButton } from './RecoverFundsButton';
import { JsonRpcProvider } from '@ethersproject/providers';

const RecoverFunds = dynamic(() => import('./RecoverFunds'), {
ssr: false,
});

const RecoverFundsPage = ({
params: { address },
}: {
params: { address: string };
}) => {
const { chain } = useNetwork();
const [operationInfo, setOperationInfo] = useState<OperationInfo | null>(
null,
);
const [destinationAddress, setDestinationAddress] = useState<string | null>(
null,
);
type OperationInfoByChainId = {
[chainId: string]: OperationInfo;
};
async function getOperationInfoByChainId(
address: string,
): Promise<OperationInfoByChainId> {
// First, obtain the aliased address of the signer
const destinationAddress = new Address(address);
const { value: aliasedAddress } = destinationAddress.applyAlias();

const targetChainID = getTargetChainId(chain?.id);
// And get its balance to find out the amount we are transferring
const operationInfoPromises = Object.entries(supportedL2Networks).map(
async ([chainId, rpcURL]) => {
const l2Provider = new JsonRpcProvider(rpcURL);

useEffect(() => {
if (!targetChainID) {
return;
}
try {
const aliasedSignerBalance = await l2Provider.getBalance(
aliasedAddress,
);

getData(targetChainID, address).then((data) => {
setOperationInfo(data);
});
}, [address, targetChainID]);
return {
balanceToRecover: hasBalanceOverThreshold(aliasedSignerBalance)
? aliasedSignerBalance
: constants.Zero,
aliasedAddress,
chainId,
};
} catch (e) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the RPC fails for whatever reason, the page will tell the user that there are no funds to recover, but that might be misleading.
Is there any other reason why the flow would catch an error here?
If a faulty RPC is the only reason, I would maybe log/show an internal error somewhere, instead of returning a 0 balance.

return {
balanceToRecover: constants.Zero,
aliasedAddress,
chainId,
};
}
},
);

const handleChange: React.ChangeEventHandler<HTMLInputElement> = (e) => {
const value = e.target.value;
setDestinationAddress(value);
};
const result = Promise.all(operationInfoPromises);
return result.then((operationInfo) => {
return operationInfo.reduce(
(acc, info) => ({
...acc,
[info.chainId]: info,
}),
{},
);
});
}

if (!operationInfo) {
return;
}
function RecoverFundsDetail({
operationInfo,
address,
}: {
operationInfo: OperationInfo;
address: string;
}) {
const { chain } = useNetwork();
const [destinationAddress, setDestinationAddress] = useState<string | null>(
null,
);

const hasBalanceToRecover = hasBalanceOverThreshold(
operationInfo.balanceToRecover,
Expand All @@ -58,6 +83,15 @@ const RecoverFundsPage = ({
utils.isAddress(destinationAddress) &&
operationInfo.aliasedAddress;

const handleChange: React.ChangeEventHandler<HTMLInputElement> = (e) => {
const value = e.target.value;
setDestinationAddress(value);
};

if (!hasBalanceOverThreshold(operationInfo.balanceToRecover)) {
return null;
}

return (
<>
<RecoverFunds address={address} operationInfo={operationInfo} />
Expand All @@ -73,14 +107,64 @@ const RecoverFundsPage = ({
)}
{hasBalanceToRecover && hasDestinationAddress && (
<RecoverFundsButton
chainID={chain.id}
chainID={Number(operationInfo.chainId)}
balanceToRecover={operationInfo.balanceToRecover}
destinationAddress={destinationAddress}
addressToRecoverFrom={address}
/>
)}
</>
);
}

const RecoverFundsPage = ({
params: { address },
}: {
params: { address: string };
}) => {
const [operationInfos, setOperationInfos] =
useState<OperationInfoByChainId | null>(null);

useEffect(() => {
getOperationInfoByChainId(address).then((operationInfoByChainId) => {
setOperationInfos(operationInfoByChainId);
});
}, [address]);

if (!operationInfos) {
return;
}

// No balance to recover on any chains
if (
Object.keys(operationInfos).every(
(chainId) =>
!hasBalanceOverThreshold(operationInfos[chainId].balanceToRecover),
)
) {
const aliasedAddress =
operationInfos[Object.keys(operationInfos)[0]].aliasedAddress;

return (
<div className="funds-message">
There are no funds stuck on {aliasedAddress}
<br />
(Alias of {address}) on Arbitrum networks
</div>
);
}

return (
<>
{Object.keys(operationInfos).map((chainId) => (
<RecoverFundsDetail
address={address}
operationInfo={operationInfos[chainId]}
key={chainId}
/>
))}
</>
);
};

export default RecoverFundsPage;
Loading