Create a Dividend paying Token - token

How can I create an erc20 token that pays it's holders a dividends monthly
Ive created a normal erc20 token but i can't seem to get this type of token

Contract can not initiate transactions by itself. But you can do something like this. In consctructor i defined 2,3 and 4 account that i have in remix, for testing, you can comment or delete it and mannually add your addresses in addParticipant();
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "#openzeppelin/contracts/token/ERC20/ERC20.sol";
import "#openzeppelin/contracts/access/Ownable.sol";
contract DividentToken is ERC20 ("DividentToken", "DVD"), Ownable{
uint dividentAmount;
address[] participants;
constructor() {
_mint(msg.sender, 1000000000000 *10 ** 18);
addParticipant(0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2);
addParticipant(0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db);
addParticipant(0x78731D3Ca6b7E34aC0F824c42a7cC18A495cabaB);
}
function mint(address to, uint amount) external onlyOwner {
_mint(to, amount);
}
//return list of addresses of participants
function viewParticipants() public view returns(address[] memory) {
return participants;
}
function setDividentAmount(uint _dividentAmount) public onlyOwner {
dividentAmount = _dividentAmount;
}
function addParticipant(address _participant) public onlyOwner {
participants.push(_participant);
}
function payDividents() public onlyOwner {
for(uint i = 0; i < participants.length; i++) {
transferFrom(owner(), participants[i], dividentAmount); //in
this case first you got to increase allowance for owner()
// _mint(participants[i], dividentAmount); also you can use mint
instead of transfer
}
}
}

Related

Applying condition of whitelisted users to sell bep20 token on pancake swap

I want to add one condition in my bep20 token, only whitelisted addresses should be able to sell my token on the exchange. I tried this but it is not working since all addresses are able to sell. Need help in figuring this out, thanks.
pragma solidity ^0.8.7;
contract CHANCE is ERC20, Ownable {
using SafeMath for uint256;
IPancakeV2Router02 public pancakeV2Router;
address public immutable pancakeV2Pair;
mapping (address => bool) public Whitelist;
constructor() ERC20("CHANCE", "CHCE") {
IPancakeV2Router02 _pancakeV2Router = IPancakeV2Router02(0xD99D1c33F9fC3444f8101754aBC46c52416550D1);
// Create a pancake pair for this new token
address _pancakeV2Pair = IPancakeV2Factory(_pancakeV2Router.factory())
.createPair(address(this), _pancakeV2Router.WETH());
pancakeV2Router = _pancakeV2Router;
pancakeV2Pair = _pancakeV2Pair;
/*
_mint is an internal function in ERC20.sol that is only called here,
and CANNOT be called ever again
*/
_mint(owner(), 200000000000 * 10 ** 18);
emit Transfer(address(0), msg.sender, totalSupply());
}
function burn (uint256 amount) public onlyOwner {
_burn(msg.sender, amount);
}
function _transfer(
address from,
address to,
uint256 amount
) internal override {
require(from != address(0), "ERC20: transfer from the zero address");
require(to != address(0), "ERC20: transfer to the zero address");
if(to == pancakeV2Pair){
require(Whitelist[msg.sender] == true,"Whitelist can mint only!");
super._transfer(from, to, amount);
}
super._transfer(from, to, amount);
}
function whitelist(address _address) public onlyOwner returns (bool){
Whitelist[_address] = true;
return true;
}
}

UnimplementedFeatureError: Copying of type struct {ContractName.StructName} memory[] memory to storage not yet supported

・I want to create the lottery contract that user can buy some lottery numbers.
And, if the lottery was finished, I want to initialize it to create a new lottery.
That's why we implemented it this way.
lotChances = new LotChance[](0);
But, I faced this is error ...πŸ‘‡
UnimplementedFeatureError: Copying of type struct Lottery.LotChance memory[] memory to storage not yet supported.
Minimal example:
contract Lottery {
// Lot Structs
struct LotChance {
address payable userAddress;
uint256 ids;
}
LotChance[] public lotChances;
function getResult() public onlyOwner {
luckyPerson.transfer(address(this).balance);
lotteryId++;
lotChances = new LotChance[](0);
}
}
Please advise me😌
For reset an array and set his values to default you can use delete keyword in Solidity. In your case, you must to change your getResult() function in this way:
function getResult() public onlyOwner {
luckyPerson.transfer(address(this).balance);
lotteryId++;
delete lotChances;
}
You can see an example of smart contract code, here following:
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
contract Lottery {
address owner;
constructor() {
owner = msg.sender;
}
// Lot Structs
struct LotChance {
address payable userAddress;
uint256 ids;
}
modifier onlyOwner() {
require(msg.sender == owner, "You aren't smart contract owner!");
_;
}
LotChance[] public lotChances;
function getResult(address _luckyPerson) public onlyOwner {
uint lotteryId = 0;
payable(_luckyPerson).transfer(address(this).balance);
lotteryId++;
// I reset array length about to '0'
delete lotChances;
}
function partecipateToLottery(uint _id) public {
lotChances.push(LotChance(payable(msg.sender), _id));
}
function getLengthArray() external view returns(uint) {
return lotChances.length;
}
}

