Keep getting an error by my constructor
pragma solidity ^0.6.9;
contract Confrence{
address public organizer;
mapping (address => uint) public registrantsPaid;
uint public numRegisrants;
uint public quota;
//so you can log these events
event Deposit(address _from, uint _amount);
event Refund(address _to, uint _amount);
Confrence() public {//consturctor
organizer = msg.sender;
quota = 500;
numRegistrants = 0;
}
}
Is it that I need to put parameters in the parenthesis?
Related
I'm tryin to mint my ERC1155 nfts with my own ERC20 so i did the following :
1- created a variable of ERC20
ERC20 public FCG = FCG<br>
and in the constructor i pass the address of the ERC20 token contract
FCG = ERC20(0x0fC5025C764cE34df352757e82f7B5c4Df39A836);
2- in my mint function i did the following code
function mintCharacterPublic(address to, uint256 id, uint256 amount) public payable returns(bool){
require(to != address(0), "ERC1155: mint to the zero address");
require(FCG.approve(address(this), MINT_PRICE), "can't approve your token");
require(uint256(FCG.allowance(msg.sender, address(this))) >= MINT_PRICE, "Not enough of tokens");
FCG.transferFrom(msg.sender, address(this), MINT_PRICE);
mintCharacter(to, id, amount);
_balances[id][to] +=amount;
return true;
}
but when i try to mint it does not apply and it throws "Not enough of tokens"
so i think that the approve function does not function correctly.
here is my smart contract for the token
// SPDX-License-Identifier: MIT
import "#openzeppelin/contracts/token/ERC20/ERC20.sol";
import "#openzeppelin/contracts/access/Ownable.sol";
pragma solidity ^0.8.2;
contract Token {
mapping(address => uint) public balances;
mapping(address => mapping(address => uint)) public allowance;
uint public totalSupply = 1000000 * 10 ** 18;
string public name = "FightClub Token";
string public symbol = "FCT";
uint public decimals = 18;
event Transfer(address indexed from, address indexed to, uint value);
event Approval(address indexed owner, address indexed spender, uint value);
constructor() {
balances[msg.sender] = totalSupply;
}
function balanceOf(address owner) public view returns(uint) {
return balances[owner];
}
function transfer(address to, uint value) public returns(bool) {
require(balanceOf(msg.sender) >= value, 'balance too low');
balances[to] += value;
balances[msg.sender] -= value;
emit Transfer(msg.sender, to, value);
return true;
}
function transferFrom(address from, address to, uint value) public returns(bool) {
require(balanceOf(from) >= value, 'balance too low');
require(allowance[from][msg.sender] >= value, 'allowance too low');
balances[to] += value;
balances[from] -= value;
emit Transfer(from, to, value);
return true;
}
function approve(address spender, uint value) public returns (bool) {
allowance[msg.sender][spender] = value;
emit Approval(msg.sender, spender, value);
return true;
}
}
This must be a two-step process:
The user authorizes your contract to spend the token.
The user calls mintCharacterPublic.
Your contract cannot grant itself permission to spend the originator's tokens. require(FCG.approve(address(this), MINT_PRICE), "can't approve your token"); means that the eip-721 contract sets its own approval to MINT_PRICE, not the end-user's.
I have designed an erc20 token and a smart contract to transfer that token, but when I trigger the function to transfer, the remix told me the following graph.
What I did was to deploy the token smart contract and then the DAO smart contract, then call the creatSubDAO function to start the transfer.
The relative code is below:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function allowance(address owner, address spender) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
contract Dtoken:
contract Dtoken is IERC20{
string public name = "DAO Token";
string public symbol = "D";
uint256 public totalSupply = 1000000000000000000000000;
uint8 public decimals = 18;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
constructor() {
balanceOf[msg.sender] = totalSupply;
}
function transfer(address _to, uint256 _value) public returns (bool success) {
require(balanceOf[msg.sender] >= _value);
balanceOf[msg.sender] -= _value;
balanceOf[_to] += _value;
emit Transfer(msg.sender, _to, _value);
return true;
}
function approve(address _spender, uint256 _value) public returns (bool success) {
allowance[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
return true;
}
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
require(_value <= balanceOf[_from]);
require(_value <= allowance[_from][msg.sender]);
balanceOf[_from] -= _value;
balanceOf[_to] += _value;
allowance[_from][msg.sender] -= _value;
emit Transfer(_from, _to, _value);
return true;
}
}
contract MasterDao:
import "./Dtoken.sol";
contract MasterDAO {
uint proposalNum;
mapping (address => Member) public members;
mapping (uint => Proposal) public proposals;
mapping (uint => SubDAO) public subDAOs;
// SubDAO[] public subDAOs;
function createSubDAO (uint _poolGap, uint _DAOID, uint _rate, Dtoken _token) public returns (address daoAddress) {
SubDAO subDAO = new SubDAO(_poolGap, _DAOID, msg.sender, _rate, this, _token);
daoAddress = address(subDAO);
}}
SubDao Contract:
contract SubDAO {
uint proposalNum;
address lender;
uint poolGap;
uint currentStake;
uint256 public DAOID;
MasterDAO master;
address stakeItem;
uint rate;
Dtoken public token;
mapping(address => Member) members;
mapping (uint => Proposal) public proposals;
string[] description;
event Transfrom(address, uint);
struct Member {
address memberAddress;
string nickName;
bool alreadyExist;
}
struct Proposal {
string description;
address payable recipient;
bool executed;
uint proposalID;
address[] agreeMembers;
address[] disagreeMembers;
bool exectuable;
uint amount;
Dtoken token;
}
modifier membership (MasterDAO masterDAO, address _staker){
require(masterDAO.addressExist(_staker) == true);
_;
}
modifier lenderLimit {
require(msg.sender == lender);
_;
}
modifier balanceLimit(uint amount) {
require(amount <= poolGap - currentStake);
_;
}
constructor (uint _poolGap, uint _DAOID, address _lender, uint _rate, MasterDAO caller, Dtoken _token) {
currentStake = 0;
poolGap = _poolGap;
DAOID = _DAOID;
lender = _lender;
master = caller;
rate = _rate;
token = _token;
token.transferFrom(address(this), address(this), 10);
}
Did I do anything wrong to transfer the token? Thanks!
From the SubDao constructor:
token.transferFrom(address(this), address(this), 10);
This snippet is trying to transfer tokens from the just created SubDao address (1st argument) to the very same address (2nd argument).
The newly created SubDao address most likely doesn't own any tokens to transfer from in the first place.
If you want to transfer tokens from the MasterDao to the newly created SubDao, you can remove the transferFrom() from the SubDao constructor, and implement the transfer in the MasterDao function createSubDAO():
function createSubDAO (uint _poolGap, uint _DAOID, uint _rate, Dtoken _token) public returns (address daoAddress) {
SubDAO subDAO = new SubDAO(_poolGap, _DAOID, msg.sender, _rate, this, _token);
daoAddress = address(subDAO);
// transfer 10 units of the `Dtoken`
// from the `MasterDao` address
// to the newly created `SubDao` address
// assuming that `MasterDao` has sufficient token balance
Dtoken(_token).transfer(daoAddress, 10);
}}
pragma solidity >=0.6.0 <0.7.0;
import "hardhat/console.sol";
import "./ExampleExternalContract.sol";
contract Staker {
ExampleExternalContract public exampleExternalContract;
mapping(address => uint256) public balances;
uint256 public constant treshold = 1 ether;
event Stake(address staker, uint256 amount);
constructor(address exampleExternalContractAddress) public {
exampleExternalContract = ExampleExternalContract(exampleExternalContractAddress);
}
function stake() public payable () {
balances[msg.sender] = msg.value;
emit Stake(msg.sender, msg.value);
}
}
I am getting a parse error Expected '{' but got '(', when I try to deploy this code. Any thoughts on what's going on?
You have an extra pair of parentheses in the stake function definition.
Replace this
function stake() public payable () {
with this
function stake() public payable {
I have an ERC20 token (Cryptos) and want to send some tokens to VulnerableTokenSale smart contract, but it shows zero balance for the token balance (get_balanceToken function). Is it possible to do that? I am using Remix on Ethereum website.
contract VulnerableTokenSale {
address public wallet;
uint256 rate;
ERC20 public token;
address public owner;
mapping(address => uint256) public balances;
address [] beneficiaries;
modifier onlyOwner() {
require(msg.sender == owner, "not owner");
_;
}
constructor(address _wallet, uint256 _rate, ERC20 _token)
public {
wallet = _wallet;
rate = _rate;
token = _token;
owner = msg.sender;
}
function sendTokensWithRatio(uint256 _numerator, uint256 _denominator) external onlyOwner {
require(_numerator <= _denominator);
for(uint256 i = 0; i < beneficiaries.length; i++){
address beneficiary = beneficiaries[i];
uint256 balance = balances[beneficiary];
if(balance > 0) {
uint256 amount = balance * _numerator;
amount = amount / _denominator;
balances[beneficiary] = balance - amount;
token.transfer(beneficiary, amount);
}
}
}
function purchaseTokens() public payable{
uint256 weiAmount = msg.value;
uint256 _tokenAmount = weiAmount * rate;
beneficiaries.push(msg.sender);
balances[msg.sender] = balances[msg.sender] + _tokenAmount;
}
}
Think your constructor is slightly wrong, I have never seen anyone take in an ERC token directly.
I have something like this in my contracts:
constructor(address _wallet, uint256 _rate, address _tokenAddress) public {
wallet = _wallet;
rate = _rate;
token = ERC20(_tokenAddress);
owner = msg.sender;
}
Note you need the ERC20 interface for the above to work:
interface ERC20 {
function totalSupply() public view returns (uint);
function balanceOf(address tokenOwner) public view returns (uint balance);
function allowance(address tokenOwner, address spender) public view returns (uint remaining);
function transfer(address to, uint tokens) public returns (bool success);
function approve(address spender, uint tokens) public returns (bool success);
function transferFrom(address from, address to, uint tokens) public returns (bool success);
// optional
function name() external view returns (string);
function symbol() external view returns (string);
function decimals() external view returns (string);
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}
/home/ubuntu/token_sale/contracts/SandeepDappToken.sol:17:34: DeclarationError: Identifier not found or not unique.
function transfer(address _to, unit256 _value) public returns (bool success)
^—–^
Compilation failed. See below
code:
pragma solidity ^0.4.23;
contract SandeepDappToken
{
// constructor
// set the v ariable
uint256 public totalSupply;
string public name = ‘Dapp’;
mapping(address => uint256) public balanceOf;
constructor(uint256 _initialSupply ) public {
balanceOf[msg.sender] = _initialSupply;
totalSupply = _initialSupply;
}
function transfer(address _to, unit256 _value) public returns (bool success)
{
require(balanceOf[msg.sender] >= _value);
}
}
Change your code:
unit256 to uint256
function transfer(address _to, uint256 _value) public returns (bool success)