Business Fees [Metamask + Discourse]

Hey guys, I’m implementing Metamask on my website. Users will be able to send ETH to other users using Metamask.

I would use a plugin like this one to set it up: GitHub - santiment/discourse-ethereum: Ethereum plugin for Discourse.

I would like to know how I can set up a “business fee” on this. Every time a user sends ETH to another user through my website using Metamask, my company would get a %.

There is any way I could do this?

Thanks!

1 Like

Thank you for your question @tiago4maral . There are a few different ways for you to build this, so I’m going to outline a framework for how you can approach solving this problem, and I hope that you find the resources useful for you.

Using a Smart Contract to Extract a Fee from a Tip Amount
Context: In order to charge a business fee, you will create and deploy a smart contract that users interact with as part of sending the tip. You will write a smart contract that contains the logic necessary to send the tip to the recipient and send a fee to an address that will receive the fee.

You could write a smart contract with the following logic (this is pseudo code, but I think it may be helpful for think out what you want to do - the syntax is not perfect, so please do not copy paste this):

   constructor(fee_address ) public {
        fee_receiver = msg.sender;
}

//in this constructor, we are initializing an address to which the fees will be paid. This address could be an externally owned account, or it could be a smart contract. This will be depend on your use case for the fee. 

function tip (address, tip_amount) {
address tipper = msg.sender;
fee = tip_amount * (0.05);
tip_minus_fee = tip_amount - fee;
tipper.transfer(tip_minus_fee);
tipper.transfer(fee);
}

//here we are creating a tip function and we are calculating the fee that needs to be taken from the tip. Then we transfer the tip to the respective addresses.

As an example, although it not a perfect one, I am going to point you to the Uniswap v2 Factory Smart Contract as a way of showing the fee structure that is implemented on a protocol level.

There is a functions within that contract that look like the following:

function setFeeTo(address _feeTo) external {
    require(msg.sender == feeToSetter, 'UniswapV2: FORBIDDEN');
    feeTo = _feeTo;
}

function setFeeToSetter(address _feeToSetter) external {
    require(msg.sender == feeToSetter, 'UniswapV2: FORBIDDEN');
    feeToSetter = _feeToSetter;
}

These interact with the pair contract in order to return a fee every time a token swap occurs.

Here are some additional resources if you are getting started on your journey learning solidity.

Also, it is important to note, if you are going to implement such a fee system - a best practice is to alert your user of the fee so they can tip the appropriate amount to cover both the tip and the fee. Let me know if you have any questions about this, or need other sources of inspiration.