0

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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