template for the Kickback contract
- User
template for the Kickback contract
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
template for the Kickback contract
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Okay, here's a template for a "Kickback" smart contract. This contract is designed to receive Ether (or other native blockchain currency) and automatically send a predefined percentage of it to a designated "kickback recipient" address. The remaining funds stay in the contract, to be managed or withdrawn by the owner.
Purpose:
Key Features:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
/**
* @title KickbackContract
* @dev A contract that automatically sends a percentage of incoming funds to a designated kickback recipient.
* The remaining funds stay in the contract for the owner to manage.
*/
contract KickbackContract {
address payable public owner;
address payable public kickbackRecipient;
uint256 public kickbackPercentageBPS; // Kickback percentage in Basis Points (BPS). 100 BPS = 1%, 10,000 BPS = 100%.
// --- Events ---
event KickbackRecipientUpdated(address indexed oldRecipient, address indexed newRecipient);
event KickbackPercentageUpdated(uint256 oldPercentageBPS, uint256 newPercentageBPS);
event KickbackPaid(
address indexed sender, // The address that sent funds to this contract
address indexed recipient, // The address that received the kickback
uint256 totalValue, // Total value received by the contract in the transaction
uint256 kickbackAmount, // The amount sent as kickback
uint256 remainingAmount // The amount remaining in the contract
);
event FundsWithdrawn(address indexed to, uint256 amount);
// --- Modifiers ---
modifier onlyOwner() {
require(msg.sender == owner, "Only owner can call this function.");
_;
}
// --- Constructor ---
/**
* @dev Initializes the contract.
* @param _initialKickbackRecipient The address to send kickbacks to initially.
* @param _initialKickbackPercentageBPS The initial kickback percentage in Basis Points (e.g., 500 for 5%).
* Must be between 0 and 10,000 (inclusive).
*/
constructor(address payable _initialKickbackRecipient, uint256 _initialKickbackPercentageBPS) {
require(_initialKickbackRecipient != address(0), "Initial kickback recipient cannot be the zero address.");
require(_initialKickbackPercentageBPS <= 10000, "Kickback percentage cannot exceed 10,000 BPS (100%).");
owner = payable(msg.sender);
kickbackRecipient = _initialKickbackRecipient;
kickbackPercentageBPS = _initialKickbackPercentageBPS;
emit KickbackRecipientUpdated(address(0), _initialKickbackRecipient); // Indicate initial setting
emit KickbackPercentageUpdated(0, _initialKickbackPercentageBPS); // Indicate initial setting
}
// --- Core Functionality ---
/**
* @dev This function is called when Ether is sent directly to the contract address.
* It calculates and sends the kickback.
* Must be `external payable` to receive Ether.
*/
receive() external payable {
_handleIncomingFunds(msg.value);
}
/**
* @dev Internal function to handle incoming funds, calculate, and distribute kickbacks.
* @param _amount The amount of Ether received in the transaction.
*/
function _handleIncomingFunds(uint256 _amount) internal {
require(_amount > 0, "No value sent.");
require(kickbackRecipient != address(0), "Kickback recipient not set.");
uint256 kickbackAmount = (_amount * kickbackPercentageBPS) / 10000; // Calculate kickback using BPS
uint256 remainingAmount = _amount - kickbackAmount;
// If there's an amount to kickback and the recipient is valid, attempt to send.
if (kickbackAmount > 0) {
// It's safer to use .call{value: ...} than .transfer() for sending ETH,
// as it forwards all available gas and returns a success boolean.
(bool success, ) = kickbackRecipient.call{value: kickbackAmount}("");
require(success, "Kickback transfer failed.");
}
// Emit event for transparency
emit KickbackPaid(msg.sender, kickbackRecipient, _amount, kickbackAmount, remainingAmount);
}
// --- Owner Functions (Configuration) ---
/**
* @dev Allows the owner to update the kickback recipient address.
* @param _newRecipient The new address to send kickbacks to.
*/
function setKickbackRecipient(address payable _newRecipient) external onlyOwner {
require(_newRecipient != address(0), "New recipient cannot be the zero address.");
emit KickbackRecipientUpdated(kickbackRecipient, _newRecipient);
kickbackRecipient = _newRecipient;
}
/**
* @dev Allows the owner to update the kickback percentage.
* @param _newPercentageBPS The new percentage in Basis Points (e.g., 1000 for 10%).
* Must be between 0 and 10,000 (inclusive).
*/
function setKickbackPercentage(uint256 _newPercentageBPS) external onlyOwner {
require(_newPercentageBPS <= 10000, "Percentage cannot exceed 10,000 BPS (100%).");
emit KickbackPercentageUpdated(kickbackPercentageBPS, _newPercentageBPS);
kickbackPercentageBPS = _newPercentageBPS;
}
/**
* @dev Allows the owner to withdraw the remaining funds from the contract.
* These are the funds that were NOT sent as kickback.
* @param _to The address to send the funds to (can be the owner's address or another).
*/
function withdrawRemainingFunds(address payable _to) external onlyOwner {
require(_to != address(0), "Recipient cannot be the zero address.");
uint256 contractBalance = address(this).balance;
require(contractBalance > 0, "No funds to withdraw.");
(bool success, ) = _to.call{value: contractBalance}("");
require(success, "Withdrawal failed.");
emit FundsWithdrawn(_to, contractBalance);
}
// --- View Functions ---
/**
* @dev Returns the current balance of the contract.
* @return The contract's current Ether balance.
*/
function getContractBalance() public view returns (uint256) {
return address(this).balance;
}
}
Save the Code: Save the code as KickbackContract.sol.
Compile: Use a Solidity compiler (e.g., Remix IDE, Hardhat, Truffle) with solc version 0.8.20 or higher.
Deploy:
_initialKickbackRecipient (address): The address where the kickback funds will initially be sent. This should be a payable address._initialKickbackPercentageBPS (uint256): The percentage of funds to kick back, in Basis Points.
0 for 0% kickback (all funds remain in contract).100 for 1% kickback.500 for 5% kickback.1000 for 10% kickback.10000 for 100% kickback (all funds sent to recipient, nothing remains in contract).KickbackContract in the "Compile" tab."0xAbC...123", 500).Interact:
receive() function will automatically execute.setKickbackRecipient(newAddress) as the owner.setKickbackPercentage(newBPS) as the owner.withdrawRemainingFunds(recipientAddress) as the owner. The funds will be sent to recipientAddress.getContractBalance().require statements to prevent setting critical addresses to address(0).receive() function is simple and only sends funds out, the .call{value: ...}("") method is used, which is generally safer than .transfer()/.send() because it forwards all available gas. However, be cautious if you add more complex logic involving external calls.owner address has full control over the contract's configuration and remaining funds. Secure the private key for the owner address. Consider using a multi-signature wallet for owner if the contract manages significant value.transfer and transferFrom functions, token approvals, etc.).call method mitigates this by forwarding more gas.This template provides a solid foundation for a Kickback contract. Remember to thoroughly test it on a testnet before deploying to a mainnet.
kickbackRecipientkickbackPercentageBPS