源码:
pragma solidity ^0.8.7;
contract TodoList {
struct Todo {
string text;
bool completed;
}
Todo[] public todos;
function create(string calldata _text) external {
todos.push(Todo({
text: _text,
completed: false
})
);
}
function updateText(uint _index, string calldata _text) external {
todos[_index].text = _text;
//把结构体装到storage中,两种方式消耗的gas费不同,更新的数据少,上面的gas消耗少,更新数据的,下面方法消耗gas少
Todo storage todo = todos[_index];
todo.text = _text;
}
function get(uint _index) external view returns (string memory, bool) {
// storage -29397
// memory -29480
Todo memory todo = todos[_index];
return(todo.text, todo.completed);
}
function toggleCompleted(uint _index) external {
todos[_index].completed = !todos[_index].completed;
}
}