I am quite new to solidity and I have the following situation: I have this struct
struct Item {
uint sku;
uint upc;
address ownerID;
address originProducerID;
string originProducerName;
string originProducerInformation;
uint productId;
string productNotes;
uint productPrice;
State itemState;
address payable consumerID;
}
In the last one (consumerID) I had to make it payable for this (I need to use transfer):
modifier checkValue(uint _upc) {
_;
uint _price = items[_upc].productPrice;
uint amountToReturn = msg.value - _price;
items[_upc].consumerID.transfer(amountToReturn);
}
The problem is when I try to create an item, I need to set that value to what it would be null or empty.
function collectMaterials(address _originProducerId, string memory _originProducerName, string memory _originProducerInformation, uint _productId, string memory _productNotes, uint _productPrice) public
{
items[sku] = Item(
{
sku: sku,
upc: upc,
ownerID: msg.sender,
originProducerID: msg.sender,
originProducerName: _originProducerName,
originProducerInformation: _originProducerInformation,
productId: sku + upc,
productNotes: _productNotes,
productPrice: _productPrice,
itemState: State.MaterialSelection,
consumerID: address(0)
});
But I get this error
Invalid type for argument in function call. Invalid implicit conversion from address to address payable requested
How can I initialise that value to empty or null? Or is there a way to make it optional to add?
Thanks!
consumerID
is of type address payable
(an extension of the type address
).
However, in the collectMaterials()
function, you're passing type address
as the value of consumerID
.
Solution: Typecast the value 0
to address payable
consumerID: payable(address(0))