Let's say you were building a marketplace and you wanted to take some type of commission for allowing a user to sell a thing on your marketplace....how exactly does that work? Is there some type of template you can use that allows a person to pay you a commission, but then receive the payment for the sale of their item?
1 Answer
I don't know of any template, but this can be done relatively easily with solidity. Consider the dummy code below:
pragma solidity ^0.8.0;
/*your code here*/
// private function because it simply calculates commission and pays out accordingly.
// Inputs & other validation likely will come from somewhere else in the contract.
function takeCommission(address seller, address platform, uint256 amountPaid, uint256 commissionPercentage) private {
// ensures the minimum amountPaid is at least 100 wei and is accurately divisible
require(amountPaid % 100 == 0);
// divide by 100 because commission percentage is expressed as a uint
uint256 platformFee = (amountPaid * commissionPercentage) / 100;
amountPaid -= platformFee;
payable(seller).transfer(amountPaid);
payable(platform).transfer(platformFee);
}
Just knowing the commission percentage and what was paid lets you calculate it within a function, then you can call the .transfer()
function twice with the respective values.
-
Would this work with polygon network? I’m looking to do the same but eth fees are too high for micro transactions in the 5-10 cents range.– chovyCommented Dec 3, 2021 at 10:27
-
1@chovy no reason it wouldn't, as per their documentation "If you're an Ethereum Developer, you're already a Polygon developer. All the tools you're familiar with are supported on Polygon out of the box: Truffle, Remix, Web3js. Switch over to Polygon's RPC and get started!" Commented Dec 15, 2021 at 1:02