1

My idea is to ask users what amount they want to tip another user. However I want to take 5% of that amount and send it to another address (as comission for site operator)

I know the user would have to confirm two transactions but does metamask or web3 have a way to do bulk transactions or one transaction that is dependent on the other?

If a user wants to tip $1 then 95 cents would go to recipient and 5 cents would go site operator.

I’d want to make sure they have to approve both transactions for them to go through (ie: stopping them from not sending the commission transaction)

Is this possible with just standard web3 transactions or would I need a smart contract that handles the commission transaction and pays the rest to the intended recipient?

2 Answers 2

2

It's probably easiest with a smart contract for it. A single transaction always targets just one address, and there is no native way to do batch transactions.

Some wallet clients may offer some sort of batch transactions, but probably those are also not what you need.

You may consider asking the user to first send the commission transactions and only after that allow him to send the real transaction. But, in my opinion, this is a waste of transactions (and money), since you can do that easily with a smart contract and a single transaction.

Example of a simple commission function (probably doesn't even compile, but hopefully illustrates the point):

function pay() payable public {
  uint mainPart = msg.value * 95 / 100;
  uint commission = msg.value - mainPart;

  address(0x123).transfer(mainPart);
  address(0x345).transfer(commission);
}
3
  • Can you provide an example contract that would deduct a commission?
    – chovy
    Dec 3, 2021 at 13:56
  • added an example Dec 3, 2021 at 19:06
  • So a couple of things based on what others have suggested: manager should be able to change commission address
    – chovy
    Dec 4, 2021 at 22:45
0

You should deploy the Payment Splitter contract by OpenZeppelin.

Here's the contract code: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.4.0/contracts/finance/PaymentSplitter.sol

Your Answer

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

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