Skip to main content

Platform Fee

import "@thirdweb-dev/contracts/extension/PlatformFee.sol";

PlatformFee allows you to charge a percentage fee wherever there is a transfer of currency (ERC20 tokens or native tokens) in your contract.

Availability in base contracts

The PlatformFee extension is not already included in any base contracts. To use this extension, check out the implementation guide and full API reference.

Unlocked Features

Once deployed, you can use the features made available by these contracts on the SDK and dashboard:

Implementing the Contract extension

The PlatformFee extension is an abstract contract, and expects you to implement the following functions by yourself:

NameTypeParametersReturnsDescription
_canSetPlatformFeeInfointernal view virtualn/aboolRuns on every attempt to set a new platform fee recipient or bps. Returns whether this info can be set in the given execution context.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@thirdweb-dev/contracts/extension/PlatformFee.sol";

contract MyContract is PlatformFee {
/**
* We store the contract deployer's address only for the purposes of the example
* in the code comment below.
*
* Doing this is not necessary to use the `PlatformFee` extension.
*/
address public deployer;

constructor() {
deployer = msg.sender;
}

/**
* This function returns who is authorized to set platform fee info for your contract.
*
* As an EXAMPLE, we'll only allow the contract deployer to set the platform fee info.
*
* You MUST complete the body of this function to use the `PlatformFee` extension.
*/
function _canSetPlatformFeeInfo() internal view virtual override returns (bool) {
return msg.sender == deployer;
}
}

Full API reference

getPlatformFeeInfo

function getPlatformFeeInfo() public view returns (address, uint16);
  • Returns the platform fee recipient and bps.
  • Set this value using the setPlatformFeeInfo function.

setPlatformFeeInfo

function setPlatformFeeInfo(address platformFeeRecipient, uint256 platformFeeBps) external;
  • Lets an authorized wallet set the platform fee recipient and bps.
  • Parameter platformFeeRecipient: The address to set as the new platform fee recipient.
  • Parameter platformFeeBps: The value to set as the new platform fee BPS.
  • The _canSetPlatformFeeInfo function is run on every call to this function. If the return value of _canSetPlatformFeeInfo is false, the setPlatformFeeInfo call will revert.

_setupPlatformFeeInfo

function _setupPlatformFeeInfo(address platformFeeRecipient, uint256 platformFeeBps) internal;
  • Sets the platform fee recipient and bps.
  • Parameter platformFeeRecipient: The address to set as the new platform fee recipient.
  • Parameter platformFeeBps: The value to set as the new platform fee BPS.
  • The _canSetPlatformFeeInfo function is not run on a call to this function.

_canSetPlatformFeeInfo

function _canSetPlatformFeeInfo() internal view virtual returns (bool);
  • Runs on every setPlatformFeeInfo function call.
  • Returns whether a new platform fee recipient or bps can be set in the given execution context.
  • For example, this function can check whether the wallet calling setPlatformFeeInfo is the contract owner, and enforce that only the current owner should be able to successfully call setPlatformFeeInfo.