Remix
獲取空白地址
我的程式碼有一點問題。我試圖用變數
currentSender
and分別跟踪交易的目前發送者和前一個發送者lastSender
,然後將它們註冊到嵌套在 struct 中的映射中Order
。然而,通過我創建的 get 函式,我看到所有lastSender
變數都給了我空白地址,同時currentSender
給了我好的地址。這是程式碼:
pragma solidity ^0.5.16; contract SupChain{ struct Status{ address currentSender; address lastSender; string test; } struct Order{ uint256 orderID; uint256 NumberOfUpdate; string test2; mapping (uint256 => Status) statutes; } uint256 public orderCount = 0; mapping (uint256 => Order) public orders; function createOrder(string memory test, string memory test2) public{ orderCount++; Order storage newOrder = orders[orderCount]; orders[orderCount] = Order(orderCount, 0, test2); newOrder.statutes[newOrder.NumberOfUpdate++] = Status(msg.sender, msg.sender, test); } function storeOrder(uint256 id, string memory test) public{ Order storage concernedOrder = orders[id]; //Getting the previous sender before updating the NumberOfUpdate Status memory previousStatus = concernedOrder.statutes[concernedOrder.NumberOfUpdate]; address previousSender = previousStatus.currentSender; concernedOrder.NumberOfUpdate = concernedOrder.NumberOfUpdate++; concernedOrder.statutes[concernedOrder.NumberOfUpdate++] = Status(msg.sender, previousSender, test); } function getStatus(uint256 id, uint256 concernedStatusNumber) public view returns(address _currentSender, address _lastSender, string memory _test){ Order storage concernedOrder = orders[id]; Status memory concernedStatus = concernedOrder.statutes[concernedStatusNumber]; return(concernedStatus.currentSender, concernedStatus.lastSender, concernedStatus.test); } }
所以,我想請求一些幫助來解決這個問題。
我提前感謝任何願意花時間幫助我的人。
我嘗試了什麼:
從函式中省略新訂單的遞增,
createOrder
並且顯然讓 else 與以前相同:function storeOrder(uint256 id, string memory test) public{ Order storage concernedOrder = orders[id]; //Getting the previous sender before updating the NumberOfUpdate Status memory previousStatus = concernedOrder.statutes[concernedOrder.NumberOfUpdate]; address previousSender = previousStatus.currentSender; concernedOrder.NumberOfUpdate = concernedOrder.NumberOfUpdate++; concernedOrder.statutes[concernedOrder.NumberOfUpdate] = Status(msg.sender, previousSender, test); }
previousStatus
為函式的變數添加一個減量,storeOrder
並保持與以前相同:function storeOrder(uint256 id, string memory test) public{ Order storage concernedOrder = orders[id]; //Getting the previous sender before updating the NumberOfUpdate Status memory previousStatus = concernedOrder.statutes[concernedOrder.NumberOfUpdate--]; address previousSender = previousStatus.currentSender; concernedOrder.NumberOfUpdate = concernedOrder.NumberOfUpdate++; concernedOrder.statutes[concernedOrder.NumberOfUpdate++] = Status(msg.sender, previousSender, test); }
現在我正在查看問題是否來自我的
getStatus
功能
您變空的原因是因為您在創建訂單時
lastSender
正在遞增。newOrder.NumberOfUpdate++
它將狀態儲存在索引處0
並遞增變數。當您設置訂單時,它會檢索增加的值,該值1
在該點上並且沒有針對該索引的狀態,因為狀態儲存在 index0
中。您可以省略 in 的遞增,
createOrder
也可以 在檢索舊狀態時NumberOfUpdate
遞減in 。1``setOrder
您的 createOrder 令人困惑,請嘗試這樣
function createOrder(string memory test, string memory test2) public{ orderCount++; orders[orderCount] = Order(orderCount, 0, test2); orders[orderCount].NumberOfUpdate++; orders[orderCount].statutes[orders[orderCount].NumberOfUpdate] = Status(msg.sender, msg.sender, test); }
我建議不要從 1 開始您的訂單計數,作為開發人員處理沒有 0 索引的數組/映射會令人困惑。從我的角度來看,這樣的東西看起來更好。
function createOrder(string memory test, string memory test2) public{ // orderCount is 0 for first order orders[orderCount] = Order(orderCount, 0, test2); // first update update is stored at 0 orders[orderCount].statutes[orders[orderCount].NumberOfUpdate] = Status(msg.sender, msg.sender, test); orders[orderCount].NumberOfUpdate++; orderCount++; }