Skip to content

Commit

Permalink
fix erc20 fund contract safe approval bug (#3460)
Browse files Browse the repository at this point in the history
* fix: erc20 fund contract safe approval bug

* fix: issues
  • Loading branch information
bhargavaparoksham authored May 24, 2024
1 parent 7b4605b commit 3d7bef5
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/common/src/allo/allo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export interface Allo {
tokenAddress: Address;
roundId: string;
amount: bigint;
requireTokenApproval?: boolean;
}) => AlloOperation<
Result<null>,
{
Expand Down
7 changes: 4 additions & 3 deletions packages/common/src/allo/backends/allo-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,8 @@ export class AlloV1 implements Allo {
args.roundData.applicationsEndTime
? dateToEthereumTimestamp(args.roundData.applicationsEndTime)
: args.roundData.roundEndTime
? dateToEthereumTimestamp(args.roundData.roundEndTime)
: maxUint256,
? dateToEthereumTimestamp(args.roundData.roundEndTime)
: maxUint256,
dateToEthereumTimestamp(args.roundData.roundStartTime),
args.roundData.roundEndTime
? dateToEthereumTimestamp(args.roundData.roundEndTime)
Expand Down Expand Up @@ -740,6 +740,7 @@ export class AlloV1 implements Allo {
tokenAddress: Address;
roundId: string;
amount: bigint;
requireTokenApproval?: boolean;
}): AlloOperation<
Result<null>,
{
Expand All @@ -754,7 +755,7 @@ export class AlloV1 implements Allo {
const roundAddress = getAddress(args.roundId);
let tx;

if (args.tokenAddress === zeroAddress) {
if (args.tokenAddress === zeroAddress || !args.requireTokenApproval) {
emit("tokenApprovalStatus", success(null));
} else {
const approvalTx = await sendTransaction(this.transactionSender, {
Expand Down
14 changes: 9 additions & 5 deletions packages/common/src/allo/backends/allo-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,9 @@ export class AlloV2 implements Allo {
factoryType: "DVMDT",
});

const txData = strategyFactory.getCreateStrategyDataByChainId(this.chainId);
const txData = strategyFactory.getCreateStrategyDataByChainId(
this.chainId
);

const txResult = await sendRawTransaction(this.transactionSender, {
to: txData.to,
Expand Down Expand Up @@ -559,8 +561,9 @@ export class AlloV2 implements Allo {
factoryType: "DGL",
});

const txData = strategyFactory.getCreateStrategyDataByChainId(this.chainId);

const txData = strategyFactory.getCreateStrategyDataByChainId(
this.chainId
);

const txResult = await sendRawTransaction(this.transactionSender, {
to: txData.to,
Expand All @@ -571,7 +574,7 @@ export class AlloV2 implements Allo {
if (txResult.type === "error") {
return txResult;
}

try {
const receipt = await this.transactionSender.wait(txResult.value);
const strategyCreatedEvent = decodeEventFromReceipt({
Expand Down Expand Up @@ -895,6 +898,7 @@ export class AlloV2 implements Allo {
tokenAddress: Address;
roundId: string;
amount: bigint;
requireTokenApproval?: boolean;
}): AlloOperation<
Result<null>,
{
Expand All @@ -911,7 +915,7 @@ export class AlloV2 implements Allo {

const poolId = BigInt(args.roundId);

if (args.tokenAddress === zeroAddress) {
if (args.tokenAddress === zeroAddress || !args.requireTokenApproval) {
emit("tokenApprovalStatus", success(null));
} else {
const approvalTx = await sendTransaction(this.transactionSender, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export type FundContractParams = {
roundId: string;
fundAmount: number;
payoutToken: PayoutToken;
requireTokenApproval?: boolean;
};

type SubmitFundParams = FundContractParams & {
Expand Down Expand Up @@ -144,6 +145,7 @@ async function _fundContract({
roundId,
fundAmount,
payoutToken,
requireTokenApproval,
}: SubmitFundParams) {
resetToInitialState(context);

Expand All @@ -159,6 +161,7 @@ async function _fundContract({
roundId,
tokenAddress: payoutToken.address,
amount,
requireTokenApproval,
})
.on("tokenApprovalStatus", (tx) => {
if (tx.type === "error") {
Expand Down
38 changes: 37 additions & 1 deletion packages/round-manager/src/features/round/FundContract.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import {
useTokenPrice,
} from "common";
import { assertAddress } from "common/src/address";
import { formatUnits } from "viem";
import { formatUnits, zeroAddress } from "viem";
import { Erc20__factory } from "../../types/generated/typechain";
import { useWallet } from "../common/Auth";
import { getAlloAddress } from "common/src/allo/backends/allo-v2";

export function useContractAmountFunded(args: {
round: Round | undefined;
Expand Down Expand Up @@ -138,6 +141,7 @@ export default function FundContract(props: {
}) {
const { address } = useAccount();
const navigate = useNavigate();
const { signer } = useWallet();

const [amountToFund, setAmountToFund] = useState("");
const [insufficientBalance, setInsufficientBalance] = useState(false);
Expand Down Expand Up @@ -654,13 +658,45 @@ export default function FundContract(props: {
setOpenProgressModal(true);
}, errorModalDelayMs);

let tokenAllowance = BigNumber.from(0);

let requireTokenApproval = false;

const amount = ethers.utils.parseUnits(
amountToFund.toString(),
matchingFundPayoutToken.decimal
);

const alloVersion = props.round?.tags?.includes("allo-v2") ? "v2" : "v1";
const roundAddress =
alloVersion === "v1" ? props.round?.id : getAlloAddress(chainId);

if (
matchingFundPayoutToken?.address !== undefined &&
matchingFundPayoutToken?.address !== zeroAddress &&
signer
) {
const erc20 = Erc20__factory.connect(
matchingFundPayoutToken?.address,
signer
);
tokenAllowance = await erc20.allowance(
address as string,
roundAddress as string
);
if (tokenAllowance.lt(amount)) {
requireTokenApproval = true;
}
}

await fundContract({
allo,
roundId: props.roundId,
fundAmount: Number(
parseFloat(amountToFund).toFixed(matchingFundPayoutToken.decimal)
),
payoutToken: matchingFundPayoutToken,
requireTokenApproval,
});
} catch (error) {
if (error === Logger.errors.TRANSACTION_REPLACED) {
Expand Down

0 comments on commit 3d7bef5

Please sign in to comment.