I'm trying to write a TicTacToe game on remix.ethereum.org but all of a sudden all my test accounts show 0 ETH balance, so I can't use them to deploy my contracts anymore.
Here is the solidity code that I was deploying when it happened:
pragma solidity ^0.4.19;
contract TicTacToe {
uint8 public boardSize = 3;
address[3][3] board;
address public player1;
address public player2;
constructor() public {
player1 = msg.sender;
}
function joinGame() public {
assert(player2 == address(0));
player2 = msg.sender;
}
function setStone(uint8 x, uint8 y) public {
board[x][y] = msg.sender;
}
}
New test accounts still show 100 ETH upon creation.
I don't think this is anything to do with deploying your Solidity code.
What blockchain environment are you using? It is most likely a problem there.
Remix environment menu
Related
I'm trying to call a method from a contract that was instantiated from another contract. I use truffle console.
Here are the details: (I think you can gloss over the code, since this question is aimed at using truffle console, not fixing any bugs)
I have a contract called MyCoinSupply.sol:
pragma solidity ^0.8.0;
import "./MyCoin.sol";
contract MyCoinSupply is MyCoin("MyCoin", "MYC") // MyCoin is ERC20
{
constructor() public // gives 1000 tokens to the owner
{
_mint(msg.sender, 1000);
}
function getCoinbase() public returns (address) // for debugging purposes
{
return block.coinbase;
}
function _mintMinerReward() internal // gives 20 tokens to the miner
{
// _mint(block.coinbase, 20); // for some reason, block.coinbase is address 0
}
function _transfer(address from, address to, uint256 value) override internal
{
_mintMinerReward();
super._transfer(from, to, value);
}
}
I instantiate MyCoinSupply.sol within MyCoinDEX.sol:
pragma solidity ^0.8.0;
import "./IERC20.sol";
import "./MyCoinSupply.sol";
contract MyCoinDEX
{
IERC20 public token;
event Bought(uint256 amount);
event Sold(uint256 amount);
constructor() public
{
token = new MyCoinSupply();
}
function showSender() public view returns (address) // for debugging purposes
{
return (msg.sender);
}
function buy(uint256 amountTobuy) payable public // send ether and get tokens in exchange; 1 token == 1 ether
{
uint256 dexBalance = token.balanceOf(address(this));
require(amountTobuy > 0, "You need to send some ether");
require(amountTobuy <= dexBalance, "Not enough tokens in the reserve");
token.transfer(msg.sender, amountTobuy);
emit Bought(amountTobuy);
}
function sell(uint256 amount) public // send tokens to get ether back
{
require(amount > 0, "You need to sell at least some tokens");
uint256 allowance = token.allowance(msg.sender, address(this));
require(allowance >= amount, "Check the token allowance");
token.transferFrom(msg.sender, address(this), amount);
payable(msg.sender).transfer(amount);
emit Sold(amount);
}
}
Here's my migration for deploying contracts:
const MyCoinSupply = artifacts.require("MyCoinSupply");
const MyCoinDEX = artifacts.require("MyCoinDEX");
module.exports = function(deployer) {
deployer.deploy(MyCoinSupply);
deployer.deploy(MyCoinDEX);
};
Here's my problem:
When I try to call functions that belong to MyCoinDEX, all works well:
truffle(development)> let instance = await MyCoinDEX.deployed()
undefined
truffle(development)> instance.buy(1)
All is good here.
However, when I try the following I get an error:
truffle(development)> instance.token.balanceOf("0xE1994C1054f9c4171B8ae9a7E7a68F404c2bF829")
evalmachine.<anonymous>:0
instance.token.balanceOf("0xE1994C1054f9c4171B8ae9a7E7a68F404c2bF829")
^
Uncaught TypeError: instance.token.balanceOf is not a function
I Googled this error (I read this solution and this solution), but I didn't really stumble upon a solution. Can someone tell me how can I call token's methods from MyCoinDEX?
Thank you in advance!
I Need To Know The Balance Of This Token After I Deploy It In Remix Ethereum Firefox. I Want To Know Where Do I Add The checkBalance Function. Plz Help Guys. This Is My First ERC20 Smart Contract.
pragma solidity ^0.5.0;
contract TusharCoin {
uint256 public totalSupply;
string public name;
string public symbol;
uint32 public decimals;
address public owner;
mapping(address => uint256 ) balances;
event Transfer(address to, uint256 amount);
constructor () public {
symbol = "TUSHAR";
name = "TusharCoin";
decimals = 5;
totalSupply = 100000000000;
owner = msg.sender;
balances[msg.sender] = totalSupply;
emit Transfer(msg.sender, totalSupply);
}
}
Your token currently isn't an ERC20 token as it doesn't fully implement the ERC20 standard yet.
To just add a balanceOf function to your existing contract you can add the following:
function balanceOf(address account) public view returns (uint256) {
return balances[account];
}
If you are creating your own ERC20 implementation then you should consider using SafeMath, see the documentation for details: https://docs.openzeppelin.com/contracts/2.x/utilities#math
If you are creating ERC20 tokens you may want to look at the OpenZeppelin Contracts implementation to see if this meets your needs. See the documentation for details: https://docs.openzeppelin.com/contracts/2.x/tokens#ERC20
An example ERC20 Token that you can deploy with Remix inheriting from the OpenZeppelin Contracts implementation is below using your specified name, symbol, decimals and totalSupply:
pragma solidity ^0.5.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.3.0/contracts/token/ERC20/ERC20.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.3.0/contracts/token/ERC20/ERC20Detailed.sol";
contract Token is ERC20, ERC20Detailed {
constructor () public ERC20Detailed("Tushar Token", "TUSHAR", 5) {
_mint(msg.sender, 1000000 * (10 ** uint256(decimals())));
}
}
If you have questions on using OpenZeppelin you can ask in the Community Forum: https://forum.openzeppelin.com/
Disclosure: I am the Community Manager at OpenZeppelin
Below, I have mention checkBalance function. In ERC20 standards checkBalance function to be stated as a balanceOf function.
In function, view means one can only read not write
function balanceOf(address accountAddress) public view returns (uint256) {
return balances[accountAddress];
}
Full source code.
pragma solidity ^0.5.0;
contract TusharCoin {
uint256 public totalSupply;
string public name;
string public symbol;
uint32 public decimals;
address public owner;
mapping(address => uint256 ) balances;
event Transfer(address to, uint256 amount);
constructor () public {
symbol = "TUSHAR";
name = "TusharCoin";
decimals = 5;
totalSupply = 100000000000;
owner = msg.sender;
balances[msg.sender] = totalSupply;
emit Transfer(msg.sender, totalSupply);
}
function balanceOf(address accountAddress) public view returns (uint256) {
return balances[accountAddress];
}
}
If you want whole code for ERC20. Let me know.
I have deployed a very basic solidity contract (pragma solidity^0.4.0) to Rinkeby. When I call the getters on my state variables, nothing is returned and I do not understand why. To compile the contract I'm using "solc": "^0.4.25"
Using Remix to test, the call to the getter works locally without issue. However, the call doesn't work on my contract on Rinkeby.
Here is my solidity code...
pragma solidity^0.4.0;
contract Contest {
address public manager;
uint public submissionCost;
uint8 public votesPerSubmission;
constructor (uint _submissionCost, uint8 _votesPerSubmission) public {
manager = msg.sender;
submissionCost = _submissionCost;
votesPerSubmission = _votesPerSubmission;
}
modifier restricted() {
require(msg.sender == manager, "Not authorized.");
_;
}
function adjustSubmissionCost(uint32 newCost) public restricted {
submissionCost = newCost;
}
function adjustVotesPerSubmission(uint8 newVotes) public restricted {
votesPerSubmission = newVotes;
}
}
The address of the deployed contract on Rinkeby is
0xacBd19113e0D8122E18DF48A320b635fB5D7Cdd0
https://rinkeby.etherscan.io/address/0xacbd19113e0d8122e18df48a320b635fb5d7cdd0
When calling any of the public variables from Remix, I expect a return value, however I only receive the message call to Contest.manager in the Remix console and nothing more.
I get the same behavior in my web3 dapp, with zero return when I try...
await Contest.methods.manager().call()
i'm trying to use a contract instance as a variable of another contract, such as the example below.
pragma solidity ^0.4.23;
contract basic {
uint num1 = 10;
function getNum1() public view returns(uint) {
return num1;
}
function setNum1(uint _num) public returns(uint) {
num1 = _num;
}
}
contract parent {
uint public num2;
basic public b;
constructor() public {
b = new basic();
num2 = 20;
}
function getNum1() public constant returns(uint) {
return b.getNum1();
}
}
while when i test the contract in remix and truffle , it worked well.
enter image description here
but util i deployed the contract "parent" on my private network, parent.getNum1() returned '0' instead of '10' as supposed.
further more, i tried other type of constructors such as take an address of 'basic' as a parameter, it didn't work as well.
i also tried some contracts thats takes another contract instance as a variable, they all didn't work well on private network.
does anybody ever meet this problem? help!!!
coming to close the question now !
i deployed my contract on ropsten test network, and the contract worked well.
it seems that my private network didn't support the usage of calling from another contract. anybody interested can have a try to see.
I'm a bit lost on this spent a few hours on this but I cannot find out why, basically I have this Solidity contract (day 3 of luring blockchain). the code works in remix JS VM environment but when I try and build local and run it in ganache-cli with web3 it will deploy the first factory contract but it will not use the factory createFund function to make the second contract. is there something different form remix JS VM environment to a local one. or am I just missing something because I'm still new to this?
below is the code I'm playing with.
pragma solidity ^0.4.24;
contract testFactory{
address[] public deployedFund;
function createFund(string NewCampaignName) public{
address newFund = new testFund(NewCampaignName, msg.sender);
deployedFund.push(newFund);
}
function getDeployedFund() public view returns (address[]){
return deployedFund;
}
}
contract testFund {
struct OurFunders{
string FristName;
address ETHaddress;
uint128 Amount;
string Email;
uint8 Funderpercent;
}
OurFunders[] public ourFunders;
address public manager;
uint8 public Fpercent;
bool public IsFunded;
string public CampaignName;
constructor (string NewCampaignName, address creater) public{
manager = creater;
CampaignName = NewCampaignName;
}
function newFunder(string FristName, address ETHaddress, uint128 Amount, string Email, uint8 Funderpercent) public{
OurFunders memory newPerson = OurFunders({
FristName: FristName,
ETHaddress: ETHaddress,
Amount: Amount,
Email: Email,
Funderpercent: Funderpercent
});
Fpercent = Fpercent + Funderpercent;
if(Fpercent == 100){
IsFunded = true;
}
ourFunders.push(newPerson);
}
}