Ethereum Smart Contract Programming

I just had to use the faucet on Rinkeby today for my work and I realised that you need to post your recently created address on Twitter so they can send you some ETH.

Once that done, you have to prove to Rinkeby that your tweet was published. It doesn’t take too long to get your first few ETHs to Metamask from them.

Apparently they are doing this to avoid SPAM on Rinkeby.

1 Like

Twitter / Rinkeby Faucet Integration

Try this and it should work.

1 Like

Thanks a lot that works very well with Twitter!

1 Like

It seems like that in the Data location assignment there are two possible solutions . The first solution, where the flow of data is is clear to me (as in the video) :

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

Another solution that works (changing the updateBalance function from memory to storage):

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

**However, it is not clear to me how the second solution works because the following function in the code (get balance function), refers to the users[id].balance and not to the **user.balance ** (isn’t it a different memory location?).

your advice …

Correct. Both will work fine.

The reason the second solution works as well is because when we create the user variable, it’s actually a reference to the user in the mapping (in storage). And it’s the same structure that we access in the getBalance function when we call users[id].balance. They both point to the mapping in storage.

So, in solidity the basic mode is like “pass value by reference” (in c++)?

thanks:)

No, only when you reference storage variables. So if you add the storage keyword it will be a reference. Otherwise, as you saw in the example in the video, it will just copy the variable.

External Contracts and Interfaces - two solutions (which one is better?):

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 dcIns = DogContract(0xa5BbdBA112e7c9B0a5FeDff7cb70643fbBd4fBE5);

    function addExternalDog(string memory _name, uint _age) public payable returns (uint){
        return dcIns.addDog.value(msg.value)(_name, _age);
    }
    
    **function getExternalBalance () public view returns (uint){**
**        return address(dcIns).balance;**
 /*  
  or also: return dcIns.getBalance()**;*/
```        }
       }

-Which one is better (if any)?
-How can we get the balance of the sender (negative one?) in addition to the owner.
Thanks.
  1. Not any big difference between the different address calls. I would think that the address(dcIns).balance would be a cheaper call to make, but not sure. The difference is that if you do dcIns.getBalance you query the contract for the balance variable in the contract. While if you do address(dcIns).balance you are actually asking the “blockchain” for the balance of that address.

  2. msg.sender.balance will give you the balance of the sender. If you have the owner saved you can just do owner.balance.

Thanks, but I think I missed something (hope only one…)
Are the "dog “contract and the “external dog” contract have the same address (and this is why both should have the same “wey” balance and number of dogs)?
so, it is still not an example of a “full transaction” - involving a seller of dogs (earn " wey”) and a buyer (gets dogs and pay “wey” and therefore his wey balance should be reduced)?
Thanks again…

Yes, the real balance will be the same. But since of them query a variable inside the contract, it might not be correct if you have a bug.

If I have a contract with a balance variable, and I set it to 9 millions ETH. I can query it with getBalance, but it won’t tell me the actual balance that’s on the blockchain. You see the difference?

What am I not seeing? Can you help please?

Filip I figured it out after taking a break at the gym. Thanks. Next time I’ll be sure to take a break and figure things out before posting. lol

Great job finding the issue! Sometimes a break is all you need to get a fresh view on it.

1 Like

It’s the same like last time, just copy and paste.

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

Good day Filip,
There wasn’t a link to MetaMask, or Faucet.MetaMask.io in the Testnet Deployment video. Just giving you a heads up

1 Like

Yes. got it. Thanks.

1 Like

Thanks, will add it.

I thinking about this owner modifier. The constructor is saving the address of the owner which is taken from the wallet (we can use our own or customer/employer), and then after deploy nobody will be able to change the value of the owner variable in the constructor, won’t they? That would mean if I as a owner will lose the account with the ownership address or I would die, without leaving my keys somewhere, nobody will be able to do modify the contract?

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

And just to confirm, code below means: // ownerToAnimals[msg.sender] is pointing which array of animals to use what is defined by msg.sender, then which animal to return by id, and the last returning string which the name of the animal; is my reasoning right?

return ownerToAnimals[msg.sender][_id].name; 

I have tried to return whole array, but it doesn’t work, it seems that I need to use some expansions to run it

function getArrayOfAnimals() public returns (Animal[] memory) {
      return ownerToAnimals[msg.sender];
    }
   

browser/Animal.sol:38:50: TypeError: This type is only supported in the new experimental ABI encoder. Use “pragma experimental ABIEncoderV2;” to enable the feature.
function getArrayOfAnimals() public returns (Animal[] memory) {
^-------------^

I have used “pragma experimental ABIEncoderV2;” below the “pragma solidity 0.5.1;” and the output was like this (instead AnimalType I got uint8, I don’t know why):

{
	"0": "tuple(string,uint256,uint8)[]: Mela,2,0,Zula,3,0,Kora,5,0"
}

Hi I’m absolutely gutted. I finished the Blockchain fundamentals course which I really enjoyed. I’ve now started the smart programming course. I was really enjoying this course as well until I got to the Solidity training. I find Ivan to be a great teacher he takes his time and explains everything. However this new guy Fillip who has taken over ruses through and hardly explains anything. My understanding from Ivan’s advertisements was this course teaches you to programme from scratch, however the way this guy explains things is certainly not for a beginner in my opinion. No offence he seems like a nice guy but not as good as Ivan as a tutor. I’m really disappointed because i’ve put a huge amount of time into this and really want to learn blockchain programming.