0

I tried to update the current campaign based on Id, target value and a new date, but getting an error when executing the function

function from contract

   function updateCampaign(
        uint _id,
        uint _newTarget,
        uint _newDeadline
    ) external {
        Campaign storage campaign = s_campaigns[_id];

        if (campaign.owner != msg.sender) revert CrowdFund__NotOwner();
        if (campaign.status != CampaignStatus.REVERTED)
            revert CrowdFund__Required();
        if (block.timestamp > campaign.deadline) revert CrowdFund__Deadline();

        campaign.target = _newTarget;
        campaign.deadline = _newDeadline;

        emit UpdatedCampaign(_id, _newTarget, _newDeadline);
    }

from my react context

type updateForm = {
  pId: number;
  target: ethers.BigNumber;
  deadline: Date | string | number;
};

  const updateCampaign = useCallback(
    async (updateForm: updateForm) => {
      const signer = accountProvider?.getSigner();
      const contractWithSigner = contract?.connect(signer);

      try {
        const updateId = await contractWithSigner?.updateCampaign(
          updateForm.pId,
          updateForm.target,
          updateForm.deadline
        );
        return updateId;
      } catch (error) {
        console.error(error);
      }
    },
    [contract, accountProvider]
  );

from my react page

interface FormProps {
  pId: number;
  target: string;
  deadline: Date;
}

type ExtendCampaignModalProps = {
  pId: number;
  target: number;
  deadline: string;
  closeModal: () => void;
};

const ExtendCampaignModal = ({
  pId,
  target,
  deadline,
  closeModal,
}: ExtendCampaignModalProps) => {
  const { updateCampaign } = useCrowdFundData();
  const { account } = useAccount();
  const [isLoading, setIsLoading] = useState(false);
  const [form, setForm] = useState<FormProps>({
    pId: pId,
    target: "",
    deadline: new Date(),
  });
  const handleFormFieldChange = (
    fieldName: string,
    e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>
  ) => {
    setForm({
      ...form,
      [fieldName]: e.target.value,
    });
  };

  const handleExtendCampaign = async () => {
    if (!account) return;
    setIsLoading(true);
    try {
      await updateCampaign({
        pId: form.pId,
        target: form.target,
        deadline: new Date(form.deadline),
      });
      closeModal();
    } catch (error) {
      console.error(error);
    } finally {
      setIsLoading(false);
    }

return (
  <div>
     <FormField
      labelName="New End Date *"
      placeholder="End Date"
      inputType="date"
       value={form.deadline}
       handleChange={e => {
       handleFormFieldChange("deadline", e);
        }}
      />
      </div>
      <div className="flex flex-wrap w-1/2 justify-between mt-5 gap-[10px]">
        <CustomButton
         btnType="button"
         title="Extend My Campaign"
         styles="w-full bg-[#374151] hover:bg-[#4acd8d] transition-all ease-out duration-300 mb-2"
         handleClick={handleExtendCampaign}
        />
  </div>
)

The error log is

Error: invalid BigNumber value (argument="value", value="2023-05-25T00:00:00.000Z", code=INVALID_ARGUMENT, version=bignumber/5.7.0)

1 Answer 1

0

Your deadline is of type Date but it should be converted to timestamp before sending the transaction.

I think you can do it here

        const updateId = await contractWithSigner?.updateCampaign(
          updateForm.pId,
          updateForm.target,
          updateForm.deadline.getTime()
        );

.getTime() shouls convert it to a timestamp number.

1
  • Thank you ser.. can see it working now :)
    – 0xhaz
    May 29, 2023 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.