当与合约交互并直接使用"合约地址:"调用函数时,直接从松露迁移输出的输出中调用函数时,错误也是无效地址。
我正在使用 react.js、truffle-contract 和 web3 创建一个 Dapp 并从 Ganache 连接到我的智能合约。我还通过 webpack 管理我的应用程序。
我在 Solidity 中编写了一个简单的合约(版本如下( - 并且可以从松露控制台毫无问题地连接到合约。
当通过一个简单的(对于这个演示(enroll(( 函数连接到合约时,我收到了一条Error: invalid address
我现在以多种方式重写了代码,并且无论如何总是收到相同的错误。
在这里查看了很多帖子后,我了解到这样做时一个相当常见的问题是我需要设置"默认值" - 当我第一次在我的 componentDidMount 函数中连接到 web3 时,以及通过 contract.defaults 函数连接到合约时,我都这样做了。到目前为止,这也没有产生任何影响。
任何关于我在这里做错了什么的想法将不胜感激!
相关版本如下
"web3": "^1.2.1","webpack": "^4.20.2",
"react": "^16.2.0",
"松露合同": "^3.0.4",
"solc": "0.4.18",
下面是尝试连接到合约的页面
componentDidMount = () => {
if(this.state.web3MetaON == false && this.state.accUnlock == false) {
if (typeof web3 != 'undefined') {
this.web3Provider = web3.currentProvider
web3 = new Web3(web3.currentProvider)
this.setState({web3MetaON: true})
const accountID = web3.eth.accounts[0];
web3.eth.defaultAccount = accountID;
console.log('Window opening in older browser')
// check if accountID is available
if(accountID !== null || accountID !== undefined) {
this.setState({accUnlock: true})
this.setState({account: accountID})
this.loadDataContract(accountID)
}
else {
console.log('Error on accessing account')
this.setState({accUnlock: false})
}
}
else {
window.alert("Please connect to Metamask.")
this.setState({web3MetaON: false})
// ::TO-DO method to invoke retry after 2 seconds
}
}
// Below loads web3 and sets state if browser
// is and a modern ethereum browser
else if (window.ethereum && this.state.web3MetaON == false && this.state.accUnlock == false) {
window.web3 = new Web3(ethereum)
try {
// Request account access if needed
const accountID = ethereum.enable()
web3.eth.sendTransaction({/* ... */})
// setting state to accountID
this.setState({account: accountID})
this.setState({accUnlock: true})
console.log('Window opening in modern browser')
} catch (error) {
console.log(error, 'Modern Browser failed')
this.setState({web3MetaON: false})
}
console.log('Non-Ethereum browser detected. You should consider trying MetaMask!')
}
};
loadDataContract = () => {
const contract = TruffleContract(DataAccess)
contract.setProvider(this.web3Provider)
contract.defaults({from: this.web3Provider});
// deploy contract
contract.deployed().then((DataAccessInstance) => {
this.DataAccessInstance = DataAccessInstance;
DataAccessInstance.enroll()
}).then(data => {
window.alert("contract loaded.", data)
}).catch(err => console.log("data load data this is ", err))
};
以下是坚固性合同的一部分
contract DataAccess {
// This declares a new complex type which
// will be used for variables
// it represents a single usersData
struct DataLocation {
string ownerName;
string ownerID;
string url;
string dateOfAccess;
string timeOfAccess;
uint accessCount;
uint index;
}
struct Index {
uint indexLocation;
}
// store Data that has a location
mapping(address => DataLocation) private datastores;
mapping (address => uint) private balances;
// store datalocation Count
uint public datalocationsCount;
// userIndex stors location of pointers
address[] public userIndex;
// stored event
event storedEvent (
uint indexed _dataLocationId
);
// event for new data location
event LogNewData (
address indexed dataAddress,
string ownerName,
string url,
string ownerID,
string dateOfAccess,
string timeOfAccess,
// uint accessCount,
uint index);
// event for new updated data location
event LogUpdateData (
address indexed dataAddress,
string ownerName,
string url,
string ownerID,
string dateOfAccess,
string timeOfAccess,
// uint accessCount,
uint index);
function enroll() public returns (uint){
/* Set the sender's balance to 1000, return the sender's balance */
address user = msg.sender;
balances[user] = 1000;
return user.balance;
}
当尝试根据反馈重写合约时,结果仍然是地址无效的错误。
loadDataContract = () => {
const contract = TruffleContract(DataAccess)
contract.setProvider(this.web3Provider)
contract.defaults({from: this.web3Provider});
// initial function
contract.at('0x8a4A12479486A427109e964e90CaEB5798C13A01').enroll().then((Output) => {
this.setState({value: Output})
}).catch(err => console.log("Enroll function error ", err))
};
您应该使用contract.new()
来部署新合约。如果要使用已部署的协定,请使用contract.at(<address of deployed contract>)
。
例如,contract.deployed()
用于测试,并在运行truffle test
命令时返回迁移脚本中部署的协定。