This is my first question here and I’ll try to keep it specific.
I have a “Baker” contract that has a function that makes a new “Cookie” contract. I deploy it in Remix and have problems interacting with the new Cookie contracts that are made.
Expected behavior:
- Use bakeNewCookie function to create a new contract and pass it’s state variable “CookieName” a string so that I can identify it later.
- When i “bakeNewCookie” the new Cookie contract has a constructor that defines it’s state variable “CookieAddress” with address(this) (I want to access each Cookie contract via the contract address).
- Then I want to use the “myCookieAddress” function which takes the address from one of the new Cookie contracts and gives me the “CookieName” variable of said Cookie contract.
Actual behavior:
- After calling “bakeNewCookie” I don’t see the new contract I created.
Question:
Do I actually create new Cookie contracts on the blockchain with this code? In the future I want users on a website to access the Cookies they have created and display them on a website.
Right now I’m trying to use the new contract address as the identifier to interact with them. Is there a better way to do it? I need a unique ID for each cookie that is tied to its information. Ideally I’d like to create a mapping of the new contracts but that doesn’t seem to be possible.
Why doesn’t the “myCookieAddress” function work as expected?
Thanks so much in advance for the help!
pragma solidity ^0.5.1;
contract Cookie {
address public CookieAddress;
string public CookieName;
constructor() public {
CookieAddress = address(this);
}
function theCookieInfo() view public returns(address, string memory) {
return (CookieAddress, CookieName);
}
}
contract Baker is Cookie {
address public BakerAddress;
address[] public CookieAddresses;
constructor() public {
BakerAddress = address(this);
}
function bakeNewCookie(string memory _name) public returns(bool) {
new Cookie();
Cookie.CookieName = _name;
CookieAddresses.push(CookieAddress);
return true;
}
function getCookieArray() view public returns(address[] memory) {
return CookieAddresses;
}
function myCookieAddress(address _addressOfCookie) view public returns(string memory) {
Cookie my_cookie = Cookie(_addressOfCookie);
return my_cookie.CookieName();
}
}
Cookie.CookieName = _name
. I suggest that you simply add the name to the constructor ofCookie
. – goodvibration Apr 16 '19 at 8:50Cookie
, will I be able to define it upon contract initialization/creation? The idea is to be able to identify these new Cookie contracts with names chosen upon creation. – AndreG Apr 16 '19 at 18:42