MyContractInstance.methods.MyMethod不是函数(合同实例无法获取合同方法)web3



我正在尝试使用web3.js实现ERC20代币合约。我创建了一个契约的实例,但当我将其记录在浏览器控制台中时,我不会得到一些后来新创建的方法。

deployAddress和abi都很好。

如果有人能帮忙的话,这是代码片段。

代币合约

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

}
});

控制台中的输出:[1]:https://i.stack.imgur.com/KXJNJ.jpg

实际上,我的一些合约方法是不可见的,所以尽管我创建了合约实例,但我无法访问它们。

为什么会发生这种情况,有什么答案吗>gt>

我得到了答案。我使用的是erc20.sol的ABI,而不是代币的ABI。sol。谢谢!

最新更新