0

I want to build a donation kind of application using blockchain. It would also have the feature of automatic refund in case let say the patient dies before the donation could be used.

My doubt is, in such a case, I would basically be sending the amount they donated back to the donor's wallet address, but I definitely can't send the entire amount as each transaction will have some associated gas costs that needs to be considered as well. Then, how can I make sure that everyone gets their donated amount back?

Is there some way to calculate an estimated total gas cost beforehand so I can subtract that and refund the remaining amount or how else can I tackle this?

1 Answer 1

1

If I've understanded your issue correctly, it seems you're concerned about estimating gas costs for each transaction. In essence, when users make deposits into your application, it's crucial to record these transactions in a database tied to each specific user. This way, you can monitor the cumulative gas costs by reviewing the transaction history for each user. Here's a Python code snippet utilizing Web3py:

from web3 import Web3
from moralis import evm_api
api_key = "YOUR_API_KEY"
# Connect to an Ethereum node 
web3 = Web3(Web3.HTTPProvider('https://rpc.ankr.com/eth'))

# User list of txns
listTxns = ['0xfbb4478e16e497e574836a9df5193e0d44e2d2fd0dec290d4b5e362d4fe4db41', '0x47896831ee854b40ea5d4ba69fac148cfeeb5ecee08c11d91df758af34fea14f']
totalGasPrices = 0
for txn in listTxns:
    params = {
        "chain": "eth",
        "include": "percent_change",
        "to_block": trans['blockNumber'],
        "address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" # WETH 
    }
    # Get txn detail
    trans = web3.eth.get_transaction(txn)
    # Get ETH price at previous transaction block
    ethUsdPrice = evm_api.token.get_token_price(
        api_key=api_key,
        params=params,
    )
    # Calculate total gas
    totalGasPrices += ethUsdPrice['usdPrice'] * trans['gas'] * trans['gasPrice']

Hope it helps!

2
  • Thanks for that.. but doesn't the gas cost and all also depend on things like network congestion at that point of time.. what if the gas cost becomes greater than what was recorded? Commented Dec 23, 2023 at 16:09
  • 1
    When making a transaction, 'gas' and 'gasPrice' values are fixed (Metamask handles this, or you can adjust it yourself). The primary factor influencing gas prices is the ETH price. During network congestion, users may pay more gas price to speed up their transactions. If 'gas' and 'gasPrice' remain constant in Metamask, the gas price remains the same unless ETH price changes. To enhance accuracy, I've added a line of code to calculate the ETH price at a specific block for precise gas price calculations. Hope it helps you.
    – tqdpham96
    Commented Dec 23, 2023 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.