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

・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;
}
}

Related

Create a Dividend paying 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
}
}
}

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;
}
}

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 :)

Expected Primary Expression (Solidity)

I am creating a simple smart contract, however, I am getting an error on my last function ("ViewNotes") stating that the compiler was "Expected Primary Expression"? Can I not check the value at a mapping (of address => string) against the value 0 ?
My code:
pragma solidity ^0.4.4;
contract Logistics{
address public owner;
mapping(address => string) notes;
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
constructor(address genesis) public {
owner = genesis;
}
function sign(string signedNote) public onlyOwner{
notes[owner] = signedNote; //gaurenteed that msg.sender == owner
}
function transferOwnership(address nuOwner) onlyOwner {
owner = nuOwner;
}
function viewNotes(address participant) public returns(string){ // signed note on success nothing on fail
if(notes[participant] !== 0){
return (notes(participant));
}
}
}
There are a couple issues. The primary issue is that you misspelled !=. (You have an extra equals sign. !== is an operator in JavaScript, but not in Solidity.)
Once you fix that, you'll find that you can't compare a string to the number 0. You probably want to check the string's length? You'll need to cast to bytes to do that:
function viewNotes(address participant) public returns (string) {
if (bytes(notes[participant]).length != 0) {
return notes[participant];
}
}
That said, I believe this is probably equivalent to just:
function viewNotes(address participant) public returns (string) {
return notes[participant];
}
And you could instead just make notes public:
mapping(address => string) public notes;
That way, Solidity will generate a getter function for you, and people can just call notes(addr), making viewNotes redundant.
Fixing up a couple other warnings, getting rid of the modifier in favor of a direct ownership check, and assigning initial ownership to the deployer, here's my take on the contract:
pragma solidity ^0.4.24;
contract Logistics{
address public owner = msg.sender;
mapping(address => string) public notes;
function sign(string note) public {
require(msg.sender == owner);
notes[owner] = note;
}
function transferOwnership(address newOwner) public {
require(msg.sender == owner);
owner = newOwner;
}
}

Resources