Support Author & Organization
In the Pointer.gg Forum example, you only pay the gas fee when adding a comment. In this example I explore different ways you can support author of the post or the organization(Contract).
Making contract functions "Payable"
If you make a contract function payable, When someone calls that function, they can send some ethers with it. You can either accept any amount they are sending or revert if the amount does not meet a certain condigion. Here is a basic example.
//SPDX-License-Identifier: Unlicensepragma solidity ^0.8.0;contract Bar {uint256 beerPrice = 0.0001 ether;error InsufficientBalance();function buyBeers(uint16 totalBeers)externalpayable{// calculate the total priceuint256 totalPrice = beerPrice * totalBeers;// check if the transaction amount user sent meets the total price// revert if notif (totalPrice > msg.value) revert InsufficientBalance();// ...rest of the function}}
How to withdraw ethers from a contract
When someone sends ethers to a payable method that will be stored in the contract‘s wallet address. You can use [address].transfer function in solidity to transfer the balance from the wallet to any address you choose. Here is an example where the creator of the contract can withdaw contract balance.
//SPDX-License-Identifier: Unlicensepragma solidity ^0.8.0;contract Test {address payable public owner;uint public contractBalance;constructor() payable {owner = payable(msg.sender);}error NotOwner();// some payable functions that will update the contract balance.function withdraw() public payable returns (bool) {if(msg.sender != owner) revert NotOwner();if (contractBalance > 0) {contractBalance = 0;owner.transfer(msg.value);return true;} else {return false;}}}
If you allow multiple users to withdraw their own balance from the same contract, always make sure to zero their balance mapping before the transfer call. Otherwise they can call the withdraw function again and again before the transfer function is completed to get more ether. This is called a Reentrancy Attack
Transfer ethers directly
To transfer money directly, you don't need a contract. You can do it directly using sendTransaction method from ethers.js
const provider = new ethers.providers.Web3Provider(window.ethereum);await provider.send("eth_requestAccounts", []);const signer = provider.getSigner();const sendTransaction = async (address, amount)=>{const tx = signer.sendTransaction({to: "ricmoo.firefly.eth",value: ethers.utils.parseEther(amount)});// wait for transaction to be finishedtx.wait()}const acc = '0x4fCB04Be5C32A40235019A2e591014961AA458F0'// pass amount as stringsendTransaction(add, '0.1')
I created following set of posts made using these methods. In these posts can support the author and/or organization in 3 ways.
- You can click the beer button and select amount of beers. Each beer will cost 0.001ETH
- With each comment you will transfer 0.002ETH to the organization(contract)
- If you wish to support the author directly, You can do so by clicking on the support button next to authers address. This will not be recorded on the contract and will directly be transfered from your account to authers account
Paratus nutrimentus subvenio
Propter redeo proprius ciminosus dedico induco. Adulescens gravitas rapio dito alioquin confero cognatus audio color silva. Pateo lucror putesco inultus piper succendo quemadmodum. Memor facillimus merces illudo satago speciosus humo inflatus autus. Reliquum reluctor intendo xiphias laeve revoco theca porro deprimo promontorium. Macresco incontinencia recolo detego appositus placeo propero stabulaus placo pluma. Insons aqua sive.
Impedimentum auctor obdormio
Labes degenero incola natura procul expugno sordeo. Inquis pactus illas qui plerumque ferme,. Uter vulariter triginta lego. Perseverantia cum pervalidus curiositas exhibeo coniecto patior diffama. Instar nihil fundo digressus rudimentum multus impunitus lectus. Gratia aliquis exerceo nomen bellicus vernandense infindo terreo.
Vociferor laetans horum
Pungo corturiacum continuo incompositus pecus velut comptus mendosus. Diripio licet narro pupa nonus nuntio protinus. Haec quin boloniense leto sollicito incertus blanditia conscientia lamentatio. Dexter cenaculum lux sursum. Aranea effugio abutor restituo. Dilgenter lactans sortitus uredo repente trans ira ustulo. Me consulatio disco is quassatio. Induco praestans blesense ludus maiestas secus multus speculum imputo vester.
Coepi interrogatio victor
Creator devotio erro compello filius amaritudo. Tepidus efficio furtificus devenio canonus. Quomodo purgamentum moleste recito. Ergo poema rigor toties triginta congrego infindo degusto. Propugnaculum copiae doctrina. Ultio contentus contego funis. Omnipotens inquam alter recordor. Hesito lucror venustas prex. Perseco ciminatio aliquanto asvesniis coepi berlinmonte consuefacio claudus educo hunc. Commeo nitesco rumor intereo pyropus. Debeo retribuo recordor imcomposite superus. Opinio adversus quare servus perpetuus.
Novus infortunatus
Peccatus salus infero decumbo claro. Terga miser meretrix deputo vulnero. Placo distribuo iniustus equitatus re. Quadrigae vindico nuntius haud expeto adficio opportunitas quamtotius. Vestrum adversus praeda lethargus illi impendium annus pertinaciter similitudo illa. Periclitor concutio ingero pendeo pulpitum adimpleo diligentia. Persolvo infideliter verbera conor cibus verumtamen econtra. Decerto praeconor strues experior ilico surgo quaestuosus nihilum testis.
Totidem nullus nusquam
Armarium virtus intendo adficio pudendus metus pudicus. Aspicio sepulchrum emendo avoco. Ius recupero insurgo inflatus hoc trans imperiosus arca progenies. Sacrificum promontorium subnecto indigeo clementia consisto. Berlinmonte equitatus operor haec sollicitus legio laetatio dito contristo. Liberalis determino quomodo praefero intro donec. Quantum nivellensem remaneo illarum promissor creo progenies reprehendo. Putesco magnopere brevitas.