embark/lib/modules/scaffolding-solidity/templates/contract.sol.hbs

51 lines
1.4 KiB
Handlebars
Raw Normal View History

pragma solidity ^0.4.24;
contract {{contractName}} {
struct {{structName}} {
{{#each fields}}
{{type}} {{name}};
{{/each}}
}
2018-10-17 21:13:36 +00:00
{{structName}}[] items;
event ItemCreated(uint id, address createdBy);
event ItemDeleted(uint id, address deletedBy);
event ItemUpdated(uint id, address updatedBy);
function add({{#each fields}}{{type}} _{{name}}{{#unless @last}}, {{/unless}}{{/each}}) public {
uint id = items.length++;
items[id] = {{structName}}({
{{#each fields}}
{{name}}: _{{name}}{{#unless @last}},{{/unless}}
{{/each}}
});
emit ItemCreated(id, msg.sender);
}
function edit(uint _id, {{#each fields}}{{type}} _{{name}}{{#unless @last}}, {{/unless}}{{/each}}) public {
require(_id < items.length, "Invalid {{structName}} id");
{{#each fields}}
items[_id].{{name}} = _{{name}};
{{/each}}
emit ItemUpdated(_id, msg.sender);
}
function remove(uint _id) public {
require(_id < items.length, "Invalid {{structName}} id");
delete items[_id];
emit ItemDeleted(_id, msg.sender);
}
2018-10-17 21:13:36 +00:00
function get(uint _id) public view returns ({{#each fields}}{{type}}{{#unless @last}},{{/unless}}{{/each}}) {
require(_id < items.length, "Invalid ArrayContractStruct id");
return ({{#each fields}}items[_id].{{name}}{{#unless @last}},{{/unless}}{{/each}});
}
}