Open freedomhero opened 2 years ago
/// Overloaded function with different return value!
function get_pid_state() view private returns (uint32) {
return reaped;
}
public view function
Visibility https://bitsofco.de/solidity-function-visibility-explained/
struct inside contract
qualified import
dynamic array: static?
x << y is equivalent to the mathematical expression x * 2**y.
x >> y is equivalent to the mathematical expression x / 2**y, rounded towards negative infinity.
Address related:
function reduce(
uint[] memory self,
function (uint, uint) pure returns (uint) f
)
internal
pure
returns (uint r)
{
r = self[0];
for (uint i = 1; i < self.length; i++) {
r = f(r, self[i]);
}
}
memory
/ storage
/ calldata
https://hackernoon.com/getting-deep-into-evm-how-ethereum-works-backstage-ac7efa1f0015
struct Campaign {
address beneficiary;
uint fundingGoal;
uint numFunders;
uint amount;
mapping (uint => Funder) funders;
}
https://solidity-cn.readthedocs.io/zh/stable/units-and-global-variables.html
function arithmetics(uint _a, uint _b)
public
pure
returns (uint o_sum, uint o_product)
{
o_sum = _a + _b;
o_product = _a * _b;
}
while
/do
/for
/break
/continue
/try
/catch
https://solidity-by-example.org/try-catch
address => Ripemd160 ?
function population_count(uint n) pure public returns (uint count) {
for (count = 0; n != 0; count++) {
n &= (n - 1);
}
}
address => Ripemd160 ?
address => PubKeyHash
enum State {
Running,
Sleeping,
Waiting,
Stopped,
Zombie,
StateCount
}
https://docs.soliditylang.org/en/v0.8.10/contracts.html#creating-contracts
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.22 <0.9.0;
contract OwnedToken {
// `TokenCreator` is a contract type that is defined below.
// It is fine to reference it as long as it is not used
// to create a new contract.
TokenCreator creator;
address owner;
bytes32 name;
// This is the constructor which registers the
// creator and the assigned name.
constructor(bytes32 _name) {
// State variables are accessed via their name
// and not via e.g. `this.owner`. Functions can
// be accessed directly or through `this.f`,
// but the latter provides an external view
// to the function. Especially in the constructor,
// you should not access functions externally,
// because the function does not exist yet.
// See the next section for details.
owner = msg.sender;
// We perform an explicit type conversion from `address`
// to `TokenCreator` and assume that the type of
// the calling contract is `TokenCreator`, there is
// no real way to verify that.
// This does not create a new contract.
creator = TokenCreator(msg.sender);
name = _name;
}
function changeName(bytes32 newName) public {
// Only the creator can alter the name.
// We compare the contract based on its
// address which can be retrieved by
// explicit conversion to address.
if (msg.sender == address(creator))
name = newName;
}
function transfer(address newOwner) public {
// Only the current owner can transfer the token.
if (msg.sender != owner) return;
// We ask the creator contract if the transfer
// should proceed by using a function of the
// `TokenCreator` contract defined below. If
// the call fails (e.g. due to out-of-gas),
// the execution also fails here.
if (creator.isTokenTransferOK(owner, newOwner))
owner = newOwner;
}
}
contract TokenCreator {
function createToken(bytes32 name)
public
returns (OwnedToken tokenAddress)
{
// Create a new `Token` contract and return its address.
// From the JavaScript side, the return type
// of this function is `address`, as this is
// the closest type available in the ABI.
return new OwnedToken(name);
}
function changeName(OwnedToken tokenAddress, bytes32 name) public {
// Again, the external type of `tokenAddress` is
// simply `address`.
tokenAddress.changeName(name);
}
// Perform checks to determine if transferring a token to the
// `OwnedToken` contract should proceed
function isTokenTransferOK(address currentOwner, address newOwner)
public
pure
returns (bool ok)
{
// Check an arbitrary condition to see if transfer should proceed
return keccak256(abi.encodePacked(currentOwner, newOwner))[0] == 0x7f;
}
}
https://docs.soliditylang.org/en/v0.8.10/contracts.html#constant-and-immutable-state-variables
contract C {
string constant TEXT = "abc";
bytes32 constant MY_HASH = keccak256("abc");
uint immutable decimals;
uint immutable maxBalance;
address immutable owner = msg.sender;
constructor(uint _decimals, address _reference) {
decimals = _decimals;
// Assignments to immutables can even access the environment.
maxBalance = _reference.balance;
}
function isBalanceTooHigh(address _other) public view returns (bool) {
return _other.balance > maxBalance;
}
}
fixed
Can initialize properties
https://docs.soliditylang.org/en/v0.8.10/contracts.html#function-overloading
& or &= between integer
function is_power_of_2(int n) : bool {
return n != 0 && (n & (n - 1)) == 0;
}
https://docs.soliditylang.org/en/v0.8.10/contracts.html#abstract-contracts
- Create contracts
https://docs.soliditylang.org/en/v0.8.10/contracts.html#creating-contracts
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.4.22 <0.9.0; contract OwnedToken { // `TokenCreator` is a contract type that is defined below. // It is fine to reference it as long as it is not used // to create a new contract. TokenCreator creator; address owner; bytes32 name; // This is the constructor which registers the // creator and the assigned name. constructor(bytes32 _name) { // State variables are accessed via their name // and not via e.g. `this.owner`. Functions can // be accessed directly or through `this.f`, // but the latter provides an external view // to the function. Especially in the constructor, // you should not access functions externally, // because the function does not exist yet. // See the next section for details. owner = msg.sender; // We perform an explicit type conversion from `address` // to `TokenCreator` and assume that the type of // the calling contract is `TokenCreator`, there is // no real way to verify that. // This does not create a new contract. creator = TokenCreator(msg.sender); name = _name; } function changeName(bytes32 newName) public { // Only the creator can alter the name. // We compare the contract based on its // address which can be retrieved by // explicit conversion to address. if (msg.sender == address(creator)) name = newName; } function transfer(address newOwner) public { // Only the current owner can transfer the token. if (msg.sender != owner) return; // We ask the creator contract if the transfer // should proceed by using a function of the // `TokenCreator` contract defined below. If // the call fails (e.g. due to out-of-gas), // the execution also fails here. if (creator.isTokenTransferOK(owner, newOwner)) owner = newOwner; } } contract TokenCreator { function createToken(bytes32 name) public returns (OwnedToken tokenAddress) { // Create a new `Token` contract and return its address. // From the JavaScript side, the return type // of this function is `address`, as this is // the closest type available in the ABI. return new OwnedToken(name); } function changeName(OwnedToken tokenAddress, bytes32 name) public { // Again, the external type of `tokenAddress` is // simply `address`. tokenAddress.changeName(name); } // Perform checks to determine if transferring a token to the // `OwnedToken` contract should proceed function isTokenTransferOK(address currentOwner, address newOwner) public pure returns (bool ok) { // Check an arbitrary condition to see if transfer should proceed return keccak256(abi.encodePacked(currentOwner, newOwner))[0] == 0x7f; } }
new
always deploys a new instancecontract metaCoin {
...
}
}
contract coinCaller{
struct transfer{
metaCoin coinContract;
uint amount;
address recipient;
bool successful;
uint balance;
}
}
https://dappsforbeginners.wordpress.com/tutorials/interactions-between-contracts/
Delegatecall
// string literal contract Employee is Person { function gender() public returns (bytes32) { return "female"; } }
Literal 1e18
return
in the middle of a function.
function retrieve() external view returns (uint256){
uint256 x = number * 10;
if (number > 10) return x;
return x + 10;
}
.push
and .indexOf
contract C {
using Search for uint[];
uint[] data;
function append(uint value) public {
data.push(value);
}
function replace(uint _old, uint _new) public {
// This performs the library function call
uint index = data.indexOf(_old);
if (index == type(uint).max)
data.push(_new);
else
data[index] = _new;
}
}
**