向该地址发送数量为 amount 的 Wei,失败时返回 false,发送 2300 gas 的矿工费用,不可调节。
注意:send安全等级比较低,他失败时(比如因为堆栈在 1024 或者 gas 不足)不会发生异常,因此往往要检查它的返回值,或者直接用transfer
1 2 3 4 5 6 7 8 9 10 11 12 13
// SPDX-License-Identifier: MIT // compiler version must be greater than or equal to 0.8.3 and less than 0.9.0 pragma solidity ^0.8.3; contract HelloWorld { string public greet = "Hello World!"; address public myAddress=address(this); uint public myBalance = myAddress.balance; bytes public myCode = myAddress.code; bytes32 public myCodehash = myAddress.codehash; function getstr() public view returns (string memory){ return greet; } }
library ArrayUtils { // 内部函数可以在内部库函数中使用, // 因为它们会成为同一代码上下文的一部分 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 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; } }
contract C { function f() public pure returns (uint24[2][4] memory) { uint24[2][4] memory x = [[uint24(0x1), 1], [0xffffff, 2], [uint24(0xff), 3], [uint24(0xffff), 4]]; // The following does not work, because some of the inner arrays are not of the right type. // uint[2][4] memory x = [[0x1, 1], [0xffffff, 2], [0xff, 3], [0xffff, 4]]; return x; } }
通过数组的字面常量创建数组,不支持动态分配内存,必须预设数组大小。uint[] memory x = [uint(1), 3, 4];报错,必须写成uint[3] memory x = [uint(1), 3, 4];。这个考虑移除这个特性,但是会造成 ABI 中数组传参的一些麻烦。
contract MappingExample { mapping(address => uint) public balances;
function update(uint newBalance) public { balances[msg.sender] = newBalance; } }
contract MappingUser { function f() public returns (uint) { MappingExample m = new MappingExample(); m.update(100); return m.balances(address(this)); } }
// 如何使用 contract User { // Just a struct holding our data. itmap data; // Apply library functions to the data type. using IterableMapping for itmap;
// Insert something function insert(uint k, uint v) public returns (uint size) { // This calls IterableMapping.insert(data, k, v) data.insert(k, v); // We can still access members of the struct, // but we should take care not to mess with them. return data.size; }
// Computes the sum of all stored data. function sum() public view returns (uint s) { for ( uint i = data.iterate_start(); data.iterate_valid(i); i = data.iterate_next(i) ) { (, uint value) = data.iterate_get(i); s += value; } } }
// Represent a 18 decimal, 256 bit wide fixed point type using a user defined value type. type UFixed256x18 is uint256;
/// A minimal library to do fixed point operations on UFixed256x18. library FixedMath { uint constant multiplier = 10**18;
/// Adds two UFixed256x18 numbers. Reverts on overflow, relying on checked /// arithmetic on uint256. function add(UFixed256x18 a, UFixed256x18 b) internal pure returns (UFixed256x18) { return UFixed256x18.wrap(UFixed256x18.unwrap(a) + UFixed256x18.unwrap(b)); } /// Multiplies UFixed256x18 and uint256. Reverts on overflow, relying on checked /// arithmetic on uint256. function mul(UFixed256x18 a, uint256 b) internal pure returns (UFixed256x18) { return UFixed256x18.wrap(UFixed256x18.unwrap(a) * b); } /// Take the floor of a UFixed256x18 number. /// @return the largest integer that does not exceed `a`. function floor(UFixed256x18 a) internal pure returns (uint256) { return UFixed256x18.unwrap(a) / multiplier; } /// Turns a uint256 into a UFixed256x18 of the same value. /// Reverts if the integer is too large. function toUFixed256x18(uint256 a) internal pure returns (UFixed256x18) { return UFixed256x18.wrap(a * multiplier); } }
bytes2 a = 0x1234; uint32 b = uint16(a); // b will be 0x00001234 uint32 c = uint32(bytes4(a)); // c will be 0x12340000 uint8 d = uint8(uint16(a)); // d will be 0x34 uint8 e = uint8(bytes1(a)); // e will be 0x12
contract C { bytes s = "abcdefgh"; function f(bytes calldata c, bytes memory m) public view returns (bytes16, bytes3) { require(c.length == 16, ""); bytes16 b = bytes16(m); // if length of m is greater than 16, truncation will happen b = bytes16(s); // padded on the right, so result is "abcdefgh\0\0\0\0\0\0\0\0" bytes3 b1 = bytes3(s); // truncated, b1 equals to "abc" b = bytes16(c[:8]); // also padded with zeros return (b, b1); } }
bytes2 a = 54321; // not allowed bytes2 b = 0x12; // not allowed bytes2 c = 0x123; // not allowed bytes2 d = 0x1234; // fine bytes2 e = 0x0012; // fine bytes4 f = 0; // fine bytes4 g = 0x0; // fine
字符串字面常量转换成定长字节类型也需要大小相同。
1 2 3 4 5 6
bytes2 a = hex"1234"; // fine bytes2 b = "xy"; // fine bytes2 c = hex"12"; // not allowed bytes2 d = hex"123"; // not allowed bytes2 e = "x"; // not allowed bytes2 f = "xyz"; // not allowed