Solidity Ide- writing a smart contract, Parser Error: Expected Primary Expression. - Function withdraw

pragma solidity ^0.8.7;
// SPDX-License-Identifier: MIT
contract FeeCollector {//hidden keys
address public owner;
uint256 public balance;
constructor () {
owner = msg.sender;
}
receive () payable external {
balance += msg.value; }
function withdraw (uint amount, address payable destAddr) { public
require(msg.sender ==owner, "only owner can withrdaw");
destAddr.transfer(amount);
balance -= amount;
}
Well just add } in the end. The rest of the code is working.

Is there a difference between token price fixed on smart contract and token price on market?

i set an initial price for my token in solidity, how does It Works the price on the market? if i set a fixed variable TokenPrice in my smart contract, can my token price change thanks to request_offer of market?
The price that i fixed in the smart contract and the price of the market are differents?
OK, I show you a small example of solidity and JS code of ICO and how you can do it
solidity:
contract OwnerContract{
address owner;
constructor(){
owner = msg.sender;
}
modifier isOwner(){
require(msg.sender == owner, "Access denied!");
_;
}
}
//Interface of standard token if you want to accept for example USDT token (as in this example)
interface IERC20{
function allowance(address owner, address spender) external view returns (uint256);
function decimals() external view returns (uint8);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
}
contract ICO_Contract is ownerContract{
//Declare the price and ICO token sell amount
uint256 eachTokenPrice;
uint256 maximumAmount;
uint256 sellAmount;
constructor(uint256 maximumAmount_){
maximumAmount = maximumAmount_;
}
modifier maximumReached(uint256 _amount){
require((sellAmount + _amount) <= maximumAmount, "Maximum amount of ICO reached!");
_;
}
//declare the USDT token
address USDTtokenAddress = USDT_TOKEN_ADDRESS_ON_DEPLOYED_NETWORK;
function changeUSDTtokenAddress(address _address) public isOwner{
USDTtokenAddress = _address;
}
address ICOtokenAddress = TOKEN_ADDRESS_ICO;
function changeICOtokenAddress(address _address) public isOwner{
ICOtokenAddress = _address;
}
IERC20 USDTtoken = IERC20(USDTtokenAddress);
IERC20 ICOtoken = IERC20(ICOtokenAddress);
function buyToken(uint256 _amount) public maximumReached(_amount){
require(_amount > 0, "You need to spend USDT");
uint256 approvedAmount = USDTtoken.allowance(msg.sender, address(this));
require(approvedAmount == _amount, "Check the token allowance, not enough approved!");
uint256 totalPrice = price * _amount * (USDtoken.decimals()/ICOtoken.decimals());
USDTtoken.transferFrom(msg.sender, address(this), totalPrice);
//ICOtoken is in the contract
ICOtoken.transfer(msg.sender, _amount);
sellAmount += _amount;
}
function showPrice() external view returns(uint256){
return price;
}
function showsoldAmount() external view returns(uint256){
return sellAmount;
}
function showMaxAmount() external view returns(uint256){
return maximumAmount;
}
}
I guess this is it. (I did not test and it definitely will throw errors which you have to handle it)
JS code (ethers.js & ethereum libraries) (Because I don't know back-end language I only deploy front-end and just use front-end):
const {ethers} = require('ethers');
let price;
let soldAmount;
let maxAmount;
let provider;
let signer;
let signerAddress;
let contract;
const contractAddress = ICO_CONTRACT_ADDRESS;
const contractABI = {'function showsoldAmount() external view returns(uint256)',
'function showPrice() external view returns(uint256)',
'function buyToken(uint256 _amount) external maximumReached(_amount)',
'function showMaxAmount() external view returns(uint256)'}
//Actually I am not sure if it needs modifier in the interface or not
async () => {
await ethereum.request({ method: 'eth_requestAccounts' });
provider = new ethers.providers.Web3Provider(window.ethereum);
signer = provider.getSigner();
signerAddress = await signer.getAddress();
contract = await new ethers.Contract(contractAddress, contractABI, signer);
price = await contract.showPrice();
soldAmount= await contract.showSoldAmount();
maxAmount = contract.showMaxAmount();
//Show price and sold amount and maximum amount of selling in your page somewhere you want
}
//set a button and input in the page to call the buyToken function and connect it to function below.
function buyToken(){
let amount = document.getElementById("INPUT_TO_KNOW_AMOUNT_ID").value;
contract.buyToken(ethers.utils.parseEther(amount));
}
I hope I did not forget anything but if you have any question simply ask
Best regards :)

creating Ethereum tokens as mining rewards

