Reactjs 从Web.js连接到Solidity contract时地址无效
**根据评论更新 当与契约交互并直接使用“契约地址:”从块菌迁移输出直接调用函数时,错误也是无效地址Reactjs 从Web.js连接到Solidity contract时地址无效,reactjs,solidity,web3,truffle,Reactjs,Solidity,Web3,Truffle,**根据评论更新 当与契约交互并直接使用“契约地址:”从块菌迁移输出直接调用函数时,错误也是无效地址 loadDataContract = () => { const contract = TruffleContract(DataAccess) contract.setProvider(this.web3Provider) contract.defaults({from: this.web3Provider}); // init
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))
};
-
刚接触Solidity contracts和web3.js(尽管我已经尝试了好几个星期了!!)
我正在使用react.js、truffle contract和web3创建一个Dapp,并从Ganache连接到我的智能合约。我还通过网页包管理我的应用程序
我已经用Solidity(如下所示的版本)编写了一份简单的合同,并且可以从truffle控制台毫无问题地连接到合同
当通过一个简单的(对于本演示)enroll()函数连接到合同时,我收到了一个错误:无效地址
,我现在已经用多种方式重新编写了代码,并且不管发生什么,总是收到相同的错误
在这里查看了很多帖子后,我了解到这样做时一个相当常见的问题是我需要设置“默认值”——这是我在componentDidMount函数中第一次连接到web3时以及通过contract.defaults函数连接到contract时都做过的。到目前为止,这也没有造成什么不同
任何关于我在这里做错了什么的想法都将不胜感激
相关版本如下
“web3”:“^1.2.1”,
“网页包”:“^4.20.2”,
“反应”:“^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;
}
下面是solidity合同的一部分
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()
contract.deployed()
例如用于测试,当您运行truffle test
命令时,它返回在迁移脚本中部署的契约。您应该使用contract.new()
部署新契约。如果要使用已部署的协定,请使用contract.at()
contract.deployed()
例如用于测试,当您运行truffle test
命令时,它返回在迁移脚本中部署的契约。谢谢@igor Sobolev。在重写代码时,我仍然无法访问合同,并且出现了相同的错误。让我们看看问题的更新代码片段。如果您对合同中的参数有任何其他想法,我们将不胜感激。默认值应该是发送交易的地址(web3Provider是否返回地址?)。此外,您可以尝试在中重写函数调用。然后()
如下:合同('0x8A4A1247948A427109E964E90CAEB5798C13A01')。然后((实例)=>return instance.enroll())。然后((输出)=>…
非常感谢您的建议!我现在得到了要加载的合同,但不得不使用这个.web3Provider.selectedAddress合同。默认值({from:this.web3Provider.selectedAddress})
这就是我阅读的文档似乎没有使用的用户地址传递给合同的方式吗?您对如何连接到合同而不写合同有什么建议吗?地址('0x8a4A12479486A427109e964e90CaEB5798C13A01')
并传递一个变量而不是合同ID?再次感谢IgorThanks@igor Sobolev。在重写代码时,我仍然无法访问合同,并且出现了相同的错误。让我们看看问题的更新代码片段。对于合同中的参数的任何其他想法,我们将不胜感激。默认值>应该是从中发送事务的地址(web3Provider是否返回地址?)=>…
非常感谢您的建议!我现在获得了要加载的合同,但必须使用this.web3Provider.selectedAddresscontract.defaults({from:this.web3Provider.selectedAddress})
这就是我阅读的文档似乎没有使用的用户地址传递给合同的方式吗?您对如何连接到合同而不写合同有什么建议吗?地址('0x8a4A12479486A427109e964e90CaEB5798C13A01')
并传递一个变量而不是合同ID?再次感谢Igor