Revenue Splitter: Automated Revenue Distribution
1.1 Revenue Splitter Overview
1.2 Key Smart Contract Features for Revenue Splitting
1.3 Smart Contract
RevenueSplitter.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract RevenueSplitter {
struct Recipient {
uint256 share; // Share percentage (e.g., 30 means 30%)
uint256 balance; // Balance available for withdrawal
}
mapping(address => Recipient) public recipients;
address[] public recipientAddresses;
uint256 public totalShares;
event RevenueReceived(uint256 amount);
event Withdrawn(address indexed recipient, uint256 amount);
modifier onlyRecipient() {
require(recipients[msg.sender].share > 0, "Not an authorized recipient");
_;
}
constructor(address[] memory _recipients, uint256[] memory _shares) {
require(_recipients.length == _shares.length, "Recipients and shares length mismatch");
for (uint256 i = 0; i < _recipients.length; i++) {
recipients[_recipients[i]] = Recipient({
share: _shares[i],
balance: 0
});
recipientAddresses.push(_recipients[i]);
totalShares += _shares[i];
}
}
// Receive funds and distribute based on shares
receive() external payable {
require(msg.value > 0, "No funds sent");
uint256 totalAmount = msg.value;
emit RevenueReceived(totalAmount);
for (uint256 i = 0; i < recipientAddresses.length; i++) {
address recipientAddr = recipientAddresses[i];
uint256 recipientShare = (totalAmount * recipients[recipientAddr].share) / totalShares;
recipients[recipientAddr].balance += recipientShare;
}
}
// Withdraw funds allocated to the caller
function withdraw() external onlyRecipient {
uint256 amount = recipients[msg.sender].balance;
require(amount > 0, "No balance available for withdrawal");
recipients[msg.sender].balance = 0;
(bool success, ) = msg.sender.call{value: amount}("");
require(success, "Transfer failed");
emit Withdrawn(msg.sender, amount);
}
// Get recipient share percentage
function getShare(address _recipient) public view returns (uint256) {
return recipients[_recipient].share;
}
// Get recipient balance available for withdrawal
function getBalance(address _recipient) public view returns (uint256) {
return recipients[_recipient].balance;
}
}1.4 Usage and Deployment
Initializing the Contract
Receiving and Distributing Revenue
Withdrawing Funds
Viewing Allocation
1.5 Best Practices and Security Considerations
PreviousIP Protection Tool: Smart Contracts for IP ProtectionNextWeb3 Dev Toolkit: Libraries and Frameworks
Last updated
