Solidity Fee Models
A fee model is a rule for taking a small charge from an amount. A basis point is one hundredth of one percent, so 10000 basis points make a whole amount.
A fixed fee takes the same amount every time
The net amount is the amount minus the fixed fee. An amount smaller than the fee reverts.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
contract FixedFee {
uint256 public constant FEE = 25;
function netAmount(uint256 amount) public pure returns (uint256) {
require(amount >= FEE, "amount too small");
return amount - FEE;
}
}
A percentage fee scales with the amount
The fee is the amount times the basis points divided by 10000. Zero basis points take nothing and 10000 take everything.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
contract PercentFee {
function feeFor(uint256 amount, uint256 bps) public pure returns (uint256) {
return (amount * bps) / 10000;
}
}
A split divides one fee between two recipients
The first share is seventy percent of the fee and the second share is the rest. The two shares always sum to the fee.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
contract FeeSplit {
function splitFee(uint256 fee) public pure returns (uint256 first, uint256 second) {
first = (fee * 70) / 100;
second = fee - first;
}
}
Example
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
contract FeeVault {
mapping(address => uint256) public balances;
address public firstRecipient;
address public secondRecipient;
constructor(address first, address second) {
firstRecipient = first;
secondRecipient = second;
}
function deposit(uint256 amount) public {
uint256 fee = (amount * 250) / 10000;
uint256 firstShare = (fee * 60) / 100;
uint256 secondShare = fee - firstShare;
balances[msg.sender] += amount - fee;
balances[firstRecipient] += firstShare;
balances[secondRecipient] += secondShare;
}
}
Key points
- Fee: A fee is a small charge taken from an amount before the rest is credited.
- Fixed fee: A fixed fee takes the same number of tokens whatever the amount is.
- Percentage fee: A percentage fee takes a share of the amount and it is written in basis points.
- Basis point: A basis point is one hundredth of one percent and 250 basis points equal two and a half percent.
- Fee split: A fee split divides one fee between two recipients and the shares sum to the fee.
- Restatement: A fee model states how much is taken and where each taken share goes.
In this example, FeeVault takes a 250 basis point fee from a deposit, splits that fee sixty forty between two recipients, and credits the depositor with the net amount so all parts sum to the deposit.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Remix IDE
Exercise
A deposit of 2000 pays a 250 basis point fee. What is the fee?