Ethereum Smart Contract Programming

Welcome to the discussion thread for the Ethereum Smart Contract Programming section of the course. Feel free to ask all questions here. Good luck!

1 Like

Here is another solution:

     User storage user = users[id];
     user.balance = balance;

For the " Data Location Assignment Solution" section.

3 Likes

hello, what happened to the section “Ethereum Smart Contracts” section? I completed everything but now it says that I did not complete a lot of vids. Do I have to do them again ?
Is there any overview of what is updated ?

If I go through the videos again, I notice that the links to the forumtopics are not correct. They all link to this topic.

1 Like

I have updated the ETH smart contract section so that the videos now cover Solidity 0.5.1, the latest version. There is also more in-depth content now. So if you want to learn even more Solidity, I would suggest that you check those out. We always strive to have up-to-date content.

This is now the discussion thread for that section. We have consolidated them. Individual assignments will still have individual threads. But all threads are still here on the forum. Nothing has been deleted. I’m sorry if this caused confusion.

6 Likes

@filip In the dog example I noticed that deploying the contract and then using the setter method was cheaper, with regards to gas, than using a constructor. Is that typical? Does this affect contract design? It was about 4% cheaper to use the setter method to set the same string value.

I couldn’t replicate that. It cost me more to deploy and use the setter function than to use the constructor. Both regarding transaction and execution cost. I’m not sure why there is a small difference.

1 Like

Ok, weird. I probably didn’t read it correctly. I will double check it again now that I have tinkered with Remix more as of late.

1 Like

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 {
DogContract dcInstance = DogContract(0x5E72914535f202659083Db3a02C984188Fa26e9f);

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

    
function getExternalBalance() public view returns(uint) {
   return dcInstance.getBalance(); 
}

}

addDog function is not working. What should I do?

What do you mean with not working?

I am getting the follwing error

Hi @filip, I have a question

  1. This is the code to set up the contract owner variable.

address owner;

constructor () public {
owner = msg.sender;
}

Though I don’t see the point in the constructor. Why not just put in the main body of the code.

address owner = msg.sender;

I need to see your code. Please post it here.

Becuase msg.sender is only available inside functions. It contains the sender of the function call, so it’s not generally available in the contract.

1 Like

Code of dog contract

pragma solidity ^0.5.1;

import “./Animal.sol”;

contract DogContract is AnimalContract {

modifier costs(uint amount) {
    require(msg.value >= amount);
    _;
    if(msg.value > amount) {
        msg.sender.send(msg.value - amount);
    }
}


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

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

}

Code of Dog interface

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 {
DogContract dcInstance = DogContract(0x692a70D2e424a56D2C6C27aA97D1a86395877b3A);

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

    
function getExternalBalance() public view returns(uint) {
   return dcInstance.getBalance(); 
}

}

The onlyOwner modifier should not be there on addDog. If you remove onlyOwner from addDog it will probably work. Try that and let me know if it works.

1 Like

It worked.
Thank you :grinning:

1 Like

Hi @filip

I have just completed the payable function section. The code works fine and is the same as yours however the compiler shows a warning that “transfer” should be used instead of “send”.

modifier costs(uint amount) { // a modifier to ensure required amount of ether has been received
require(msg.value >= amount);
_;
if(msg.value > amount) { //checks if value of ether sent is great than the required ammount
msg.sender.send(msg.value - amount); // refunds the ammount over what is required (send/transfer?)
}
}

Is this warning anything to consider?

1 Like

External contract code to get balance.

function getBalance() public view returns (uint) {
return dcInstance.getBalance();
}

1 Like

Good question. I use send just out of old habits. It’s not a fatal thing, you can use both. However, for most people the transfer function is the best one to use. That’s because transfer() throws an error on failure while send() just returns false.

So you generally use send() when you want to handle the errors yourself. Transfer() handles that for you by throwing an error that will stop the execution.

I will look into this and perhaps change the code in the lecture.

2 Likes

Filip In addition to your progamming class on teachable I have been following along with your truffle tutorial on youtube to get more background on ethereum dapps and when I try to duplicate what you were doing in 3_deploy_crowdsale.js in How to Build Truffle.js Migrations - Truffle Tutorial Part 5 I could not get your console.log statement to compile:

console.log(’======================
Deployed Contracts:
Token: ${Token.address}

Crowdsale: ${Crowdsale.address}

========================

Balances:
Crowdsale => ${await tokenInstance.balanceOf(Crowdsale.address)} tokens
Owner => ${await tokenInstance.balanceOf(owner)} tokens
========================’);

The error i get back is:

3_deploy_crowdsale.js:37
console.log(’=======================================
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

SyntaxError: Invalid or unexpected token

I’ve tried various changes to get this to work but to no avail. It is an exact copy of your example in the video - could you let me know where the problem is?

Thank you