Hi @filip,
Should the withdraw function also be payable? So the contract would send out ether as opposed to just adjusting the sender’s balance?
Thanks!
Hi @filip,
Should the withdraw function also be payable? So the contract would send out ether as opposed to just adjusting the sender’s balance?
Thanks!
Hi @Jose_Hernandez, hope you are great.
Payable functions provide a mechanism to collect / receive funds in ethers to your contract .
Meaning the keyword payable
should only be used when a function should receive funds.
If you have any more questions, please let us know so we can help you!
Carlos Z.
Thank you very much!
Am I correct in understanding that using modifiers is (instead of, for example, using a function that would accomplish the same output goal) a way to reduce overall gas fee costs per contract?
Indeed it is, modifiers are merely a way to reuse conditions, so code redundancy is optimized, that way you are also reducing the overall gas fees of your functions that requires conditionals that can be just reused with a modifier.
Carlos Z
Does anyone have an example of an actual smart contract that I can look at?
With the Payable Function - “function deposit() public payable returns (uint)” -
does this really need to be written -" function deposit() external payable returns (uint)" - replacing public with external?
I am reading this needs to be the case from soliditylang.org which is quoting v0.6.2.
Hi @Mrbryce , hope you are great.
Payable functions provide a mechanism to collect / receive funds in ethers to your contract .
Meaning the keyword payable
should only be used when a function should receive funds.
External functions are part of the contract interface, which means they can be called from other contracts and via transactions. An external function f
cannot be called internally (i.e. f()
does not work, but this.f()
works). External functions are sometimes more efficient when they receive large arrays of data, because the data is not copied from calldata to memory.
https://docs.soliditylang.org/en/v0.8.1/contracts.html#visibility-and-getters
If you have any more questions, please let us know so we can help you!
Carlos Z.
Thanks for the Carlos. Do you have any examples of a smart contract as this would be helpful in trying to understand what it is I am trying to build? Is the Metamask wallet or Uniswap examples of smart contracts. Thanks
Something to note. I had to backtrack from solidity 0.8.1 -> 0.7.5 to follow this section. 0.8.1 was complaining that msg.sender.transfer() was invalid due to the type not being a payable address.
Had the same issue, thanks for posting this!
Payable Assignment Solution
pragma solidity 0.5.12;
contract HelloWorld{
struct Person {
uint id;
string name;
uint age;
uint height;
bool senior;
}
event personCreated(string name, bool senior);
event personDeleted(string name, bool senior, address deletedBy);
uint256 public contractBalance;
address public owner;
modifier onlyOwner(){
require(msg.sender == owner);
_; //Continue execution
}
constructor() public{
owner = msg.sender;
}
mapping (address => Person) private people;
address[] private creators;
function createPerson(string memory name, uint age, uint height) public payable{
require(age < 150, "Age needs to be below 150");
require(msg.value > 1 ether);
contractBalance += msg.value;
//This creates a person
Person memory newPerson;
newPerson.name = name;
newPerson.age = age;
newPerson.height = height;
if(age >= 65){
newPerson.senior = true;
}
else{
newPerson.senior = false;
}
insertPerson(newPerson);
creators.push(msg.sender);
assert(
keccak256(
abi.encodePacked(
people[msg.sender].name,
people[msg.sender].age,
people[msg.sender].height,
people[msg.sender].senior
)
)
==
keccak256(
abi.encodePacked(
newPerson.name,
newPerson.age,
newPerson.height,
newPerson.senior
)
)
);
emit personCreated(newPerson.name, newPerson.senior);
}
function insertPerson(Person memory newPerson) private {
address creator = msg.sender;
people[creator] = newPerson;
}
function getPerson() public view returns(string memory name, uint age, uint height, bool senior){
address creator = msg.sender;
return (people[creator].name, people[creator].age, people[creator].height, people[creator].senior);
}
function deletePerson(address creator) public onlyOwner {
string memory name = people[creator].name;
bool senior = people[creator].senior;
delete people[creator];
assert(people[creator].age == 0);
emit personDeleted(name, senior, owner);
}
function getCreator(uint index) public view onlyOwner returns(address){
return creators[index];
}
}
}
need some help here. I keep getting an error on line 26 saying ‘send and transfer are only available for address payable’ however i don’t recall how to define an address as payable. i assumed from the video that msg.sender should be automatically payable
pragma solidity 0.8.1;
contract goinBack{
mapping (address => uint) balance;
address owner;
event depositDone(uint amoubt, address indexed depositedTo);
modifier onlyOwner {
require (msg.sender == owner, “Only the owner can add and send money”);
_;
}
constructor(){
owner = msg.sender ;
}
function deposit() public payable returns(uint){
balance [msg.sender] += msg.value;
emit depositDone (msg.value, msg.sender);
return balance [msg.sender];
}
function withdraw(uint amount) public returns (uint){
msg.sender.transfer (amount);
}
function getBalance ( )public view returns(uint){
return balance[msg.sender];
}
function transfer (address recipient, uint amount) public{
require(balance [msg.sender]>=amount, “you broke bro”);
require(msg.sender!= recipient, “dont send money to yourself”);
uint previousSenderBalance = balance[msg.sender]; // this variable was made just to assert that the balance is acurrate
_transfer(msg.sender, recipient, amount);
assert(balance[msg.sender]== previousSenderBalance - amount); // assert is used to check that the balance is what it should be after the ftransfer
}
function _transfer (address from, address to, uint amount) onlyOwner private {
balance [from]-= amount;
balance [to] += amount;
}
}
Hey @Moneymonty
In the newer versions of Solidity you have to cast msg.sender
to payable.
payable(msg.sender).transfer(amount);
Cheers,
Dani
I’m getting an error message that I don’t understand. I’m following Filip’s steps one by one and can not figure out why I get this error and not Filip. Can someone help me out?
function withdraw(uint amount) public returns (uint){
msg.sender.transfer(amount);
}
Hi @thomascarl
From solidity 0.8 or > msg.sender is not considered payable anymore. You have to cast it as payable as suggested by the error message.
payable(msg.sender).transfer etc…
Cheers,
Dani
Hey @filip ,
I have a question. In the video “Transfer” you use the transfer() function in the withdraw() function and refer to it as a member of msg.sender.
My question: is the transfer() function a built-in function to solidity, similar to push() and pop() for JavaScript, or are you referring to the transfer() function that was created on earlier in the course?
Thanks in advance
Emmerich
Thanks again Dani. You are really helping me out.
Hey Dani,
Thx for the info. Appreciate the help.
Regards
Em