Why hasn't this value changed ,use slodity 0.8.6 - solidity

I use hardhat test my contract,contract code like this:
uint256 public investLength;
function A()external{
uint256 id = investLength++;
_a(id);
}
function _a(uint256 _Id) internal returns (address) {
require(_id != 0, 'id zero');
}
when running test,return id zero.This code doesn't seem to be working =>investLength++,this value did not change to 1;

uint256 id = investLength++;
This snippet assigns the current value of investLength (which is 0 by default) to id, and then increments investLength.
If you want to increment the investLength first, and then assign the already incremented value to id, use this expression:
uint256 id = ++investLength;

Related

What is the meaning of comma sequence like ,,,,, in Solidity?

I recently came across the following Solidity function:
function testHealthFactor() public {
(, , , , , uint256 healthFactor) = ILendingPool(lendingPool).getUserAccountData(user);
console.log("health factor", healthFactor);
assertEq(healthFactor < 1 ether, true);
}
I don't know Solidity enought yet, so I wander what is the mining of that sequence of 5 commas?
Solidity allows you to return multiple values within a function. If you don't need these values, you can omit them, and move to the next with the ,.
For example:
function returnStuff() public returns (uint256, uint256) {
return (1, 3);
}
( , uint256 ourNum) = returnStuff();
// ourNum = 3

SmartContract/Solidity: The transaction has been reverted to the initial state

I have the code below inside a contract, whenever I run the function getTimeUntilRewrdClaimable it works until the time is under zero.. If the time is under zero it throws this error...
VM error: revert.
revert
The transaction has been reverted to the initial state.
Note: The called function should be payable if you send value and the value you
send should be less than your current balance.
Debug the transaction to get more information.
My Code inside my contract:
mapping( uint256 => ObjectDetails) private attributes;
// Object Structure...
struct ObjectDetails {
uint dailyClaim;
uint lastClaimDate;
}
function getTimeUntilRewrdClaimable(uint256 tokenId) public view returns (int) {
return int(attributes[tokenId].lastClaimDate + 60 - block.timestamp);
}
Thanks to any and all feedback!
attributes[tokenId].lastClaimDate is a uint, so attributes[tokenId].lastClaimDate + 60 - block.timestamp is as well. This causes an issue when attributes[tokenId].lastClaimDate + 60 is less than block.timestamp. To fix, cast both parts to int before doing the subtraction:
return int(attributes[tokenId].lastClaimDate + 60) - int(block.timestamp);

VM error: revert in solidity while adding a uint8 to an int literal

I have this code:
function somePureFunction() public pure returns(uint256){
uint8 temp = 255;
return 2 + temp;
}
This code gives:
call to SimpleStorage.somePureFunction errored: VM error: revert.
revert
The transaction has been reverted to the initial state.
Note: The called function should be payable if you send value and the value you send should be less than your current balance.
But this works:
function somePureFunction() public pure returns(uint256){
return 2 + 255;
}
In particular your problem refers to value about temp that you give to this variable.
When you declare a variable with datatype uint8 you must to put inside it a value from 0 - 255 (255 excluded). For calculate the range of a specific uint, you can use this statement:
MaximumRange = 2*[numberOfBit]-1
Example:
Issue: I want know what is the range about uint32.
Expression = 2*32-1
Result = 0 - 4294967295
In your specific case, if you change this line:
uint8 temp = 255;
with this:
uint16 temp = 255;
it'll work successfully.
NOTE: You can change current datatype temp variable with other uint datatype like: uint16, uint32, uint64 and others. You must to keep in your mind the range of a single datatype and the value that you want to store inside the variable.

An array field in my Struct is not visible in test, other fields are

This is a struct I created in an interface:
struct Auction {
// ID for the Noun (ERC721 token ID)
uint256 nounId;
// proposals created for this auction
uint256[12] seeds;
// The current highest bid amount
uint256 amount;
// The current highest bid proposalIndex
uint8 winner;
// The time that the auction started
uint256 startTime;
// The time that the auction is scheduled to end
uint256 endTime;
// The address of the current highest bid
address payable bidder;
// Whether or not the auction has been settled
bool settled;
}
in my test, I'm trying to check the length of the seeds and getting a typescript error.
expect(auction.seeds.length).to.be.equal(0);
Error:
test/auction.test.ts:92:20 - error TS2339: Property 'seeds' does not exist on type '[BigNumber, BigNumber, number, BigNumber, BigNumber, string, boolean] & { nounId: BigNumber; amount: BigNumber; ... 4 more ...; settled: boolean; }'.
I added the field winner after I added seed and winner doesn't seem to have a problem in the test. Am I doing something wrong with the array?

How to set msg.value in Remix IDE

This is probably an easy error I'm missing, but I cannot for the life of me figure out how to set the msg.value variable in this contract. I've read online that this value is the amount of wei associated with the transaction, but how do I, as a caller of the contract, specifically set that value. Here's the contract I'm struggling with.
pragma solidity 0.8.7;
contract VendingMachine {
// Declare state variables of the contract
address public owner;
mapping (address => uint) public cupcakeBalances;
// When 'VendingMachine' contract is deployed:
// 1. set the deploying address as the owner of the contract
// 2. set the deployed smart contract's cupcake balance to 100
constructor() {
owner = msg.sender;
cupcakeBalances[address(this)] = 100;
}
// Allow the owner to increase the smart contract's cupcake balance
function refill(uint amount) public {
require(msg.sender == owner, "Only the owner can refill.");
cupcakeBalances[address(this)] += amount;
}
// Allow anyone to purchase cupcakes
function purchase(uint amount) public payable {
require(msg.value >= amount * 1 ether, "You must pay at least 1 ETH per cupcake");
require(cupcakeBalances[address(this)] >= amount, "Not enough cupcakes in stock to complete this purchase");
cupcakeBalances[address(this)] -= amount;
cupcakeBalances[msg.sender] += amount;
}
}
Every time I enter an amount, I'm getting thrown the error that says "You must pay at least 1 ETH per cupcake"
There's nowhere for me to specifically enter in a value for how much I'm going to pay for this, any help would be great
here's what I'm able to input when I deploy the contract on Remix
Top of the Deploy Button you can see the Value Field :
when you want to call the purchase , first fill the value field and select Ether after that calls your function.
I try this way with your code and it works fine.