i am trying to execute function that changing the state of the contract, currently i am testing it on my localhost hardhat node.
this is my function (in my contract)
address owner;
string[] public deployedCampaignsUuid;
mapping(string => Campaign) campaigns;
struct Campaign {
string uuid;
address owner;
string campaignName;
string campaignDescription;
address[] contributorsKeys;
uint256 endDate;
uint256 goalAmount;
uint256 totalContributions;
string campaignType;
mapping(address => uint256) contributors;
}
function createCampaign(
string memory uuid,
string memory campaignName,
string memory campaignDescription,
uint256 endDate,
uint256 goalAmount,
string memory campaignType
) public {
require(bytes(uuid).length > 0, "UUID is required");
require(bytes(campaigns[uuid].uuid).length == 0, "This campaign UUID already exists");
campaigns[uuid].uuid = uuid;
campaigns[uuid].owner = msg.sender;
campaigns[uuid].campaignName = campaignName;
campaigns[uuid].campaignDescription = campaignDescription;
campaigns[uuid].endDate = endDate;
campaigns[uuid].goalAmount = goalAmount;
campaigns[uuid].totalContributions = 0;
campaigns[uuid].campaignType = campaignType;
deployedCampaignsUuid.push(uuid);
delete campaigns[uuid].contributorsKeys;
emit CampaignCreated(uuid, msg.sender);
}
When i am executing the function from the javascript i don't see any error and in fact i am seeing information on the hardhat localhost.
but after so when i am trying to retreive the new infromation that was stored by the UUID i am not getting any result, the state of the contract didn't change.
i am invoking from my Typescript this function
export const getCampaignFactoryContract = async () => {
try {
const provider = await new ethers.JsonRpcProvider(process.env.BLOCKCHAIN_URL);
const signer = await provider.getSigner()
const abi = await generateABI();
const campaignFactoryContract = await new ethers.Contract(CONTRACT_ADDRESS, abi, signer);
campaignFactoryContract.on("CampaignCreated", async (uuid: string, owner: string) => {
console.log("New campaign created with the following parameters");
console.log("Owner Wallet Address: ", owner);
console.log("Campaign UUID: ", uuid);
});
campaignFactoryContract.on("Contribution", async (uuid: string, owner: string, amount: bigint, time:number) => {
console.log("New Contribution");
console.log("Contiruber: ", owner);
console.log("Campaign UUID: ", uuid);
console.log("Amount: ", amount);
console.log("Time: ", time);
});
return campaignFactoryContract;
} catch (e) {
console.log("Failed to get blockchain factory contract ", e);
return null;
}
}
export const requestBlockchainForNewCampaign = async (
uuid: string,
campaignName: string,
description: string,
endDate: number,
goalAmountInWei: bigint,
campaignType: string,
) => {
try {
const service = await getCampaignFactoryContract();
const providers = await getProviders()
if (service && providers) {
const signer = await providers?.getSigner()
const unsignedTx = await service['createCampaign'].populateTransaction(
uuid,
campaignName,
description,
endDate,
goalAmountInWei,
campaignType,
);
const signedTx = await signer.signTransaction(unsignedTx);
console.log("Transaction hash:", signedTx);
return true
} else {
console.log("Failed to obtain campaign factory contract.");
return false
}
} catch (error) {
console.log("Failed to request creation of new campaign ", error);
return false
}
}
i am using Ether js. i don't understand how i can sign those transaction to change the state of the contract.
also i am using Metamask as my wallet, user can "connect" their metamask to my site