I have managed to create a registered Ethereum "Token" using Primarily the "how to" from the Frontier website. I intend to proceed with a crowdsource ing contract to raise funds for what will be a fundraising event capable of doing some good in the world, but more on that later. The token creation text includes this suggestion for improving the functionality of my new token:
You could for example reward ethereum miners, by creating a transaction that will reward who found the current block:
mapping (uint => address) miningReward;
function claimMiningReward() {
if (miningReward[block.number] == 0) {
coinBalanceOf[block.coinbase] += 1;
miningReward[block.number] = block.coinbase;
}
}
simply pasting this code into my contract naturally produces error messages.
Q: What do I need to tweak, enter, change, to make it possible to reward minors with one of my tokens? for each and every mined new block.
Thank you.
You can copy and paste your code snippet into the token contract. It will look like that:
contract token {
mapping (address => uint) public coinBalanceOf;
event CoinTransfer(address sender, address receiver, uint amount);
/* Initializes contract with initial supply tokens to the creator of the contract */
function token(uint supply) {
if (supply == 0) supply = 10000;
coinBalanceOf[msg.sender] = supply;
}
/* Very simple trade function */
function sendCoin(address receiver, uint amount) returns(bool sufficient) {
if (coinBalanceOf[msg.sender] < amount) return false;
coinBalanceOf[msg.sender] -= amount;
coinBalanceOf[receiver] += amount;
CoinTransfer(msg.sender, receiver, amount);
return true;
}
mapping (uint => address) miningReward;
/* Reward Ethereum block miner with a token */
function claimMiningReward() {
if (miningReward[block.number] == 0) {
coinBalanceOf[block.coinbase] += 1;
miningReward[block.number] = block.coinbase;
}
}
}
I dont know if you figured this out yet. Anyone else having the same issue try the following snippet:
contract MyToken {
/* Public variables of the token */
string public name;
string public symbol;
uint8 public decimals;
/* This creates an array with all balances */
mapping (address => uint256) public balanceOf;
/* This generates a public event on the blockchain that will notify clients */
event Transfer(address indexed from, address indexed to, uint256 value);
/* Initializes contract with initial supply tokens to the creator of the contract */
function MyToken(uint256 _supply, string _name, string _symbol, uint8 _decimals) {
/* if supply not given then generate 1 million of the smallest unit of the token */
if (_supply == 0) _supply = 1000000;
/* Unless you add other functions these variables will never change */
balanceOf[msg.sender] = _supply;
name = _name;
symbol = _symbol;
/* If you want a divisible token then add the amount of decimals the base unit has */
decimals = _decimals;
}
/* Send coins */
function transfer(address _to, uint256 _value) {
/* if the sender doenst have enough balance then stop */
if (balanceOf[msg.sender] < _value) throw;
if (balanceOf[_to] + _value < balanceOf[_to]) throw;
/* Add and subtract new balances */
balanceOf[msg.sender] -= _value;
balanceOf[_to] += _value;
/* Notifiy anyone listening that this transfer took place */
Transfer(msg.sender, _to, _value);
}
}
Then adding your code to reward the miners but changing "coinBalanceOf" with "balanceOf" like so:
mapping (uint => address) miningReward;
function claimMiningReward() {
if (miningReward[block.number] == 0) {
balanceOf[block.coinbase] += 1;
miningReward[block.number] = block.coinbase;
}
}
So Your final contract would look like this:
contract MyToken {
/* Public variables of the token */
string public name;
string public symbol;
uint8 public decimals;
/* This creates an array with all balances */
mapping (address => uint256) public balanceOf;
/* This generates a public event on the blockchain that will notify clients */
event Transfer(address indexed from, address indexed to, uint256 value);
/* Initializes contract with initial supply tokens to the creator of the contract */
function MyToken(uint256 _supply, string _name, string _symbol, uint8 _decimals) {
/* if supply not given then generate 1 million of the smallest unit of the token */
if (_supply == 0) _supply = 1000000;
/* Unless you add other functions these variables will never change */
balanceOf[msg.sender] = _supply;
name = _name;
symbol = _symbol;
/* If you want a divisible token then add the amount of decimals the base unit has */
decimals = _decimals;
}
/* Send coins */
function transfer(address _to, uint256 _value) {
/* if the sender doenst have enough balance then stop */
if (balanceOf[msg.sender] < _value) throw;
if (balanceOf[_to] + _value < balanceOf[_to]) throw;
/* Add and subtract new balances */
balanceOf[msg.sender] -= _value;
balanceOf[_to] += _value;
/* Notifiy anyone listening that this transfer took place */
Transfer(msg.sender, _to, _value);
}
mapping (uint => address) miningReward;
function claimMiningReward() {
if (miningReward[block.number] == 0) {
balanceOf[block.coinbase] += 1;
miningReward[block.number] = block.coinbase;
}
}
}

Resources