Is it possible to set a mapping using ethers.js

ethers.jshardhatmappingsolidityunittesting

I'm doing Unit testing and I need to set/add a mapping to my smart contract for some bypassing for a certain test case.

I have a mapping in my smartcontract like so:

mapping(string => uint256) public claimTransactions;

enter image description here

I have no problem reading the smart contract's mapping:

await contract.claimTransactions("2");
returns a BigInt of the integer mapping of the passed string
enter image description here

However, when I need to set a value or a mapping in my unit test file using ethers.js:
enter image description here

I'm trying to mimic this line of code from my smartcontract (solidity)
claimTransactions[txId] = amount;

enter image description here

I need help if this is possible, or any reference that I can check on would be very helpful.

Best Answer

When you defined the claimTransactions mapping, solidity automatically created a getter method for it, which you accessed by doing claimTransactions(string). However, solidity does not automatically create a setter method for it, which is required to change the state.

You would have to define your own setter method and then call it in your javascript

function modifyTransactions(string memory txId, uint256 amount) public {
    //probably want onlyOwner or define some other logic to determine who can change the state
    claimTransactions[txId] = amount;
}

then in your javascript:

await contract.modifyTransactions("2", 1);
Related Topic