I'm studing Solidity and tring to understand how it works.
Starting from Safe Remote Purchase in the "Solidity section by example" the doc (https://docs.soliditylang.org/en/v0.8.12/solidity-by-example.html#safe-remote-purchase) I tried to use the code as in an object-oriented language. I would like to understand what is the best way to write the following code.
In particular, I create two different contract: singlePuchase and Puchase.
This is singlePuchase:
contract singlePuchase{
string nome;
uint importo;
Stages stage;
address payable venditore;
enum Stages{
MessaVendita,
AccettaCliente,
ProdottoInviato,
VenditaConclusa
}
// si impostano le variabili base quali: nome del prodotto, importo, lo stato iniziale (MessaVendita), chi è il venditore
constructor(string memory prod, uint i) payable{
require(i*2000000000000000000==msg.value, string(abi.encodePacked("importo non valido: ", Strings.toString(msg.value)," e ", Strings.toString(i*2000000000000000000))));
nome = prod;
importo = i*1000000000000000000;
stage = Stages.MessaVendita;
venditore = payable(msg.sender);
}
\\ other function
}
Puchase wants to manage more "singlePuchase elements" and contains an array of singlePuchase instance. Here the code: :
contract Puchase{
singlePuchase[] prodotti;
mapping(string => uint) mapPosizione;
address creatore;
//definiamo del creatore e garante
constructor(){
creatore = msg.sender;
prodotti.push(new singlePuchase(string(""),0));
}
function propostaVendita(string memory prod, uint i) public payable{
if(mapPosizione[prod] == 0){
require(msg.sender!=creatore);
prodotti.push(new singlePuchase(prod,i));
} else {
prodotti[mapPosizione[prod]].modificaVendita(i);
}
}
// other function
}
When I call the function propostaVendita
the line prodotti.push(new singlePuchase(prod,i))
return an error because the contract Puchase call the singlePuchase constractor with different value of msg.value and msg.sender.
I tried to resolve the problem with delegatecall
, the singlePuchase constructor in Puchase return: (true, 0x0)
.
How can I resolve the ploblem? How is possible use msg.value and msg.sender from the original contract (Puchase) to the called contract (singlePuchase)?