Complete the code to specify the data storage location in a smart contract.
contract Storage {
string public data;
function setData(string memory [1]) public {
data = [1];
}
}The parameter name value is commonly used to represent the input data to be stored, which is important for clarity in smart contracts.
Complete the code to calculate gas cost based on data location.
function calculateCost(uint256 [1]) public pure returns (uint256) { uint256 cost = [1] * 2000; return cost; }
The variable size represents the amount of data, which affects gas cost.
Fix the error in the code that calculates cost for storing data in different locations.
function getCost(string memory [1]) public pure returns (uint256) { if (keccak256(bytes([1])) == keccak256(bytes("storage"))) { return 5000; } else { return 1000; } }
The parameter dataLocation clearly indicates the location of data, matching the comparison inside the function.
Fill both blanks to create a dictionary that maps data locations to their gas costs.
mapping(string => uint256) public gasCosts = {
"storage": [1],
"memory": [2]
};Storage costs more gas (5000) than memory (1000) because storing data permanently is more expensive.
Fill all three blanks to complete the function that returns gas cost based on data location and size.
function computeGasCost(string memory [1], uint256 [2]) public pure returns (uint256) { uint256 baseCost = (keccak256(bytes([1])) == keccak256(bytes("storage"))) ? [3] : 1000; return baseCost * [2]; }
The function uses dataLocation to check where data is stored, dataSize for the amount of data, and assigns a base cost of 5000 gas for storage.