Skip to content

Commit

Permalink
Merge pull request #228 from polywrap/nerfzael-currency-aggregation
Browse files Browse the repository at this point in the history
Properly aggregating currency
  • Loading branch information
cbrzn authored Feb 23, 2024
2 parents ae4c394 + 6dd8cd5 commit e9ca7d5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 36 deletions.
30 changes: 12 additions & 18 deletions web/components/Strategy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import Image from "next/image";
import { pluralize } from "@/utils/pluralize";
import { findMostRepeatedString } from "@/utils/findMostRepeatedString";
import { useTweetShare } from "@/hooks/useTweetShare";
import { BigNumber } from "ethers";
import { parseUnits } from "ethers/lib/utils";
import clsx from "clsx";

export default function Strategy(props: {
Expand Down Expand Up @@ -117,7 +119,7 @@ export default function Strategy(props: {
setIsFundingPending(true);

try {
if (selectedStrategiesLength === 0 || amount === "0") return;
if (selectedStrategiesLength === 0 || !amount || amount === "0") return;

const currentNetworkId = SUPPORTED_NETWORKS[selectedNetwork];
if (connectedChain && currentNetworkId !== +connectedChain.id) {
Expand Down Expand Up @@ -145,32 +147,24 @@ export default function Strategy(props: {
selectedNetwork
);

const donations = strategies
.filter((x) => x.selected)
.map((strategy) => {
const networkIndex = strategy.networks.indexOf(selectedNetwork);
return {
amount: strategy.amount as string,
description: strategy.project.description as string,
title: strategy.project.title as string,
recipient: strategy.recipients[networkIndex],
};
});
const selectedStrategies = strategies.filter((x) => x.selected);

const amounts = donations
const amounts = selectedStrategies
.filter((x) => x.amount)
.map((x) => Number(x.amount))
.filter((x) => x > 0);

const totalAmount = amounts.reduce((a, b) => a + b, 0);

if (+allowance < totalAmount) {
const recipientAddresses = selectedStrategies
.map((strategy) => strategy.recipients[strategy.networks.indexOf(selectedNetwork)]);

const totalAmount = amounts.reduce((a, b) => a.add(parseUnits(b.toString(), selectedToken.decimals)), BigNumber.from(0));
if (allowance.lt(totalAmount)) {
await approve(wallet, selectedToken, totalAmount, selectedNetwork);
}

await executeDonation(
selectedNetwork,
selectedToken,
donations.map((x) => x.recipient),
recipientAddresses,
amounts
);

Expand Down
27 changes: 9 additions & 18 deletions web/hooks/useDonation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import {
getTokensForNetwork,
splitTransferFunds,
} from "@/utils/ethereum";
import { ethers } from "ethers";
import { BigNumber, ethers } from "ethers";

export function useDonation() {
const [{ wallet }] = useConnectWallet();

const execute = async (
selectedNetwork: NetworkName,
selectedToken: TokenInformation,
network: NetworkName,
token: TokenInformation,
recipientAddresses: string[],
amounts: number[]
) => {
Expand All @@ -27,20 +27,13 @@ export function useDonation() {

const signer = ethersProvider.getSigner();

const token = getTokensForNetwork(selectedNetwork).find(
(t) => t.name == selectedToken.name
);

if (!token) {
throw new Error(`Token with name: ${selectedToken} is not valid`);
}
console.log(recipientAddresses, amounts, signer, token, selectedNetwork);
console.log(recipientAddresses, amounts.map(x => x.toString()), signer, token, network);

await splitTransferFunds(
recipientAddresses,
amounts,
signer,
selectedNetwork,
network,
token.address,
token.decimals
);
Expand All @@ -59,7 +52,7 @@ export function useDonation() {
return ethers.utils.formatUnits(balance.toString(), token.decimals);
};

const getAllowance = async (wallet: WalletState, token: TokenInformation, network: NetworkName) => {
const getAllowance = async (wallet: WalletState, token: TokenInformation, network: NetworkName): Promise<BigNumber> => {
const ethersProvider = new ethers.providers.Web3Provider(
wallet.provider,
"any"
Expand All @@ -70,14 +63,13 @@ export function useDonation() {
const currentAddress = await signer.getAddress();
const contractAddress = DISPERSE_CONTRACT_ADDRESSES[network];

const balance = await tokenContract.allowance(currentAddress, contractAddress);
return ethers.utils.formatUnits(balance.toString(), token.decimals);
return await tokenContract.allowance(currentAddress, contractAddress);
};

const approve = async (
wallet: WalletState,
token: TokenInformation,
amount: number,
amount: BigNumber,
network: NetworkName
) => {
const ethersProvider = new ethers.providers.Web3Provider(
Expand All @@ -88,8 +80,7 @@ export function useDonation() {
const tokenContract = new ethers.Contract(token.address, ERC20_ABI, signer);

const contractAddress = DISPERSE_CONTRACT_ADDRESSES[network];
const amountInDecimals = ethers.utils.parseUnits(amount.toString(), token.decimals);
const approveTx = await tokenContract.approve(contractAddress, amountInDecimals);
const approveTx = await tokenContract.approve(contractAddress, amount);
await approveTx.wait(1);
};

Expand Down

0 comments on commit e9ca7d5

Please sign in to comment.