Skip to content

Commit

Permalink
Fix signer
Browse files Browse the repository at this point in the history
  • Loading branch information
smitch88 committed Aug 8, 2023
1 parent f293a54 commit f066203
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 65 deletions.
37 changes: 18 additions & 19 deletions client/components/Web3Button.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Link from "next/link";
import { ConnectButton as RainbowKitConnect } from "@rainbow-me/rainbowkit";
import useShowDelegationModalOption from "utils/useShowDelegationModalOption";
import classnames from "classnames";
Expand Down Expand Up @@ -69,14 +70,23 @@ const ConnectButton = ({ inPage = false, needToShowDelegation = false }) => {
}

return (
<button
onClick={openAccountModal}
type="button"
className="flex flex-row items-center space-x-2 p-3 bg-secondary-content text-white rounded-full text-sm leading-none capitalize cursor-pointer"
>
<span className="w-3 h-3 bg-[#4bbc8a] rounded-full" />
<span>{account.displayName}</span>
</button>
<div className="flex flex-row items-center space-x-2">
<button
onClick={openAccountModal}
type="button"
className="flex flex-row items-center space-x-2 p-3 bg-secondary-content text-white rounded-full text-sm leading-none cursor-pointer"
>
<span className="w-3 h-3 bg-[#4bbc8a] rounded-full" />
<span>{account.displayName}</span>
</button>
{needToShowDelegation && (
<Link href="/register-vote">
<button className="hidden lg:flex items-center justify-center text-sm py-2 text-white px-3 bg-gradient-to-r from-gradient-from to-gradient-to rounded-full">
Vote register
</button>
</Link>
)}
</div>
);
})()}
</div>
Expand All @@ -86,17 +96,6 @@ const ConnectButton = ({ inPage = false, needToShowDelegation = false }) => {
);
};

/*
{needToShowDelegation && (
<Link
className="flex items-center justify-center text-sm py-2 text-white px-3 bg-gradient-to-r from-gradient-from to-gradient-to rounded-full"
href="/register-vote"
>
Vote register
</Link>
)}
*/

interface Web3ButtonProps {
inPage?: boolean;
}
Expand Down
46 changes: 26 additions & 20 deletions client/components/vote-escrow/LockupForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import numeral from "numeraljs";
import { decimal18Bn } from "utils";
import useStakingAPY from "utils/useStakingAPY";
import classnames from "classnames";
import { useSigner } from "wagmi";

