# Smart Contract Basics **Published by:** [meteosrds](https://paragraph.com/@cryptovuive/) **Published on:** 2023-01-10 **URL:** https://paragraph.com/@cryptovuive/smart-contract-basics ## Content Solidity Syntax // SPDX-License-Identifier: MIT pragma solidity ^0.8.4; contract Contract { bool public a = true; bool public b = false; } // SPDX-License-Identifier: MIT pragma solidity ^0.8.4; contract Contract { uint8 public a = 3; uint16 public b = 268; uint256 public sum = a + b; } // SPDX-License-Identifier: MIT pragma solidity ^0.8.4; contract Contract { int8 public a = 10; int8 public b = -15; int16 public difference = a - b; } // SPDX-License-Identifier: MIT pragma solidity ^0.8.4; contract Contract { bytes32 public msg1 = "Hello World"; string public msg2 = "ccccccccccccccccccccccccccccccccabcd"; } // SPDX-License-Identifier: MIT pragma solidity ^0.8.4; contract Contract { enum Foods { Apple, Pizza, Bagel, Banana } Foods public food1 = Foods.Apple; Foods public food2 = Foods.Pizza; Foods public food3 = Foods.Bagel; Foods public food4 = Foods.Banana; } Functions // SPDX-License-Identifier: MIT pragma solidity ^0.8.4; contract Contract { uint public x; constructor(uint _x) { x = _x; } } // SPDX-License-Identifier: MIT pragma solidity ^0.8.4; contract Contract { uint public x; constructor(uint _x) { x = _x; } function increment() external { x = x + 1; } } // SPDX-License-Identifier: MIT pragma solidity ^0.8.4; contract Contract { uint public x; constructor(uint _x) { x = _x; } function increment() external { x = x + 1; } function add(uint _x) external view returns(uint) { uint sum = x + _x; return sum; } } // SPDX-License-Identifier: MIT pragma solidity ^0.8.4; contract Contract { function double(uint x) external pure returns(uint d) { d = x * 2; } } // SPDX-License-Identifier: MIT pragma solidity ^0.8.4; contract Contract { function double(uint x) external pure returns(uint d) { d = x * 2; } function double(uint x, uint y) public pure returns(uint, uint) { return (x*2, y*2); } } Smart Contract Communication /** * Find the `value` stored in the contract * * @param {ethers.Contract} contract - ethers.js contract instance * @return {promise} a promise which resolves with the `value` */ function getValue(contract) { let value = contract.value(); return value; } module.exports = getValue; /** * Modify the `value` stored in the contract * * @param {ethers.Contract} contract - ethers.js contract instance * @return {promise} a promise of transaction */ function setValue(contract) { return contract.modify(6); } module.exports = setValue; /** * Transfer funds on the contract from the current signer * to the friends address * * @param {ethers.Contract} contract - ethers.js contract instance * @param {string} friend - a string containing a hexadecimal ethereum address * @return {promise} a promise of the transfer transaction */ function transfer(contract, friend) { return contract.transfer(friend, 1000); } module.exports = transfer; /** * Set the message on the contract using the signer passed in * * @param {ethers.Contract} contract - ethers.js contract instance * @param {ethers.types.Signer} signer - ethers.js signer instance * @return {promise} a promise of transaction modifying the `message` */ function setMessage(contract, signer) { return contract.connect(signer).modify("abc"); } module.exports = setMessage; const ethers = require('ethers'); /** * Deposit at least 1 ether into the contract * * @param {ethers.Contract} contract - ethers.js contract instance * @return {promise} a promise of the deposit transaction */ function deposit(contract) { return contract.deposit({ value: ethers.utils.parseEther("3") }); } module.exports = deposit; Address Interactions // SPDX-License-Identifier: MIT pragma solidity ^0.8.4; contract Contract { address public owner; constructor() { owner = msg.sender; } } // SPDX-License-Identifier: MIT pragma solidity ^0.8.4; contract Contract { address public owner; constructor() { owner = msg.sender; } receive() external payable { } } // SPDX-License-Identifier: MIT pragma solidity ^0.8.4; contract Contract { address public owner; constructor() { owner = msg.sender; } receive() external payable { } function tip() public payable { (bool ret, ) = owner.call{ value: msg.value }(""); require(ret); } } // SPDX-License-Identifier: MIT pragma solidity ^0.8.4; contract Contract { address public owner; address public charity; constructor(address _charity) { owner = msg.sender; charity = _charity; } receive() external payable { } function tip() public payable { (bool ret, ) = owner.call{ value: msg.value }(""); require(ret); } function donate() public { (bool ret, ) = charity.call{ value: address(this).balance }(""); require(ret); selfdestruct(payable(msg.sender)); } } // SPDX-License-Identifier: MIT pragma solidity ^0.8.4; contract Contract { address public owner; address public charity; constructor(address _charity) { owner = msg.sender; charity = _charity; } receive() external payable { } function tip() public payable { (bool ret, ) = owner.call{ value: msg.value }(""); require(ret); } function donate() public { (bool ret, ) = charity.call{ value: address(this).balance }(""); require(ret); selfdestruct(payable(msg.sender)); } } // SPDX-License-Identifier: MIT pragma solidity ^0.8.4; contract Contract { address deployer; constructor() payable { require(msg.value >= 1000000000000000000); deployer = msg.sender; } function withdraw() public { (bool ret, ) = deployer.call{ value: address(this).balance }(""); require(ret); } } // SPDX-License-Identifier: MIT pragma solidity ^0.8.4; contract Contract { address deployer; constructor() payable { require(msg.value >= 1000000000000000000); deployer = msg.sender; } function withdraw() public { require(deployer == msg.sender); (bool ret, ) = deployer.call{ value: address(this).balance }(""); require(ret); } } // SPDX-License-Identifier: MIT pragma solidity ^0.7.5; contract Contract { address owner; uint configA; uint configB; uint configC; constructor() { owner = msg.sender; } function setA(uint _configA) public onlyOwner { configA = _configA; } function setB(uint _configB) public onlyOwner { configB = _configB; } function setC(uint _configC) public onlyOwner { configC = _configC; } modifier onlyOwner { require(owner == msg.sender); _; } } // SPDX-License-Identifier: MIT pragma solidity ^0.8.4; interface IHero { function alert() external; } contract Sidekick { function sendAlert(address hero) external { IHero(hero).alert(); } } // SPDX-License-Identifier: MIT pragma solidity ^0.8.4; contract Sidekick { function sendAlert(address hero) external { bytes4 signature = bytes4(keccak256("alert()")); (bool ret, ) = hero.call(abi.encodePacked(signature)); require(ret); } } // SPDX-License-Identifier: MIT pragma solidity ^0.8.4; contract Sidekick { function sendAlert(address hero, uint enemies, bool armed) external { (bool ret, ) = hero.call( abi.encodeWithSignature("alert(uint256,bool)", enemies, armed) ); require(ret); } } // SPDX-License-Identifier: MIT pragma solidity ^0.8.4; contract Sidekick { function relay(address hero, bytes memory data) external { (bool ret, ) = hero.call(data); require(ret); } } // SPDX-License-Identifier: MIT pragma solidity ^0.8.4; contract Sidekick { function makeContact(address hero) external { (bool ret, ) = hero.call(abi.encodeWithSignature("fallback()")); require(ret); } } pragma solidity ^0.8.4; contract Contract { function sumAndAverage(uint a, uint b, uint c, uint d) external pure returns(uint,uint) { uint sum = a + b + c + d; uint average = sum / 4; return (sum, average); } } // SPDX-License-Identifier: MIT pragma solidity ^0.8.4; contract Contract { uint countdown = 10; function tick() external { countdown--; if (0 == countdown) { selfdestruct(payable(msg.sender)); } } } ## Publication Information - [meteosrds](https://paragraph.com/@cryptovuive/): Publication homepage - [All Posts](https://paragraph.com/@cryptovuive/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@cryptovuive): Subscribe to updates - [Twitter](https://twitter.com/meteosrds): Follow on Twitter