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.

Payable function solidity
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
contract Bar {
uint256 beerPrice = 0.0001 ether;
error InsufficientBalance();
function buyBeers(uint16 totalBeers)
external
payable
{
// calculate the total price
uint256 totalPrice = beerPrice * totalBeers;
// check if the transaction amount user sent meets the total price
// revert if not
if (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.

Withdraw solidity
//SPDX-License-Identifier: Unlicense
pragma 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

Send Transaction javascript
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 finished
tx.wait()
}
const acc = '0x4fCB04Be5C32A40235019A2e591014961AA458F0'
// pass amount as string
sendTransaction(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

Full Contracts

Beer solidity
Paid Comments solidity

Deployed Contracts

Contract - Beers

Contract - Paid Comments