Ethereum Smart Contract Programming


#24

Mapping Assignment for Ethereum Smart Contracts UPDATED

(I’m not sure I’m posting this in the right section so please advise me if it’s out of place.)

The problem that we have created in the initial mapping video is that one address can only be mapped to one dog. Therefore one address cannot own multiple dogs.

At the moment I’m not sure how to correct this error so I’ll be looking forward to one of the next videos. :slight_smile:


#25

Thank you for watching :wink:

When you do multi line strings you need to use a specific quotation mark, namely this one `, while you used this one ’.

I know that toshitimes forum sometimes encodes these quotation marks a bit weird. So copy pasting might not work.


#26

Filip,

Thank you. I never use that particular quotation mark so I couldn’t figure out what I had copied incorrectly.


#27

Exercise to call getBalance function in DogContract from ExternalContract:

DogContract:

pragma solidity 0.5.1;

import “./animal2.sol”;

contract DogContract is AnimalContract{

modifier costs(uint amount) {
    require(msg.value >= amount);
    _;
    // refund the difference when user sends more than the required amount
    if(msg.value > amount) {
      msg.sender.transfer(msg.value-amount);
    }
}

function addDog(string memory _name, uint _age) public payable costs(10000) returns (uint) {
    return _addAnimal(_name, _age, AnimalType.DOG);
}

  function getBalance() public view returns(uint) {
  return address(this).balance;
}

}

ExternalContract:

pragma solidity 0.5.1;

contract DogContract{

function addDog(string memory _name, uint _age) public payable returns (uint);

function getBalance() public view returns (uint);

}

contract ExternalContract {
// Interact with DogContract
//
DogContract dcInstance = DogContract(0x0971B5d216af52c411C9016BBc63665b4E6f2542);

function addExternalDog(string memory _name, uint _age) public payable returns (uint){
    return dcInstance.addDog.value(msg.value)(_name, _age);
}
function externalGetBalance() public view returns (uint) {
    return dcInstance.getBalance(); 
}

}


#28

Does anyone else get an error when you try to replace “string” with “int”?
contract testContract{

string output;

constructor(string memory _output) public{
output = _output;
}

function testOutput() public view returns(string memory){
return output;
}
function setOutput(string memory _output) public{
output = _output;
}
}


#29

If you are talking about having string as the input parameter you also need to remove memory. You don’t need that for integers.


#30

Yeah, I just realized that after messing around with it. I guess I’ll find out why we can’t use memory in the next video.


#31

Wow! i did it in my firt shot… @filip you are a good teacher! YEAH!!!


#32

For mappings assignment is the error not being able to store multiple objects in a address? Also if I skipped directly to the EOS section will the cause me any problems because I didn’t learn the other stuff?


#33

Yes, and that it will get overwritten if you try to add a second one.


#34

Great job Blank! I found the same issue when playing with the code


#35

For the ethereum memory assignment the bug was that the changes were written to memory and not to storage (long term).

the fix ==>

function updateBalance(uint id, uint balance) public {
  users[id].balance = balance;
}