mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
+45
-30
@@ -757,20 +757,26 @@ No::
|
||||
|
||||
pragma solidity >=0.4.0 <0.7.0;
|
||||
|
||||
|
||||
// Base contracts just to make this compile
|
||||
contract B {
|
||||
constructor(uint) public {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
contract C {
|
||||
constructor(uint, uint) public {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
contract D {
|
||||
constructor(uint) public {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
contract A is B, C, D {
|
||||
uint x;
|
||||
|
||||
@@ -778,12 +784,12 @@ No::
|
||||
B(param1)
|
||||
C(param2, param3)
|
||||
D(param4)
|
||||
public
|
||||
{
|
||||
public {
|
||||
x = param5;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
contract X is B, C, D {
|
||||
uint x;
|
||||
|
||||
@@ -792,10 +798,11 @@ No::
|
||||
C(param2, param3)
|
||||
D(param4)
|
||||
public {
|
||||
x = param5;
|
||||
}
|
||||
x = param5;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
When declaring short functions with a single statement, it is permissible to do it on a single line.
|
||||
|
||||
Permissible::
|
||||
@@ -973,27 +980,32 @@ Yes::
|
||||
|
||||
pragma solidity >=0.4.0 <0.7.0;
|
||||
|
||||
|
||||
// Owned.sol
|
||||
contract Owned {
|
||||
address public owner;
|
||||
address public owner;
|
||||
|
||||
constructor() public {
|
||||
owner = msg.sender;
|
||||
}
|
||||
constructor() public {
|
||||
owner = msg.sender;
|
||||
}
|
||||
|
||||
modifier onlyOwner {
|
||||
require(msg.sender == owner);
|
||||
_;
|
||||
}
|
||||
modifier onlyOwner {
|
||||
require(msg.sender == owner);
|
||||
_;
|
||||
}
|
||||
|
||||
function transferOwnership(address newOwner) public onlyOwner {
|
||||
owner = newOwner;
|
||||
}
|
||||
function transferOwnership(address newOwner) public onlyOwner {
|
||||
owner = newOwner;
|
||||
}
|
||||
}
|
||||
|
||||
// Congress.sol
|
||||
and in ``Congress.sol``::
|
||||
|
||||
pragma solidity >=0.4.0 <0.7.0;
|
||||
|
||||
import "./Owned.sol";
|
||||
|
||||
|
||||
contract Congress is Owned, TokenRecipient {
|
||||
//...
|
||||
}
|
||||
@@ -1002,32 +1014,34 @@ No::
|
||||
|
||||
pragma solidity >=0.4.0 <0.7.0;
|
||||
|
||||
|
||||
// owned.sol
|
||||
contract owned {
|
||||
address public owner;
|
||||
address public owner;
|
||||
|
||||
constructor() public {
|
||||
owner = msg.sender;
|
||||
}
|
||||
constructor() public {
|
||||
owner = msg.sender;
|
||||
}
|
||||
|
||||
modifier onlyOwner {
|
||||
require(msg.sender == owner);
|
||||
_;
|
||||
}
|
||||
modifier onlyOwner {
|
||||
require(msg.sender == owner);
|
||||
_;
|
||||
}
|
||||
|
||||
function transferOwnership(address newOwner) public onlyOwner {
|
||||
owner = newOwner;
|
||||
}
|
||||
function transferOwnership(address newOwner) public onlyOwner {
|
||||
owner = newOwner;
|
||||
}
|
||||
}
|
||||
|
||||
// Congress.sol
|
||||
and in ``Congress.sol``::
|
||||
|
||||
import "./owned.sol";
|
||||
|
||||
|
||||
contract Congress is owned, tokenRecipient {
|
||||
//...
|
||||
}
|
||||
|
||||
|
||||
Struct Names
|
||||
==========================
|
||||
|
||||
@@ -1104,6 +1118,7 @@ added looks like the one below::
|
||||
|
||||
pragma solidity >=0.4.0 <0.7.0;
|
||||
|
||||
|
||||
/// @author The Solidity Team
|
||||
/// @title A simple storage example
|
||||
contract SimpleStorage {
|
||||
@@ -1126,4 +1141,4 @@ added looks like the one below::
|
||||
|
||||
It is recommended that Solidity contracts are fully annontated using `NatSpec <natspec>`_ for all public interfaces (everything in the ABI).
|
||||
|
||||
Please see the section about `NatSpec <natspec>`_ for a detailed explanation.
|
||||
Please see the section about `NatSpec <natspec>`_ for a detailed explanation.
|
||||
+86
-70
@@ -625,100 +625,116 @@ Example that shows how to use the members::
|
||||
|
||||
pragma solidity >=0.4.16 <0.7.0;
|
||||
|
||||
|
||||
contract Example {
|
||||
function f() public payable returns (bytes4) {
|
||||
return this.f.selector;
|
||||
}
|
||||
function g() public {
|
||||
this.f.gas(10).value(800)();
|
||||
}
|
||||
function f() public payable returns (bytes4) {
|
||||
return this.f.selector;
|
||||
}
|
||||
|
||||
function g() public {
|
||||
this.f.gas(10).value(800)();
|
||||
}
|
||||
}
|
||||
|
||||
Example that shows how to use internal function types::
|
||||
|
||||
pragma solidity >=0.4.16 <0.7.0;
|
||||
|
||||
|
||||
library ArrayUtils {
|
||||
// internal functions can be used in internal library functions because
|
||||
// they will be part of the same code context
|
||||
function map(uint[] memory self, function (uint) pure returns (uint) f)
|
||||
internal
|
||||
pure
|
||||
returns (uint[] memory r)
|
||||
{
|
||||
r = new uint[](self.length);
|
||||
for (uint i = 0; i < self.length; i++) {
|
||||
r[i] = f(self[i]);
|
||||
// internal functions can be used in internal library functions because
|
||||
// they will be part of the same code context
|
||||
function map(uint[] memory self, function (uint) pure returns (uint) f)
|
||||
internal
|
||||
pure
|
||||
returns (uint[] memory r)
|
||||
{
|
||||
r = new uint[](self.length);
|
||||
for (uint i = 0; i < self.length; i++) {
|
||||
r[i] = f(self[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
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]);
|
||||
|
||||
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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
function range(uint length) internal pure returns (uint[] memory r) {
|
||||
r = new uint[](length);
|
||||
for (uint i = 0; i < r.length; i++) {
|
||||
r[i] = i;
|
||||
|
||||
function range(uint length) internal pure returns (uint[] memory r) {
|
||||
r = new uint[](length);
|
||||
for (uint i = 0; i < r.length; i++) {
|
||||
r[i] = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
contract Pyramid {
|
||||
using ArrayUtils for *;
|
||||
function pyramid(uint l) public pure returns (uint) {
|
||||
return ArrayUtils.range(l).map(square).reduce(sum);
|
||||
}
|
||||
function square(uint x) internal pure returns (uint) {
|
||||
return x * x;
|
||||
}
|
||||
function sum(uint x, uint y) internal pure returns (uint) {
|
||||
return x + y;
|
||||
}
|
||||
using ArrayUtils for *;
|
||||
|
||||
function pyramid(uint l) public pure returns (uint) {
|
||||
return ArrayUtils.range(l).map(square).reduce(sum);
|
||||
}
|
||||
|
||||
function square(uint x) internal pure returns (uint) {
|
||||
return x * x;
|
||||
}
|
||||
|
||||
function sum(uint x, uint y) internal pure returns (uint) {
|
||||
return x + y;
|
||||
}
|
||||
}
|
||||
|
||||
Another example that uses external function types::
|
||||
|
||||
pragma solidity >=0.4.22 <0.7.0;
|
||||
|
||||
|
||||
contract Oracle {
|
||||
struct Request {
|
||||
bytes data;
|
||||
function(uint) external callback;
|
||||
}
|
||||
Request[] requests;
|
||||
event NewRequest(uint);
|
||||
function query(bytes memory data, function(uint) external callback) public {
|
||||
requests.push(Request(data, callback));
|
||||
emit NewRequest(requests.length - 1);
|
||||
}
|
||||
function reply(uint requestID, uint response) public {
|
||||
// Here goes the check that the reply comes from a trusted source
|
||||
requests[requestID].callback(response);
|
||||
}
|
||||
struct Request {
|
||||
bytes data;
|
||||
function(uint) external callback;
|
||||
}
|
||||
|
||||
Request[] private requests;
|
||||
event NewRequest(uint);
|
||||
|
||||
function query(bytes memory data, function(uint) external callback) public {
|
||||
requests.push(Request(data, callback));
|
||||
emit NewRequest(requests.length - 1);
|
||||
}
|
||||
|
||||
function reply(uint requestID, uint response) public {
|
||||
// Here goes the check that the reply comes from a trusted source
|
||||
requests[requestID].callback(response);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
contract OracleUser {
|
||||
Oracle constant oracle = Oracle(0x1234567); // known contract
|
||||
uint exchangeRate;
|
||||
function buySomething() public {
|
||||
oracle.query("USD", this.oracleResponse);
|
||||
}
|
||||
function oracleResponse(uint response) public {
|
||||
require(
|
||||
msg.sender == address(oracle),
|
||||
"Only oracle can call this."
|
||||
);
|
||||
exchangeRate = response;
|
||||
}
|
||||
Oracle constant private ORACLE_CONST = Oracle(0x1234567); // known contract
|
||||
uint private exchangeRate;
|
||||
|
||||
function buySomething() public {
|
||||
ORACLE_CONST.query("USD", this.oracleResponse);
|
||||
}
|
||||
|
||||
function oracleResponse(uint response) public {
|
||||
require(
|
||||
msg.sender == address(ORACLE_CONST),
|
||||
"Only oracle can call this."
|
||||
);
|
||||
exchangeRate = response;
|
||||
}
|
||||
}
|
||||
|
||||
.. note::
|
||||
|
||||
Reference in New Issue
Block a user