Welcome to the thread about Payable functions & Modifiers in Ethereum. Here you can discuss everything about this chapter.
Hi Filip,
When constructing the modifier, you had no units of value included in the function. How does the function know whether you have sent greater than 100 Wei or 100 Ether? Or are all payables assuming the unit of Wei?
Thanks,
Hello yes I think all numbers are in Wei when writing solidity. All calculations should also be done in wei too to avoid errors. When on the user end people convert to ether for ease of reading. This can be accomplished using web3 object using web3.js.
web3.fromWei(number, unit)
for example
var value = web3.fromWei('21000000000000', 'finney'); console.log(value); // "0.021"
You can also convert to other units using this method.
Do you guys know any reasons why it would be important to use ether within a solidity contract itself? I think it would be safest to keep everything in terms of wei and only convert to ether once on the client side using javascript. I think I read that on the forums or something.
Continuing the discussion.
from the solidity documentation
// Modifiers can be used to change // the body of a function. // If this modifier is used, it will // prepend a check that only passes // if the function is called from // a certain address. modifier onlyBy(address _account) { require( msg.sender == _account, "Sender not authorized." ); // Do not forget the "_;"! It will // be replaced by the actual function // body when the modifier is used. _; }
Yes I think is the correct way to do things, it’s like the satoshi unit in Bitcoin, all transactions are in satoshis and the conversion is performed in the wallet (front end) to deliver it to the user. Tell me if I’m wrong.
Hi Filip, you are talking about exercises but I don’t find them. Could you tell me more about that ? Where can I find excises to train myself ? Thanks.
Hi, sorry about the confusion. I mistakenly called the quizzes for exercises. There are a few coding exercises. Most of them are quizzes though.
Hi Filip
I`m surprised no one has spotted the logic error in this.
I am a little confused with payable bit of the code !
-
The dog gets transferred but the funds are deducted from the owners account !!!
I thought it was my code that was wrong but yours does the same. if you try with 10 ETH you will see the sellers account get deducted by 10 ETH instead of the buyers. -
The buyers account gets a new dog but the account does not have any ETH deducted ! ie the buyer gets a FREE DOGS.
-
The funds disappear outside the scope of the function. I tried putting emitters in the dog contract and the kennel contract but they get show 0 value. How do you access the value outside of the function ?
Please explain how this transaction can be done ?
1/2. Yes, this is the point of the contract. Not that it’s a useful contract, but it’s meant to demonstrate how you use payable functions in solidity. That’s why I didn’t call the function sell or buy. It’s a transfer where the owner pays the kennel for doing the transfer. Not a realistic contract, but an example.
To do a real seller and buyer function would be way more difficult, since I as a seller can’t take funds from the buyers account. You would have to put orders in the contract, freeze funds etc.
- I’m not sure what you mean. The ether sent to the contract will stay in the contract. It’s not saved in the function itself. What do you mean by putting emitters?