**根据评论进行了更新
与合同进行交互并直接从松露迁移输出的输出中直接使用“合同地址:”调用函数时,错误也是无效地址。
-
Solidity合同和web3.js的新功能-(尽管我已经尝试了好几个星期了!)
我正在使用react.js,truffle-contract和web3创建一个Dapp,并从Ganache连接到我的智能合约。我也在通过webpack管理我的应用程序。
我已经在Solidity中编写了一个简单的合同(下面列出了版本),并且可以从松露控制台毫无问题地连接到合同。
通过简单的(对于本演示而言)enroll()函数连接到合同时,我收到了Error: invalid address
,现在我已经以多种方式重新编写了代码,无论如何总是收到相同的错误。 / p>
在查看了此处的大量帖子后,我了解到这样做的一个相当普遍的问题是我需要设置“默认值”,这是我在componentDidMount函数中首次连接到web3时第一次完成的操作,通过contract.defaults函数连接到合同时也是如此。到目前为止,这也没有改变。
任何关于我在这里做错事情的想法都将不胜感激!
相关版本如下
“ web3”:“ ^ 1.2.1”, “ webpack”:“ ^ 4.20.2”, “ react”:“ ^ 16.2.0”,
“ 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))
};
答案 0 :(得分:0)
您应使用contract.new()
部署新合同。如果要使用已部署的合同,请使用contract.at(<address of deployed contract>)
。
contract.deployed()
例如用于测试,它会在您运行truffle test
命令时返回在迁移脚本中部署的合同。