CFP
Contributing Funds

Contributing Funds

Contributions are allowed as long as the following conditions are met:

  1. The campaign has started
  2. The campaign has not ended
  3. The campaign max goal is not met
  4. The contribution amount doesn't violate the min and max contribution amounts for the contributing account

Contributing ETH

Contributions are made to ETH denominated contracts by invoking the contributeEth() function with msg.value set to the amount of wei.

import { Contract, Signer } from 'ethers';
 
const abi = require('path/to/CrowdFinancingV1.abi.json');
const contribute = async (campaignAddress: string, signer: Signer, amount: bigint) : Promise<any> => {
  const contract = new Contract(campaignAddress, abi, signer);
  return contract.contributeEth({
    gasLimit: 200_000,
    value: amount,
  });
}

Contributing ERC-20 Tokens

Contributions are made to ERC-20 denominated contracts by invoking the contributeERC20(amount) function. This first requires that the campaign contract has an allowance greater than or equal to the amount specified in the call.

If the contracts allowance for the ERC-20 token is less than the amount specified, then the transaction will revert.

import { Contract, Signer } from 'ethers';
 
const abi = require('path/to/CrowdFinancingV1.abi.json');
const contribute = async (campaignAddress: string, signer: Signer, amount: bigint) : Promise<any> => {
  const erc20 = new Contract(erc20ContractAddress, erc20Abi, signer);
  const txn = await erc20.approve(campaignAddress, amount);
 
  await pollForReceipt(txn.hash);
 
  const contract = new Contract(campaignAddress, abi, signer);
  return contract.contributeERC20(amount, {
    gasLimit: 200_000,
  });
}

Preflight Checks

Before contributing funds to the contract, it's best to check if contributions are allowed and to check the min and max contribution amounts for a given account.

⚠️

If an account attempts to contribute a value outside the range, the transaction will revert

async function isContributionValid(contract: Contract, account: string, amount: bigint) : Promise<boolean> {
  const allowed = await contract.isContributionAllowed();
  const { 0: low, 1: high } = await contract.contributionRangeFor(account);
  return allowed && amount >= low && amount <= high;
}