Share:
Notifications
Clear all

Earn While Learning Web3 Programming>> Start Here FREE!!!

[Solved] Solidity Functions By Payment Transfer Capability

1 Posts
1 Users
2 Reactions
606 Views
1
Topic starter

EXERCISE:

Write Event and Emit for "Payable" Functions:
i. See the the Smart Contract Code used in this course video in the forum answer below

ii. Include and Emit Event for Deposit (depositEther)

iii. Include and Emit Event for Withdraw (withdrawEther)

iv. Use solidity 0.8.7 instead of 0.7.5 used in the video (and then try your best to solve the withdraw address not payable error we got earlier in this video)

v. Post your code in this forum:

(NOTE: please, don't go and copy paste from other students in the forum, try things on your own, so you can master it yourself)

 

 

 

NOTE:

This Is An Exercise From Our FREE Smart Contract Development With Solidity For Beginners Course (If you have not enrolled yet, you can join the course totally for FREE).

 

1 Answer
1
Topic starter

Functions in solidity by Payment/Fund Transfer capability allows you to program your smart contract to send out fund to a wallet(s).

You must make the wallet to receive payment from the smart contract "payable" wallet before the the transfer function can be executed successfully.

 

Payable transfer function is written in solidity as:

mgs.sender.transfer(amount To Transfer From Smart Contract To Payable Wallet Address);

 

//note that mgs.sender.transfer has default error handling pre-built in it to ensure it throws error when neccessary and revert the transaction.

 

Want to know how to make your smart contract receive funds in solidity? Then checkout this Payable Functions in Solidity

 

 

 

Example Payable Function Source Code Used in THE FREE Course VIDEO ABOVE You Should Copy and Use To Do the above Exercise

 

pragma solidity 0.7.5;

contract TransferWithdrawPayableFunction {

/*
SC SUMMARY: To learn about Payable Transfer/Withdraw Function in solidity.

This SC code contains both Payable Deposit and Transfer Functions.
*/

//mapping balance
mapping (address => uint) Balance;

//track SC all users total Deposited Balance
uint totalDeposited;

//PAYABLE FUNCTION - Deposited
function depositEther() public payable {

//track deposit to mapping
Balance[msg.sender] += msg.value;

//add to total SC deposited Balance
totalDeposited += msg.value;

}

//Check Balances
function etherBalance() public view returns(uint, uint) {

return (Balance[msg.sender], totalDeposited);

}

//PAYABLE FUNCTION - Transfer/Withdraw
function withdrawEther(uint _ethAmount) public {

//SC Transfers to user wallet
msg.sender.transfer(_ethAmount);

//withdrawEther
Balance[msg.sender] -= _ethAmount;

//totaDepositWithdraw
totalDeposited -= _ethAmount;

}

/*
ESSENTIAL SOLIDITY SMART CONTRACT FUNCTIONS - Part 2
Intro To Functions In Solidity Part 3 - Payment Deposit Capability
3- Functions By Payment Capability:
1. Payable - Deposit

Intro To Functions In Solidity Part 4 - Payment Transfer/Withdraw Capability
4- Functions By Payment Capability:
2. Payable - Transfer/Withdraw (msg.sender.transfer has built in error handling by default)

*/

}

 

Share: