I am trying to implement ERC20 Token Contract using web3.js. I created an instance of the contract, but when I am logging it in the browser console, I am not getting some of the later newly created methods.
The deployAddress and the abi are perfectly alright.
Here, is the code snippet, if anyone can help.
Token contract
pragma solidity ^0.6.0;
import "/contracts/ERC20.sol";
contract YatharthaMudra is IERC20{
string public symbol;
string public name;
uint public decimals;
uint public __totalSupply;
mapping(address => uint) private __balanceOf;
mapping(address => mapping(address => uint)) __allowances;
constructor () public{
symbol = "YMT";
name = "Yathaartha Mudra";
decimals = 18;
__totalSupply = 1000*10**decimals;
__balanceOf[msg.sender] = __totalSupply;
}
function getName() public view returns (string memory){
return name;
}
function getSymbol() public view returns (string memory){
return symbol;
}
function getDecimal() public view returns (uint){
return decimals;
}
function totalSupply()external view override returns (uint _totalSupply){
_totalSupply = __totalSupply;
}
function balanceOf(address _addr) public view override returns(uint _balanceOf){
return __balanceOf[_addr];
}
function transfer(address _to, uint256 _value) external override returns (bool _success){
//require instead of if
if(_value <= __balanceOf[msg.sender]&&
_value > 0){
__balanceOf[msg.sender] -= _value;
__balanceOf[_to] += _value;
return true;
}
return false;
}
function transferFrom(address _from, address _to, uint256 _value) external override returns (bool _success){
if(__allowances[_from][msg.sender] > 0 &&
_value > 0 &&
__allowances[_from][msg.sender] >= _value &&
__balanceOf[_from] >= _value){
__balanceOf[_from] -= _value;
__balanceOf[_to] += _value;
__allowances[_from][msg.sender] -= _value;
return true;
}
return false;
}
function approve(address _spender, uint256 _value) external override returns (bool _success){
__allowances[msg.sender][_spender] = _value;
return true;
}
function allowance(address _owner, address _spender) external view override returns (uint256 _remaining){
return __allowances[_owner][_spender];
}
}
index.js(web3.js)
$(document).ready(function(){
if(!window.ethereum){
alert("Please install Metamask!");
}
else{
window.web3 = new Web3(window.ethereum);
console.log("Metamask connected");
console.log(window.ethereum);
web3.eth.net.getId().then(console.log);
web3.eth.getAccounts().then(function(accounts){
let account = accounts[0];
web3.eth.getBalance(account).then(function(balance){
let balance1 = web3.utils.fromWei(balance);
console.log(balance1+" ETH");
});
});
var abi = /* abi here */
var contractAddress = /* contract deploy address here */
var YTMTokenContract = new web3.eth.Contract(abi, contractAddress);
console.log(YTMTokenContract);
YTMTokenContract.methods.getName().call(function(token_name){
console.log(token_name);
tokenSupply.innerHTML = token_name[enter image description here][1];
}).catch(function(err) {
console.log(err);
});
YTMTokenContract.methods.totalSupply().call(function(total_supply){
console.log(total_supply);
tokenSupply.innerHTML = total_supply;
}).catch(function(err) {
console.log(err);
});
}
});
Output in the console: [1]: https://i.stack.imgur.com/KXJNJ.jpg
Actually, some of my contract methods are not visible, so I cannot access them although I created the contract Instance.
Any answers to why this happens?>>>
I got my answer. I was using the ABI of erc20.sol and not of the token.sol . Thankyou!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.