I have a rather interesting but challenging question. I want to create ERC-1155 and ERC-721 tokens that will be primarily traded using a Decentralized Exchange (DEX) implemented for a newly created protocol. However, I want to allow those tokens to be traded on external DEXes such as OpenSea. That is possible, no question about, but my question relates to protocol fees earned on transactions.
Is there a way of accruing protocol fees on transactions (trades) even if they are not on the native DEX? It would seem to me it should be possible by programming the smart contract such the protocol fee is always collected, but I am not clear on how that would work practically.
Any ideas? Is there something obvious that I am not thinking about?
Possible solution
Could I implement the solution suggested by @porton as follows? This is now slightly updated to include a percentage.
contract myNFT {
address public immutable feeAddress;
uint256 public immutable feePercentage;
// Set address to receive fees
constructor (address feeAddress, feePercentage) public {
feeAddress = _feeAddress;
feePercentage = _feePercentage;
}
//******************
// Some other stuff
//******************
function safeTransferFrom(address _from, address _to, uint256 _tokenId, bytes data) external payable {
uint256 private _feeAmount;
_feeAmount = (getLastTradePrice()*feePercentage/10^18);
// This only works if sufficient funds have been provided for the fees. Otherwise the trade will fail
address(feeAddress).transfer(feeAmount);
//******************
// Some other stuff
//******************
}
}
getLastTradePrice() will access an Oracle managed by the protocol to track trades made on external trading platforms.
Could this work?
Transfer
events.