interface LockupFormProps {
existingLockup?: Object;
Expand Down Expand Up @@ -126,6 +127,7 @@ const LockupForm: FunctionComponent<LockupFormProps> = ({ existingLockup }) => {
blockTimestamp,
} = useStore();
const router = useRouter();
const { data: signer } = useSigner();

const [lockupAmount, setLockupAmount] = useState(
existingLockup
Expand Down Expand Up @@ -219,11 +221,11 @@ const LockupForm: FunctionComponent<LockupFormProps> = ({ existingLockup }) => {

let transaction;
try {
transaction = await contracts.OriginDollarGovernance.approve(
contracts.OgvStaking.address,
ethers.constants.MaxUint256,
{ gasLimit: 144300 }
);
transaction = await contracts.OriginDollarGovernance.connect(
signer
).approve(contracts.OgvStaking.address, ethers.constants.MaxUint256, {
gasLimit: 144300,
});
} catch (e) {
setTransactionError("Error approving!");
setApprovalStatus("ready");
Expand Down Expand Up @@ -273,6 +275,7 @@ const LockupForm: FunctionComponent<LockupFormProps> = ({ existingLockup }) => {

// If lockup amount === 100% of the user's OGV balance then use the actual balance BigNumber
let amountToStake = ethers.utils.parseUnits(lockupAmount);

if (lockupAmount === formattedOgvBalance) {
amountToStake = balances.ogv; // Prevents rounding
}
Expand All @@ -282,15 +285,15 @@ const LockupForm: FunctionComponent<LockupFormProps> = ({ existingLockup }) => {
// When the user has delegation set already, it'll be ~200k.
const maxGasNeeded = 350000 * 1.2; // Adds a buffer

const gasEstimate = await contracts.OgvStaking.estimateGas[
"stake(uint256,uint256)"
](amountToStake, duration);
const gasEstimate = await contracts.OgvStaking.connect(
signer
).estimateGas["stake(uint256,uint256)"](amountToStake, duration);

transaction = await contracts.OgvStaking["stake(uint256,uint256)"](
amountToStake,
duration,
{ gasLimit: Math.max(Math.ceil(gasEstimate * 1.25), maxGasNeeded) }
);
transaction = await contracts.OgvStaking.connect(signer)[
"stake(uint256,uint256)"
](amountToStake, duration, {
gasLimit: Math.max(Math.ceil(gasEstimate * 1.25), maxGasNeeded),
});
} catch (e) {
setTransactionError("Error locking up!");
setLockupStatus("ready");
Expand Down Expand Up @@ -348,15 +351,18 @@ const LockupForm: FunctionComponent<LockupFormProps> = ({ existingLockup }) => {
// When the user has delegation set already, it'll be ~200k.
const maxGasNeeded = 300000 * 1.2; // Adds a buffer

const gasEstimate = await contracts.OgvStaking.estimateGas[
"extend(uint256,uint256)"
](existingLockup.lockupId, duration);

transaction = await contracts.OgvStaking["extend(uint256,uint256)"](
const gasEstimate = await contracts.OgvStaking.connect(
signer
).estimateGas["extend(uint256,uint256)"](
existingLockup.lockupId,
duration,
{ gasLimit: Math.max(Math.ceil(gasEstimate * 1.25), maxGasNeeded) }
duration
);

transaction = await contracts.OgvStaking.connect(signer)[
"extend(uint256,uint256)"
](existingLockup.lockupId, duration, {
gasLimit: Math.max(Math.ceil(gasEstimate * 1.25), maxGasNeeded),
});
} catch (e) {
setTransactionError("Error extending lockup!");
setLockupStatus("ready");
Expand Down
14 changes: 10 additions & 4 deletions client/components/vote-escrow/LockupsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ import { truncateEthAddress } from "utils";
import EtherscanIcon from "components/EtherscanIcon";
import ExternalLinkIcon from "../ExternalLinkIcon";
import classnames from "classnames";
import { useSigner } from "wagmi";

const LockupsTable: FunctionComponent = () => {
const { data: signer } = useSigner();

const { lockups, pendingTransactions, contracts, blockTimestamp, chainId } =
useStore();

Expand All @@ -36,10 +39,13 @@ const LockupsTable: FunctionComponent = () => {
}

const handleUnlock = async (lockupId) => {
const transaction = await contracts.OgvStaking["unstake(uint256)"](
lockupId,
{ gasLimit: 210000 }
);
const gasLimit = await contracts.OgvStaking.connect(signer).estimateGas[
"unstake(uint256)"
](lockupId);

const transaction = await contracts.OgvStaking.connect(signer)[
"unstake(uint256)"
](lockupId, { gasLimit });

useStore.setState({
pendingTransactions: [
Expand Down
28 changes: 18 additions & 10 deletions client/components/vote-escrow/YourLockups.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
import { FunctionComponent, useState } from "react";
import dynamic from "next/dynamic";
import { toast } from "react-toastify";
import Image from "next/image";
import { useAccount, useSigner } from "wagmi";
import Card from "components/Card";
import Link from "components/Link";
import { SectionTitle } from "components/SectionTitle";
import { useStore } from "utils/store";
import { Loading } from "components/Loading";
import { toast } from "react-toastify";
import useAccountBalances from "utils/useAccountBalances";
import Image from "next/image";
import DisabledButtonToolTip from "../DisabledButtonTooltip";
import LockupsTable from "./LockupsTable";
import { Web3Button } from "components/Web3Button";
import useStakingAPY from "utils/useStakingAPY";
import { useAccount, useSigner } from "wagmi";
import DisabledButtonToolTip from "../DisabledButtonTooltip";

interface YourLockupsProps {}

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

const YourLockups: FunctionComponent<YourLockupsProps> = () => {
const { isConnected } = useAccount();
const { lockups, pendingTransactions, contracts, balances, totalBalances } =
useStore();
const {
lockups,
pendingTransactions,
contracts,
balances,
totalBalances,
rpcProvider,
} = useStore();
const { data: signer } = useSigner();
const { ogv, accruedRewards } = balances;
const { totalPercentageOfLockedUpOgv } = totalBalances;
Expand Down Expand Up @@ -64,9 +74,7 @@ const YourLockups: FunctionComponent<YourLockupsProps> = () => {

let receipt;
try {
receipt = await contracts.rpcProvider.waitForTransaction(
transaction.hash
);
receipt = await rpcProvider.waitForTransaction(transaction.hash);
} catch (e) {
setCollectRewardsStatus("ready");
throw e;
Expand Down
17 changes: 15 additions & 2 deletions client/pages/stake/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
import dynamic from "next/dynamic";
import { PageTitle } from "components/PageTitle";
import { SectionTitle } from "components/SectionTitle";
import CardGroup from "components/CardGroup";
import Wrapper from "components/Wrapper";
import AccountBalances from "components/vote-escrow/AccountBalances";
import YourLockups from "components/vote-escrow/YourLockups";
import Seo from "components/Seo";

const AccountBalances = dynamic(
() => import("components/vote-escrow/AccountBalances"),
{
ssr: false,
}
);

const YourLockups = dynamic(
() => import("components/vote-escrow/YourLockups"),
{
ssr: false,
}
);

export default function VoteEscrow() {
return (
<Wrapper narrow>
Expand Down
8 changes: 7 additions & 1 deletion client/pages/stake/new.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { NextPage } from "next";
import { useState, useEffect } from "react";
import { Disconnected } from "components/Disconnected";
import Wrapper from "components/Wrapper";
import { PageTitle } from "components/PageTitle";
Expand All @@ -11,8 +12,13 @@ import { useAccount } from "wagmi";

const LockupNew: NextPage = () => {
const { isConnected } = useAccount();
const [isMounted, setIsMounted] = useState(false);

if (!isConnected) {
useEffect(() => {
setIsMounted(true);
}, []);

if (!isMounted || !isConnected) {
return (
<Wrapper narrow>
<Disconnected />
Expand Down
22 changes: 14 additions & 8 deletions client/utils/useBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,36 @@ import { useEffect, useState } from "react";
import { useStore } from "utils/store";
import { useNetworkInfo } from "utils/index";
import { useAccount } from "wagmi";
import { ethers } from "ethers";
import { RPC_URLS } from "@/constants/index";

const useBlock = () => {
const networkInfo = useNetworkInfo();
const { isConnected } = useAccount();
const { provider } = useStore();
const [refetchBlock, setRefetchBlock] = useState(0);

useEffect(() => {
const getBlockTimestamp = async () => {
const provider = new ethers.providers.JsonRpcProvider(
RPC_URLS[networkInfo.envNetwork]
);
const currentBlock = await provider?.getBlockNumber();
const block = await provider?.getBlock(currentBlock);
return block?.timestamp;
};

let intervalId: ReturnType<typeof setInterval>;

if (isConnected && networkInfo.correct) {
intervalId = setInterval(() => {
Promise.all([getBlockTimestamp()]).then(([blockTimestamp]) => {
useStore.setState({
blockTimestamp,
});
const pullTimestamp = () => {
Promise.all([getBlockTimestamp()]).then(([blockTimestamp]) => {
useStore.setState({
blockTimestamp,
});
}, 4000);
});
};

if (isConnected && networkInfo.correct) {
intervalId = setInterval(pullTimestamp, 4000);
}

return () => clearInterval(intervalId);
Expand Down
3 changes: 2 additions & 1 deletion client/utils/useContracts.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect } from "react";
import { ethers } from "ethers";
import OUSDContracts from "networks/network.mainnet.json";
import { CHAIN_CONTRACTS, mainnetNetworkUrl, RPC_URLS } from "constants/index";
import { CHAIN_CONTRACTS, RPC_URLS } from "constants/index";
import { useStore } from "utils/store";
import { useNetworkInfo } from "utils/index";
import { useAccount, useNetwork, useSigner } from "wagmi";
Expand All @@ -20,6 +20,7 @@ const useContracts = () => {

const loadContracts = async () => {
useStore.setState({
provider,
contracts: {
loaded: false,
},
Expand Down

0 comments on commit f066203

Please sign in to comment.