├── LICENSE ├── README.md ├── contracts ├── GetAndSet.sol ├── GetAndSet2.sol ├── Migrations.sol └── Visibility.sol ├── migrations ├── 1_initial_migration.js └── 2_deploy_contracts.js ├── test └── testGetAndSet2.js ├── truffle-config.js └── truffle.js /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 jim-steele 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # solidity-examples 2 | Example Ethereum Solidity Contracts 3 | -------------------------------------------------------------------------------- /contracts/GetAndSet.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.5.0; 2 | 3 | /// @title Simple example of setting and getting storage data 4 | /// @author Dev Name 5 | /// @dev Storage is costly and should only be used for critical data 6 | contract GetAndSet { 7 | uint16[3] storedData; 8 | 9 | function setStoredData(uint8 n, uint16 x) public { 10 | storedData[n] = x; 11 | } 12 | 13 | function getStoredData(uint8 n) public view returns (uint16) { 14 | return storedData[n]; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /contracts/GetAndSet2.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.5.0; 2 | 3 | /// @title Simple example of setting and getting storage data with owner and selfdestruct 4 | /// @author Dev Name 5 | /// @dev Storage is costly and should only be used for critical data 6 | contract GetAndSet2 { 7 | address payable public owner; 8 | string[2] storedData; 9 | 10 | function setStoredData(uint8 n, string memory x) public { 11 | require(n < 2); 12 | storedData[n] = x; 13 | } 14 | 15 | function getStoredData(uint8 n) public view returns (string memory) { 16 | require(n < 2); 17 | return storedData[n]; 18 | } 19 | 20 | function getOwner() public returns (address) { 21 | return owner; 22 | } 23 | 24 | constructor() public { 25 | owner = msg.sender; 26 | } 27 | 28 | function goodbye() public { 29 | selfdestruct(owner); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /contracts/Migrations.sol: -------------------------------------------------------------------------------- 1 | pragma solidity >=0.4.21 <0.6.0; 2 | 3 | contract Migrations { 4 | address public owner; 5 | uint public last_completed_migration; 6 | 7 | constructor() public { 8 | owner = msg.sender; 9 | } 10 | 11 | modifier restricted() { 12 | if (msg.sender == owner) _; 13 | } 14 | 15 | function setCompleted(uint completed) public restricted { 16 | last_completed_migration = completed; 17 | } 18 | 19 | function upgrade(address new_address) public restricted { 20 | Migrations upgraded = Migrations(new_address); 21 | upgraded.setCompleted(last_completed_migration); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /contracts/Visibility.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.5.0; 2 | 3 | /// @title addOne 4 | 5 | contract Visibility { 6 | uint public data1; 7 | mapping (uint => string) public values; 8 | mapping (bytes32 => uint) public balances; 9 | 10 | function getData() public view returns (uint) { return data1; } 11 | function doIt(uint a) public pure returns (uint b) { return a+1; } 12 | function keepIt(uint a) public { data1 = 2*doIt(a); } 13 | 14 | function assignv(uint a, string memory b) public { values[a] = b;} 15 | function readv(uint a) public view returns (string memory) { return values[a]; } 16 | 17 | function assign(bytes32 a, uint b) public { balances[a] = b; } 18 | function read(bytes32 a) public view returns (uint) { return balances[a]; } 19 | } 20 | 21 | contract childOfAddOne is Visibility { 22 | } 23 | 24 | contract anotherOne { 25 | //function tryToDoIt(uint a) { doIt(a); } 26 | //function tryToKeepIt(uint a) { keepIt(a); } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /migrations/1_initial_migration.js: -------------------------------------------------------------------------------- 1 | var Migrations = artifacts.require("./Migrations.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Migrations); 5 | }; 6 | -------------------------------------------------------------------------------- /migrations/2_deploy_contracts.js: -------------------------------------------------------------------------------- 1 | var getAndSet = artifacts.require("./GetAndSet.sol"); 2 | var getAndSet2 = artifacts.require("./GetAndSet2.sol"); 3 | var viz = artifacts.require("./Visibility.sol"); 4 | 5 | module.exports = function(deployer) { 6 | deployer.deploy(getAndSet); 7 | deployer.deploy(getAndSet2); 8 | deployer.deploy(viz); 9 | }; 10 | -------------------------------------------------------------------------------- /test/testGetAndSet2.js: -------------------------------------------------------------------------------- 1 | var GetAndSet2 = artifacts.require("./GetAndSet2.sol"); 2 | 3 | contract('GetAndSet2TestAsync', async (accounts) => { 4 | 5 | it("should be zero if unset", async () => { 6 | let instance = await GetAndSet2.deployed(); 7 | let value = await instance.getStoredData(1); 8 | assert.equal(value.valueOf(), 0, "Unset parameter was not zero"); 9 | }); 10 | 11 | it("should store the specified value", async () => { 12 | var specifiedIdx = 0; 13 | var specifiedVal = 'howdy'; 14 | 15 | let instance = await GetAndSet2.deployed(); 16 | await instance.setStoredData(specifiedIdx, specifiedVal); 17 | let storedData = await instance.getStoredData(specifiedIdx); 18 | assert.equal(storedData, specifiedVal, "Set parameter was not returned"); 19 | }); 20 | 21 | }); 22 | -------------------------------------------------------------------------------- /truffle-config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | }; 5 | -------------------------------------------------------------------------------- /truffle.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | networks: { 5 | development: { 6 | host: "localhost", 7 | port: 8545, 8 | network_id: "*" // Match any network id 9 | } 10 | } 11 | }; 12 | --------------------------------------------------------------------------------