Solidity
乙太坊合約問題
我已經編寫了一個契約
Transfer
,並且契約中定義的函式Transfer
在另一個契約中使用SecondTransfer
。我在契約中定義了一個建構子Transfer
並在該建構子中初始化了值。pragma solidity ^0.4.2; contract Transfer { address public userA; address public user; mapping (address => uint) balances; uint value2; // constructor function Transfer() { userA=msg.sender; balances[userA]=1000; } function sendEther(address user,uint value)returns (bool success){ if(balances[userA]< value){ return false; } else { balances[userA] -= value; balances[user] += value; return true; } } function sendAddressToAddress(address fromuser,address touser)returns (bool success){ uint value=balances[fromuser]; if(value < 0){ return false; } else { balances[fromuser] -= value; balances[touser] += value; return true; } } function getBalance(address user) returns (uint _balance){ return balances[user]; } } contract SecondTransfer { function sendEtherCall(address contractaddr,address user,uint value) returns (bool success) { Transfer transfer=Transfer(contractaddr); return transfer.sendEther(user,value); } function sendAddressToAddressCall(address contractaddr,address fromuser,address touser) returns (bool success) { Transfer transfer=Transfer(contractaddr); return transfer.sendAddressToAddress(fromuser,touser); } function getBalanceCall(address contractaddr,address user) returns (uint _balance) { Transfer transfer=Transfer(contractaddr); return transfer.getBalance(user); } }
我曾嘗試使用 browser-solidity 執行此合約。我已將合約的地址
Transfer
作為參數傳遞給合約Second Transfer
。但是該函式getBalanceCall
返回一個 VMException 錯誤。請幫我糾正這些問題。合約中的建構子初始化有什麼問題
Transfer
嗎?我也在嘗試使用帶有此契約的 Truffle 創建一個 DApp。在這種情況下,如何將
Transfer
合約地址作為參數傳遞給SecondTransfer
合約?
我已經嘗試過你的程式碼,它毫無例外地工作,我認為你傳遞了錯誤的合約地址。